1 package org.apache.turbine.services.intake.model; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.apache.commons.pool.BaseKeyedPoolableObjectFactory; 29 30 import org.apache.turbine.om.Retrievable; 31 import org.apache.turbine.services.intake.IntakeException; 32 import org.apache.turbine.services.intake.TurbineIntake; 33 import org.apache.turbine.services.intake.xmlmodel.AppData; 34 import org.apache.turbine.services.intake.xmlmodel.XmlField; 35 import org.apache.turbine.services.intake.xmlmodel.XmlGroup; 36 import org.apache.turbine.util.TurbineException; 37 import org.apache.turbine.util.parser.ValueParser; 38 39 47 public class Group 48 { 49 public static final String EMPTY = ""; 50 51 54 public static final String NEW = "_0"; 55 56 private static final Log log; 57 private static final boolean isDebugEnabled; 58 59 static 60 { 61 log = LogFactory.getLog(Group.class); 62 isDebugEnabled = log.isDebugEnabled(); 63 } 64 65 69 protected final String gid; 70 71 74 protected final String name; 75 76 79 private final int poolCapacity; 80 81 84 protected Map fields; 85 86 89 protected Map mapToObjectFields; 90 91 94 protected Field[] fieldsArray; 95 96 100 protected String oid; 101 102 105 protected ValueParser pp; 106 107 110 protected boolean isDeclared; 111 112 120 public Group(XmlGroup group) throws IntakeException 121 { 122 gid = group.getKey(); 123 name = group.getName(); 124 poolCapacity = Integer.parseInt(group.getPoolCapacity()); 125 126 List inputFields = group.getFields(); 127 int size = inputFields.size(); 128 fields = new HashMap ((int) (1.25 * size + 1)); 129 mapToObjectFields = new HashMap ((int) (1.25 * size + 1)); 130 fieldsArray = new Field[size]; 131 for (int i = size - 1; i >= 0; i--) 132 { 133 XmlField f = (XmlField) inputFields.get(i); 134 Field field = FieldFactory.getInstance(f, this); 135 fieldsArray[i] = field; 136 fields.put(f.getName(), field); 137 138 List tmpFields = (List ) mapToObjectFields.get(f.getMapToObject()); 140 if (tmpFields == null) 141 { 142 tmpFields = new ArrayList (size); 143 mapToObjectFields.put(f.getMapToObject(), tmpFields); 144 } 145 tmpFields.add(field); 146 } 147 148 for (Iterator keys = mapToObjectFields.keySet().iterator(); keys.hasNext();) 150 { 151 Object key = keys.next(); 152 List tmpFields = (List ) mapToObjectFields.get(key); 153 mapToObjectFields.put(key, 154 tmpFields.toArray(new Field[tmpFields.size()])); 155 } 156 } 157 158 164 public Group init(ValueParser pp) throws TurbineException 165 { 166 return init(NEW, pp); 167 } 168 169 176 public Group init(String key, ValueParser pp) throws IntakeException 177 { 178 this.oid = key; 179 this.pp = pp; 180 for (int i = fieldsArray.length - 1; i >= 0; i--) 181 { 182 fieldsArray[i].init(pp); 183 } 184 return this; 185 } 186 187 193 public Group init(Retrievable obj) 194 { 195 this.oid = obj.getQueryKey(); 196 197 Class cls = obj.getClass(); 198 while (cls != null) 199 { 200 Field[] flds = (Field[]) mapToObjectFields.get(cls.getName()); 201 if (flds != null) 202 { 203 for (int i = flds.length - 1; i >= 0; i--) 204 { 205 flds[i].init(obj); 206 } 207 } 208 209 cls = cls.getSuperclass(); 210 } 211 212 return this; 213 } 214 215 220 public String [] getFieldNames() 221 { 222 String nameList[] = new String [fieldsArray.length]; 223 for (int i = 0; i < nameList.length; i++) 224 { 225 nameList[i] = fieldsArray[i].name; 226 } 227 return nameList; 228 } 229 230 236 public String getIntakeGroupName() 237 { 238 return name; 239 } 240 241 246 public int getPoolCapacity() 247 { 248 return poolCapacity; 249 } 250 251 257 public String getGID() 258 { 259 return gid; 260 } 261 262 268 public String getOID() 269 { 270 return oid; 271 } 272 273 278 public String getObjectKey() 279 { 280 return gid + oid; 281 } 282 283 290 public ArrayList getObjects(ValueParser pp) throws IntakeException 291 { 292 ArrayList objs = null; 293 String [] oids = pp.getStrings(gid); 294 if (oids != null) 295 { 296 objs = new ArrayList (oids.length); 297 for (int i = oids.length - 1; i >= 0; i--) 298 { 299 objs.add(TurbineIntake.getGroup(name).init(oids[i], pp)); 300 } 301 } 302 return objs; 303 } 304 305 310 public Field get(String fieldName) 311 throws IntakeException 312 { 313 if (fields.containsKey(fieldName)) 314 { 315 return (Field) fields.get(fieldName); 316 } 317 else 318 { 319 throw new IntakeException("Intake Field name: " + fieldName + 320 " not found!"); 321 } 322 } 323 324 329 public boolean isAllValid() 330 { 331 boolean valid = true; 332 for (int i = fieldsArray.length - 1; i >= 0; i--) 333 { 334 valid &= fieldsArray[i].isValid(); 335 if (isDebugEnabled && !fieldsArray[i].isValid()) 336 { 337 log.debug("Group(" + oid + "): " + name + "; Field: " 338 + fieldsArray[i].name + "; value=" + 339 fieldsArray[i].getValue() + " is invalid!"); 340 } 341 } 342 return valid; 343 } 344 345 352 public void setProperties(Object obj) throws IntakeException 353 { 354 Class cls = obj.getClass(); 355 356 while (cls != null) 357 { 358 if (isDebugEnabled) 359 { 360 log.debug("setProperties(" + cls.getName() + ")"); 361 } 362 363 Field[] flds = (Field[]) mapToObjectFields.get(cls.getName()); 364 if (flds != null) 365 { 366 for (int i = flds.length - 1; i >= 0; i--) 367 { 368 flds[i].setProperty(obj); 369 } 370 } 371 372 cls = cls.getSuperclass(); 373 } 374 log.debug("setProperties() finished"); 375 } 376 377 384 public void setValidProperties(Object obj) 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 try 395 { 396 flds[i].setProperty(obj); 397 } 398 catch (Exception e) 399 { 400 } 402 } 403 } 404 405 cls = cls.getSuperclass(); 406 } 407 } 408 409 419 public void getProperties(Object obj) throws IntakeException 420 { 421 Class cls = obj.getClass(); 422 while (cls != null) 423 { 424 Field[] flds = (Field[]) mapToObjectFields.get(cls.getName()); 425 if (flds != null) 426 { 427 for (int i = flds.length - 1; i >= 0; i--) 428 { 429 flds[i].getProperty(obj); 430 } 431 } 432 433 cls = cls.getSuperclass(); 434 } 435 } 436 437 441 public void removeFromRequest() 442 { 443 if (pp != null) 444 { 445 String [] groups = pp.getStrings(gid); 446 if (groups != null) 447 { 448 pp.remove(gid); 449 for (int i = 0; i < groups.length; i++) 450 { 451 if (groups[i] != null && !groups[i].equals(oid)) 452 { 453 pp.add(gid, groups[i]); 454 } 455 } 456 for (int i = fieldsArray.length - 1; i >= 0; i--) 457 { 458 fieldsArray[i].removeFromRequest(); 459 } 460 } 461 } 462 } 463 464 468 public void resetDeclared() 469 { 470 isDeclared = false; 471 } 472 473 479 public String getHtmlFormInput() 480 { 481 StringBuffer sb = new StringBuffer (64); 482 appendHtmlFormInput(sb); 483 return sb.toString(); 484 } 485 486 490 public void appendHtmlFormInput(StringBuffer sb) 491 { 492 if (!isDeclared) 493 { 494 isDeclared = true; 495 sb.append("<input type=\"hidden\" name=\"") 496 .append(gid) 497 .append("\" value=\"") 498 .append(oid) 499 .append("\"/>\n"); 500 } 501 } 502 503 505 public static class GroupFactory 506 extends BaseKeyedPoolableObjectFactory 507 { 508 private AppData appData; 509 510 public GroupFactory(AppData appData) 511 { 512 this.appData = appData; 513 } 514 515 520 public Object makeObject(Object key) throws IntakeException 521 { 522 return new Group(appData.getGroup((String ) key)); 523 } 524 525 529 public void passivateObject(Object key, Object obj) 530 { 531 Group group = (Group) obj; 532 group.oid = null; 533 group.pp = null; 534 for (int i = group.fieldsArray.length - 1; i >= 0; i--) 535 { 536 group.fieldsArray[i].dispose(); 537 } 538 group.isDeclared = false; 539 } 540 } 541 } 542 543 544 | Popular Tags |