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