KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > editor > DjenericContextManager


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.editor;
31
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.DjDomain;
39 import com.genimen.djeneric.repository.DjList;
40 import com.genimen.djeneric.repository.DjObject;
41 import com.genimen.djeneric.repository.exceptions.DjenericException;
42 import com.genimen.djeneric.structure.PropertyUsage;
43 import com.genimen.djeneric.tools.specifier.interfaces.DjenericResourceManager;
44 import com.genimen.djeneric.tools.specifier.interfaces.ObjectViewer;
45
46 /**
47  * @author Wido Riezebos
48  *
49  * To change this generated comment edit the template variable "typecomment":
50  * Window>Preferences>Java>Templates.
51  * To enable and disable the creation of type comments go to
52  * Window>Preferences>Java>Code Generation.
53  */

54 public class DjenericContextManager
55 {
56   private HashMap JavaDoc _context = new HashMap JavaDoc(10);
57   DjenericResourceManager _resourceManager;
58   HashMap JavaDoc _parameters = new HashMap JavaDoc();
59
60   /**
61    * Constructor for DjenericContextManager.
62    */

63   public DjenericContextManager(DjenericResourceManager resourceManager)
64   {
65     _resourceManager = resourceManager;
66   }
67
68   public void addParameter(String JavaDoc id, DjObject obj)
69   {
70     _parameters.put(id, obj);
71   }
72
73   public DjObject getParameter(String JavaDoc id)
74   {
75     return (DjObject) _parameters.get(id);
76   }
77
78   public void registerViewer(String JavaDoc id, ObjectViewer viewer)
79   {
80     _context.put(id.toLowerCase(), viewer);
81   }
82
83   public ObjectViewer getViewer(String JavaDoc id)
84   {
85     return (ObjectViewer) _context.get(id.toLowerCase());
86   }
87
88   public Object JavaDoc evaluatePath(String JavaDoc path, DjList objects, DjObject object, PropertyUsage pu) throws DjenericException
89   {
90     if (path == null || path.trim().length() == 0) return null;
91
92     if (path.startsWith(":"))
93     {
94       return evaluateParameter(path, objects, object, pu);
95     }
96
97     if (path.startsWith("$"))
98     {
99       return evaluateFunction(path, objects, object, pu);
100     }
101
102     int idx = path.indexOf(".");
103
104     String JavaDoc rootName = path;
105     if (idx != -1)
106     {
107       rootName = path.substring(0, idx);
108       path = path.substring(idx + 1);
109     }
110
111     ObjectViewer rootView = getViewer(rootName);
112     if (rootView == null)
113     {
114       String JavaDoc descr = "";
115       if (pu != null) descr += pu.getPropertyName() + "; expr=";
116       descr += path;
117
118       throw new DjenericException(Messages.getString("DjenericObjectModel.UnknownIdentifier", rootName, descr));
119     }
120     DjObject obj = rootView.getSelectedValue();
121
122     Object JavaDoc result = obj;
123     if (path.trim().length() > 0)
124     {
125       if (path.indexOf("+") != -1 || path.indexOf("\"") != -1) result = obj.evaluateStringExpression(path);
126       else result = obj.evaluateObjectExpression(path);
127     }
128     return result;
129   }
130
131   private Object JavaDoc evaluateParameter(String JavaDoc path, DjList objects, DjObject object, PropertyUsage pu)
132       throws DjenericException
133   {
134     String JavaDoc paramName;
135     int dotIdx = Math.min(path.indexOf('.'), path.indexOf('+'));
136     if (dotIdx != -1)
137     {
138       paramName = path.substring(1, dotIdx).trim();
139       path = path.substring(dotIdx + 1).trim();
140     }
141     else
142     {
143       paramName = path.substring(1).trim();
144       path = "";
145     }
146
147     DjObject contextObject = getParameter(paramName);
148     if (contextObject == null) return null;
149     if (path.length() == 0) return contextObject;
150
151     if (path.indexOf("+") != -1 || path.indexOf("\"") != -1) return contextObject.evaluateStringExpression(path);
152     else return contextObject.evaluateObjectExpression(path);
153
154   }
155
156   public Object JavaDoc evaluateFunction(String JavaDoc path, DjList objects, DjObject object, PropertyUsage pu)
157       throws DjenericException
158   {
159     DjDomain domain = null;
160     String JavaDoc fmt = null;
161
162     if (pu != null && pu.getProperty().getType() instanceof DjDomain)
163     {
164       domain = (DjDomain) pu.getProperty().getType();
165       fmt = domain.getFormatMask();
166     }
167
168     if (path.equalsIgnoreCase("$timestamp"))
169     {
170       if (fmt != null)
171       {
172         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(fmt);
173         return sdf.format(new Date JavaDoc());
174       }
175       return new Date JavaDoc().toString();
176     }
177
178     if (path.equalsIgnoreCase("$user"))
179     {
180       return object.getSession().getPersistenceManager().getCurrentUser().getCode();
181     }
182
183     if (path.equalsIgnoreCase("$username"))
184     {
185       return object.getSession().getPersistenceManager().getCurrentUser().getName();
186     }
187
188     if (path.equalsIgnoreCase("$count"))
189     {
190       return String.valueOf(objects.size() + 1);
191     }
192
193     if (path.equalsIgnoreCase("$seq"))
194     {
195       String JavaDoc propName = pu.getPropertyName();
196       int seq = 0;
197       Iterator JavaDoc it = objects.iterator();
198       while (it.hasNext())
199       {
200         DjObject o = (DjObject) it.next();
201         int i = o.getInt(propName);
202         if (i > seq) seq = i;
203       }
204       seq += 10;
205       return String.valueOf(seq);
206     }
207
208     if (path.equalsIgnoreCase("$unique"))
209     {
210       String JavaDoc propName = pu.getPropertyName();
211       int seq = 0;
212       Iterator JavaDoc it = objects.iterator();
213       while (it.hasNext())
214       {
215         DjObject o = (DjObject) it.next();
216         int i = o.getInt(propName);
217         if (i > seq) seq = i;
218       }
219       seq += 1;
220       return String.valueOf(seq);
221     }
222
223     throw new DjenericException(Messages.getString("DjenericContextManager.UndefinedFunction", path));
224   }
225
226   public DjenericResourceManager getResourceManager()
227   {
228     return _resourceManager;
229   }
230
231 }
Popular Tags