1 30 package com.genimen.djeneric.tools.specifier.editor; 31 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 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 54 public class DjenericContextManager 55 { 56 private HashMap _context = new HashMap (10); 57 DjenericResourceManager _resourceManager; 58 HashMap _parameters = new HashMap (); 59 60 63 public DjenericContextManager(DjenericResourceManager resourceManager) 64 { 65 _resourceManager = resourceManager; 66 } 67 68 public void addParameter(String id, DjObject obj) 69 { 70 _parameters.put(id, obj); 71 } 72 73 public DjObject getParameter(String id) 74 { 75 return (DjObject) _parameters.get(id); 76 } 77 78 public void registerViewer(String id, ObjectViewer viewer) 79 { 80 _context.put(id.toLowerCase(), viewer); 81 } 82 83 public ObjectViewer getViewer(String id) 84 { 85 return (ObjectViewer) _context.get(id.toLowerCase()); 86 } 87 88 public Object evaluatePath(String 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 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 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 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 evaluateParameter(String path, DjList objects, DjObject object, PropertyUsage pu) 132 throws DjenericException 133 { 134 String 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 evaluateFunction(String path, DjList objects, DjObject object, PropertyUsage pu) 157 throws DjenericException 158 { 159 DjDomain domain = null; 160 String 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 sdf = new SimpleDateFormat (fmt); 173 return sdf.format(new Date ()); 174 } 175 return new Date ().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 propName = pu.getPropertyName(); 196 int seq = 0; 197 Iterator 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 propName = pu.getPropertyName(); 211 int seq = 0; 212 Iterator 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 |