1 package org.apache.fulcrum.intake; 2 3 56 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.List ; 60 import java.util.Map ; 61 62 import org.apache.fulcrum.intake.model.Group; 63 import org.apache.fulcrum.parser.ValueParser; 64 import org.apache.fulcrum.pool.Recyclable; 65 import org.apache.log4j.Category; 66 67 73 public class Intake 74 implements Recyclable 75 { 76 public static final String DEFAULT_KEY = "_0"; 77 private HashMap groups; 78 private ValueParser pp; 79 80 HashMap declaredGroups = new HashMap (); 81 StringBuffer allGroupsSB = new StringBuffer (256); 82 StringBuffer groupSB = new StringBuffer (128); 83 84 85 private Map pullMap; 86 87 90 Category category = Category.getInstance(getClass().getName()); 91 92 public Intake() 93 { 94 String [] groupNames = TurbineIntake.getGroupNames(); 95 groups = new HashMap ((int)(1.25*groupNames.length + 1)); 96 pullMap = new HashMap ((int)(1.25*groupNames.length + 1)); 97 99 for (int i=groupNames.length-1; i>=0; i--) 100 { 101 pullMap.put(groupNames[i], new PullHelper(groupNames[i])); 102 } 103 } 104 105 108 public void init(ValueParser pp) 109 { 110 this.pp = pp; 111 String [] groupKeys = pp.getStrings("intake-grp"); 112 String [] groupNames = null; 113 if ( groupKeys == null || groupKeys.length == 0 ) 114 { 115 groupNames = TurbineIntake.getGroupNames(); 116 } 117 else 118 { 119 groupNames = new String [groupKeys.length]; 120 for ( int i=groupKeys.length-1; i>=0; i-- ) 121 { 122 groupNames[i] = TurbineIntake.getGroupName(groupKeys[i]); 123 } 124 125 } 126 127 for (int i=groupNames.length-1; i>=0; i--) 128 { 129 try 130 { 131 List foundGroups = TurbineIntake.getGroup(groupNames[i]) 132 .getObjects(pp); 133 134 if ( foundGroups != null ) 135 { 136 Iterator iter = foundGroups.iterator(); 137 while (iter.hasNext()) 138 { 139 Group group = (Group)iter.next(); 140 groups.put(group.getObjectKey(), group); 141 } 142 } 143 } 144 catch(Exception e) 145 { 146 category.error("", e); 147 } 148 } 149 } 150 151 public void addGroupsToParameters(ValueParser vp) 152 { 153 Iterator i = groups.values().iterator(); 154 while ( i.hasNext() ) 155 { 156 Group group = (Group)i.next(); 157 if ( !declaredGroups.containsKey(group.getIntakeGroupName()) ) 158 { 159 declaredGroups.put(group.getIntakeGroupName(), null); 160 vp.add("intake-grp", group.getGID()); 161 } 162 vp.add(group.getGID(), group.getOID()); 163 } 164 declaredGroups.clear(); 165 } 166 167 176 public String declareGroups() 177 { 178 allGroupsSB.setLength(0); 179 Iterator i = groups.values().iterator(); 180 while ( i.hasNext() ) 181 { 182 declareGroup( (Group)i.next(), allGroupsSB ); 183 } 184 return allGroupsSB.toString(); 185 } 186 187 191 public String declareGroup(Group group) 192 { 193 groupSB.setLength(0); 194 declareGroup(group, groupSB); 195 return groupSB.toString(); 196 } 197 198 202 public void declareGroup(Group group, StringBuffer sb) 203 { 204 if ( !declaredGroups.containsKey(group.getIntakeGroupName()) ) 205 { 206 declaredGroups.put(group.getIntakeGroupName(), null); 207 sb.append("<input type=\"hidden\" name=\"") 208 .append("intake-grp\" value=\"") 209 .append(group.getGID()) 210 .append("\"/>\n"); 211 } 212 group.appendHtmlFormInput(sb); 213 } 214 215 public void newForm() 216 { 217 declaredGroups.clear(); 218 Iterator i = groups.values().iterator(); 219 while ( i.hasNext() ) 220 { 221 ((Group)i.next()).resetDeclared(); 222 } 223 } 224 225 228 public class PullHelper 229 { 230 String groupName; 231 232 private PullHelper(String groupName) 233 { 234 this.groupName = groupName; 235 } 236 237 public Group getDefault() 238 throws Exception 239 { 240 return setKey(DEFAULT_KEY); 241 } 242 243 public Group setKey(String key) 244 throws Exception 245 { 246 return setKey(key, true); 247 } 248 249 public Group setKey(String key, boolean create) 250 throws Exception 251 { 252 Group g = null; 253 254 String inputKey = TurbineIntake.getGroupKey(groupName) + key; 255 if ( groups.containsKey(inputKey)) 256 { 257 g = (Group)groups.get(inputKey); 258 } 259 else if (create) 260 { 261 g = TurbineIntake.getGroup(groupName); 262 groups.put(inputKey, g); 263 g.init(key, pp); 264 } 265 266 return g; 267 } 268 269 270 public Group mapTo(Retrievable obj) 271 throws Exception 272 { 273 Group g = null; 274 275 try 276 { 277 String inputKey = TurbineIntake.getGroupKey(groupName) 278 + obj.getQueryKey(); 279 if ( groups.containsKey(inputKey)) 280 { 281 g = (Group)groups.get(inputKey); 282 } 283 else 284 { 285 g = TurbineIntake.getGroup(groupName); 286 groups.put(inputKey, g); 287 } 288 return g.init(obj); 289 } 290 catch(Exception e) 291 { 292 category.error("", e); 293 } 294 295 return null; 296 } 297 } 298 299 302 public PullHelper get(String groupName) 303 throws Exception 304 { 305 return (PullHelper)pullMap.get(groupName); 306 } 307 308 312 public boolean isAllValid() 313 { 314 boolean allValid = true; 315 Iterator iter = groups.values().iterator(); 316 while (iter.hasNext()) 317 { 318 Group group = (Group)iter.next(); 319 allValid &= group.isAllValid(); 320 } 321 return allValid; 322 } 323 324 327 public Group get(String groupName, String key) 328 throws Exception 329 { 330 if (groupName == null) 331 { 332 throw new Exception ("Intake.get: groupName == null"); 333 } 334 if (key == null) 335 { 336 throw new Exception ("Intake.get: key == null"); 337 } 338 return ((PullHelper)get(groupName)).setKey(key); 339 } 340 341 345 public Group get(String groupName, String key, boolean create) 346 throws Exception 347 { 348 return ((PullHelper)get(groupName)).setKey(key, create); 349 } 350 351 356 public void remove(Group group) 357 { 358 groups.remove(group.getObjectKey()); 359 group.removeFromRequest(); 360 TurbineIntake.releaseGroup(group); 361 } 362 363 368 public void removeAll() 369 { 370 Object [] allGroups = groups.values().toArray(); 371 for (int i=allGroups.length-1; i>=0; i-- ) 372 { 373 Group group = (Group)allGroups[i]; 374 remove(group); 375 } 376 } 377 378 379 381 private boolean disposed; 382 383 393 public void recycle() 394 { 395 disposed = false; 396 } 397 398 403 public void dispose() 404 { 405 Iterator iter = groups.values().iterator(); 406 while ( iter.hasNext() ) 407 { 408 Group g = (Group)iter.next(); 409 TurbineIntake.releaseGroup(g); 410 } 411 412 groups.clear(); 413 declaredGroups.clear(); 414 pp = null; 415 416 disposed = true; 417 } 418 419 423 public boolean isDisposed() 424 { 425 return disposed; 426 } 427 428 429 } 430 431 432 433 434 435 436 | Popular Tags |