KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > organization > action > EditOrganizationAction


1 package com.dotmarketing.portlets.organization.action;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator 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.servlet.http.HttpServletRequest JavaDoc;
11
12 import org.apache.commons.beanutils.BeanUtils;
13 import org.apache.struts.action.ActionForm;
14 import org.apache.struts.action.ActionMapping;
15
16 import com.dotmarketing.beans.Host;
17 import com.dotmarketing.beans.Identifier;
18 import com.dotmarketing.beans.Inode;
19 import com.dotmarketing.beans.MultiTree;
20 import com.dotmarketing.beans.Permission;
21 import com.dotmarketing.beans.WebAsset;
22 import com.dotmarketing.cache.PermissionCache;
23 import com.dotmarketing.db.DotHibernate;
24 import com.dotmarketing.factories.HostFactory;
25 import com.dotmarketing.factories.IdentifierFactory;
26 import com.dotmarketing.factories.InodeFactory;
27 import com.dotmarketing.factories.MultiTreeFactory;
28 import com.dotmarketing.factories.PermissionFactory;
29 import com.dotmarketing.factories.PublishFactory;
30 import com.dotmarketing.factories.RoleFactory;
31 import com.dotmarketing.factories.WebAssetFactory;
32 import com.dotmarketing.portal.struts.DotPortletAction;
33 import com.dotmarketing.portlets.categories.model.Category;
34 import com.dotmarketing.portlets.containers.model.Container;
35 import com.dotmarketing.portlets.contentlet.model.Contentlet;
36 import com.dotmarketing.portlets.files.model.File;
37 import com.dotmarketing.portlets.folders.factories.FolderFactory;
38 import com.dotmarketing.portlets.folders.model.Folder;
39 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
40 import com.dotmarketing.portlets.links.model.Link;
41 import com.dotmarketing.portlets.organization.factories.OrganizationFactory;
42 import com.dotmarketing.portlets.organization.model.Organization;
43 import com.dotmarketing.portlets.organization.struts.OrganizationForm;
44 import com.dotmarketing.portlets.structure.model.Structure;
45 import com.dotmarketing.portlets.templates.factories.TemplateFactory;
46 import com.dotmarketing.portlets.templates.model.Template;
47 import com.dotmarketing.util.Config;
48 import com.dotmarketing.util.Logger;
49 import com.dotmarketing.util.UtilMethods;
50 import com.dotmarketing.util.Validator;
51 import com.dotmarketing.util.WebKeys;
52 import com.liferay.portal.model.Role;
53 import com.liferay.portal.model.User;
54 import com.liferay.portal.util.Constants;
55 import com.liferay.portlet.ActionRequestImpl;
56 import com.liferay.util.servlet.SessionMessages;
57
58 /**
59  * @author Maria
60  */

61
62 public class EditOrganizationAction extends DotPortletAction {
63
64     public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req,
65             ActionResponse res) throws Exception JavaDoc {
66
67         String JavaDoc cmd = req.getParameter(Constants.CMD);
68
69         // get the user
70
User user = null;
71         try {
72             user = com.liferay.portal.util.PortalUtil.getUser(req);
73         } catch (Exception JavaDoc e) {
74             _handleException(e, req);
75         }
76
77         /*
78          * get the mainglist object, stick it in request
79          *
80          */

81         try {
82             _retrieveOrganization(req, res, config, form);
83         } catch (Exception JavaDoc ae) {
84             _handleException(ae, req);
85         }
86
87         /*
88          * if we are saving
89          *
90          */

91         if ((cmd != null) && cmd.equals(Constants.ADD)) {
92             try {
93                 if (Validator.validate(req, form, mapping)) {
94                     if (_saveOrganization(req, res, config, form, user)) {
95                         _sendToReferral(req, res, "");
96                         return;
97                     }
98                 } else {
99                     setForward(req, mapping.getInput());
100                     return;
101                 }
102             } catch (Exception JavaDoc ae) {
103                 _handleException(ae, req);
104             }
105         }
106
107         /*
108          * deleting the list, return to listing page
109          *
110          */

111         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
112             try {
113                 _deleteOrganization(req, res, config, form, user);
114             } catch (Exception JavaDoc ae) {
115                 _handleException(ae, req);
116             }
117             _sendToReferral(req, res, "");
118             return;
119         }
120
121         /*
122          * Copy copy props from the db to the form bean
123          *
124          */

