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