1 16 package com.blandware.atleap.webapp.action.core.contentField; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.*; 20 import com.blandware.atleap.search.SearchManager; 21 import com.blandware.atleap.service.core.ContentFieldManager; 22 import com.blandware.atleap.webapp.action.core.BaseAction; 23 import com.blandware.atleap.webapp.form.ContentFieldForm; 24 import com.blandware.atleap.webapp.util.core.CacheUtil; 25 import com.blandware.atleap.webapp.util.core.WebappConstants; 26 import com.blandware.atleap.webapp.util.core.WebappUtil; 27 import org.apache.commons.validator.GenericValidator; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.action.ActionMessage; 32 import org.apache.struts.action.ActionMessages; 33 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import java.util.List ; 37 import java.util.Set ; 38 39 59 public final class DeleteContentFieldAction extends BaseAction { 60 69 public ActionForward execute(ActionMapping mapping, ActionForm form, 70 HttpServletRequest request, HttpServletResponse response) throws Exception { 71 72 ContentFieldForm contentFieldForm = (ContentFieldForm) form; 73 74 Long ownerId = null; 75 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) { 76 ownerId = Long.valueOf(contentFieldForm.getOwnerId()); 77 } else { 78 if ( log.isWarnEnabled() ) { 79 log.warn("Missing owner ID. Returning to index..."); 80 } 81 return mapping.findForward("admin"); 82 } 83 84 request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId); 85 86 Long contentFieldId = null; 87 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) { 88 contentFieldId = Long.valueOf(contentFieldForm.getId()); 89 } else { 90 if ( log.isWarnEnabled() ) { 91 log.warn("Missing content field ID. Returning to list..."); 92 } 93 return mapping.findForward("listContentFields"); 94 } 95 96 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 97 ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId); 98 99 if ( contentField == null ) { 100 ActionMessages errors = new ActionMessages(); 102 errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound")); 103 saveErrors(request, errors); 104 return mapping.findForward("listContentFields"); 105 } 106 107 Localizable owner = contentField.getOwner(); 108 if ( owner instanceof Layout && !request.isUserInRole("core-contentField-delete") ) { 109 110 String fieldIdentifier = contentField.getIdentifier(); 111 int k = fieldIdentifier.indexOf('['); 112 String index = null; 113 if ( k != -1 ) { 114 index = fieldIdentifier.substring(k + 1, fieldIdentifier.indexOf(']', k)); 115 fieldIdentifier = fieldIdentifier.substring(0, k); 116 contentFieldForm.setIdentifier(fieldIdentifier); 117 contentFieldForm.setIndex(index); 118 } 119 120 Set identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 121 boolean overridable = identifiers.contains(fieldIdentifier); 122 123 if ( !overridable && (index == null || index.equalsIgnoreCase("0")) ) { 124 ActionMessages errors = new ActionMessages(); 125 errors.add("contentFieldCannotDelete", new ActionMessage("core.contentField.errors.unindexed.cannotDelete")); 126 saveErrors(request, errors); 127 return mapping.findForward("listContentFields"); 128 } 129 } 130 131 if (owner instanceof ActionPage && !request.isUserInRole("core-contentField-delete")) { 134 Set identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 135 String identifier = contentField.getIdentifierWithoutIndex(); 136 boolean canDelete = identifiers != null && !identifiers.isEmpty() && identifiers.contains(identifier); 137 if (!canDelete) { 138 ActionMessages errors = new ActionMessages(); 139 errors.add("contentFieldCannotDelete", new ActionMessage("core.contentField.errors.actionPageField.cannotDelete")); 140 saveErrors(request, errors); 141 return mapping.findForward("listContentFields"); 142 } 143 } 144 145 contentFieldManager.deleteContentField(contentFieldId); 146 147 CacheUtil cacheUtil = CacheUtil.getInstance(request); 149 cacheUtil.flushFieldIndices(); 150 151 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 153 if ( owner instanceof Layout ) { 154 Layout layout = (Layout) owner; 155 156 List contentPages = layout.getContentPages(); 158 for ( int i = 0; i < contentPages.size(); i++ ) { 159 ContentPage contentPage = (ContentPage) contentPages.get(i); 160 cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri()); 161 searchManager.reIndexPage(contentPage, request); 162 } 163 cacheUtil.flushLayoutFieldValueCache(layout.getDefinition()); 164 165 } else if ( owner instanceof Page ) { 166 Page page = (Page) owner; 167 searchManager.reIndexPage(page, request); 168 cacheUtil.flushPageFieldValueCache(page.getUri()); 169 if ( owner instanceof ContentPage ) { 170 cacheUtil.updateContentPageLastModifiedInCache(page.getUri()); 171 } 172 } 173 174 request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId); 175 return mapping.findForward("listContentFields"); 176 } 177 } | Popular Tags |