125         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
126             try {
127                 _editOrganization(req, res, config, form);
128             } catch (Exception JavaDoc ae) {
129                 _handleException(ae, req);
130             }
131         }
132
133         /*
134          * return to edit page
135          *
136          */

137         setForward(req, "portlet.ext.organization.edit_organization");
138     }
139
140     private void _retrieveOrganization(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form)
141             throws Exception JavaDoc {
142
143         Organization organization = OrganizationFactory.getOrganization(req.getParameter("inode"));
144         req.setAttribute(WebKeys.ORGANIZATION_EDIT, organization);
145
146         List JavaDoc systems = OrganizationFactory.getAllSystems();
147         req.setAttribute("systems", systems.iterator());
148
149     }
150
151     private void _editOrganization(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form)
152             throws Exception JavaDoc {
153
154         Organization organization = (Organization) req.getAttribute(WebKeys.ORGANIZATION_EDIT);
155         BeanUtils.copyProperties(form, organization);
156         OrganizationForm organizationForm = (OrganizationForm) form;
157
158         // to set the previous parent system
159
Organization parent = OrganizationFactory.getParentOrganization(organization);
160         if (parent.getInode() > 0) {
161             organizationForm.setParentSystem(parent.getInode());
162         }
163
164         // gets the partner portal information and sets it on the bean
165
if (organizationForm.getParentSystem() > 0 && organization.getInode() == 0) {
166             parent = OrganizationFactory.getOrganization(organizationForm.getParentSystem());
167             organizationForm.setPartnerUrl(parent.getPartnerUrl());
168             organizationForm.setPartnerKey(parent.getPartnerKey());
169             organizationForm.setPartnerLogo(parent.getPartnerLogo());
170         }
171
172         // gets the categories and sets them on the bean
173
java.util.List JavaDoc _cats = InodeFactory.getParentsOfClass(organization, Category.class);
174         String JavaDoc[] categories = new String JavaDoc[_cats.size()];
175         java.util.Iterator JavaDoc it = _cats.iterator();
176         int i = 0;
177         while (it.hasNext()) {
178             Category cat = (Category) it.next();
179             categories[i++] = String.valueOf(cat.getInode());
180         }
181         organizationForm.setCategories(categories);
182
183         // gets partner logo
184
if (organizationForm.getPartnerLogo() > 0) {
185             Identifier identifier = (Identifier) InodeFactory.getInode(organizationForm.getPartnerLogo(),
186                     Identifier.class);
187             File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier, File.class);
188             organizationForm.setLogoImage(file.getInode());
189         }
190     }
191
192     private void updateOrgsSharingPartnerPortal(Organization organization, String JavaDoc oldPartnerURL) {
193         List JavaDoc organizations = OrganizationFactory.getOrganizationsSharingPartnerPortal(oldPartnerURL);
194         Iterator JavaDoc orgIter = organizations.iterator();
195         while (orgIter.hasNext()) {
196             Organization orgSibbling = (Organization) orgIter.next();
197             orgSibbling.setPartnerUrl(organization.getPartnerUrl());
198             orgSibbling.setPartnerLogo(organization.getPartnerLogo());
199             orgSibbling.setPartnerKey(organization.getPartnerKey());
200         }
201     }
202
203     private boolean _saveOrganization(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form,
204             User user) throws Exception JavaDoc {
205
206         // wraps request to get session object
207
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
208         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
209
210         Organization organization = (Organization) req.getAttribute(WebKeys.ORGANIZATION_EDIT);
211         OrganizationForm organizationForm = (OrganizationForm) form;
212
213         Organization organizationByPartnerURL = OrganizationFactory
214                 .getFirstLevelOrganizationByPartnerURL(organizationForm.getPartnerUrl());
215         ;
216         if (UtilMethods.isSet(organizationForm.getPartnerUrl()) && organizationByPartnerURL.getInode() > 0
217                 && organizationByPartnerURL.getInode() != organization.getInode()
218                 && organizationByPartnerURL.getInode() != organizationForm.getParentSystem()) {
219             SessionMessages.add(httpReq, "message", "error.organization.duplicate.partner_url");
220             return false;
221         } else {
222
223             String JavaDoc oldPartnerURL = organization.getPartnerUrl();
224
225             BeanUtils.copyProperties(organization, form);
226
227             // gets partner logo image inode and saves the identifier inode as
228
// the Partner Logo column on the organization table
229
long logoImageInode = organizationForm.getLogoImage();
230             if (logoImageInode > 0) {
231                 File file = (File) InodeFactory.getInode(logoImageInode, File.class);
232                 Identifier identifier = IdentifierFactory.getIdentifierByInode(file);
233                 organization.setPartnerLogo(identifier.getInode());
234             }
235
236             InodeFactory.saveInode(organization);
237
238             if (organizationForm.getParentSystem() > 0) {
239                 if (UtilMethods.isSet(organizationForm.getPartnerUrl())) {
240                     // we will update all facilities sharing the same partner
241
// portal site
242
updateOrgsSharingPartnerPortal(organization, oldPartnerURL);
243                 }
244                 // looks for the previous parent organization.
245
Organization oldParent = OrganizationFactory.getParentOrganization(organization);
246                 // only if we changed it, it's going to save it
247
if (oldParent.getInode() != organizationForm.getParentSystem()) {
248                     if (oldParent.getInode() > 0) {
249                         oldParent.deleteChild(organization);
250                     }
251
252                     // there is a parent system
253
Organization parent = OrganizationFactory.getOrganization(organizationForm.getParentSystem());
254                     parent.addChild(organization);
255                     organization.setParentOrganization(parent.getInode());
256                     InodeFactory.saveInode(organization);
257                 }
258             } else {
259                 Organization oldParent = OrganizationFactory.getParentOrganization(organization);
260                 if (oldParent.getInode() != organizationForm.getParentSystem()) {
261                     if (oldParent.getInode() > 0) {
262                         oldParent.deleteChild(organization);
263                     }
264                 }
265                 organization.setParentOrganization(0);
266             }
267
268             // wipe out the old categories
269
java.util.List JavaDoc _cats = InodeFactory.getParentsOfClass(organization, Category.class);
270             java.util.Iterator JavaDoc it = _cats.iterator();
271             while (it.hasNext()) {
272                 Category cat = (Category) it.next();
273                 cat.deleteChild(organization);
274             }
275
276             // add the new categories
277
String JavaDoc[] arr = organizationForm.getCategories();
278             if (arr != null) {
279                 for (int i = 0; i < arr.length; i++) {
280                     Category node = (Category) InodeFactory.getInode(arr[i], Category.class);
281                     node.addChild(organization);
282                 }
283             }
284             /*if (UtilMethods.isSet(organizationForm.getPartnerUrl())) {
285                 try {
286                     String partnerUrl = organizationForm.getPartnerUrl();
287                     _generatePartnerFolder(partnerUrl, req);
288
289                 } catch (Exception p) {
290                     p.printStackTrace();
291                 }
292             }*/

293             SessionMessages.add(httpReq, "message", "message.organization.save");
294             return true;
295         }
296     }
297
298     private void _deleteOrganization(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form,
299             User user) throws Exception JavaDoc {
300
301         // wraps request to get session object
302
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
303         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
304
305         Organization organization = (Organization) req.getAttribute(WebKeys.ORGANIZATION_EDIT);
306         InodeFactory.deleteInode(organization);
307         // gets the session object for the messages
308
SessionMessages.add(httpReq, "message", "message.organization.delete");
309     }
310
311     /**
312      * This method was create to add the partner folder in the CMS
313      *
314      * @author Oswaldo Gallango
315      * @param uri
316      * partner URL
317      * @param req
318      */

