1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.db.search.value;
23  
24  import org.efaps.admin.datamodel.Status;
25  import org.efaps.db.AbstractObjectQuery;
26  import org.efaps.db.Context;
27  import org.efaps.db.Instance;
28  import org.efaps.db.search.AbstractQPart;
29  import org.efaps.db.search.compare.AbstractQAttrCompare;
30  import org.efaps.db.search.compare.QEqual;
31  import org.efaps.db.search.compare.QMatch;
32  import org.efaps.db.wrapper.SQLSelect;
33  import org.efaps.util.EFapsException;
34  
35  /**
36   * TODO comment!
37   *
38   * @author The eFaps Team
39   * @version $Id$
40   */
41  public class QStringValue
42      extends AbstractQValue
43  {
44      /**
45       * Activate / deactivate the escape for the given string.
46       */
47      private boolean noEscape = false;
48  
49      /**
50       * Value for this StringValue.
51       */
52      private String value;
53  
54      /**
55       * @param _value Value
56       */
57      public QStringValue(final String _value)
58      {
59          this.value = _value;
60      }
61  
62      /**
63       * {@inheritDoc}
64       *
65       */
66      @Override
67      public QStringValue prepare(final AbstractObjectQuery<?> _query,
68                                  final AbstractQPart _part)
69          throws EFapsException
70      {
71          if (_part instanceof AbstractQAttrCompare) {
72              if (((AbstractQAttrCompare) _part).isIgnoreCase()) {
73                  this.value = this.value.toUpperCase(Context.getThreadContext().getLocale());
74              }
75              if (_part instanceof QMatch) {
76                  this.value = Context.getDbType().prepare4Match(this.value);
77              }
78              if (_part instanceof QEqual && ((QEqual) _part).getAttribute().getAttribute() != null) {
79                  // check if the string is an status key and must be converted in
80                  // a long
81                  if (((QEqual) _part).getAttribute().getAttribute().getParent().isCheckStatus()
82                                  && ((QEqual) _part).getAttribute().getAttribute().equals(
83                                     ((QEqual) _part).getAttribute().getAttribute().getParent().getStatusAttribute())) {
84                      final Status status = Status.find(
85                                      ((QEqual) _part).getAttribute().getAttribute().getLink().getUUID(), this.value);
86                      if (status != null) {
87                          this.value = Long.valueOf(status.getId()).toString();
88                          this.noEscape = true;
89                      }
90                   // check if the string is an oid and must be converted in a long
91                  } else if (((QEqual) _part).getAttribute().getAttribute().hasLink()) {
92                      final Instance insTmp = Instance.get(this.value);
93                      if (insTmp.isValid()) {
94                          this.value = Long.valueOf(insTmp.getId()).toString();
95                          this.noEscape = true;
96                      }
97                  }
98              }
99          }
100         return this;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public QStringValue appendSQL(final SQLSelect _sql)
108     {
109         if (this.noEscape) {
110             _sql.addValuePart(this.value);
111         } else {
112             _sql.addEscapedValuePart(this.value);
113         }
114         return this;
115     }
116 }