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  package org.efaps.db;
22  
23  import java.io.Serializable;
24  
25  import org.apache.commons.lang3.builder.ToStringBuilder;
26  import org.apache.commons.lang3.builder.ToStringStyle;
27  import org.hibernate.search.annotations.Analyze;
28  import org.hibernate.search.annotations.Field;
29  import org.hibernate.search.annotations.Indexed;
30  
31  /**
32   * TODO comment!
33   *
34   * @author The eFaps Team
35   * @version $Id$
36   */
37  @Indexed
38  public final class QueryKey
39      implements Serializable
40  {
41      /**
42       *
43       */
44      private static final long serialVersionUID = 1L;
45  
46      /**
47       * The sql statement executed.
48       */
49      private final String sql;
50  
51      /**
52       * The name of the sql statement.
53       */
54      @Field(analyze = Analyze.NO)
55      private final String key;
56  
57      /**
58       * @param _key key
59       * @param _sql sql statement
60       */
61      private QueryKey(final String _key,
62                       final String _sql)
63      {
64          this.key = _key;
65          this.sql = _sql;
66      }
67  
68      /**
69       * Getter method for the instance variable {@link #sql}.
70       *
71       * @return value of instance variable {@link #sql}
72       */
73      public String getSql()
74      {
75          return this.sql;
76      }
77  
78      /**
79       * Getter method for the instance variable {@link #key}.
80       *
81       * @return value of instance variable {@link #key}
82       */
83      public String getKey()
84      {
85          return this.key;
86      }
87  
88      /**
89       * @return string used as unique value for indexing
90       */
91      protected String getIndexKey()
92      {
93          return getKey() + getSql();
94      }
95  
96      /**
97       * @return id represented by this instance
98       */
99      @Override
100     public int hashCode()
101     {
102         return this.key.hashCode();
103     }
104 
105     /**
106      * @param _obj Object to compare
107      * @return <i>true</i> if the given object in _obj is an instance and holds
108      *         the same type and id
109      */
110     @Override
111     public boolean equals(final Object _obj)
112     {
113         boolean ret = false;
114         if (_obj instanceof QueryKey) {
115             final QueryKey queryKey = (QueryKey) _obj;
116             ret = getKey().equals(queryKey.getKey()) && getSql().equals(queryKey.getSql());
117         } else {
118             super.equals(_obj);
119         }
120         return ret;
121     }
122 
123     @Override
124     public String toString()
125     {
126         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
127     }
128 
129     /**
130      * @param _key key
131      * @param _sql sql
132      * @return new QueryKey
133      */
134     public static QueryKey get(final String _key,
135                                final String _sql)
136     {
137         return new QueryKey(_key, _sql);
138     }
139 }