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 org.apache.commons.lang3.StringEscapeUtils;
24  import org.efaps.admin.datamodel.Dimension;
25  import org.efaps.admin.datamodel.Dimension.UoM;
26  import org.efaps.admin.dbproperty.DBProperties;
27  import org.efaps.admin.ui.AbstractUserInterfaceObject.TargetMode;
28  import org.efaps.admin.ui.field.Field;
29  import org.efaps.util.EFapsException;
30  import org.efaps.util.cache.CacheReloadException;
31  
32  /**
33   * Class to represent a String for the user interface.
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   *
38   */
39  public class StringWithUoMUI
40      extends AbstractUI
41  {
42  
43      /**
44       * Needed for serialization.
45       */
46      private static final long serialVersionUID = 1L;
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      public String getReadOnlyHtml(final FieldValue _fieldValue)
53      {
54          final StringBuilder ret = new StringBuilder();
55          final Field field = _fieldValue.getField();
56          final Object value = _fieldValue.getValue();
57  
58          if (value instanceof Object[]) {
59              final Object[] values =  (Object[]) value;
60              final String tmp = values[0] != null ?  values[0].toString() : "";
61              final UoM uom = (UoM) values[1];
62              ret.append("<span><span name=\"").append(field.getName()).append("\" ")
63                  .append(UIInterface.EFAPSTMPTAG).append(">")
64                  .append(StringEscapeUtils.escapeHtml4(tmp).replaceAll("\\n", "<br/>"))
65                  .append("</span>&nbsp;")
66                  .append("<span name=\"").append(field.getName()).append("UoM\" ").append(">")
67                  .append(uom.getName()).append("</span></span>");
68          }
69          return ret.toString();
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public String getEditHtml(final FieldValue _fieldValue)
77          throws EFapsException
78      {
79          final StringBuilder ret = new StringBuilder();
80          final Field field = _fieldValue.getField();
81          final Object value = _fieldValue.getValue();
82          String strValue = null;
83          UoM uomValue = null;
84          if (value instanceof Object[]) {
85              final Object[] values =  (Object[]) value;
86              strValue = values[0] != null ?  values[0].toString() : "";
87              uomValue = (UoM) values[1];
88          }
89  
90          if (_fieldValue.getTargetMode().equals(TargetMode.SEARCH)) {
91              ret.append("<input type=\"text\"").append(" size=\"").append(field.getCols()).append("\" name=\"").append(
92                             field.getName()).append("\" value=\"").append(value != null ? value : "*").append("\" />");
93          } else {
94              ret.append("<span><input type=\"text\" size=\"").append(field.getCols())
95                  .append("\" name=\"").append(field.getName())
96                  .append("\" value=\"").append(strValue != null ? strValue : "").append("\"")
97                  .append(UIInterface.EFAPSTMPTAG).append("/>")
98                  .append("<select name=\"").append(_fieldValue.getField().getName()).append("UoM\" size=\"1\">");
99  
100             final Dimension dim = _fieldValue.getAttribute().getDimension();
101             for (final UoM uom : dim.getUoMs()) {
102                 ret.append("<option value=\"").append(uom.getId());
103                 if ((uomValue == null && uom.equals(dim.getBaseUoM())) || (uomValue != null && uomValue.equals(uom))) {
104                     ret.append("\" selected=\"selected");
105                 }
106                 ret.append("\">").append(uom.getName()).append("</option>");
107             }
108             ret.append("</select></span>");
109 
110         }
111         return ret.toString();
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getStringValue(final FieldValue _fieldValue)
119         throws EFapsException
120     {
121         return _fieldValue.getValue() == null ? "" : _fieldValue.getValue().toString();
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public int compare(final FieldValue _fieldValue,
129                        final FieldValue _fieldValue2)
130     {
131         final String value = _fieldValue.getValue().toString();
132         final String value2 = _fieldValue2.getValue().toString();
133         return value.compareTo(value2);
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public String validateValue(final UIValue _value)
141         throws CacheReloadException
142     {
143         String ret = null;
144         if (_value.getAttribute() != null && _value.getDbValue() != null) {
145             if (String.valueOf(_value.getDbValue()).length() > _value.getAttribute().getSize()) {
146                 ret = DBProperties.getProperty(StringWithUoMUI.class.getName() + ".InvalidValue") + " "
147                                 + _value.getAttribute().getSize();
148             }
149         }
150         return ret;
151     }
152 }