1 package com.dotmarketing.portlets.organization.action; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 import javax.portlet.ActionRequest; 8 import javax.portlet.ActionResponse; 9 import javax.portlet.PortletConfig; 10 import javax.servlet.http.HttpServletRequest ; 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 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 { 66 67 String cmd = req.getParameter(Constants.CMD); 68 69 User user = null; 71 try { 72 user = com.liferay.portal.util.PortalUtil.getUser(req); 73 } catch (Exception e) { 74 _handleException(e, req); 75 } 76 77 81 try { 82 _retrieveOrganization(req, res, config, form); 83 } catch (Exception ae) { 84 _handleException(ae, req); 85 } 86 87 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 ae) { 103 _handleException(ae, req); 104 } 105 } 106 107 111 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 112 try { 113 _deleteOrganization(req, res, config, form, user); 114 } catch (Exception ae) { 115 _handleException(ae, req); 116 } 117 _sendToReferral(req, res, ""); 118 return; 119 } 120 121 125 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 126 try { 127 _editOrganization(req, res, config, form); 128 } catch (Exception ae) { 129 _handleException(ae, req); 130 } 131 } 132 133 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 { 142 143 Organization organization = OrganizationFactory.getOrganization(req.getParameter("inode")); 144 req.setAttribute(WebKeys.ORGANIZATION_EDIT, organization); 145 146 List 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 { 153 154 Organization organization = (Organization) req.getAttribute(WebKeys.ORGANIZATION_EDIT); 155 BeanUtils.copyProperties(form, organization); 156 OrganizationForm organizationForm = (OrganizationForm) form; 157 158 Organization parent = OrganizationFactory.getParentOrganization(organization); 160 if (parent.getInode() > 0) { 161 organizationForm.setParentSystem(parent.getInode()); 162 } 163 164 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 java.util.List _cats = InodeFactory.getParentsOfClass(organization, Category.class); 174 String [] categories = new String [_cats.size()]; 175 java.util.Iterator 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 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 oldPartnerURL) { 193 List organizations = OrganizationFactory.getOrganizationsSharingPartnerPortal(oldPartnerURL); 194 Iterator 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 { 205 206 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 208 HttpServletRequest 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 oldPartnerURL = organization.getPartnerUrl(); 224 225 BeanUtils.copyProperties(organization, form); 226 227 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 updateOrgsSharingPartnerPortal(organization, oldPartnerURL); 243 } 244 Organization oldParent = OrganizationFactory.getParentOrganization(organization); 246 if (oldParent.getInode() != organizationForm.getParentSystem()) { 248 if (oldParent.getInode() > 0) { 249 oldParent.deleteChild(organization); 250 } 251 252 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 java.util.List _cats = InodeFactory.getParentsOfClass(organization, Category.class); 270 java.util.Iterator it = _cats.iterator(); 271 while (it.hasNext()) { 272 Category cat = (Category) it.next(); 273 cat.deleteChild(organization); 274 } 275 276 String [] 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 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 { 300 301 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 303 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 304 305 Organization organization = (Organization) req.getAttribute(WebKeys.ORGANIZATION_EDIT); 306 InodeFactory.deleteInode(organization); 307 SessionMessages.add(httpReq, "message", "message.organization.delete"); 309 } 310 311 319 @SuppressWarnings ( { "unchecked", "unchecked" }) 320 private void _generatePartnerFolder(String uri, ActionRequest req) { 321 322 String 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 334 if (folder.getInode() == 0) { 335 336 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 355 String 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 <MultiTree> asociateMultiTrees = MultiTreeFactory.getMultiTree(homePageIdentifier); 361 for (MultiTree tree : asociateMultiTrees) { 362 363 MultiTree m = new MultiTree(newPageIdentifier.getInode(), tree.getParent2(), tree.getChild()); 364 MultiTreeFactory.saveMultiTree(m); 365 } 366 367 368 369 List <Template> templates = TemplateFactory.getTemplateByCondition("title like '" 370 + Config.getStringProperty("PARTNERS_TEMPLATE") + "'"); 371 long template = ((Template) templates.get(0)).getInode(); 372 373 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 384 _applyPermissions(req, workingAsset); 385 386 387 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 388 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 389 390 PublishFactory.publishAsset(workingAsset, httpReq); 391 392 } 393 394 } catch (Exception ex) { 395 ex.printStackTrace(); 396 Logger.debug(EditOrganizationAction.class, "Error creating Folder " + folderPath + " ."); 397 } 398 } 399 400 @SuppressWarnings ("unchecked") 401 public void _applyPermissions(ActionRequest req, Inode inode) { 402 403 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 currentPerms = PermissionFactory.getPermissions(inode); 415 java.util.Iterator iterPerms = currentPerms.iterator(); 416 while (iterPerms.hasNext()) { 417 PermissionFactory.deletePermission((Permission) iterPerms.next()); 418 } 419 420 Role roleAnonymous = RoleFactory.getRoleByName(Config.getStringProperty("CMS_ANONYMOUS_ROLE")); 422 423 if (roleAnonymous != null) { 424 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 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 Role roleCMSAdministrator = RoleFactory.getRoleByName(Config.getStringProperty("CMS_ADMINISTRATOR_ROLE")); 439 440 if (roleCMSAdministrator != null) { 441 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 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 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 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 children = new ArrayList (); 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 iterChildren = children.iterator(); 493 while (iterChildren.hasNext()) { 494 _applyPermissions(req, (Inode) iterChildren.next()); 495 } 496 } 497 498 } 499 500 } 501 | Popular Tags |