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.ActionPage; 20 import com.blandware.atleap.model.core.ContentField; 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.service.core.ContentFieldManager; 25 import com.blandware.atleap.service.core.LookupManager; 26 import com.blandware.atleap.webapp.action.core.BaseAction; 27 import com.blandware.atleap.webapp.form.ContentFieldForm; 28 import com.blandware.atleap.webapp.util.core.WebappConstants; 29 import com.blandware.atleap.webapp.util.core.WebappUtil; 30 import org.apache.commons.validator.GenericValidator; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 import org.apache.struts.action.ActionMessage; 35 import org.apache.struts.action.ActionMessages; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import java.util.Set ; 40 import java.util.TreeSet ; 41 42 61 public final class CallUpdateContentFieldAction extends BaseAction { 62 71 public ActionForward execute(ActionMapping mapping, ActionForm form, 72 HttpServletRequest request, HttpServletResponse response) throws Exception { 73 74 ContentFieldForm contentFieldForm = (ContentFieldForm) form; 75 76 Long ownerId = null; 77 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) { 78 ownerId = Long.valueOf(contentFieldForm.getOwnerId()); 79 } else { 80 if ( log.isWarnEnabled() ) { 81 log.warn("Missing owner ID. Returning to index..."); 82 } 83 return mapping.findForward("admin"); 84 } 85 86 request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId); 87 88 if ( isCancelled(request) ) { 89 String requestUrl = (String ) request.getSession().getAttribute(WebappConstants.REDIRECT_URL_KEY); 90 if ( !GenericValidator.isBlankOrNull(requestUrl) ) { 91 request.getSession().removeAttribute(WebappConstants.REDIRECT_URL_KEY); 92 return new ActionForward(requestUrl, true); 93 } else { 94 return mapping.findForward("listContentFields"); 95 } 96 } 97 98 if (!request.isUserInRole("core-contentField-update") && !request.isUserInRole("core-contentField-updateOverriden") && !request.isUserInRole("core-contentField-updateIndexed")) { 99 response.sendError(HttpServletResponse.SC_FORBIDDEN); 100 return null; 101 } 102 103 Long contentFieldId = null; 104 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) { 105 contentFieldId = Long.valueOf(contentFieldForm.getId()); 106 } else { 107 if ( log.isWarnEnabled() ) { 108 log.warn("Missing content field ID. Returning to list..."); 109 } 110 return mapping.findForward("listContentFields"); 111 } 112 113 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 114 ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId); 115 if ( contentField == null ) { 116 ActionMessages errors = new ActionMessages(); 118 errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound")); 119 saveErrors(request, errors); 120 return mapping.findForward("listContentFields"); 121 } 122 123 WebappUtil.copyProperties(contentFieldForm, contentField, request); 124 125 String fieldIdentifier = contentField.getIdentifier(); 126 int k = fieldIdentifier.indexOf('['); 127 String index = null; 128 if ( k != -1 ) { 129 index = fieldIdentifier.substring(k + 1, fieldIdentifier.indexOf(']', k)); 130 fieldIdentifier = fieldIdentifier.substring(0, k); 131 contentFieldForm.setIdentifier(fieldIdentifier); 132 contentFieldForm.setIndex(index); 133 } 134 135 LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN); 136 Localizable owner = lookupManager.retrieveLocalizable(ownerId); 137 138 if ( owner == null ) { 139 ActionMessages errors = new ActionMessages(); 140 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound")); 141 saveErrors(request, errors); 142 return mapping.findForward("admin"); 143 } 144 145 154 155 boolean canCreateOrUpdate = false; 156 Set identifiers = null; 157 Set ownerIdentifiers = WebappUtil.getLocalizableUnIndexedFieldsIdentifiers(owner); 158 if ( owner instanceof ContentPage || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-update")) ) { 159 if ( !request.isUserInRole("core-contentField-updateOverriden") ) { 161 response.sendError(HttpServletResponse.SC_FORBIDDEN); 162 return null; 163 } 164 identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 165 identifiers.removeAll(ownerIdentifiers); 166 identifiers.add(fieldIdentifier); 167 canCreateOrUpdate = identifiers != null && !identifiers.isEmpty() && identifiers.contains(fieldIdentifier); 168 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers); 169 } else if ( owner instanceof Layout && !request.isUserInRole("core-contentField-update") ) { 170 boolean canUpdateOverriden = request.isUserInRole("core-contentField-updateOverriden"); 172 boolean canUpdateIndexed = request.isUserInRole("core-contentField-updateIndexed"); 173 if ( !canUpdateIndexed && !canUpdateOverriden ) { 174 response.sendError(HttpServletResponse.SC_FORBIDDEN); 175 return null; 176 } 177 Layout ownerLayout = (Layout) owner; 178 if (canUpdateIndexed) { 179 identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 180 if (identifiers == null) { 181 identifiers = new TreeSet (); 182 } 183 } 184 Set overradableIdentifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 185 if ( (index == null || index.equalsIgnoreCase("0")) && !overradableIdentifiers.contains(fieldIdentifier) ) { 187 ActionMessages errors = new ActionMessages(); 188 errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate")); 189 saveErrors(request, errors); 190 return mapping.findForward("listContentFields"); 191 } 192 if (canUpdateOverriden) { 193 identifiers.addAll(overradableIdentifiers); 194 } 195 identifiers.removeAll(ownerIdentifiers); 196 identifiers.add(fieldIdentifier); 197 canCreateOrUpdate = identifiers != null && !identifiers.isEmpty(); 198 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY, identifiers); 199 } else { 200 request.getSession().removeAttribute(WebappConstants.CONTENT_FIELD_IDENTIFIERS_COLLECTION_KEY); 203 if ( !request.isUserInRole("core-contentField-update") ) { 204 response.sendError(HttpServletResponse.SC_FORBIDDEN); 205 return null; 206 } else { 207 canCreateOrUpdate = true; 208 } 209 } 210 211 if ( !canCreateOrUpdate ) { 212 ActionMessages errors = new ActionMessages(); 213 errors.add("cantCreateOrUpdate", new ActionMessage("core.contentField.errors.cantCreateOrUpdate")); 214 saveErrors(request, errors); 215 return mapping.findForward("listContentFields"); 216 } 217 218 String ownerInfo = WebappUtil.getLocalizableInfo(owner, request); 219 request.getSession().setAttribute(WebappConstants.OWNER_INFO_KEY, ownerInfo); 220 221 saveToken(request); 223 return mapping.findForward("updateContentField"); 224 } 225 226 } | Popular Tags |