KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > containers > action > EditContainerAction


1 package com.dotmarketing.portlets.containers.action;
2
3 import java.net.URLDecoder JavaDoc;
4 import java.net.URLEncoder JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.portlet.ActionRequest;
8 import javax.portlet.ActionResponse;
9 import javax.portlet.PortletConfig;
10 import javax.portlet.WindowState;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12
13 import org.apache.commons.beanutils.BeanUtils;
14 import org.apache.struts.action.ActionForm;
15 import org.apache.struts.action.ActionMapping;
16
17 import com.dotmarketing.beans.Host;
18 import com.dotmarketing.beans.Identifier;
19 import com.dotmarketing.db.DotHibernate;
20 import com.dotmarketing.factories.HostFactory;
21 import com.dotmarketing.factories.IdentifierFactory;
22 import com.dotmarketing.factories.InodeFactory;
23 import com.dotmarketing.factories.WebAssetFactory;
24 import com.dotmarketing.portal.struts.DotPortletAction;
25 import com.dotmarketing.portal.struts.DotPortletActionInterface;
26 import com.dotmarketing.portlets.containers.factories.ContainerFactory;
27 import com.dotmarketing.portlets.containers.model.Container;
28 import com.dotmarketing.portlets.containers.struts.ContainerForm;
29 import com.dotmarketing.portlets.structure.factories.StructureFactory;
30 import com.dotmarketing.portlets.structure.model.Structure;
31 import com.dotmarketing.portlets.templates.model.Template;
32 import com.dotmarketing.services.ContainerServices;
33 import com.dotmarketing.util.Logger;
34 import com.dotmarketing.util.UtilMethods;
35 import com.dotmarketing.util.Validator;
36 import com.dotmarketing.util.WebKeys;
37 import com.liferay.portal.model.User;
38 import com.liferay.portal.util.Constants;
39 import com.liferay.portlet.ActionRequestImpl;
40 import com.liferay.util.servlet.SessionMessages;
41
42 /**
43  * Portlet action used to maintain the containers
44  * @author Maria
45  */

