View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.taglib;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.taglib.TagUtils;
23  import org.itracker.core.resources.ITrackerResources;
24  import org.itracker.model.CustomField;
25  import org.itracker.model.CustomFieldValue;
26  import org.itracker.model.NameValuePair;
27  import org.itracker.model.util.CustomFieldUtilities;
28  import org.itracker.web.util.HTMLUtilities;
29  import org.itracker.web.util.LoginUtilities;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.tagext.TagSupport;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Locale;
37  import java.util.Map;
38  
39  public final class FormatCustomFieldTag extends TagSupport {
40      /**
41       *
42       */
43      private static final long serialVersionUID = 1L;
44      public static final String DISPLAY_TYPE_EDIT = "edit";
45      public static final String DISPLAY_TYPE_VIEW = "view";
46  
47      private static final Logger logger = Logger
48              .getLogger(FormatCustomFieldTag.class);
49  
50      private CustomField field;
51      private String currentValue;
52      private String displayType;
53      private String formName;
54      private Map<Integer, List<NameValuePair>> listOptions;
55  
56      public CustomField getField() {
57          return field;
58      }
59  
60      public void setField(CustomField value) {
61          field = value;
62      }
63  
64      public String getCurrentValue() {
65          return currentValue;
66      }
67  
68      public void setCurrentValue(String value) {
69          currentValue = value;
70      }
71  
72      public String getDisplayType() {
73          return displayType;
74      }
75  
76      public void setDisplayType(String value) {
77          displayType = value;
78      }
79  
80      public String getFormName() {
81          return formName;
82      }
83  
84      public void setFormName(String value) {
85          formName = value;
86      }
87  
88      public Map<Integer, List<NameValuePair>> getListOptions() {
89          return (listOptions == null ? new HashMap<Integer, List<NameValuePair>>()
90                  : listOptions);
91      }
92  
93      public void setListOptions(Map<Integer, List<NameValuePair>> value) {
94          listOptions = value;
95      }
96  
97      public int doStartTag() throws JspException {
98          return SKIP_BODY;
99      }
100 
101     public int doEndTag() throws JspException {
102         Locale locale = null;
103 
104         if (field != null) {
105             locale = LoginUtilities
106                     .getCurrentLocale((HttpServletRequest) pageContext
107                             .getRequest());
108 
109             StringBuilder buf = new StringBuilder();
110             buf
111             .append("<div class=\"form-group\"><label>")
112             .append(CustomFieldUtilities.getCustomFieldName(field.getId(), locale))
113             .append(":")
114             .append("</label>\n");
115 
116             if (DISPLAY_TYPE_VIEW.equalsIgnoreCase(displayType)) {
117                 buf.append("<p class=\"form-control-static\">\n");
118                 if (currentValue != null) {
119                     if (field.getFieldType() == CustomField.Type.LIST) {
120                         buf.append(CustomFieldUtilities
121                                 .getCustomFieldOptionName(getField(),
122                                         currentValue, locale));
123                     } else {
124                         buf.append(currentValue);
125                     }
126                 }
127                 buf.append("</p>");
128 
129             } else {
130                 Object requestValue = TagUtils.getInstance().lookup(
131                         pageContext,
132                         org.apache.struts.taglib.html.Constants.BEAN_KEY,
133                         "customFields(" + field.getId() + ")", null);
134                 if (currentValue == null && requestValue != null) {
135                     currentValue = requestValue.toString();
136                 }
137 
138 
139                 if (field.getFieldType() == CustomField.Type.LIST) {
140                     final List<CustomFieldValue> options = field.getOptions();
141 
142                     buf.append("<select name=\"customFields(").append(
143                             field.getId()).append(
144                             ")\" class=\"form-control\">\n");
145                     for (CustomFieldValue option : options) {
146                         buf.append("<option value=\"").append(
147                                 HTMLUtilities.escapeTags(option
148                                         .getValue())).append("\"");
149                         if (currentValue != null
150                                 && currentValue.equals(option
151                                 .getValue())) {
152                             buf.append(" selected=\"selected\"");
153                         }
154                         buf.append(" class=\"editColumnText\">");
155                         buf.append(CustomFieldUtilities
156                                 .getCustomFieldOptionName(option,
157                                         locale));
158                         buf.append("</option>\n");
159                     }
160                     buf.append("</select>\n");
161 
162 
163                 } else if (field.getFieldType() == CustomField.Type.DATE) {
164                     String df = ITrackerResources.getString(
165                             "itracker.dateformat.dateonly", locale);
166                     if (field.getDateFormat().equals("full")) {
167                         df = ITrackerResources.getString(
168                                 "itracker.dateformat.full", locale);
169                     }
170 
171                     String fieldName = "customFields(" + field.getId() + ")";
172                     buf.append("<div class=\"input-group date\" data-format=\"")
173                             .append(HTMLUtilities.getJSDateFormat(df))
174                             .append("\">");
175                     buf.append("<input  type=\"text\" name=\"")
176                             .append(fieldName).append("\" id=\"")
177                             .append(fieldName).append("\"");
178                     buf.append((currentValue != null
179                             && !currentValue.equals("") ? " value=\""
180                             + currentValue + "\"" : ""));
181                     buf.append(" class=\"form-control\" />")
182                     .append("<span class=\"input-group-addon\"><i class=\"glyphicon glyphicon-th\"></i></span>")
183                     .append("</div>");
184 
185                 } else  if (null != getListOptions() && null != getListOptions().get(field.getId())
186                         && !getListOptions().get(field.getId()).isEmpty()) {
187                     List<NameValuePair> options = getListOptions().get(field.getId());
188                     buf.append("<select name=\"customFields(").append(
189                                field.getId()).append(
190                                ")\" class=\"form-control\">\n");
191                     for (NameValuePair option : options) {
192                         buf.append("<option value=\"").append(
193                                 HTMLUtilities.escapeTags(option
194                                         .getValue())).append("\"");
195                         if (currentValue != null
196                                 && currentValue.equals(option
197                                 .getValue())) {
198                             buf.append(" selected=\"selected\"");
199                         }
200                         buf.append(option.getName());
201                         buf.append("</option>\n");
202                     }
203                        buf.append("</select>\n");
204                 } else {
205                     buf.append("<input type=\"text\" name=\"customFields(")
206                             .append(field.getId()).append(")\"");
207                     buf.append((currentValue != null
208                             && !currentValue.equals("") ? " value=\""
209                             + currentValue + "\"" : ""));
210                     buf.append(" class=\"form-control\">");
211                 }
212             }
213             buf.append("</div>");
214 
215             TagUtils.getInstance().write(pageContext, buf.toString());
216         }
217 
218         clearState();
219         return (EVAL_PAGE);
220     }
221 
222     public void release() {
223         super.release();
224         clearState();
225     }
226 
227     private void clearState() {
228         field = null;
229         currentValue = null;
230         displayType = null;
231         listOptions = null;
232         formName = null;
233     }
234 
235 
236 }