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.admin.ui.field;
23  
24  import java.io.Serializable;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * Filter definition for a field inside a table.
32   *
33   * @author The eFaps Team
34   * @version $Id$
35   */
36  public class Filter
37      implements Serializable
38  {
39  
40      /**
41       * filtertype.
42       */
43      public enum Type
44      {
45          /** Filter for classification. */
46          CLASSIFICATION,
47          /** Filter for freetext. */
48          FREETEXT,
49          /** No filter. */
50          NONE,
51          /** Filter presenting a picker list. */
52          PICKLIST,
53          /** Filter for Status Attributes. */
54          STATUS;
55      }
56  
57      /**
58       * Filterbase.
59       */
60      public enum Base
61      {
62          /** Filter work on memory base. */
63          MEMORY,
64          /** Filter work against the data base. */
65          DATABASE;
66      }
67  
68      /**
69       * Needed for serialization.
70       */
71      private static final long serialVersionUID = 1L;
72  
73      /**
74       * Logger for this class.
75       */
76      private static final Logger LOG = LoggerFactory.getLogger(Field.class);
77  
78      /**
79       * Is this filter required.
80       */
81      private boolean required = false;
82  
83      /**
84       * Set the default value for a filter.
85       */
86      private String defaultValue;
87  
88      /**
89       * String containing the attributes to be used for the filter. It may
90       * contain up to two attributes separated by a comma. This allows to filter
91       * by an attribute and display a phrase.
92       */
93      private String attributes;
94  
95      /**
96       * Type of this filter (NONE is the default value).
97       */
98      private Type type = Type.NONE;
99  
100     /**
101      * Base of this filter (Memorbased is the default value).
102      */
103     private Base base = Base.MEMORY;
104 
105         /**
106      * Getter method for the instance variable {@link #base}.
107      *
108      * @return value of instance variable {@link #base}
109      */
110     public Base getBase()
111     {
112         return this.base;
113     }
114 
115     /**
116      * Setter method for instance variable {@link #base}.
117      *
118      * @param _baseName value for instance variable {@link #base}
119      */
120     protected void evalBase(final String _baseName)
121     {
122         try {
123             final Base baseTmp = Base.valueOf(_baseName.toUpperCase());
124             if (baseTmp != null) {
125                 this.base = baseTmp;
126             }
127         } catch (final IllegalArgumentException e) {
128             Filter.LOG.error("Invalid value '{}' for FilterBase", _baseName);
129         }
130         activate();
131     }
132 
133     /**
134      * Getter method for the instance variable {@link #type}.
135      *
136      * @return value of instance variable {@link #type}
137      */
138     public Type getType()
139     {
140         return this.type;
141     }
142 
143     /**
144      * Setter method for instance variable {@link #type}.
145      *
146      * @param _typeName value for instance variable {@link #type}
147      */
148     protected void evalType(final String _typeName)
149     {
150         try {
151             final Type typTmp = Type.valueOf(_typeName.toUpperCase());
152             if (typTmp != null) {
153                 this.type = typTmp;
154             }
155         } catch (final IllegalArgumentException e) {
156             Filter.LOG.error("Invalid value '{}' for FilterType", _typeName);
157         }
158     }
159 
160     /**
161      * Getter method for the instance variable {@link #defaultValue}.
162      *
163      * @return value of instance variable {@link #defaultValue}
164      */
165     public String getDefaultValue()
166     {
167         return this.defaultValue;
168     }
169 
170     /**
171      * Setter method for instance variable {@link #defaultValue}.
172      *
173      * @param _defaultValue value for instance variable {@link #defaultValue}
174      */
175     protected void setDefaultValue(final String _defaultValue)
176     {
177         this.defaultValue = _defaultValue;
178         activate();
179     }
180 
181     /**
182      * Getter method for the instance variable {@link #attributes}.
183      *
184      * @return value of instance variable {@link #attributes}
185      */
186     public String getAttributes()
187     {
188         return this.attributes;
189     }
190 
191     /**
192      * Setter method for instance variable {@link #attributes}.
193      *
194      * @param _attributes value for instance variable {@link #attributes}
195      */
196     protected void setAttributes(final String _attributes)
197     {
198         this.attributes = _attributes;
199         activate();
200     }
201 
202     /**
203      * Getter method for the instance variable {@link #required}.
204      *
205      * @return value of instance variable {@link #required}
206      */
207     public boolean isRequired()
208     {
209         return this.required;
210     }
211 
212     /**
213      * Setter method for instance variable {@link #required}.
214      *
215      * @param _required value for instance variable {@link #required}
216      */
217     protected void setRequired(final boolean _required)
218     {
219         this.required = _required;
220         activate();
221     }
222 
223     /**
224      * Activate the filter.
225      */
226     private void activate()
227     {
228         if (this.type.equals(Type.NONE)) {
229             this.type = Type.PICKLIST;
230         }
231     }
232 }