46 public class EditContainerAction extends DotPortletAction implements
47         DotPortletActionInterface {
48
49     /**
50      * Main method called by the portlet's container to trigger actions over the contrainers
51      */

52     public void processAction(ActionMapping mapping, ActionForm form,
53             PortletConfig config, ActionRequest req, ActionResponse res)
54             throws Exception JavaDoc {
55
56         String JavaDoc cmd = req.getParameter(Constants.CMD);
57         String JavaDoc referer = req.getParameter("referer");
58
59         // wraps request to get session object
60
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
61         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
62
63         if ((referer != null) && (referer.length() != 0)) {
64             referer = URLDecoder.decode(referer, "UTF-8");
65         }
66
67         Logger.debug(this, "EditContainerAction cmd=" + cmd);
68
69         // if we are poping up the "add variables" page
70
if (com.dotmarketing.util.Constants.CONTAINER_ADD_VARIABLE.equals(cmd)) {
71             setForward(req, "portlet.ext.containers.add_variables");
72             return;
73         }
74
75         DotHibernate.startTransaction();
76
77         User user = _getUser(req);
78
79         try {
80             Logger.debug(this, "Calling Retrieve method");
81             _retrieveWebAsset(req, res, config, form, user, Container.class,
82                     WebKeys.CONTAINER_EDIT);
83
84         } catch (Exception JavaDoc ae) {
85             _handleException(ae, req);
86             return;
87         }
88
89         /*
90          * We are editing the container
91          */

92         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
93             try {
94                 Logger.debug(this, "Calling Edit Method");
95                 _editWebAsset(req, res, config, form, user);
96
97             } catch (Exception JavaDoc ae) {
98                 if ((referer != null) && (referer.length() != 0)) {
99                     if (ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) {
100                         // The web asset edit threw an exception because it's
101
// locked so it should redirect back with message
102
java.util.Map JavaDoc<String JavaDoc, String JavaDoc[]> params = new java.util.HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
103                         params.put("struts_action",
104                                 new String JavaDoc[] { "/ext/director/direct" });
105                         params.put("cmd", new String JavaDoc[] { "editContainer" });
106                         params.put("container", new String JavaDoc[] { req
107                                 .getParameter("inode") });
108                         params.put("referer", new String JavaDoc[] { URLEncoder.encode(
109                                 referer, "UTF-8") });
110
111                         String JavaDoc directorURL = com.dotmarketing.util.PortletURLUtil
112                                 .getActionURL(httpReq, WindowState.MAXIMIZED
113                                         .toString(), params);
114
115                         _sendToReferral(req, res, directorURL);
116                         return;
117                     }
118                 }
119                 _handleException(ae, req);
120                 return;
121             }
122         }
123
124         /*
125          * If we are updating the container, copy the information from the
126          * struts bean to the hbm inode and run the update action and return to
127          * the list
128          */

129         if ((cmd != null) && cmd.equals(Constants.ADD)) {
130             try {
131
132                 if (Validator.validate(req, form, mapping)) {
133
134                     Logger.debug(this, "Calling Save Method");
135                     _saveWebAsset(req, res, config, form, user);
136
137                     String JavaDoc subcmd = req.getParameter("subcmd");
138
139                     if ((subcmd != null)
140                             && subcmd
141                                     .equals(com.dotmarketing.util.Constants.PUBLISH)) {
142                         try {
143                             Logger.debug(this, "Calling Publish Method");
144                             _publishWebAsset(req, res, config, form, user,
145                                     WebKeys.CONTAINER_FORM_EDIT);
146
147                         } catch (Exception JavaDoc ae) {
148                             _handleException(ae, req);
149                             return;
150                         }
151                     }
152                     _sendToReferral(req, res, referer);
153                 }
154
155             } catch (Exception JavaDoc ae) {
156                 _handleException(ae, req);
157             }
158
159         }
160         /*
161          * If we are deleteing the container, run the delete action and return
162          * to the list
163          *
164          */

165         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
166             try {
167                 Logger.debug(this, "Calling Delete Method");
168                 _deleteWebAsset(req, res, config, form, user,
169                         WebKeys.CONTAINER_EDIT);
170
171             } catch (Exception JavaDoc ae) {
172                 _handleException(ae, req);
173                 return;
174             }
175             _sendToReferral(req, res, referer);
176         }
177         /*
178          * If we are undeleting the container, run the undelete action and
179          * return to the list
180          *
181          */

182         else if ((cmd != null)
183                 && cmd.equals(com.dotmarketing.util.Constants.UNDELETE)) {
184             try {
185                 Logger.debug(this, "Calling UnDelete Method");
186                 _undeleteWebAsset(req, res, config, form, user,
187                         WebKeys.CONTAINER_EDIT);
188
189             } catch (Exception JavaDoc ae) {
190                 _handleException(ae, req);
191                 return;
192             }
193             _sendToReferral(req, res, referer);
194
195         }
196         /*
197          * If we are deleting the container version, run the deeleteversion
198          * action and return to the list
199          */

200         else if ((cmd != null)
201                 && cmd.equals(com.dotmarketing.util.Constants.DELETEVERSION)) {
202             try {
203                 Logger.debug(this, "Calling Delete Version Method");
204                 _deleteVersionWebAsset(req, res, config, form, user,
205                         WebKeys.CONTAINER_EDIT);
206
207             } catch (Exception JavaDoc ae) {
208                 _handleException(ae, req);
209                 return;
210             }
211             _sendToReferral(req, res, referer);
212         }
213         /*
214          * If we are unpublishing the container, run the unpublish action and
215          * return to the list
216          */

217         else if ((cmd != null)
218                 && cmd.equals(com.dotmarketing.util.Constants.UNPUBLISH)) {
219             try {
220                 Logger.debug(this, "Calling Unpublish Method");
221                 _unPublishWebAsset(req, res, config, form, user,
222                         WebKeys.CONTAINER_EDIT);
223
224             } catch (Exception JavaDoc ae) {
225                 _handleException(ae, req);
226                 return;
227             }
228             _sendToReferral(req, res, referer);
229
230         }
231         /*
232          * If we are getting the container version back, run the getversionback
233          * action and return to the list
234          */

