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