319     @SuppressWarnings JavaDoc( { "unchecked", "unchecked" })
320     private void _generatePartnerFolder(String JavaDoc uri, ActionRequest req) {
321
322         String JavaDoc folderPath = Config.getStringProperty("PARTNERS_URL_FOLDER") + uri.substring(0, 2).toLowerCase() + uri
323                 + "/";
324
325         try {
326
327             Host currentHost = HostFactory.getCurrentHost(req);
328
329             Folder folder = FolderFactory.getFolderByPath(folderPath, currentHost);
330             HTMLPage workingAsset = null;
331             User user = _getUser(req);
332
333             /* Check if exists the organization folder */
334             if (folder.getInode() == 0) {
335
336                 /* If don`t exits add the folder and the html index default page */
337                 Folder parentFolder = FolderFactory.createFolders(folderPath, currentHost, req);
338                 parentFolder.setHostInode(currentHost.getInode());
339                 InodeFactory.saveInode(parentFolder);
340
341                 Logger.debug(EditOrganizationAction.class, "Folder " + folderPath + " .Created");
342
343                 HTMLPage newHtmlPage = new HTMLPage();
344                 newHtmlPage.setTitle("index");
345                 newHtmlPage.setFriendlyName("index");
346                 newHtmlPage.setHttpsRequired(false);
347                 newHtmlPage.setPageUrl("index." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"));
348                 newHtmlPage.setType("htmlpage");
349                 newHtmlPage.setLive(true);
350
351                 WebAssetFactory.createAsset(newHtmlPage, user.getUserId(), parentFolder);
352                 workingAsset = newHtmlPage;
353
354                 /** ** Added ** */
355                 String JavaDoc baseHomePageURL = Config.getStringProperty("PARTNERS_URL_FOLDER") + "/home.dot";
356                 Identifier homePageIdentifier = IdentifierFactory.getIdentifierByURI(baseHomePageURL, currentHost);
357                 Identifier newPageIdentifier = IdentifierFactory.getIdentifierByURI(folderPath + "index."
358                         + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"), currentHost);
359
360                 List JavaDoc<MultiTree> asociateMultiTrees = MultiTreeFactory.getMultiTree(homePageIdentifier);
361                 for (MultiTree tree : asociateMultiTrees) {
362                     /* Adding relation */
363                     MultiTree m = new MultiTree(newPageIdentifier.getInode(), tree.getParent2(), tree.getChild());
364                     MultiTreeFactory.saveMultiTree(m);
365                 }
366
367                 /** *** */
368
369                 List JavaDoc<Template> templates = TemplateFactory.getTemplateByCondition("title like '"
370                         + Config.getStringProperty("PARTNERS_TEMPLATE") + "'");
371                 long template = ((Template) templates.get(0)).getInode();
372
373                 // Adds template children for partners pages
374
Template templateInode = (Template) InodeFactory.getInode(template, Template.class);
375                 Identifier templateIdentifier = IdentifierFactory.getParentIdentifier(templateInode);
376                 Template templateWorking = (Template) IdentifierFactory.getWorkingChildOfClass(templateIdentifier,
377                         Template.class);
378                 templateWorking.addChild(workingAsset);
379
380                 DotHibernate.flush();
381                 DotHibernate.save(workingAsset);
382
383                 /* add the permission */
384                 _applyPermissions(req, workingAsset);
385
386                 /* Publishing asset */
387                 ActionRequestImpl reqImpl = (ActionRequestImpl) req;
388                 HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
389
390                 PublishFactory.publishAsset(workingAsset, httpReq);
391
392             }
393
394         } catch (Exception JavaDoc ex) {
395             ex.printStackTrace();
396             Logger.debug(EditOrganizationAction.class, "Error creating Folder " + folderPath + " .");
397         }
398     }
399
400     @SuppressWarnings JavaDoc("unchecked")
401     public void _applyPermissions(ActionRequest req, Inode inode) {
402
403         // If the permission has been changed, the method save the permission
404
// otherwise not
405

406         Inode origInode = inode;
407
408         if (!(inode instanceof Folder || inode instanceof Host || inode instanceof Structure)) {
409             inode = IdentifierFactory.getIdentifierByInode(inode);
410         }
411
412         Permission permission = null;
413
414         java.util.List JavaDoc currentPerms = PermissionFactory.getPermissions(inode);
415         java.util.Iterator JavaDoc iterPerms = currentPerms.iterator();
416         while (iterPerms.hasNext()) {
417             PermissionFactory.deletePermission((Permission) iterPerms.next());
418         }
419
420         // Add permissions to anonymous users
421
Role roleAnonymous = RoleFactory.getRoleByName(Config.getStringProperty("CMS_ANONYMOUS_ROLE"));
422
423         if (roleAnonymous != null) {
424             // adds read permissions to the inode
425
Logger.debug(this, "_applyPermissions: Saving Read Permission for=" + roleAnonymous.getRoleId());
426             permission = new Permission(inode.getInode(), Long.parseLong(roleAnonymous.getRoleId()), Config
427                     .getIntProperty("PERMISSION_READ"));
428             PermissionFactory.savePermission(permission);
429
430             // adds publish permissions to the inode
431
Logger.debug(this, "_applyPermissions: Saving Publish Permission for=" + roleAnonymous.getRoleId());
432             permission = new Permission(inode.getInode(), Long.parseLong(roleAnonymous.getRoleId()), Config
433                     .getIntProperty("PERMISSION_PUBLISH"));
434             PermissionFactory.savePermission(permission);
435         }
436
437         // Add permissions to administrator user
438
Role roleCMSAdministrator = RoleFactory.getRoleByName(Config.getStringProperty("CMS_ADMINISTRATOR_ROLE"));
439
440         if (roleCMSAdministrator != null) {
441             // adds read permissions to the inode
442
Logger.debug(this, "_applyPermissions: Saving Read Permission for=" + roleCMSAdministrator.getRoleId());
443             permission = new Permission(inode.getInode(), Long.parseLong(roleCMSAdministrator.getRoleId()), Config
444                     .getIntProperty("PERMISSION_READ"));
445             PermissionFactory.savePermission(permission);
446
447             // adds write permissions to the inode
448
Logger.debug(this, "_applyPermissions: Saving Write Permission for=" + roleCMSAdministrator.getRoleId());
449             permission = new Permission(inode.getInode(), Long.parseLong(roleCMSAdministrator.getRoleId()), Config
450                     .getIntProperty("PERMISSION_WRITE"));
451             PermissionFactory.savePermission(permission);
452
453             // adds publish permissions to the inode
454
Logger.debug(this, "_applyPermissions: Saving Publish Permission for=" + roleCMSAdministrator.getRoleId());
455             permission = new Permission(inode.getInode(), Long.parseLong(roleCMSAdministrator.getRoleId()), Config
456                     .getIntProperty("PERMISSION_PUBLISH"));
457             PermissionFactory.savePermission(permission);
458
459         }
460
461         // Update the permissions cache
462
if (origInode instanceof Folder) {
463             Folder f = (Folder) origInode;
464             PermissionCache.addToPermissionCache(f);
465         } else if (origInode instanceof Host) {
466             Host host = (Host) origInode;
467             PermissionCache.addToPermissionCache(host);
468         } else if (origInode instanceof WebAsset) {
469             WebAsset asset = (WebAsset) origInode;
470             PermissionCache.addToPermissionCache(asset);
471         }
472
473         if (inode instanceof Folder || inode instanceof Host || inode instanceof Structure) {
474
475             Logger.debug(this, "_applyPermissions: applying permissions to children");
476
477             java.util.List JavaDoc children = new ArrayList JavaDoc();
478
479             if (inode instanceof Folder || inode instanceof Host) {
480                 children.addAll(InodeFactory.getChildrenClass(inode, Folder.class));
481                 children.addAll(InodeFactory.getChildrenClass(inode, Contentlet.class));
482                 children.addAll(InodeFactory.getChildrenClass(inode, Container.class));
483                 children.addAll(InodeFactory.getChildrenClass(inode, Template.class));
484                 children.addAll(InodeFactory.getChildrenClass(inode, HTMLPage.class));
485                 children.addAll(InodeFactory.getChildrenClass(inode, Link.class));
486                 children.addAll(InodeFactory.getChildrenClass(inode, File.class));
487             } else if (inode instanceof Structure) {
488                 children.addAll(InodeFactory.getInodesOfClassByCondition(Contentlet.class, "structure_inode = "
489                         + inode.getInode()));
490             }
491
492             java.util.Iterator JavaDoc iterChildren = children.iterator();
493             while (iterChildren.hasNext()) {
494                 _applyPermissions(req, (Inode) iterChildren.next());
495             }
496         }
497
498     }
499
500 }
501
Popular Tags