235         else if ((cmd != null)
236                 && cmd.equals(com.dotmarketing.util.Constants.GETVERSIONBACK)) {
237             try {
238                 Logger.debug(this, "Calling Get Version Back Method");
239                 _getVersionBackWebAsset(req, res, config, form, user);
240
241             } catch (Exception JavaDoc ae) {
242                 _handleException(ae, req);
243                 return;
244             }
245             _sendToReferral(req, res, referer);
246         }
247         /*
248          * If we are getting the container versions, run the assetversions
249          * action and return to the list
250          */

251         else if ((cmd != null)
252                 && cmd.equals(com.dotmarketing.util.Constants.ASSETVERSIONS)) {
253             try {
254                 Logger.debug(this, "Calling Get Versions Method");
255                 _getVersionsWebAsset(req, res, config, form, user,
256                         WebKeys.CONTAINER_EDIT, WebKeys.CONTAINER_VERSIONS);
257
258             } catch (Exception JavaDoc ae) {
259                 _handleException(ae, req);
260                 return;
261             }
262         }
263         /*
264          * If we are unlocking the container, run the unlock action and return
265          * to the list
266          */

267         else if ((cmd != null)
268                 && cmd.equals(com.dotmarketing.util.Constants.UNLOCK)) {
269             try {
270                 Logger.debug(this, "Calling Unlock Method");
271                 _unLockWebAsset(req, res, config, form, user,
272                         WebKeys.CONTAINER_EDIT);
273
274             } catch (Exception JavaDoc ae) {
275                 _handleException(ae, req);
276                 return;
277             }
278             _sendToReferral(req, res, referer);
279
280         }
281         /*
282          * If we are copying the container, run the copy action and return to
283          * the list
284          */

285         else if ((cmd != null)
286                 && cmd.equals(com.dotmarketing.util.Constants.COPY)) {
287             try {
288                 Logger.debug(this, "Calling Copy Method");
289                 _copyWebAsset(req, res, config, form, user);
290             } catch (Exception JavaDoc ae) {
291                 _handleException(ae, req);
292             }
293             _sendToReferral(req, res, referer);
294         }
295         /*
296          * If we are moving the container, run the copy action and return to the
297          * list
298          */

299         else if ((cmd != null)
300                 && cmd.equals(com.dotmarketing.util.Constants.MOVE)) {
301             try {
302                 Logger.debug(this, "Calling Move Method");
303                 _moveWebAsset(req, res, config, form, user, Container.class,
304                         WebKeys.CONTAINER_EDIT);
305             } catch (Exception JavaDoc ae) {
306                 _handleException(ae, req);
307             }
308             _sendToReferral(req, res, referer);
309         } else
310             Logger.debug(this, "Unspecified Action");
311
312         DotHibernate.commitTransaction();
313         setForward(req, "portlet.ext.containers.edit_container");
314     }
315
316     // /// ************** ALL METHODS HERE *************************** ////////
317

318     /**
319      * Method called to load the edit attributes in the request
320      */

321     @SuppressWarnings JavaDoc("unchecked")
322     public void _editWebAsset(ActionRequest req, ActionResponse res,
323             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
324
325         // calls edit method from super class that returns parent folder
326
super._editWebAsset(req, res, config, form, user, WebKeys.CONTAINER_EDIT);
327
328         // setting parent folder path and inode on the form bean
329
ContainerForm cf = (ContainerForm) form;
330
331         // This can't be done on the WebAsset so it needs to be done here.
332
Container container = (Container) req
333                 .getAttribute(WebKeys.CONTAINER_EDIT);
334
335         if (UtilMethods.isSet(container.getLuceneQuery())) {
336             cf.setDynamic(true);
337         }
338         
339         // Permission variable
340
req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, container);
341
342         // Getting container structure
343
if (cf.getStructureInode() == 0) {
344             Structure currentStructure;
345             if (container.getInode() == 0) {
346                 currentStructure = StructureFactory.getDefaultStructure();
347             } else {
348                 currentStructure = (Structure) InodeFactory.getParentOfClass(
349                         container, Structure.class);
350                 if (currentStructure.getInode() == 0)
351                     currentStructure = StructureFactory.getDefaultStructure();
352             }
353             cf.setStructureInode(currentStructure.getInode());
354         }
355         
356         //gets the template host
357
Host host = HostFactory.getParentHost(container);
358         cf.setHostInode(host.getInode());
359         
360         //Asset Versions to list in the versions tab
361
req.setAttribute(WebKeys.VERSIONS_INODE_EDIT, container);
362
363     }
364
365     /**
366      * Method called to save container in the system
367      */

