KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > CmsAdminAction


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" and
27  * "Apache Jetspeed" must not be used to endorse or promote products
28  * derived from this software without prior written permission. For
29  * written permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  * "Apache Jetspeed", nor may "Apache" appear in their name, without
33  * prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */

54
55
56 package org.apache.jetspeed.modules.actions.portlets;
57
58 // JDK stuff
59
import java.util.Vector JavaDoc;
60
61 // Jetspeed Struff
62
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
63 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
64 import org.apache.jetspeed.util.PortletConfigState;
65 import org.apache.jetspeed.services.rundata.JetspeedRunData;
66 import org.apache.jetspeed.services.statemanager.SessionState;
67
68 import org.apache.jetspeed.services.cms.CmsService;
69 import org.apache.jetspeed.services.cms.manager.CmsManager;
70 import org.apache.jetspeed.services.cms.manager.CmsFactory;
71 import org.apache.jetspeed.services.cms.JetspeedCMSException;
72 import org.apache.jetspeed.om.cms.Catalog;
73
74
75 // Turbine stuff
76
import org.apache.turbine.util.Log;
77 import org.apache.turbine.util.RunData;
78 import org.apache.turbine.services.TurbineServices;
79
80
81 // Velocity Stuff
82
import org.apache.velocity.context.Context;
83
84
85 /**
86  * Action used for CMS Admin (CRUD on content/catalog).
87  *
88  * <p>Don't call it from the URL, the Portlet and the Action are automatically
89  * associated through the registry PortletName
90  *
91  * @author <a HREF="mailto:Christophe.lombart@skynet.be">Christophe Lombart</a>
92  */

