1 64 package com.jcorporate.expresso.core.dataobjects.jdbc; 65 66 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 67 import org.apache.commons.digester.Digester; 68 import org.apache.log4j.Logger; 69 import org.xml.sax.SAXException ; 70 71 import javax.xml.parsers.FactoryConfigurationError ; 72 import java.io.InputStream ; 73 import java.net.URL ; 74 import java.util.List ; 75 import java.util.Map ; 76 import java.util.StringTokenizer ; 77 78 79 86 87 public class JoinedDigesterBean { 88 89 92 private static final Logger log = Logger.getLogger(JoinedDigesterBean.class); 93 94 97 private List dataObjects = new java.util.ArrayList (); 98 99 102 private List relations = new java.util.ArrayList (); 103 104 105 private Map permissions = new java.util.HashMap (); 106 107 110 private boolean distinct = false; 111 112 115 private String description = ""; 116 117 118 public JoinedDigesterBean() { 119 } 120 121 public void addDataObject(String className, String definitionName, String alias, String fieldExpressionList) { 122 dataObjects.add(new DigesterJoinedDataObject(className, definitionName, alias, fieldExpressionList)); 123 } 124 125 public void addDataObject(String className, String definitionName, String alias) { 126 addDataObject(className, definitionName, alias, null); 127 } 128 129 138 public void addRelation(String localAlias, String localKey, 139 String foreignAlias, String foreignKey, String joinType) { 140 DigesterJoinRelations newRelation = new DigesterJoinRelations(); 141 142 StringTokenizer stoklocal = new StringTokenizer (localKey, "|"); 143 StringTokenizer stokForeign = new StringTokenizer (foreignKey, "|"); 144 if (stoklocal.countTokens() != stokForeign.countTokens()) { 145 throw new IllegalArgumentException ("Foreign key and local" + 146 " key must have the same number of keys"); 147 } 148 newRelation.setLocalAlias(localAlias); 149 newRelation.setLocalKey(localKey); 150 newRelation.setForeignAlias(foreignAlias); 151 newRelation.setForeignKey(foreignKey); 152 newRelation.setJoinType(joinType); 153 relations.add(newRelation); 154 155 } 156 157 163 public void loadJoinData(URL location) { 164 Digester d = buildDigester(); 165 digest(d, location); 166 } 167 168 176 public void setPermissions(Boolean create, Boolean read, Boolean update, Boolean delete) { 177 178 permissions.put(SecuredDBObject.ADD, create); 179 permissions.put(SecuredDBObject.SEARCH, read); 180 permissions.put(SecuredDBObject.UPDATE, update); 181 permissions.put(SecuredDBObject.DELETE, delete); 182 } 183 184 189 public Map getPermissions() { 190 return permissions; 191 } 192 193 198 public Digester buildDigester() { 199 Digester digester = new Digester(); 200 digester.setClassLoader(Thread.currentThread().getContextClassLoader()); 201 setupResolvers(digester); 202 203 digester.push(this); 204 digester.addSetProperties("dataobject-join"); 205 digester.addCallMethod("dataobject-join/dataobject", "addDataObject", 4); 206 digester.addCallParam("dataobject-join/dataobject", 0, "className"); 207 digester.addCallParam("dataobject-join/dataobject", 1, "definitionName"); 208 digester.addCallParam("dataobject-join/dataobject", 2, "alias"); 209 digester.addCallParam("dataobject-join/dataobject", 3, "fieldExpressionList"); 210 211 digester.addCallMethod("dataobject-join/distinct", "setDistinct", 1, new Class []{java.lang.Boolean .class}); 212 digester.addCallParam("dataobject-join/distinct", 0, "distinctJoin"); 213 214 digester.addCallMethod("dataobject-join/permissions", "setPermissions", 4, 215 new Class []{java.lang.Boolean .class, 216 java.lang.Boolean .class, java.lang.Boolean .class, java.lang.Boolean .class}); 217 digester.addCallParam("dataobject-join/permissions", 0, "add"); 218 digester.addCallParam("dataobject-join/permissions", 1, "read"); 219 digester.addCallParam("dataobject-join/permissions", 2, "update"); 220 digester.addCallParam("dataobject-join/permissions", 3, "delete"); 221 222 digester.addCallMethod("dataobject-join/relations/foreign-key", "addRelation", 5); 223 digester.addCallParam("dataobject-join/relations/foreign-key", 0, "local-alias-ref"); 224 digester.addCallParam("dataobject-join/relations/foreign-key", 1, "local-alias-key"); 225 digester.addCallParam("dataobject-join/relations/foreign-key", 2, "foreign-alias-ref"); 226 digester.addCallParam("dataobject-join/relations/foreign-key", 3, "foreign-alias-key"); 227 digester.addCallParam("dataobject-join/relations/foreign-key", 4, "join-type"); 228 return digester; 229 } 230 231 236 private void setupResolvers(Digester digester) { 237 URL url = this.getClass().getResource("/com/jcorporate/expresso/core/dataobjects/jdbc/jdbc-join_5_1.dtd"); 238 if (url != null) { 239 digester.register("-//Jcorporate Ltd//DTD Expresso DataObject Join 5.1//EN", 240 url.toString()); 241 } else { 242 throw new IllegalArgumentException ("Unable to locate " + 243 "jdbc-join_5_1.dtd in package"); 244 } 245 } 246 247 248 255 public void digest(Digester parser, URL location) { 256 try { 257 InputStream is = location.openStream(); 258 parser.parse(is); 259 } catch (FactoryConfigurationError ex) { 260 log.error("Fatal error trying to find a suitable Digester compatible parser.", ex); 261 } catch (SAXException ex) { 262 log.error("Fatal error trying to digest expresso-services.xml file", ex); 263 } catch (java.io.IOException ex) { 264 log.error("Fatal IO error parsing input.", ex); 265 } 266 267 } 268 269 274 public List getDataObjects() { 275 return dataObjects; 276 } 277 278 283 public boolean isDistinct() { 284 return distinct; 285 } 286 287 293 public void setDistinct(boolean distinct) { 294 this.distinct = distinct; 295 } 296 297 298 304 public void setDistinct(Boolean newValue) { 305 if (newValue != null) { 306 this.distinct = newValue.booleanValue(); 307 } 308 } 309 310 316 public void setDistinct(String newValue) { 317 this.distinct = com.jcorporate.expresso.core.misc.StringUtil.toBoolean(newValue); 318 } 319 320 321 326 public List getRelations() { 327 return relations; 328 } 329 330 public String getDescription() { 331 return description; 332 } 333 334 public void setDescription(String description) { 335 this.description = description; 336 } 337 338 339 345 public class DigesterJoinedDataObject { 346 347 private String definitionName; 348 private String alias; 349 private String className; 350 private String fieldExpressionList; 351 352 private DigesterJoinedDataObject() { 353 } 354 355 public DigesterJoinedDataObject(String name, String definition, String newAlias, 356 String fieldExpression) { 357 className = name; 358 definitionName = definition; 359 alias = newAlias; 360 fieldExpressionList = fieldExpression; 361 } 362 363 public String getClassName() { 364 return this.className; 365 } 366 367 public String getDefinitionName() { 368 return definitionName; 369 } 370 371 public void setDefinitionName(String definitionName) { 372 this.definitionName = definitionName; 373 } 374 375 public void setAlias(String alias) { 376 this.alias = alias; 377 } 378 379 public String getAlias() { 380 return alias; 381 } 382 383 public String getFieldExpressionList() { 384 return fieldExpressionList; 385 } 386 387 } 388 389 395 public class DigesterJoinRelations { 396 397 private String localAlias; 398 private String localKey; 399 private String foreignAlias; 400 private String foreignKey; 401 private String joinType; 402 403 public DigesterJoinRelations() { 404 } 405 406 public String getLocalAlias() { 407 return localAlias; 408 } 409 410 public void setLocalAlias(String localAlias) { 411 this.localAlias = localAlias; 412 } 413 414 public void setLocalKey(String localKey) { 415 this.localKey = localKey; 416 } 417 418 public String getLocalKey() { 419 return localKey; 420 } 421 422 public void setForeignAlias(String foreignAlias) { 423 this.foreignAlias = foreignAlias; 424 } 425 426 public String getForeignAlias() { 427 return foreignAlias; 428 } 429 430 public void setForeignKey(String foreignKey) { 431 this.foreignKey = foreignKey; 432 } 433 434 public String getForeignKey() { 435 return foreignKey; 436 } 437 438 public String getJoinType() { 439 return joinType; 440 } 441 442 public void setJoinType(String newValue) { 443 joinType = newValue; 444 } 445 } 446 } 447 | Popular Tags |