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.admin.datamodel.ui;
22  
23  import java.io.Serializable;
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import org.apache.commons.lang3.BooleanUtils;
28  import org.efaps.admin.datamodel.Attribute;
29  import org.efaps.admin.dbproperty.DBProperties;
30  import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
31  import org.efaps.admin.ui.field.Field;
32  import org.efaps.util.EFapsException;
33  import org.efaps.util.cache.CacheReloadException;
34  
35  /**
36   * A boolean value is shown in create mode with radio boxen which are only
37   * preselected if a defaultvalue for the attribute was defined. In edit mode,
38   * the user could select a value. The value is received from the DBProperties
39   * using the AttributeName and a parameter.<br>
40   * e.g. <br>
41   * Key = TypeName/AttributeName.false <br>
42   * Value = inactive
43   *
44   * @author The eFaps Team
45   * @version $Id$
46   */
47  public class BooleanUI
48      extends AbstractUI
49  {
50      /**
51       * Needed for serialization.
52       */
53      private static final long serialVersionUID = 1L;
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public String getEditHtml(final FieldValue _fieldValue)
60      {
61          final StringBuilder ret = new StringBuilder();
62          final Field field = _fieldValue.getField();
63          final Attribute attribute = _fieldValue.getAttribute();
64  
65          final Boolean bool;
66          if (_fieldValue.getValue() instanceof Boolean) {
67              bool = (Boolean) _fieldValue.getValue();
68          } else if (_fieldValue.getValue() instanceof String && ((String) _fieldValue.getValue()).length() > 0) {
69              if (((String) _fieldValue.getValue()).equalsIgnoreCase("TRUE")) {
70                  bool = true;
71              } else {
72                  bool = false;
73              }
74          } else {
75              bool = null;
76          }
77  
78          if (_fieldValue.getTargetMode().equals(TargetMode.SEARCH)) {
79              ret.append("<script language=\"javascript\" type=\"text/javascript\">")
80                  .append("var checked").append(field.getName()).append(";")
81                  .append("function Clear").append(field.getName()).append("(_btn) {")
82                  .append("if (checked").append(field.getName()).append(" == _btn){")
83                  .append("_btn.checked = false;")
84                  .append("checked").append(field.getName()).append(" = null;")
85                  .append("} else { ")
86                  .append("checked").append(field.getName()).append(" = _btn; }")
87                  .append("}").append("</script>")
88                  .append("<input type=\"radio\" ").append((bool != null && bool) ? "checked=\"checked\" " : "")
89                  .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("TRUE")
90                  .append("\" onclick=\"Clear").append(field.getName()).append("(this)\"/>")
91                  .append(getTrue(attribute)).append("<br/>")
92                  .append("<input type=\"radio\" ").append((bool != null && !bool) ? "checked=\"checked\" " : "")
93                  .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("FALSE")
94                  .append("\" onclick=\"Clear").append(field.getName()).append("(this)\"/>").append(getFalse(attribute));
95          } else {
96              ret.append("<input type=\"radio\" ").append((bool != null && bool) ? "checked=\"checked\" " : "")
97                  .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("TRUE")
98                  .append("\"/>").append(getTrue(attribute)).append("<br/>")
99                  .append("<input type=\"radio\" ").append((bool != null && !bool) ? "checked=\"checked\" " : "")
100                 .append("name=\"").append(field.getName()).append("\" ").append("value=\"").append("FALSE")
101                 .append("\"/>").append(getFalse(attribute));
102         }
103         return ret.toString();
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public String getReadOnlyHtml(final FieldValue _fieldValue)
111     {
112         String ret = null;
113         final Attribute attribute = _fieldValue.getAttribute();
114         if (_fieldValue.getValue() instanceof Boolean) {
115             final boolean bool = (Boolean) _fieldValue.getValue();
116             if (bool) {
117                 ret = getTrue(attribute);
118             } else {
119                 ret = getFalse(attribute);
120             }
121         }
122         return ret;
123     }
124 
125     /**
126      *  {@inheritDoc}
127      */
128     @Override
129     public Object getObject4Compare(final FieldValue _fieldValue)
130     {
131         Object ret = null;
132         if (_fieldValue.getValue() instanceof Boolean) {
133             final boolean bool = (Boolean) _fieldValue.getValue();
134             if (bool) {
135                 ret = getTrue(_fieldValue.getAttribute());
136             } else {
137                 ret = getFalse(_fieldValue.getAttribute());
138             }
139         }
140         return ret;
141     }
142 
143     /**
144      *  {@inheritDoc}
145      */
146     @Override
147     public int compare(final FieldValue _fieldValue,
148                        final FieldValue _fieldValue2)
149     {
150         String value = null;
151         String value2 = null;
152         // in case we have a boolean
153         if (_fieldValue.getValue() instanceof Boolean && _fieldValue2.getValue() instanceof Boolean) {
154             if ((Boolean) _fieldValue.getValue()) {
155                 value = getTrue(_fieldValue.getAttribute());
156             } else {
157                 value = getFalse(_fieldValue.getAttribute());
158             }
159             if ((Boolean) _fieldValue2.getValue()) {
160                 value2 = getTrue(_fieldValue.getAttribute());
161             } else {
162                 value2 = getFalse(_fieldValue.getAttribute());
163             }
164         }
165         // in case we have allready a string
166         if (_fieldValue.getValue() instanceof String && _fieldValue2.getValue() instanceof String) {
167             value = (String) _fieldValue.getValue();
168             value2 = (String) _fieldValue2.getValue();
169         }
170         return value.compareTo(value2);
171     }
172 
173     /**
174      * Method to evaluate a String representation for the boolean.
175      *
176      * @param _attribute Attribute the String representation is wanted for
177      * @return String representation, default "FALSE"
178      */
179     private String getFalse(final Attribute _attribute)
180     {
181         String ret;
182 
183         if (DBProperties.hasProperty(_attribute.getKey() + ".false")) {
184             ret = DBProperties.getProperty(_attribute.getKey() + ".false");
185         } else {
186             ret = "FALSE";
187         }
188         return ret;
189     }
190 
191     /**
192      * Method to evaluate a String representation for the boolean.
193      *
194      * @param _attribute Attribute the String representation is wanted for
195      * @return String representation, default "TRUE"
196      */
197     private String getTrue(final Attribute _attribute)
198     {
199         String ret;
200         if (DBProperties.hasProperty(_attribute.getKey() + ".true")) {
201             ret = DBProperties.getProperty(_attribute.getKey() + ".true");
202         } else {
203             ret = "TRUE";
204         }
205         return ret;
206     }
207 
208     /**
209      * Method to evaluate a String representation for the boolean.
210      *
211      * @param _uiValue  UIValue the String representation is wanted for
212      * @param _key      key the String representation is wanted for
213      * @return String representation
214      */
215     private String getLabel(final UIValue _uiValue,
216                             final Boolean _key)
217         throws CacheReloadException
218     {
219         String ret = BooleanUtils.toStringTrueFalse(_key);
220         if (_uiValue.getAttribute() != null
221                         && DBProperties.hasProperty(_uiValue.getAttribute().getKey() + "."
222                                         + BooleanUtils.toStringTrueFalse(_key))) {
223             ret = DBProperties.getProperty(_uiValue.getAttribute().getKey() + "."
224                             + BooleanUtils.toStringTrueFalse(_key));
225         } else if (DBProperties
226                         .hasProperty(_uiValue.getField().getLabel() + "." + BooleanUtils.toStringTrueFalse(_key))) {
227             ret = DBProperties.getProperty(_uiValue.getField().getLabel() + "." + BooleanUtils.toStringTrueFalse(_key));
228         }
229         return ret;
230     }
231 
232     @Override
233     public Object getValue(final UIValue _uiValue)
234         throws EFapsException
235     {
236         final Map<Object, Object> ret = new TreeMap<Object, Object>();
237         ret.put(getLabel(_uiValue, Boolean.TRUE), Boolean.TRUE);
238         ret.put(getLabel(_uiValue, Boolean.FALSE), Boolean.FALSE);
239         return ret;
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     @Override
246     public Object transformObject(final UIValue _uiValue,
247                                   final Object _object)
248         throws EFapsException
249     {
250         Object ret = null;
251         if (_object instanceof Map) {
252             ret = _object;
253         } else if (_object instanceof Serializable) {
254             _uiValue.setDbValue((Serializable) _object);
255             ret = getValue(_uiValue);
256         }
257         return ret;
258     }
259 }