368     public void _saveWebAsset(ActionRequest req, ActionResponse res,
369             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
370
371         // wraps request to get session object
372
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
373         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
374
375         ContainerForm fm = (ContainerForm) form;
376
377         // gets the new information for the container from the request object
378
req.setAttribute(WebKeys.CONTAINER_FORM_EDIT,
379                 new com.dotmarketing.portlets.containers.model.Container());
380         BeanUtils.copyProperties(req.getAttribute(WebKeys.CONTAINER_FORM_EDIT),
381                 form);
382
383         // gets the new information for the container from the request object
384
Container container = (Container) req
385                 .getAttribute(WebKeys.CONTAINER_FORM_EDIT);
386         // gets the current container being edited from the request object
387
Container currentContainer = (Container) req
388                 .getAttribute(WebKeys.CONTAINER_EDIT);
389
390         // Checking permissions
391
_checkPermissions(currentContainer, user, httpReq);
392
393         // Current asociated templates
394
List JavaDoc currentTemplates = InodeFactory.getChildrenClass(currentContainer,
395                 Template.class);
396
397         // gets user id from request for mod user
398
String JavaDoc userId = user.getUserId();
399
400         // gets identifier from the current asset
401
Identifier identifier = IdentifierFactory.getIdentifierByInode(currentContainer);
402
403         // it saves or updates the asset
404
if (currentContainer.getInode() > 0) {
405             WebAssetFactory.createAsset(container, userId, identifier, false);
406             container = (Container) WebAssetFactory.saveAsset(container,
407                     identifier);
408         } else {
409             WebAssetFactory.createAsset(container, userId);
410         }
411
412         req.setAttribute(WebKeys.CONTAINER_FORM_EDIT, container);
413
414         // Get templates of the old version so you can update the working
415
// information to this new version.
416
java.util.Iterator JavaDoc it = currentTemplates.iterator();
417
418         // update templates to new version
419
while (it.hasNext()) {
420             Template parentInode = (Template) it.next();
421             parentInode.addChild(container);
422         }
423
424         // Associating the current structure
425
Structure currentStructure = null;
426         if (fm.getStructureInode() == 0) {
427             currentStructure = StructureFactory.getDefaultStructure();
428         } else {
429             currentStructure = (Structure) InodeFactory.getInode(fm
430                     .getStructureInode(), Structure.class);
431         }
432         container.addParent(currentStructure);
433
434         //Saving the host of the template
435
Host host = HostFactory.getHost(fm.getHostInode());
436         container.addParent(host);
437         
438         SessionMessages.add(httpReq, "message", "message.containers.save");
439         // saves to working folder under velocity
440
ContainerServices.writeContainerToFile(container, true);
441
442         // Saving permissions
443
_applyPermissions(req, container);
444
445         // copies the information back into the form bean
446
BeanUtils.copyProperties(form, req
447                 .getAttribute(WebKeys.CONTAINER_FORM_EDIT));
448     }
449
450     public void _copyWebAsset(ActionRequest req, ActionResponse res,
451             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
452
453         // wraps request to get session object
454
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
455         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
456
457         Logger.debug(this, "I'm copying the Container");
458
459         // gets the current template being edited from the request object
460
Container currentContainer = (Container) req
461                 .getAttribute(WebKeys.CONTAINER_EDIT);
462
463         // Checking permissions
464
_checkCopyAndMovePermissions(currentContainer, user, httpReq,
465                 "copy");
466
467         // Calling the factory to execute the copy operation
468
ContainerFactory.copyContainer(currentContainer);
469
470         SessionMessages.add(httpReq, "message", "message.containers.copy");
471     }
472
473     public void _getVersionBackWebAsset(ActionRequest req, ActionResponse res,
474             PortletConfig config, ActionForm form, User user) throws Exception JavaDoc {
475         Container workingContainer = (Container) super._getVersionBackWebAsset(
476                 req, res, config, form, user, Container.class,
477                 WebKeys.CONTAINER_EDIT);
478         ContainerServices.writeContainerToFile(workingContainer, true);
479     }
480
481 }
482
Popular Tags