1 package mx.com.brained.taglib;
2
3 import java.util.*;
4
5 import javax.servlet.jsp.*;
6
7 import org.apache.struts.taglib.*;
8 import org.springframework.web.context.*;
9 import mx.com.brained.resource.*;
10 import org.apache.commons.lang.*;
11
12 /***
13 *
14 * <p>Title: AwTaglib</p>
15 *
16 * <p>Description: Active Widgets Tag Library</p>
17 * <p>This is the most important class of the whole framework.</br>
18 * It contains a series of utility methods that help in rendering the grid as a whole, and works intimately with the Grid Iterator Tag</p>
19 * <p>Copyright: Copyright (c) 2005</p>
20 *
21 * <p>Company: Brain-ED</p>
22 *
23 * @author Isidoro Treviņo
24 * @version 1.0
25 */
26 public class GridUtil {
27 private Collection data;
28 private Collection attributes;
29 private Collection row;
30 private WebApplicationContext context;
31 private String name;
32 private boolean empty=false;
33
34 public GridUtil() {
35 row=new ArrayList();
36 data=new ArrayList();
37 attributes = new ArrayList();
38 }
39
40 /***
41 * Add a new data cell to the data list
42 * @param dato String
43 */
44 public void setData(String dato){
45 row.add(dato);
46 }
47 /***
48 * Create a new row for the data list
49 */
50 public void newRow(){
51 data.add(row);
52 row=new ArrayList();
53 }
54 /***
55 * Add a new header and attributes for the given list
56 * @param atrib HashMap
57 */
58 public void addTitle(HashMap atrib){
59 attributes.add(atrib);
60 }
61
62 /***
63 * Return the data entered into the grid
64 * @return Collection
65 */
66 public Collection getData() {
67 return data;
68 }
69
70 /***
71 * Get a collection full of hashmaps with the attributes depending on the kind of field
72 * @return Collection
73 */
74 public Collection getAttributes() {
75 return attributes;
76 }
77
78 /***
79 * Get a row from the datalist
80 * @return Collection
81 */
82 public Collection getRow() {
83 return row;
84 }
85
86 public void setEmptyCollection(){
87 empty=true;
88 }
89
90 /***
91 * Return a string representation of the datalist, ready to be used by the grid
92 * @return String
93 */
94 public String getDataArray(){
95 StringBuffer buffer = new StringBuffer();
96 Collection row;
97 if(!empty){
98 for (Iterator i = data.iterator(); i.hasNext(); ) {
99 buffer.append("[");
100 row = (Collection) i.next();
101 for (Iterator j = row.iterator(); j.hasNext(); ) {
102 buffer.append("'");
103 buffer.append(j.next());
104 buffer.append("',");
105 }
106 buffer.deleteCharAt(buffer.length() - 1);
107 buffer.append("],");
108 }
109 }
110 else{
111 buffer.append(",");
112 }
113 buffer.deleteCharAt(buffer.length()-1);
114 return buffer.toString();
115 }
116 /***
117 * Gets a string representing the header array.
118 * @return String
119 */
120 public String getTitleArray(){
121 StringBuffer buffer = new StringBuffer();
122 buffer.append("\n var myColumns = [");
123 for(Iterator i=attributes.iterator();i.hasNext();){
124 buffer.append("\"");
125 buffer.append(((HashMap)i.next()).get("title"));
126 buffer.append("\",");
127 }
128 buffer.deleteCharAt(buffer.length()-1);
129 buffer.append("];");
130 return buffer.toString();
131 }
132 /***
133 * Get the column templates to be rendered by the grid
134 * @return String
135 * @throws Exception
136 */
137 public String getColumnProperties() throws Exception {
138 StringBuffer buffer = new StringBuffer();
139 HashMap map;
140 String tipo;
141 int cont=0;
142 ResourceManagerIface resource = (ResourceManagerIface) context.getBean(GridConstants.RESOURCEMANAGER);
143 for(Iterator i=attributes.iterator();i.hasNext();cont++){
144 map=(HashMap) i.next();
145 tipo = (String) map.get(GridConstants.TYPE);
146 if(tipo.equals(GridConstants.READONLY)){
147 buffer.append(getReadOnlyField(map,cont,resource));
148 }
149 else if(tipo.equals(GridConstants.EDITABLE)){
150 buffer.append(getEditableField(map,cont,resource));
151 }
152 else if(tipo.equals(GridConstants.CHECK)){
153 buffer.append(getCheckboxField(map,cont,resource));
154 }
155 else if(tipo.equals(GridConstants.DATE)){
156 buffer.append(getDateField(map,cont,resource));
157 }
158 else if(tipo.equals(GridConstants.COMBOBOX)){
159 buffer.append(getComboBoxField(map,cont,resource));
160 }
161 else if(tipo.equals(GridConstants.LIST)){
162 buffer.append(getListField(map,cont,resource));
163 }
164 }
165 return buffer.toString();
166 }
167
168 /***
169 * Return a list template
170 *
171 * @param map HashMap
172 * @param cont int
173 * @return String
174 */
175 public String getListField(HashMap map, int cont,ResourceManagerIface resource) throws Exception {
176 String result = resource.getResourceAsString(GridConstants.LIST_SCRIPT);
177 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
178 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(cont));
179 result = StringUtils.replace(result,GridConstants.VALUE_COLUMN_TOKEN,((String)map.get(GridConstants.VALUE_COLUMN)));
180 result = StringUtils.replace(result,GridConstants.OPTIONS_LIST_TOKEN,((String)map.get(GridConstants.LIST_OPTIONS)));
181 result = StringUtils.replace(result,GridConstants.TITLES_TOKEN,((String)map.get(GridConstants.TITLES)));
182 return result;
183 }
184
185 /***
186 * Return a ComboBox template
187 *
188 * @param map HashMap
189 * @param cont int
190 * @return char
191 */
192 public String getComboBoxField(HashMap map, int cont,ResourceManagerIface resource) throws
193 Exception {
194 String result = resource.getResourceAsString(GridConstants.COMBOBOX_SCRIPT);
195 if(((String)map.get(GridConstants.HIDDEN_VALUES)).equals(Boolean.toString(true))){
196 String dynaCombo = resource.getResourceAsString(GridConstants.DYNACOMBO_SCRIPT);
197 result = result.concat(dynaCombo);
198 result = StringUtils.replace(result,GridConstants.DOT_OPTIONS_TOKEN,((String)map.get(GridConstants.DOT_OPTIONS)));
199 }
200 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(cont));
201 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
202 result = StringUtils.replace(result,GridConstants.OPTIONS_COMBO_TOKEN,((String)map.get(GridConstants.COMBOBOX_OPTIONS)));
203 result = StringUtils.replace(result,GridConstants.ACTION_TOKEN,((String)map.get(GridConstants.ACTION)));
204 return result;
205 }
206
207 /***
208 * Return a Date template
209 *
210 * @param map HashMap
211 * @param cont int
212 * @return char
213 */
214 public String getDateField(HashMap map, int cont,ResourceManagerIface resource) throws Exception {
215 String result = resource.getResourceAsString(GridConstants.DATE_SCRIPT);
216 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(cont));
217 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
218 return result;
219 }
220
221 /***
222 * Return a Checkbox template
223 *
224 * @param map HashMap
225 * @param cont int
226 * @return char
227 */
228 public String getCheckboxField(HashMap map, int cont,ResourceManagerIface resource) throws
229 Exception {
230 String result = resource.getResourceAsString(GridConstants.CHECKBOX_SCRIPT);
231 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(cont));
232 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
233 result = StringUtils.replace(result,GridConstants.TRUE_VALUE_TOKEN,((String)map.get(GridConstants.TRUE_VALUE)));
234 result = StringUtils.replace(result,GridConstants.FALSE_VALUE_TOKEN,((String)map.get(GridConstants.FALSE_VALUE)));
235 return result;
236 }
237
238 /***
239 * Return an Editable template
240 *
241 * @param map HashMap
242 * @param cont int
243 * @return char
244 */
245 public String getEditableField(HashMap map, int cont,ResourceManagerIface resource) throws
246 Exception {
247 String result = resource.getResourceAsString(GridConstants.EDITABLE_SCRIPT);
248 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(cont));
249 result = StringUtils.replace(result,GridConstants.NAME_TOKEN,name);
250 return result;
251 }
252
253 /***
254 * Return a Read Only template
255 * @param map HashMap
256 * @param index int
257 * @param resource ResourceManagerIface
258 * @return String
259 * @throws Exception
260 */
261 public String getReadOnlyField(HashMap map,int index,ResourceManagerIface resource) throws
262 Exception {
263 String result = resource.getResourceAsString(GridConstants.READONLY_SCRIPT);
264 result = StringUtils.replace(result,GridConstants.COLUMN_TOKEN,String.valueOf(index));
265 return result;
266 }
267
268 public void setContext(WebApplicationContext context) {
269 this.context = context;
270 }
271
272 public void setName(String name) {
273 this.name = name;
274 }
275
276 /***
277 * Returns a String representing an array with the properties of the bean being rendered
278 *
279 * @return String
280 */
281 public String getPropertyArray() {
282 StringBuffer buffer = new StringBuffer();
283 for(Iterator i=attributes.iterator();i.hasNext();){
284 buffer.append("'");
285 buffer.append(((HashMap)i.next()).get(GridConstants.PROPERTY));
286 buffer.append("',");
287 }
288 buffer.deleteCharAt(buffer.length()-1);
289 return buffer.toString();
290 }
291
292 /***
293 * Returns a string representing an array with the headers being rendered
294 *
295 * @return String
296 */
297 public String getHeaderArray() {
298 StringBuffer buffer = new StringBuffer();
299 for(Iterator i=attributes.iterator();i.hasNext();){
300 buffer.append("'");
301 buffer.append(((HashMap)i.next()).get(GridConstants.HEADER));
302 buffer.append("',");
303 }
304 buffer.deleteCharAt(buffer.length()-1);
305 return buffer.toString();
306 }
307
308 /***
309 * Return all the templates that will be rendered
310 *
311 * @return String
312 */
313 public String getTemplates() {
314 try {
315 return getColumnProperties();
316 } catch (Exception ex) {
317 ex.printStackTrace();
318 return "";
319 }
320 }
321
322 }