1 package org.apache.turbine.services.intake;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.HashMap;
58 import java.util.Iterator;
59 import java.util.List;
60 import java.util.Map;
61 import org.apache.turbine.om.Retrievable;
62 import org.apache.turbine.services.intake.model.Group;
63 import org.apache.turbine.services.pull.ApplicationTool;
64 import org.apache.turbine.util.Log;
65 import org.apache.turbine.util.RunData;
66 import org.apache.turbine.util.pool.Recyclable;
67
68 /***
69 * A Pull tool to make intake objects available to a template
70 *
71 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
72 * @version $Id: IntakeTool.java,v 1.5 2002/07/11 16:53:28 mpoeschl Exp $
73 */
74 public class IntakeTool
75 implements ApplicationTool, Recyclable
76 {
77 public static final String DEFAULT_KEY = "_0";
78 private HashMap groups;
79 private RunData data;
80 // private boolean allValid;
81 // private String omToolKey;
82 // private OMTool omTool;
83
84 /*** The cache of PullHelpers. **/
85 private Map pullMap = new HashMap();
86
87 public IntakeTool()
88 {
89 String[] groupNames = TurbineIntake.getGroupNames();
90 groups = new HashMap((int)(1.25*groupNames.length + 1));
91 pullMap = new HashMap((int)(1.25*groupNames.length + 1));
92 // omToolKey = TurbineResources.getString("tool.intake.om");
93
94 for (int i=groupNames.length-1; i>=0; i--)
95 {
96 pullMap.put(groupNames[i], new PullHelper(groupNames[i]));
97 }
98 }
99
100 /***
101 * Prepares intake for a single request
102 */
103 public void init(Object runData)
104 {
105 data = (RunData)runData;
106
107 String[] groupKeys = data.getParameters().getStrings("intake-grp");
108 String[] groupNames = null;
109 if ( groupKeys == null || groupKeys.length == 0 )
110 {
111 groupNames = TurbineIntake.getGroupNames();
112 }
113 else
114 {
115 groupNames = new String[groupKeys.length];
116 for ( int i=groupKeys.length-1; i>=0; i-- )
117 {
118 groupNames[i] = TurbineIntake.getGroupName(groupKeys[i]);
119 }
120
121 }
122
123 for (int i=groupNames.length-1; i>=0; i--)
124 {
125 try
126 {
127 List foundGroups = TurbineIntake.getGroup(groupNames[i])
128 .getObjects(data);
129
130 if ( foundGroups != null )
131 {
132 Iterator iter = foundGroups.iterator();
133 while (iter.hasNext())
134 {
135 Group group = (Group)iter.next();
136 groups.put(group.getObjectKey(), group);
137 }
138 }
139 }
140 catch(Exception e)
141 {
142 Log.error(e);
143 }
144 }
145 }
146
147 /***
148 * A convenience method to write out the hidden form fields
149 * that notify intake of the relevant groups. It should be used
150 * only in templates with 1 form. In multiform templates, the groups
151 * that are relevant for each form need to be declared using
152 * $intake.newForm() and $intake.declareGroup($group) for the relevant
153 * groups in the form.
154 *
155 */
156 public String declareGroups()
157 {
158 allGroupsSB.setLength(0);
159 Iterator i = groups.values().iterator();
160 while ( i.hasNext() )
161 {
162 declareGroup( (Group)i.next(), allGroupsSB );
163 }
164 return allGroupsSB.toString();
165 }
166
167 HashMap declaredGroups = new HashMap();
168 StringBuffer allGroupsSB = new StringBuffer(256);
169 StringBuffer groupSB = new StringBuffer(128);
170
171 /***
172 * A convenience method to write out the hidden form fields
173 * that notify intake of the group.
174 */
175 public String declareGroup(Group group)
176 {
177 groupSB.setLength(0);
178 declareGroup(group, groupSB);
179 return groupSB.toString();
180 }
181
182 /***
183 * xhtml valid hidden input field(s) that notifies intake of the
184 * group's presence.
185 */
186 public void declareGroup(Group group, StringBuffer sb)
187 {
188 if ( !declaredGroups.containsKey(group.getIntakeGroupName()) )
189 {
190 declaredGroups.put(group.getIntakeGroupName(), null);
191 sb.append("<input type=\"hidden\" name=\"")
192 .append("intake-grp\" value=\"")
193 .append(group.getGID())
194 .append("\"></input>\n");
195 }
196 group.appendHtmlFormInput(sb);
197 }
198
199 public void newForm()
200 {
201 declaredGroups.clear();
202 Iterator i = groups.values().iterator();
203 while ( i.hasNext() )
204 {
205 ((Group)i.next()).resetDeclared();
206 }
207 }
208
209 /***
210 * Implementation of ApplicationTool interface is not needed for this
211 * tool as it is request scoped
212 */
213 public void refresh()
214 {
215 // empty
216 }
217
218 /***
219 * Inner class to present a nice interface to the template designer
220 */
221 public class PullHelper
222 {
223 String groupName;
224
225 private PullHelper(String groupName)
226 {
227 this.groupName = groupName;
228 }
229
230 /***
231 * populates the object with the default values from the XML File
232 *
233 * @return a Group object with the default values
234 */
235
236 public Group getDefault()
237 throws Exception
238 {
239 return setKey(DEFAULT_KEY);
240 }
241
242 public Group setKey(String key)
243 throws Exception
244 {
245 return setKey(key, true);
246 }
247
248 public Group setKey(String key, boolean create)
249 throws Exception
250 {
251 Group g = null;
252
253 String inputKey = TurbineIntake.getGroupKey(groupName) + key;
254 if ( groups.containsKey(inputKey))
255 {
256 g = (Group)groups.get(inputKey);
257 }
258 else if (create)
259 {
260 g = TurbineIntake.getGroup(groupName);
261 groups.put(inputKey, g);
262 g.init(key, data);
263 }
264
265 return g;
266 }
267
268
269 /***
270 * maps an Intake Group to the values from a Retrievable object.
271 *
272 * @param obj A retrievable object
273 *
274 * @return an Intake Group
275 *
276 */
277
278 public Group mapTo(Retrievable obj)
279 throws Exception
280 {
281 Group g = null;
282
283 try
284 {
285 String inputKey = TurbineIntake.getGroupKey(groupName)
286 + obj.getQueryKey();
287 if ( groups.containsKey(inputKey))
288 {
289 g = (Group)groups.get(inputKey);
290 }
291 else
292 {
293 g = TurbineIntake.getGroup(groupName);
294 groups.put(inputKey, g);
295 }
296 return g.init(obj);
297 }
298 catch(Exception e)
299 {
300 Log.error(e);
301 }
302
303 return null;
304 }
305 }
306
307
308 public Object get(String groupName)
309 throws Exception
310 {
311 return pullMap.get(groupName);
312 }
313
314 public boolean isAllValid()
315 {
316 boolean allValid = true;
317 Iterator iter = groups.values().iterator();
318 while (iter.hasNext())
319 {
320 Group group = (Group)iter.next();
321 allValid &= group.isAllValid();
322 }
323 return allValid;
324 }
325
326 public Group get(String groupName, String key)
327 throws Exception
328 {
329 return ((PullHelper)get(groupName)).setKey(key);
330 }
331
332 public Group get(String groupName, String key, boolean create)
333 throws Exception
334 {
335 return ((PullHelper)get(groupName)).setKey(key, create);
336 }
337
338 /***
339 * Removes group. Primary use is to remove a group that has
340 * been processed by an action and is no longer appropriate
341 * in the view (screen).
342 */
343 public void remove(Group group)
344 {
345 groups.remove(group.getObjectKey());
346 group.removeFromRequest();
347 TurbineIntake.releaseGroup(group);
348 }
349
350 /***
351 * Removes all groups. Primary use is to remove groups that have
352 * been processed by an action and are no longer appropriate
353 * in the view (screen).
354 */
355 public void removeAll()
356 {
357 Object[] allGroups = groups.values().toArray();
358 for (int i=allGroups.length-1; i>=0; i-- )
359 {
360 Group group = (Group)allGroups[i];
361 remove(group);
362 }
363 }
364
365
366 // ****************** Recyclable implementation ************************
367
368 private boolean disposed;
369
370 /***
371 * Recycles the object for a new client. Recycle methods with
372 * parameters must be added to implementing object and they will be
373 * automatically called by pool implementations when the object is
374 * taken from the pool for a new client. The parameters must
375 * correspond to the parameters of the constructors of the object.
376 * For new objects, constructors can call their corresponding recycle
377 * methods whenever applicable.
378 * The recycle methods must call their super.
379 */
380 public void recycle()
381 {
382 disposed = false;
383 }
384
385 /***
386 * Disposes the object after use. The method is called
387 * when the object is returned to its pool.
388 * The dispose method must call its super.
389 */
390 public void dispose()
391 {
392 Iterator iter = groups.values().iterator();
393 while ( iter.hasNext() )
394 {
395 Group g = (Group)iter.next();
396 TurbineIntake.releaseGroup(g);
397 }
398
399 groups.clear();
400 declaredGroups.clear();
401 data = null;
402
403 disposed = true;
404 }
405
406 /***
407 * Checks whether the recyclable has been disposed.
408 * @return true, if the recyclable is disposed.
409 */
410 public boolean isDisposed()
411 {
412 return disposed;
413 }
414 }
This page was automatically generated by Maven