93 public class CmsAdminAction extends VelocityPortletAction
94 {
95
96
97     private static final String JavaDoc CUSTOMIZE_TEMPLATE = "customize-template";
98     
99     private static final String JavaDoc FROM_URI = "fromuri";
100     
101     private static final String JavaDoc FROM_URI_DEFAULT_VALUE = "/catalog";
102     
103     private static final String JavaDoc TO_URI = "touri";
104        
105     private static final String JavaDoc NEW_CATALOG_EVENT = "NewCatalog";
106     
107     private static final String JavaDoc INSERT_CATALOG_EVENT = "InsertCatalog";
108     
109     private static final String JavaDoc NEW_CATALOG_TEMPLATE = "new-catalog-template";
110         
111     private static final String JavaDoc SUMMARY_CATALOG_TEMPLATE = "summary-catalog-template";
112     
113     private static final String JavaDoc CATALOG_PARAM = "catalog";
114     
115     private static final String JavaDoc CATALOG_PATH_PARAM = "catalogPath";
116     
117     private static final String JavaDoc SUB_CATALOG_PARAM = "subCatalog";
118     
119     private static final String JavaDoc CUSTOMIZE_EVENT_PARAM = "customizeEvent";
120     
121     private static final String JavaDoc MODE_PARAM = "mode";
122     
123     private static final String JavaDoc ADD_MODE_VALUE = "add";
124     
125     
126     
127     /**
128      * Subclasses should override this method if they wish to
129      * build specific content when maximized. Default behavior is
130      * to do the same as normal content.
131      */

132     protected void buildMaximizedContext( VelocityPortlet portlet,
133                                           Context context,
134                                           RunData rundata )
135                throws JetspeedCMSException
136     {
137         
138         try
139         {
140             buildNormalContext( portlet, context, rundata);
141         }
142         catch (JetspeedCMSException e)
143         {
144             throw e;
145         }
146
147     }
148
149     /**
150      * Subclasses should override this method if they wish to
151      * provide their own customization behavior.
152      * Default is to use Portal base customizer action
153      */

154     protected void buildConfigureContext( VelocityPortlet portlet,
155                                           Context context,
156                                           RunData rundata )
157     {
158         try {
159         
160             SessionState state = this.getSessionState(portlet, rundata);
161             
162             String JavaDoc customizeEvent = rundata.getParameters().getString(CUSTOMIZE_EVENT_PARAM);
163             
164             if (customizeEvent == null)
165             {
166                 setTemplate(rundata, getParameterUsingFallback(portlet, rundata, CUSTOMIZE_TEMPLATE, null));
167             }
168             else
169             {
170                 if (customizeEvent.equals(NEW_CATALOG_EVENT))
171                 {
172                     this.doNewCatalog(portlet,context,rundata);
173                 }
174             
175                 if (customizeEvent.equals(INSERT_CATALOG_EVENT))
176                 {
177                     this.doInsertCatalog(portlet,context,rundata);
178                 }
179             }
180             // Store cataloPath and catalog into the context
181
context.put(CATALOG_PATH_PARAM, state.getAttribute(CATALOG_PATH_PARAM));
182             context.put(CATALOG_PARAM, state.getAttribute(CATALOG_PARAM));
183         }
184         catch (JetspeedCMSException e)
185         {
186             e.printStackTrace();
187         }
188
189
190     }
191
192     /**
193      * Subclasses must override this method to provide default behavior
194      * for the portlet action
195      */

196     protected void buildNormalContext( VelocityPortlet portlet,
197                                        Context context,
198                                        RunData rundata )
199                throws JetspeedCMSException
200     {
201         
202         try
203         {
204             
205             SessionState state = this.getSessionState(portlet, rundata);
206             
207             String JavaDoc fromcatalogName = this.getParameterUsingFallback(portlet, rundata, FROM_URI, FROM_URI_DEFAULT_VALUE);
208             String JavaDoc tocatalogName = rundata.getParameters().getString(TO_URI);
209             
210             
211            
212             if (tocatalogName == null)
213             {
214                 // this case occur when the normal context is calling for the first time
215
if (state.getAttribute(CATALOG_PARAM) == null)
216                 {
217                     tocatalogName = fromcatalogName;
218                 }
219                 // this case occur when the end-used moves from the configure context to the normal context
220
else
221                 {
222                     context.put(CATALOG_PATH_PARAM, state.getAttribute(CATALOG_PATH_PARAM));
223                     context.put(CATALOG_PARAM, state.getAttribute(CATALOG_PARAM));
224                     return;
225                 }
226                                 
227             }
228                       
229             // get all catalogs objects used for the "catalog path"
230
Vector JavaDoc catalogPath = this.getCmsManager().getCatalogs(fromcatalogName, tocatalogName);
231             
232             // get the catalog selected in the velocity template
233
Catalog catalog = (Catalog) catalogPath.lastElement();
234             // Retrieve catalog items
235
this.getCmsManager().populateCatalog(catalog,true,true,1);
236                                     
237             // Store the cataloPath collection and the catalog into the portlet session for performance reason
238
// (can be used by the customizer without a second retrieve in the cms repository
239
state.setAttribute(CATALOG_PATH_PARAM, catalogPath);
240             state.setAttribute(CATALOG_PARAM, catalog);
241             
242             
243             // Store cataloPath and catalog into the context
244
context.put(CATALOG_PATH_PARAM, catalogPath);
245             context.put(CATALOG_PARAM, catalog);
246             
247         }
248         catch ( Exception JavaDoc e ) {
249             throw new JetspeedCMSException("Impossible to build the normal context");
250         }
251         
252         
253     }
254
255
256     /**
257      * Add a new catalog into the velocity context
258      */

259     protected void doNewCatalog( VelocityPortlet portlet,
260                                           Context context,
261                                           RunData rundata )
262                throws JetspeedCMSException
263     {
264         
265          try
266          {
267              Catalog catalog = (Catalog) CmsFactory.getCmsOmInstance("Catalog");
268              setTemplate(rundata, getParameterUsingFallback(portlet, rundata, NEW_CATALOG_TEMPLATE, null));
269              context.put(SUB_CATALOG_PARAM, catalog);
270              context.put(MODE_PARAM, ADD_MODE_VALUE);
271              
272          }
273          catch (Exception JavaDoc e)
274          {
275              throw new JetspeedCMSException("Impossible to create Catalog object");
276          }
277                     
278     }
279
280     /**
281      * Insert a new catalog into the CMS repository
282      */

283     protected void doInsertCatalog( VelocityPortlet portlet,
284                                           Context context,
285                                           RunData rundata )
286                throws JetspeedCMSException
287     {
288         
289          try
290          {
291              
292              Catalog newCatalog = (Catalog) CmsFactory.getCmsOmInstance("Catalog");
293              
294              // Assign values to the catalog object
295
rundata.getParameters().setProperties(newCatalog);
296              
297              Catalog parentCatalog = (Catalog) this.getSessionState(portlet, rundata)
298                                           .getAttribute(CATALOG_PARAM);
299              
300              newCatalog.setParentUri(parentCatalog.getUri());
301              
302              newCatalog.setUri(newCatalog.getParentUri() +
303                                "/" + newCatalog.getLogicalName());
304              
305              // Create catalog into the repository
306
this.getCmsManager().createResource(newCatalog);
307              
308              // Refresh parent catalog
309
this.getCmsManager().populateCatalog(parentCatalog);
310              
311              setTemplate(rundata, getParameterUsingFallback(portlet, rundata, SUMMARY_CATALOG_TEMPLATE, null));
312              
313              context.put(CATALOG_PARAM, parentCatalog);
314              context.put(MODE_PARAM, ADD_MODE_VALUE);
315
316              
317          }
318          catch (Exception JavaDoc e)
319          {
320              throw new JetspeedCMSException("Impossible to create Catalog object");
321          }
322                     
323     }
324     
325
326     protected String JavaDoc getParameterUsingFallback(VelocityPortlet portlet, RunData rundata,
327                                              String JavaDoc attrName, String JavaDoc attrDefValue)
328     {
329         return PortletConfigState.getParameter(portlet, rundata, attrName, attrDefValue);
330     }
331     
332     protected CmsManager getCmsManager()
333                throws JetspeedCMSException
334     {
335         try
336         {
337             return ((CmsService) TurbineServices.getInstance()
338                                             .getService(CmsService.SERVICE_NAME))
339                                             .getCmsManager();
340         }
341         catch (Exception JavaDoc e)
342         {
343            throw new JetspeedCMSException("Impossible to get the CMS service");
344         }
345     }
346     
347     
348     protected SessionState getSessionState(VelocityPortlet portlet,RunData rundata) {
349         
350         return ((JetspeedRunData) rundata).getPortletSessionState(portlet.getID());
351     }
352     
353     
354 }
355
Popular Tags