1   /*
2   0 * 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.math.BigDecimal;
24  import java.text.DecimalFormat;
25  import java.text.NumberFormat;
26  
27  import org.efaps.admin.datamodel.Dimension;
28  import org.efaps.admin.datamodel.Dimension.UoM;
29  import org.efaps.admin.datamodel.attributetype.DecimalType;
30  import org.efaps.admin.dbproperty.DBProperties;
31  import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
32  import org.efaps.admin.ui.field.Field;
33  import org.efaps.db.Context;
34  import org.efaps.util.EFapsException;
35  
36  
37  /**
38   * Class to represent a String for the user interface.
39   *
40   * @author The eFaps Team
41   * @version $Id$
42   *
43   */
44  public class DecimalWithUoMUI
45      extends AbstractUI
46  {
47      /**
48       * Needed for serialization.
49       */
50      private static final long serialVersionUID = 1L;
51  
52      /**
53       * {@inheritDoc}
54       *
55       */
56      @Override
57      public String getReadOnlyHtml(final FieldValue _fieldValue)
58          throws EFapsException
59      {
60          final StringBuilder ret = new StringBuilder();
61          final Field field = _fieldValue.getField();
62          final Object value = _fieldValue.getValue();
63  
64          if (value instanceof Object[]) {
65              final Object[] values =  (Object[]) value;
66              final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Context.getThreadContext()
67                              .getLocale());
68              final String strValue = values[0] != null ? values[0] instanceof Number
69                                                  ? formatter.format(values[0]) : values[0].toString() : "";
70              final UoM uom = (UoM) values[1];
71              ret.append("<span><span name=\"").append(field.getName()).append("\" ")
72                  .append(UIInterface.EFAPSTMPTAG).append(">")
73                  .append(strValue).append("</span>&nbsp;");
74              if (strValue.length() > 0 && uom != null) {
75                  ret.append("<span name=\"").append(field.getName()).append("UoM\" ").append(">")
76                  .append(uom.getName()).append("</span>");
77              }
78              ret.append("</span>");
79          }
80          return ret.toString();
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getStringValue(final FieldValue _fieldValue)
88          throws EFapsException
89      {
90          final StringBuilder ret = new StringBuilder();
91          final Object value = _fieldValue.getValue();
92          if (value instanceof Object[]) {
93              final Object[] values =  (Object[]) value;
94              final DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Context.getThreadContext()
95                              .getLocale());
96              final String strValue = values[0] != null ? values[0] instanceof Number
97                                                  ? formatter.format(values[0]) : values[0].toString() : "";
98              final UoM uom = (UoM) values[1];
99              ret.append(strValue).append(" ");
100             if (strValue.length() > 0 && uom != null) {
101                 ret.append(uom.getName());
102             }
103         }
104         return ret.toString();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String getEditHtml(final FieldValue _fieldValue)
112         throws EFapsException
113     {
114         final StringBuilder ret = new StringBuilder();
115         final Field field = _fieldValue.getField();
116         final Object value = _fieldValue.getValue();
117         String strValue = null;
118         UoM uomValue = null;
119         if (value instanceof Object[]) {
120             final Object[] values =  (Object[]) value;
121             final DecimalFormat formatter
122                 = (DecimalFormat) NumberFormat.getInstance(Context.getThreadContext().getLocale());
123             strValue =  values[0] != null
124                 ? values[0] instanceof Number ? formatter.format(values[0]) : values[0].toString() : "";
125             uomValue = (UoM) values[1];
126         }
127 
128         if (_fieldValue.getTargetMode().equals(TargetMode.SEARCH)) {
129             ret.append("<input type=\"text\" size=\"").append(field.getCols())
130                 .append("\" name=\"").append(field.getName())
131                 .append("\" value=\"").append(value != null ? value : "*").append("\" />");
132         } else {
133             ret.append("<span><input type=\"text\" size=\"").append(field.getCols())
134                 .append("\" name=\"").append(field.getName())
135                 .append("\" value=\"").append(strValue != null ? strValue : "").append("\"")
136                 .append(UIInterface.EFAPSTMPTAG).append("/>")
137                 .append("<select name=\"").append(_fieldValue.getField().getName()).append("UoM\" size=\"1\">");
138 
139             final Dimension dim = _fieldValue.getAttribute().getDimension();
140             for (final UoM uom : dim.getUoMs()) {
141                 ret.append("<option value=\"").append(uom.getId());
142                 if (uomValue == null && uom.equals(dim.getBaseUoM()) || uomValue != null && uomValue.equals(uom)) {
143                     ret.append("\" selected=\"selected");
144                 }
145                 ret.append("\">").append(uom.getName()).append("</option>");
146             }
147             ret.append("</select></span>");
148 
149         }
150         return ret.toString();
151     }
152 
153     /**
154      * {@inheritDoc}
155      */
156     @Override
157     public String validateValue(final UIValue _value)
158     {
159         String ret = null;
160         try {
161             if (_value.getDbValue() != null) {
162                 DecimalType.parseLocalized(String.valueOf(_value.getDbValue()));
163             }
164         } catch (final EFapsException e) {
165             ret = DBProperties.getProperty(DecimalUI.class.getName() + ".InvalidValue");
166         }
167         return ret;
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public Object format(final Object _object,
175                          final String _pattern)
176         throws EFapsException
177     {
178         final Object ret;
179         final DecimalFormat formatter
180             = (DecimalFormat) NumberFormat.getInstance(Context.getThreadContext().getLocale());
181         formatter.applyPattern(_pattern);
182         if (_object instanceof Object[]) {
183             final String tmp = formatter.format(((Object[]) _object)[0]);
184             ((Object[]) _object)[0] = tmp;
185             ret = _object;
186         } else {
187             ret = formatter.format(_object);
188         }
189         return ret;
190     }
191 
192     /**
193      * {@inheritDoc}
194      */
195     @SuppressWarnings("unchecked")
196     @Override
197     public int compare(final FieldValue _fieldValue,
198                        final FieldValue _fieldValue2)
199         throws EFapsException
200     {
201         int ret = 0;
202         if (_fieldValue.getValue() instanceof Object[] && _fieldValue2.getValue() instanceof Object[]) {
203             final Object[] values =  (Object[]) _fieldValue.getValue();
204             final Object[] values2 =  (Object[]) _fieldValue2.getValue();
205 
206             if (values.length == 3 && values2.length == 3) {
207                 ret = ((Double) values[2]).compareTo((Double) values2[2]);
208             } else {
209                 final BigDecimal val = (BigDecimal) values[0];
210                 final BigDecimal val2 = (BigDecimal) values2[0];
211                 final UoM uom = (UoM) values[1];
212                 final UoM uom2 = (UoM) values2[1];
213                 final BigDecimal tmpVal = val.multiply(new BigDecimal(uom.getNumerator())
214                                 .setScale(12, BigDecimal.ROUND_HALF_UP)
215                                 .divide(new BigDecimal(uom.getDenominator()), BigDecimal.ROUND_HALF_UP));
216                 final BigDecimal tmpVal2 = val2.multiply(new BigDecimal(uom2.getNumerator())
217                                 .setScale(12, BigDecimal.ROUND_HALF_UP)
218                                 .divide(new BigDecimal(uom2.getDenominator()), BigDecimal.ROUND_HALF_UP));
219                 ret = tmpVal.compareTo(tmpVal2);
220             }
221         } else if (_fieldValue.getValue() instanceof Comparable && _fieldValue2.getValue() instanceof Comparable) {
222             ret = ((Comparable<Object>)_fieldValue.getValue()).compareTo(_fieldValue2.getValue());
223         } else {
224             ret = super.compare(_fieldValue, _fieldValue2);
225         }
226         return ret;
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public Object getObject4Compare(final FieldValue _fieldValue)
234         throws EFapsException
235     {
236         Object ret = null;
237         if (_fieldValue.getValue() != null && _fieldValue.getValue() instanceof Object[]) {
238             final Object[] values =  (Object[]) _fieldValue.getValue();
239             if (values.length == 3) {
240                 ret = values[2];
241             } else {
242                 ret = values[0];
243             }
244         }
245         return ret;
246     }
247 }