1 package org.apache.fulcrum.intake.model; 2 3 56 57 import java.util.ArrayList ; 58 import java.util.HashMap ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 63 import org.apache.commons.pool.BaseKeyedPoolableObjectFactory; 64 import org.apache.fulcrum.ServiceException; 65 import org.apache.fulcrum.intake.Retrievable; 66 import org.apache.fulcrum.intake.TurbineIntake; 67 import org.apache.fulcrum.intake.xmlmodel.AppData; 68 import org.apache.fulcrum.intake.xmlmodel.XmlField; 69 import org.apache.fulcrum.intake.xmlmodel.XmlGroup; 70 import org.apache.fulcrum.parser.ValueParser; 71 import org.apache.log4j.Category; 72 73 74 public class Group 75 { 76 public static final String EMPTY = ""; 77 78 81 public static final String NEW = "_0"; 82 83 private static final Category log; 84 private static final boolean isDebugEnabled; 85 86 static 87 { 88 log = Category.getInstance(Group.class.getName()); 89 isDebugEnabled = log.isDebugEnabled(); 90 } 91 92 96 protected final String gid; 97 98 101 protected final String name; 102 103 106 private final int poolCapacity; 107 108 111 protected Map fields; 112 113 116 protected Map mapToObjectFields; 117 118 121 protected Field[] fieldsArray; 122 123 127 protected String oid; 128 129 132 protected ValueParser pp; 133 134 137 protected boolean isDeclared; 138 139 147 public Group(XmlGroup group) 148 throws Exception 149 { 150 gid = group.getKey(); 151 name = group.getName(); 152 poolCapacity = Integer.parseInt(group.getPoolCapacity()); 153 154 List inputFields = group.getFields(); 155 int size = inputFields.size(); 156 fields = new HashMap ((int)(1.25*size + 1)); 157 mapToObjectFields = new HashMap ((int)(1.25*size + 1)); 158 fieldsArray = new Field[size]; 159 for (int i=size-1; i>=0; i--) 160 { 161 XmlField f = (XmlField)inputFields.get(i); 162 Field field = FieldFactory.getInstance(f, this); 163 fieldsArray[i]= field; 164 fields.put(f.getName(), field); 165 166 List tmpFields = (List )mapToObjectFields.get(f.getMapToObject()); 168 if ( tmpFields == null ) 169 { 170 tmpFields = new ArrayList (size); 171 mapToObjectFields.put(f.getMapToObject(), tmpFields); 172 } 173 tmpFields.add(field); 174 } 175 176 Iterator keys = mapToObjectFields.keySet().iterator(); 178 while ( keys.hasNext() ) 179 { 180 Object key = keys.next(); 181 List tmpFields = (List )mapToObjectFields.get(key); 182 mapToObjectFields.put(key, 183 tmpFields.toArray(new Field[tmpFields.size()])); 184 } 185 } 186 187 193 public Group init(ValueParser pp) throws ServiceException 194 { 195 return init(NEW, pp); 196 } 197 198 205 public Group init(String key, ValueParser pp) 206 throws ServiceException 207 { 208 this.oid = key; 209 this.pp = pp; 210 for (int i=fieldsArray.length-1; i>=0; i--) 211 { 212 fieldsArray[i].init(pp); 213 } 214 return this; 215 } 216 217 218 224 public Group init(Retrievable obj) 225 { 226 this.oid = obj.getQueryKey(); 227 228 Class cls = obj.getClass(); 229 while ( cls != null ) 230 { 231 Field[] flds = (Field[])mapToObjectFields.get(cls.getName()); 232 if ( flds != null ) 233 { 234 for (int i=flds.length-1; i>=0; i--) 235 { 236 flds[i].init(obj); 237 } 238 } 239 240 cls = cls.getSuperclass(); 241 } 242 243 return this; 244 } 245 246 247 252 public String [] getFieldNames() 253 { 254 String nameList[] = new String [fieldsArray.length]; 255 for(int i = 0; i < nameList.length; i++) 256 { 257 nameList[i] = fieldsArray[i].name; 258 } 259 return nameList; 260 } 261 262 263 269 public String getIntakeGroupName() 270 { 271 return name; 272 } 273 274 279 public int getPoolCapacity() 280 { 281 return poolCapacity; 282 } 283 284 290 public String getGID() 291 { 292 return gid; 293 } 294 295 301 public String getOID() 302 { 303 return oid; 304 } 305 306 311 public String getObjectKey() 312 { 313 return gid + oid; 314 } 315 316 323 public ArrayList getObjects(ValueParser pp) 324 throws ServiceException 325 { 326 ArrayList objs = null; 327 String [] oids = pp.getStrings(gid); 328 if (oids != null) 329 { 330 objs = new ArrayList (oids.length); 331 for (int i=oids.length-1; i>=0; i--) 332 { 333 objs.add( TurbineIntake.getGroup(name).init(oids[i], pp) ); 334 } 335 } 336 return objs; 337 } 338 339 343 public Field get(String fieldName) 344 throws ServiceException 345 { 346 if (fields.containsKey(fieldName)) 347 { 348 return (Field)fields.get(fieldName); 349 } 350 else 351 { 352 throw new ServiceException ("Intake Field name: " + fieldName + 353 " not found!"); 354 } 355 } 356 357 362 public boolean isAllValid() 363 { 364 boolean valid = true; 365 for (int i=fieldsArray.length-1; i>=0; i--) 366 { 367 valid &= fieldsArray[i].isValid(); 368 if ( isDebugEnabled && !fieldsArray[i].isValid()) 369 { 370 log.debug("[Intake] Group(" + oid + "): " + name + "; Field: " 371 + fieldsArray[i].name + "; value=" + 372 fieldsArray[i].getValue() + " is invalid!"); 373 } 374 } 375 return valid; 376 } 377 378 383 public void setProperties(Object obj) 384 throws ServiceException 385 { 386 Class cls = obj.getClass(); 387 while ( cls != null ) 388 { 389 Field[] flds = (Field[])mapToObjectFields.get(cls.getName()); 390 if ( flds != null ) 391 { 392 for (int i=flds.length-1; i>=0; i--) 393 { 394 flds[i].setProperty(obj); 395 } 396 } 397 398 cls = cls.getSuperclass(); 399 } 400 } 401 408 public void setValidProperties(Object obj) 409 { 410 Class cls = obj.getClass(); 411 while ( cls != null ) 412 { 413 Field[] flds = (Field[])mapToObjectFields.get(cls.getName()); 414 if ( flds != null ) 415 { 416 for (int i=flds.length-1; i>=0; i--) 417 { 418 try 419 { 420 flds[i].setProperty(obj); 421 } 422 catch(ServiceException e) 423 { 424 } 426 } 427 } 428 429 cls = cls.getSuperclass(); 430 } 431 } 432 433 438 public void getProperties(Object obj) 439 throws Exception 440 { 441 Class cls = obj.getClass(); 442 while (cls != null) 443 { 444 Field[] flds = (Field[])mapToObjectFields.get(cls.getName()); 445 if( flds != null ) 446 { 447 for (int i=flds.length-1; i>=0; i--) 448 { 449 flds[i].getProperty(obj); 450 } 451 } 452 453 cls = cls.getSuperclass(); 454 } 455 } 456 457 461 public void removeFromRequest() 462 { 463 if (pp != null) 464 { 465 String [] groups = pp.getStrings(gid); 466 if ( groups != null ) 467 { 468 pp.remove(gid); 469 for (int i=0; i<groups.length; i++) 470 { 471 if ( groups[i] != null && !groups[i].equals(oid) ) 472 { 473 pp.add(gid,groups[i]); 474 } 475 } 476 for (int i=fieldsArray.length-1; i>=0; i--) 477 { 478 fieldsArray[i].removeFromRequest(); 479 } 480 } 481 } 482 } 483 484 488 public void resetDeclared() 489 { 490 isDeclared = false; 491 } 492 493 499 public String getHtmlFormInput() 500 { 501 StringBuffer sb = new StringBuffer (64); 502 appendHtmlFormInput(sb); 503 return sb.toString(); 504 } 505 506 510 public void appendHtmlFormInput(StringBuffer sb) 511 { 512 if ( !isDeclared ) 513 { 514 isDeclared = true; 515 sb.append("<input type=\"hidden\" name=\"") 516 .append(gid) 517 .append("\" value=\"") 518 .append(oid) 519 .append("\"/>\n"); 520 } 521 } 522 523 525 public static class GroupFactory 526 extends BaseKeyedPoolableObjectFactory 527 { 528 private AppData appData; 529 530 public GroupFactory(AppData appData) 531 { 532 this.appData = appData; 533 } 534 535 539 public Object makeObject(Object key) 540 throws Exception 541 { 542 return new Group(appData.getGroup((String )key)); 543 } 544 545 549 public void passivateObject(Object key, Object obj) 550 throws Exception 551 { 552 Group group = (Group)obj; 553 group.oid = null; 554 group.pp = null; 555 for (int i=group.fieldsArray.length-1; i>=0; i--) 556 { 557 group.fieldsArray[i].dispose(); 558 } 559 group.isDeclared = false; 560 } 561 } 562 } 563 564 565 | Popular Tags |