KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > contrib > sam > models > MapDecorator


1 /*
2  * Copyright (C) 2003 Stefan Armbruster [stefan@armbruster-it.de]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: MapDecorator.java,v 1.9 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.contrib.sam.models;
21
22 import java.util.*;
23 import java.text.*;
24
25 import org.enhydra.barracuda.core.comp.*;
26 import org.enhydra.barracuda.core.event.*;
27
28 import org.apache.log4j.*;
29
30 /** MapDecorator is a Decorator class using the well known decorator pattern.<br>
31  * It modifies the return values of the {@link #getItem(TemplateDirective) } method of the
32  * decorated model using a Map.
33  * @author Stefan Armbruster
34  * @version $Id: MapDecorator.java,v 1.9 2004/02/01 05:16:27 christianc Exp $
35  */

36 public class MapDecorator extends TemplateModelDecorator {
37
38     protected static Logger logger = Logger.getLogger(MapDecorator.class);
39     protected Map mapping;
40
41      public MapDecorator(TemplateModel tm) {
42         super(tm);
43      }
44
45     /** Creates a new instance of MapDecorator
46      * @param tm decorated model
47      * @param mapping Table containing the mappings, the keys represent the the keys in the {@link
48      * #getItem(TemplateDirective)} method
49      */

50     public MapDecorator(TemplateModel tm, Map mapping) {
51         super(tm);
52             setMapping(mapping);
53     }
54
55      public void setMapping(Map mapping) {
56         this.mapping = mapping;
57         if (logger.isDebugEnabled()) {
58             Iterator iter = mapping.entrySet().iterator();
59             while (iter.hasNext()) {
60                 Map.Entry entry = (Map.Entry)iter.next();
61                 logger.debug("Mapping table: key: " + entry.getKey() + " value: " + entry.getValue());
62             }
63         }
64         }
65
66     /** getItem first checks, if the is a matching entry in {@link #mapping }. If so,
67      * the value of the decorated model is modified depending on the type of the mapper
68      * entry. The following modifications are possible: <br>
69      * <B>Lookup map, implements {@link java.util.Map }</B>
70      * If the Map contains the value of the underlying model, it is replaced by the
71      * value the the lookup map.
72      * <B>formatting to a specific numeric format, implements {@link java.text.NumberFormat }</B>
73      * <B>formatting to a specific date format, implements {@link java.text.DateFormat }</B>
74      * <B>formatting to a user defined class, must implement UserMapping</B>
75      * <B>returning a {@link org.enhydra.barracuda.core.comp.BAction }, implements {@link
76      * org.enhydra.barracuda.core.event.ControlEvent }</B>
77      * If the value of the mapping table is a ControlEvent, a BAction with the
78      * ControlEvent as href is returned.
79      * @param td the template directive
80      * @return modified return value of underlying model
81      */

82 //csc_030703.1 public Object getItem(String key) {
83
public Object JavaDoc getItem(TemplateDirective td) { //csc_030703.1
84
String JavaDoc key = td.getKeyName(); //csc_030703.1
85

86 //csc_030703.1 logger.debug("getItem called, key : "+key);
87
//csc_030703.1 Object value = _templateModel.getItem(key);
88
logger.debug("getItem called, td : "+td); //csc_030703.1
89
Object JavaDoc value = _templateModel.getItem(td); //csc_030703.1
90
logger.debug("_templateModels's getItem called, value : "+value);
91         Object JavaDoc mapper = mapping.get(key);
92         if (logger.isDebugEnabled()) {
93             if (mapper==null) {
94                 logger.debug("mapper is null");
95             } else {
96                 logger.debug("mapper's class is " + mapper.getClass().getName());
97             }
98         }
99
100         if (mapper instanceof Map) {
101 //csc_030703.1 value=mapItemAsLookup(key, _templateModel.getItem(key), (Map)mapper );
102
value=mapItemAsLookup(key, _templateModel.getItem(td), (Map)mapper ); //csc_030703.1
103
} else if (mapper instanceof NumberFormat) {
104 //csc_030703.1 value=mapItemAsNumberFormat(key, _templateModel.getItem(key), (NumberFormat)mapper );
105
value=mapItemAsNumberFormat(key, _templateModel.getItem(td), (NumberFormat)mapper ); //csc_030703.1
106
} else if (mapper instanceof DateFormat) {
107 //csc_030703.1 value=mapItemAsDateFormat(key, _templateModel.getItem(key), (DateFormat)mapper );
108
value=mapItemAsDateFormat(key, _templateModel.getItem(td), (DateFormat)mapper ); //csc_030703.1
109
} else if (mapper instanceof ControlEvent) {
110 //csc_030703.1 value=mapItemAsEvent(key, _templateModel.getItem(key), (ControlEvent) mapper);
111
value=mapItemAsEvent(key, _templateModel.getItem(td), (ControlEvent) mapper); //csc_030703.1
112
} else if (mapper instanceof Link) {
113 //csc_030703.1 value=mapItemAsLink(key, (Link) mapper);
114
value=mapItemAsLink(td, (Link) mapper); //csc_030703.1
115
} else if (mapper instanceof UserMapping) {
116                 value = mapItemAsUserMapping(key, (UserMapping)mapper);
117           } else if (mapper instanceof String JavaDoc) {
118                 value = mapper;
119         } else {
120             if (mapper == null) {
121                 logger.info("mapper is null");
122             } else {
123                 logger.error("invalid mapper class: " + mapper.getClass().getName());
124             }
125         }
126         return value;
127     }
128
129     private Object JavaDoc mapItemAsUserMapping(String JavaDoc key, UserMapping um) {
130                 logger.debug("processing UserMapping");
131             Object JavaDoc value=um.get(key, _templateModel);
132                 logger.debug("got from UserMapping: " + value);
133                 return value;
134     }
135
136
137     private Object JavaDoc mapItemAsLookup(String JavaDoc key, Object JavaDoc value, Map lookup) {
138         Object JavaDoc ret = lookup.get(value);
139         logger.debug("mapItemAsLookup " + key + " value " + value.getClass().getName()+ " ret " + ret);
140         logger.debug("lookup: "+ lookup);
141         if (ret == null) {
142             ret = lookup.get("default");
143             if (ret == null) ret = value;
144         }
145         return ret;
146     }
147
148     private Object JavaDoc mapItemAsNumberFormat(String JavaDoc key, Object JavaDoc value, NumberFormat format) {
149         double d;
150         if (value instanceof String JavaDoc) {
151             d = Double.parseDouble((String JavaDoc)value);
152         } else if (value instanceof Double JavaDoc) {
153             d = ((Double JavaDoc)value).doubleValue();
154         } else {
155             logger.warn("no number conversion possible: key : " + key + " value: " + value);
156             return value;
157         }
158         return format.format(d);
159     }
160
161     private Object JavaDoc mapItemAsDateFormat(String JavaDoc key, Object JavaDoc value, DateFormat format) {
162         java.util.Date JavaDoc d;
163         if (value instanceof String JavaDoc) {
164             DateFormat dateFormatIn = new SimpleDateFormat();
165             try {
166                 d = dateFormatIn.parse((String JavaDoc)value);
167             } catch (ParseException ex) {
168                 logger.error(ex.getMessage(), ex);
169                 return value;
170             }
171         } else if (value instanceof java.util.Date JavaDoc) {
172             d = (java.util.Date JavaDoc)value;
173         } else {
174             logger.warn("no number conversion possible: key : " + key + " value: " + value);
175             return value;
176         }
177         return format.format(d);
178     }
179
180     private Object JavaDoc mapItemAsEvent(String JavaDoc key, Object JavaDoc value, ControlEvent event) {
181         BAction ret = new BLink((String JavaDoc)value, event);
182         ret.setParam(key, (String JavaDoc)value);
183         return ret;
184     }
185
186 //csc_030703.1 private Object mapItemAsLink(String key, Link link) {
187
private Object JavaDoc mapItemAsLink(TemplateDirective td, Link link) { //csc_030703.1
188
logger.debug("building link to " + link.getEvent().getClass().getName());
189             String JavaDoc label = link.getLabel();
190             if (!link.isLabelStatic()) {
191 //csc_030703.1 label = (String)_templateModel.getItem(label);
192
label = (String JavaDoc)_templateModel.getItem(new TemplateDirective(td.getCommand(), td.getModelName(), label, td.getKeyData())); //csc_030703.1
193
}
194
195             BAction ret;
196             if (label == null) {
197                 ret = new BAction(link.getEvent());
198             } else {
199                 ret = new BLink(label, link.getEvent());
200             }
201
202             // add link parameters
203
Iterator iter = link.getIterator();
204             while (iter.hasNext()) {
205                 Map.Entry entry = (Map.Entry) iter.next();
206                 String JavaDoc k = (String JavaDoc) entry.getKey();
207                 StringBoolCompound c = (StringBoolCompound) entry.getValue();
208
209                 String JavaDoc v = c.getString();
210                 if (!c.getBool()) {
211 //csc_030703.1 v = (String) _templateModel.getItem( v);
212
v = (String JavaDoc) _templateModel.getItem(new TemplateDirective(td.getCommand(), td.getModelName(), v, td.getKeyData())); //csc_030703.1
213
}
214                 ret.setParam(k, v);
215                 logger.debug("setting link parameter " +k+ " to " + v);
216
217             }
218         return ret;
219     }
220
221     public static class Link {
222         protected ControlEvent event;
223         protected String JavaDoc label;
224         protected boolean isLabelStatic;
225
226         protected Map parameters = new HashMap();
227
228         public Link(ControlEvent event, String JavaDoc label, boolean isLabelStatic) {
229             this.event = event;
230             this.label = label;
231             this.isLabelStatic = isLabelStatic;
232         }
233
234         public String JavaDoc getLabel() {
235             return label;
236         }
237
238         public ControlEvent getEvent() {
239             return event;
240         }
241
242         public boolean isLabelStatic() {
243             return isLabelStatic;
244         }
245
246         public Link addParameter(String JavaDoc key, String JavaDoc value, boolean isStatic) {
247             parameters.put(key, new StringBoolCompound(value, isStatic));
248             return this;
249         }
250
251         public Iterator getIterator() {
252             return parameters.entrySet().iterator();
253         }
254     }
255
256     public static class StringBoolCompound {
257         protected String JavaDoc string;
258         protected boolean bool;
259         public StringBoolCompound(String JavaDoc string, boolean bool) {
260             this.string = string;
261             this.bool = bool;
262         }
263
264         public String JavaDoc getString() {
265             return string;
266         }
267
268         public boolean getBool() {
269             return bool;
270         }
271     }
272
273     /** Interface for handling user defined mappings */
274     public static interface UserMapping {
275         public Object JavaDoc get(String JavaDoc s, TemplateModel tm );
276     }
277
278 }
279
Popular Tags