1 16 package com.blandware.atleap.webapp.action.core.contentFieldValue; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.model.core.ContentField; 20 import com.blandware.atleap.model.core.ContentFieldValue; 21 import com.blandware.atleap.model.core.ContentPage; 22 import com.blandware.atleap.model.core.Layout; 23 import com.blandware.atleap.model.core.Localizable; 24 import com.blandware.atleap.model.core.Page; 25 import com.blandware.atleap.search.SearchManager; 26 import com.blandware.atleap.service.core.ContentFieldManager; 27 import com.blandware.atleap.webapp.action.core.BaseAction; 28 import com.blandware.atleap.webapp.form.core.ContentFieldValueForm; 29 import com.blandware.atleap.webapp.util.core.CacheUtil; 30 import com.blandware.atleap.webapp.util.core.WebappConstants; 31 import org.apache.commons.validator.GenericValidator; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.ActionMessage; 36 import org.apache.struts.action.ActionMessages; 37 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import java.util.ArrayList ; 41 import java.util.List ; 42 43 63 public final class DeleteContentFieldValueAction extends BaseAction { 64 73 public ActionForward execute(ActionMapping mapping, ActionForm form, 74 HttpServletRequest request, HttpServletResponse response) throws Exception { 75 76 ContentFieldValueForm contentFieldValueForm = (ContentFieldValueForm) form; 77 Long contentFieldId = null; 78 if ( !GenericValidator.isBlankOrNull(contentFieldValueForm.getContentFieldId()) ) { 79 contentFieldId = Long.valueOf(contentFieldValueForm.getContentFieldId()); 80 } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY) != null ) { 81 contentFieldId = (Long ) request.getSession().getAttribute(WebappConstants.CONTENT_FIELD_ID_KEY); 82 } else { 83 if ( log.isWarnEnabled() ) { 84 log.warn("Missing content field ID. Returning to index..."); 85 } 86 return mapping.findForward("admin"); 87 } 88 89 List cfvLocales = null; 90 91 if ( request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY) != null ) { 92 cfvLocales = (List ) request.getAttribute(WebappConstants.CONTENT_FIELD_VALUE_SELECTED_LOCALES_KEY); 93 } else if ( !GenericValidator.isBlankOrNull(request.getParameter("locale")) ) { 94 String locale = request.getParameter("locale"); 95 cfvLocales = new ArrayList (); 96 cfvLocales.add(locale); 97 } else { 98 if ( log.isWarnEnabled() ) { 99 log.warn("Missing content field value locale. Returning to list..."); 100 } 101 return mapping.findForward("listContentFieldValues"); 102 } 103 104 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 105 ContentField contentField = null; 106 boolean alreadyDeletedExist = false; 107 for ( int i = 0; i < cfvLocales.size(); i++ ) { 108 String localeIdentifier = (String ) cfvLocales.get(i); 109 ContentFieldValue contentFieldValue = contentFieldManager.findContentFieldValueByContentFieldIdAndLocale(contentFieldId, localeIdentifier); 110 if ( contentFieldValue == null ) { 111 alreadyDeletedExist = true; 112 } else { 113 114 if ( contentField == null ) { 115 contentField = contentFieldValue.getContentField(); 116 contentFieldId = contentField.getId(); 117 } 118 119 contentFieldManager.deleteContentFieldValue(contentFieldValue.getId()); 120 121 CacheUtil cacheUtil = CacheUtil.getInstance(request); 123 cacheUtil.flushFieldIndices(); 124 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 125 126 Localizable owner = contentField.getOwner(); 127 if ( owner instanceof Layout ) { 128 Layout layout = (Layout) owner; 129 cacheUtil.flushLayoutFieldValueCache(layout.getDefinition()); 130 131 List contentPages = layout.getContentPages(); 134 for ( int j = 0; j < contentPages.size(); j++ ) { 135 ContentPage contentPage = (ContentPage) contentPages.get(j); 136 searchManager.reIndexPage(contentPage, request); 137 cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri()); 138 } 139 } else if ( owner instanceof Page ) { 140 Page page = (Page) owner; 141 cacheUtil.flushPageFieldValueCache(page.getUri(), contentField.getIdentifier(), localeIdentifier); 142 searchManager.reIndexPage(page, request); 143 if ( page instanceof ContentPage ) { 144 cacheUtil.updateContentPageLastModifiedInCache(page.getUri()); 145 } 146 } 147 } 148 } 149 150 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId); 151 152 if ( alreadyDeletedExist ) { 153 ActionMessages errors = new ActionMessages(); 154 errors.add("someValuesDeleted", new ActionMessage("core.contentFieldValue.errors.someValuesDeleted")); 155 saveErrors(request, errors); 156 } 157 158 return mapping.findForward("listContentFieldValues"); 159 } 160 } | Popular Tags |