1 package org.apache.turbine.services.intake;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.fulcrum.intake.IntakeException;
32 import org.apache.fulcrum.intake.IntakeServiceFacade;
33 import org.apache.fulcrum.intake.Retrievable;
34 import org.apache.fulcrum.intake.model.Group;
35 import org.apache.fulcrum.parser.ValueParser;
36 import org.apache.fulcrum.pool.Recyclable;
37 import org.apache.turbine.services.pull.ApplicationTool;
38 import org.apache.turbine.util.RunData;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class IntakeTool
52 implements ApplicationTool, Recyclable
53 {
54
55 protected static Log log = LogFactory.getLog(IntakeTool.class);
56
57
58 public static final String DEFAULT_KEY = "_0";
59
60
61 public static final String INTAKE_GRP = "intake-grp";
62
63
64 protected HashMap<String, Group> groups;
65
66
67 protected ValueParser pp;
68
69 private final HashMap<String, Group> declaredGroups = new HashMap<String, Group>();
70 private final StringBuffer allGroupsSB = new StringBuffer(256);
71 private final StringBuffer groupSB = new StringBuffer(128);
72
73
74 private final Map<String, IntakeTool.PullHelper> pullMap;
75
76
77
78
79 @SuppressWarnings("null")
80 public IntakeTool()
81 {
82 String[] groupNames = IntakeServiceFacade.getGroupNames();
83 int groupCount = 0;
84 if (groupNames != null)
85 {
86 groupCount = groupNames.length;
87 }
88 groups = new HashMap<String, Group>((int) (1.25 * groupCount + 1));
89 pullMap = new HashMap<String, IntakeTool.PullHelper>((int) (1.25 * groupCount + 1));
90
91 for (int i = groupCount - 1; i >= 0; i--)
92 {
93 pullMap.put(groupNames[i], new PullHelper(groupNames[i]));
94 }
95 }
96
97
98
99
100 public void init(Object runData)
101 {
102 this.pp = ((RunData) runData).getParameters();
103
104 String[] groupKeys = pp.getStrings(INTAKE_GRP);
105 String[] groupNames = null;
106 if (groupKeys == null || groupKeys.length == 0)
107 {
108 groupNames = IntakeServiceFacade.getGroupNames();
109 }
110 else
111 {
112 groupNames = new String[groupKeys.length];
113 for (int i = groupKeys.length - 1; i >= 0; i--)
114 {
115 groupNames[i] = IntakeServiceFacade.getGroupName(groupKeys[i]);
116 }
117
118 }
119
120 for (int i = groupNames.length - 1; i >= 0; i--)
121 {
122 try
123 {
124 List foundGroups = IntakeServiceFacade.getGroup(groupNames[i])
125 .getObjects(pp);
126
127 if (foundGroups != null)
128 {
129 for (Iterator iter = foundGroups.iterator();
130 iter.hasNext();)
131 {
132 Group group = (Group) iter.next();
133 groups.put(group.getObjectKey(), group);
134 }
135 }
136 }
137 catch (IntakeException e)
138 {
139 log.error(e);
140 }
141 }
142 }
143
144 public void addGroupsToParameters(ValueParser vp)
145 {
146 for (Iterator i = groups.values().iterator(); i.hasNext();)
147 {
148 Group group = (Group) i.next();
149 if (!declaredGroups.containsKey(group.getIntakeGroupName()))
150 {
151 declaredGroups.put(group.getIntakeGroupName(), null);
152 vp.add("intake-grp", group.getGID());
153 }
154 vp.add(group.getGID(), group.getOID());
155 }
156 declaredGroups.clear();
157 }
158
159
160
161
162
163
164
165
166
167
168 public String declareGroups()
169 {
170 allGroupsSB.setLength(0);
171 for (Iterator i = groups.values().iterator(); i.hasNext();)
172 {
173 declareGroup((Group) i.next(), allGroupsSB);
174 }
175 return allGroupsSB.toString();
176 }
177
178
179
180
181
182 public String declareGroup(Group group)
183 {
184 groupSB.setLength(0);
185 declareGroup(group, groupSB);
186 return groupSB.toString();
187 }
188
189
190
191
192
193 public void declareGroup(Group group, StringBuffer sb)
194 {
195 if (!declaredGroups.containsKey(group.getIntakeGroupName()))
196 {
197 declaredGroups.put(group.getIntakeGroupName(), null);
198 sb.append("<input type=\"hidden\" name=\"")
199 .append(INTAKE_GRP)
200 .append("\" value=\"")
201 .append(group.getGID())
202 .append("\"/>\n");
203 }
204 group.appendHtmlFormInput(sb);
205 }
206
207 public void newForm()
208 {
209 declaredGroups.clear();
210 for (Iterator i = groups.values().iterator(); i.hasNext();)
211 {
212 ((Group) i.next()).resetDeclared();
213 }
214 }
215
216
217
218
219
220 public void refresh()
221 {
222
223 }
224
225
226
227
228 public class PullHelper
229 {
230
231 String groupName;
232
233
234
235
236
237
238 protected PullHelper(String groupName)
239 {
240 this.groupName = groupName;
241 }
242
243
244
245
246
247
248
249 public Group getDefault()
250 throws IntakeException
251 {
252 return setKey(DEFAULT_KEY);
253 }
254
255
256
257
258
259
260
261
262 public Group setKey(String key)
263 throws IntakeException
264 {
265 return setKey(key, true);
266 }
267
268
269
270
271
272
273
274
275 public Group setKey(String key, boolean create)
276 throws IntakeException
277 {
278 Group g = null;
279
280 String inputKey = IntakeServiceFacade.getGroupKey(groupName) + key;
281 if (groups.containsKey(inputKey))
282 {
283 g = groups.get(inputKey);
284 }
285 else if (create)
286 {
287 g = IntakeServiceFacade.getGroup(groupName);
288 groups.put(inputKey, g);
289 g.init(key, pp);
290 }
291
292 return g;
293 }
294
295
296
297
298
299
300
301 public Group mapTo(Retrievable obj)
302 {
303 Group g = null;
304
305 try
306 {
307 String inputKey = IntakeServiceFacade.getGroupKey(groupName)
308 + obj.getQueryKey();
309 if (groups.containsKey(inputKey))
310 {
311 g = groups.get(inputKey);
312 }
313 else
314 {
315 g = IntakeServiceFacade.getGroup(groupName);
316 groups.put(inputKey, g);
317 }
318
319 return g.init(obj);
320 }
321 catch (IntakeException e)
322 {
323 log.error(e);
324 }
325
326 return null;
327 }
328 }
329
330
331
332
333 public PullHelper get(String groupName)
334 {
335 return pullMap.get(groupName);
336 }
337
338
339
340
341
342
343
344 public PullHelper get(String groupName, boolean throwExceptions)
345 throws IntakeException
346 {
347 return pullMap.get(groupName);
348 }
349
350
351
352
353
354 public boolean isAllValid()
355 {
356 boolean allValid = true;
357 for (Iterator iter = groups.values().iterator(); iter.hasNext();)
358 {
359 Group group = (Group) iter.next();
360 allValid &= group.isAllValid();
361 }
362 return allValid;
363 }
364
365
366
367
368 public Group get(String groupName, String key)
369 throws IntakeException
370 {
371 if (groupName == null)
372 {
373 throw new IntakeException("IntakeServiceFacade.get: groupName == null");
374 }
375 if (key == null)
376 {
377 throw new IntakeException("IntakeServiceFacade.get: key == null");
378 }
379
380 PullHelper ph = get(groupName);
381 return (ph == null) ? null : ph.setKey(key);
382 }
383
384
385
386
387
388 public Group get(String groupName, String key, boolean create)
389 throws IntakeException
390 {
391 if (groupName == null)
392 {
393 throw new IntakeException("IntakeServiceFacade.get: groupName == null");
394 }
395 if (key == null)
396 {
397 throw new IntakeException("IntakeServiceFacade.get: key == null");
398 }
399
400 PullHelper ph = get(groupName);
401 return (ph == null) ? null : ph.setKey(key, create);
402 }
403
404
405
406
407
408
409 public void remove(Group group)
410 {
411 if (group != null)
412 {
413 groups.remove(group.getObjectKey());
414 group.removeFromRequest();
415
416 String[] groupKeys = pp.getStrings(INTAKE_GRP);
417
418 pp.remove(INTAKE_GRP);
419
420 if (groupKeys != null)
421 {
422 for (int i = 0; i < groupKeys.length; i++)
423 {
424 if (!groupKeys[i].equals(group.getGID()))
425 {
426 pp.add(INTAKE_GRP, groupKeys[i]);
427 }
428 }
429 }
430
431
432 try
433 {
434 IntakeServiceFacade.releaseGroup(group);
435 }
436 catch (IntakeException ie)
437 {
438 log.error("Tried to release unknown group "
439 + group.getIntakeGroupName());
440 }
441 }
442 }
443
444
445
446
447
448
449 public void removeAll()
450 {
451 Object[] allGroups = groups.values().toArray();
452 for (int i = allGroups.length - 1; i >= 0; i--)
453 {
454 Group group = (Group) allGroups[i];
455 remove(group);
456 }
457 }
458
459
460
461
462
463
464 public Map getGroups()
465 {
466 return groups;
467 }
468
469
470
471 private boolean disposed;
472
473
474
475
476
477
478
479
480
481
482
483 public void recycle()
484 {
485 disposed = false;
486 }
487
488
489
490
491
492
493 public void dispose()
494 {
495 for (Iterator iter = groups.values().iterator(); iter.hasNext();)
496 {
497 Group g = (Group) iter.next();
498
499 try
500 {
501 IntakeServiceFacade.releaseGroup(g);
502 }
503 catch (IntakeException ie)
504 {
505 log.error("Tried to release unknown group "
506 + g.getIntakeGroupName());
507 }
508 }
509
510 groups.clear();
511 declaredGroups.clear();
512 pp = null;
513
514 disposed = true;
515 }
516
517
518
519
520
521
522 public boolean isDisposed()
523 {
524 return disposed;
525 }
526 }