KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > Intake


1 package org.apache.fulcrum.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 JavaDoc;
58 import java.util.Iterator JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.Map JavaDoc;
61
62 import org.apache.fulcrum.intake.model.Group;
63 import org.apache.fulcrum.parser.ValueParser;
64 import org.apache.fulcrum.pool.Recyclable;
65 import org.apache.log4j.Category;
66
67 /**
68  * The main class through which Intake is accessed.
69  *
70  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
71  * @version $Id: Intake.java,v 1.1 2004/11/12 10:25:56 epugh Exp $
72  */

73 public class Intake
74     implements Recyclable
75 {
76     public static final String JavaDoc DEFAULT_KEY = "_0";
77     private HashMap JavaDoc groups;
78     private ValueParser pp;
79
80     HashMap JavaDoc declaredGroups = new HashMap JavaDoc();
81     StringBuffer JavaDoc allGroupsSB = new StringBuffer JavaDoc(256);
82     StringBuffer JavaDoc groupSB = new StringBuffer JavaDoc(128);
83
84     /** The cache of PullHelpers. **/
85     private Map JavaDoc pullMap;
86
87     /**
88      * Log4j category
89      */

90     Category category = Category.getInstance(getClass().getName());
91
92     public Intake()
93     {
94         String JavaDoc[] groupNames = TurbineIntake.getGroupNames();
95         groups = new HashMap JavaDoc((int)(1.25*groupNames.length + 1));
96         pullMap = new HashMap JavaDoc((int)(1.25*groupNames.length + 1));
97         // omToolKey = TurbineResources.getString("tool.intake.om");
98

99         for (int i=groupNames.length-1; i>=0; i--)
100         {
101             pullMap.put(groupNames[i], new PullHelper(groupNames[i]));
102         }
103     }
104
105     /**
106      * Prepares intake for a single request
107      */

108     public void init(ValueParser pp)
109     {
110         this.pp = pp;
111         String JavaDoc[] groupKeys = pp.getStrings("intake-grp");
112         String JavaDoc[] groupNames = null;
113         if ( groupKeys == null || groupKeys.length == 0 )
114         {
115             groupNames = TurbineIntake.getGroupNames();
116         }
117         else
118         {
119             groupNames = new String JavaDoc[groupKeys.length];
120             for ( int i=groupKeys.length-1; i>=0; i-- )
121             {
122                 groupNames[i] = TurbineIntake.getGroupName(groupKeys[i]);
123             }
124
125         }
126
127         for (int i=groupNames.length-1; i>=0; i--)
128         {
129             try
130             {
131                 List JavaDoc foundGroups = TurbineIntake.getGroup(groupNames[i])
132                     .getObjects(pp);
133
134                 if ( foundGroups != null )
135                 {
136                     Iterator JavaDoc iter = foundGroups.iterator();
137                     while (iter.hasNext())
138                     {
139                         Group group = (Group)iter.next();
140                         groups.put(group.getObjectKey(), group);
141                     }
142                 }
143             }
144             catch(Exception JavaDoc e)
145             {
146                 category.error("", e);
147             }
148         }
149     }
150
151     public void addGroupsToParameters(ValueParser vp)
152     {
153         Iterator JavaDoc i = groups.values().iterator();
154         while ( i.hasNext() )
155         {
156             Group group = (Group)i.next();
157             if ( !declaredGroups.containsKey(group.getIntakeGroupName()) )
158             {
159                 declaredGroups.put(group.getIntakeGroupName(), null);
160                 vp.add("intake-grp", group.getGID());
161             }
162             vp.add(group.getGID(), group.getOID());
163         }
164         declaredGroups.clear();
165     }
166
167     /**
168      * A convenience method to write out the hidden form fields
169      * that notify intake of the relevant groups. It should be used
170      * only in templates with 1 form. In multiform templates, the groups
171      * that are relevant for each form need to be declared using
172      * $intake.newForm() and $intake.declareGroup($group) for the relevant
173      * groups in the form.
174      *
175      */

176     public String JavaDoc declareGroups()
177     {
178         allGroupsSB.setLength(0);
179         Iterator JavaDoc i = groups.values().iterator();
180         while ( i.hasNext() )
181         {
182             declareGroup( (Group)i.next(), allGroupsSB );
183         }
184         return allGroupsSB.toString();
185     }
186
187     /**
188      * A convenience method to write out the hidden form fields
189      * that notify intake of the group.
190      */

191     public String JavaDoc declareGroup(Group group)
192     {
193         groupSB.setLength(0);
194         declareGroup(group, groupSB);
195         return groupSB.toString();
196     }
197
198     /**
199      * xhtml valid hidden input field(s) that notifies intake of the
200      * group's presence.
201      */

202     public void declareGroup(Group group, StringBuffer JavaDoc sb)
203     {
204         if ( !declaredGroups.containsKey(group.getIntakeGroupName()) )
205         {
206             declaredGroups.put(group.getIntakeGroupName(), null);
207             sb.append("<input type=\"hidden\" name=\"")
208               .append("intake-grp\" value=\"")
209               .append(group.getGID())
210               .append("\"/>\n");
211         }
212         group.appendHtmlFormInput(sb);
213     }
214
215     public void newForm()
216     {
217         declaredGroups.clear();
218         Iterator JavaDoc i = groups.values().iterator();
219         while ( i.hasNext() )
220         {
221              ((Group)i.next()).resetDeclared();
222         }
223     }
224
225     /**
226      * Inner class to present a nice interface to the template designer
227      */

228     public class PullHelper
229     {
230         String JavaDoc groupName;
231
232         private PullHelper(String JavaDoc groupName)
233         {
234             this.groupName = groupName;
235         }
236
237         public Group getDefault()
238             throws Exception JavaDoc
239         {
240             return setKey(DEFAULT_KEY);
241         }
242
243         public Group setKey(String JavaDoc key)
244             throws Exception JavaDoc
245         {
246             return setKey(key, true);
247         }
248
249         public Group setKey(String JavaDoc key, boolean create)
250             throws Exception JavaDoc
251         {
252             Group g = null;
253
254             String JavaDoc inputKey = TurbineIntake.getGroupKey(groupName) + key;
255             if ( groups.containsKey(inputKey))
256             {
257                 g = (Group)groups.get(inputKey);
258             }
259             else if (create)
260             {
261                 g = TurbineIntake.getGroup(groupName);
262                 groups.put(inputKey, g);
263                 g.init(key, pp);
264             }
265
266             return g;
267         }
268
269
270         public Group mapTo(Retrievable obj)
271             throws Exception JavaDoc
272         {
273             Group g = null;
274
275             try
276             {
277                 String JavaDoc inputKey = TurbineIntake.getGroupKey(groupName)
278                     + obj.getQueryKey();
279                 if ( groups.containsKey(inputKey))
280                 {
281                     g = (Group)groups.get(inputKey);
282                 }
283                 else
284                 {
285                     g = TurbineIntake.getGroup(groupName);
286                     groups.put(inputKey, g);
287                 }
288                 return g.init(obj);
289             }
290             catch(Exception JavaDoc e)
291             {
292                 category.error("", e);
293             }
294
295             return null;
296         }
297     }
298
299     /**
300      * get a specific group
301      */

302     public PullHelper get(String JavaDoc groupName)
303         throws Exception JavaDoc
304     {
305         return (PullHelper)pullMap.get(groupName);
306     }
307
308     /**
309      * Loops through all of the Groups and checks to see if
310      * the data within the Group is valid.
311      */

312     public boolean isAllValid()
313     {
314         boolean allValid = true;
315         Iterator JavaDoc iter = groups.values().iterator();
316         while (iter.hasNext())
317         {
318             Group group = (Group)iter.next();
319             allValid &= group.isAllValid();
320         }
321         return allValid;
322     }
323
324     /**
325      * Get a specific group by name and key.
326      */

327     public Group get(String JavaDoc groupName, String JavaDoc key)
328         throws Exception JavaDoc
329     {
330         if (groupName == null)
331         {
332             throw new Exception JavaDoc ("Intake.get: groupName == null");
333         }
334         if (key == null)
335         {
336             throw new Exception JavaDoc ("Intake.get: key == null");
337         }
338         return ((PullHelper)get(groupName)).setKey(key);
339     }
340
341     /**
342      * Get a specific group by name and key. Also specify
343      * whether or not you want to create a new group.
344      */

345     public Group get(String JavaDoc groupName, String JavaDoc key, boolean create)
346         throws Exception JavaDoc
347     {
348         return ((PullHelper)get(groupName)).setKey(key, create);
349     }
350
351     /**
352      * Removes group. Primary use is to remove a group that has
353      * been processed by an action and is no longer appropriate
354      * in the view (screen).
355      */

356     public void remove(Group group)
357     {
358         groups.remove(group.getObjectKey());
359         group.removeFromRequest();
360         TurbineIntake.releaseGroup(group);
361     }
362
363     /**
364      * Removes all groups. Primary use is to remove groups that have
365      * been processed by an action and are no longer appropriate
366      * in the view (screen).
367      */

368     public void removeAll()
369     {
370         Object JavaDoc[] allGroups = groups.values().toArray();
371         for (int i=allGroups.length-1; i>=0; i-- )
372         {
373             Group group = (Group)allGroups[i];
374             remove(group);
375         }
376     }
377
378
379     // ****************** Recyclable implementation ************************
380

381     private boolean disposed;
382
383     /**
384      * Recycles the object for a new client. Recycle methods with
385      * parameters must be added to implementing object and they will be
386      * automatically called by pool implementations when the object is
387      * taken from the pool for a new client. The parameters must
388      * correspond to the parameters of the constructors of the object.
389      * For new objects, constructors can call their corresponding recycle
390      * methods whenever applicable.
391      * The recycle methods must call their super.
392      */

393     public void recycle()
394     {
395         disposed = false;
396     }
397
398     /**
399      * Disposes the object after use. The method is called
400      * when the object is returned to its pool.
401      * The dispose method must call its super.
402      */

403     public void dispose()
404     {
405         Iterator JavaDoc iter = groups.values().iterator();
406         while ( iter.hasNext() )
407         {
408             Group g = (Group)iter.next();
409             TurbineIntake.releaseGroup(g);
410         }
411
412         groups.clear();
413         declaredGroups.clear();
414         pp = null;
415
416         disposed = true;
417     }
418
419     /**
420      * Checks whether the recyclable has been disposed.
421      * @return true, if the recyclable is disposed.
422      */

423     public boolean isDisposed()
424     {
425         return disposed;
426     }
427
428
429 }
430
431
432
433
434
435
436
Popular Tags