KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > entities > action > EditEntityAction


1 package com.dotmarketing.portlets.entities.action;
2
3 import javax.portlet.ActionRequest;
4 import javax.portlet.ActionResponse;
5 import javax.portlet.PortletConfig;
6 import javax.portlet.RenderRequest;
7 import javax.portlet.RenderResponse;
8
9 import org.apache.commons.beanutils.BeanUtils;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13
14 import com.dotmarketing.factories.InodeFactory;
15 import com.dotmarketing.portal.struts.DotPortletAction;
16 import com.dotmarketing.portlets.categories.model.Category;
17 import com.dotmarketing.portlets.entities.model.Entity;
18 import com.dotmarketing.util.Logger;
19 import com.dotmarketing.util.Validator;
20 import com.dotmarketing.util.WebKeys;
21 import com.liferay.util.servlet.SessionMessages;
22
23 public class EditEntityAction extends DotPortletAction {
24     
25     public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig config, RenderRequest req,
26             RenderResponse res) throws Exception JavaDoc {
27         
28         _editEntity(form, req, res);
29         BeanUtils.copyProperties(form,req.getAttribute(WebKeys.ENTITY_EDIT));
30         return mapping.findForward("portlet.ext.entities.edit_entity");
31     }
32     
33     public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req,
34             ActionResponse res) throws Exception JavaDoc {
35         String JavaDoc cmd = req.getParameter(com.liferay.portal.util.Constants.CMD);
36         
37         _editEntity(form, req, res);
38         
39         String JavaDoc referer = req.getParameter("referer");
40         
41         // Saving Entity
42
if (com.liferay.portal.util.Constants.SAVE.equals(cmd)) {
43             
44             Logger.debug(this, "Entity: Saving Entity");
45             
46             if (!Validator.validate(req, form, mapping)) {
47                 Logger.debug(this, "Entity Form Validation Failed Entity");
48                 setForward(req, "portlet.ext.entities.edit_entity");
49                 return;
50             } else {
51                 try {
52                     _saveEntity(form,req,res);
53                 }
54                 catch (Exception JavaDoc e) {
55                     _handleException(e,req);
56                 }
57                 _sendToReferral(req,res,referer);
58             }
59         }
60         // Deleting Entity
61
if (com.liferay.portal.util.Constants.DELETE.equals(cmd)) {
62             Logger.debug(this, "Entity: Deleting Entity");
63             try {
64                 _deleteEntity(form, req, res);
65             }
66             catch (Exception JavaDoc e) {
67                 _handleException(e,req);
68             }
69             _sendToReferral(req,res,referer);
70         }
71     }
72     /* Private Methods */
73     
74     private void _editEntity(ActionForm form, ActionRequest req, ActionResponse res) throws Exception JavaDoc {
75         
76         Entity e= (Entity) InodeFactory.getInode(req.getParameter("inode"),Entity.class);
77         Logger.debug(this, "EditEntityAction: entityInode=" + e.getInode());
78         req.setAttribute(WebKeys.ENTITY_EDIT, e);
79         
80     }
81     
82     private void _editEntity(ActionForm form, RenderRequest req, RenderResponse res) throws Exception JavaDoc {
83         
84         Entity e= (Entity) InodeFactory.getInode(req.getParameter("inode"),Entity.class);
85         Logger.debug(this, "EditEntityAction: entityInode=" + e.getInode());
86         req.setAttribute(WebKeys.ENTITY_EDIT, e);
87         
88     }
89     
90     private void _deleteEntity(ActionForm form, ActionRequest req, ActionResponse res) throws Exception JavaDoc {
91         
92         Entity e = ( Entity ) req.getAttribute(WebKeys.ENTITY_EDIT);
93         InodeFactory.deleteInode(e);
94         
95         //For messages to be displayed on messages page
96
SessionMessages.add(req, "message", "message.entity.delete");
97         
98     }
99     
100     private void _saveEntity(ActionForm form, ActionRequest req, ActionResponse res) throws Exception JavaDoc {
101         
102         BeanUtils.copyProperties(req.getAttribute(WebKeys.ENTITY_EDIT),form);
103         Entity e = (Entity) req.getAttribute(WebKeys.ENTITY_EDIT);
104         Logger.debug(this, "UpdateEntityAction: Inode 1=" + e.getInode());
105         InodeFactory.saveInode(e);
106         Logger.debug(this, "UpdateEntityAction: Inode 2=" + e.getInode());
107         
108         //wipe out the old categories
109
java.util.List JavaDoc _cats = InodeFactory.getParentsOfClass(e, Category.class);
110         java.util.Iterator JavaDoc it = _cats.iterator();
111         boolean delChild = true;
112         while (it.hasNext()) {
113             Category cat = ( Category ) it.next();
114             delChild = cat.deleteChild(e);
115         }
116         
117         //add the new categories
118
String JavaDoc[] arr = e.getCategories();
119         if (arr != null) {
120             for (int i = 0; i < arr.length; i++) {
121                 Category node = ( Category ) InodeFactory.getInode(arr[i], Category.class);
122                 node.addChild(e);
123             }
124         }
125         //For messages to be displayed on messages page
126
SessionMessages.add(req, "message", "message.entity.save");
127     }
128     
129 }
130
Popular Tags