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.service.exception.BeanAlreadyExistsException; 27 import com.blandware.atleap.service.exception.OwnerNotFoundException; 28 import com.blandware.atleap.webapp.action.core.BaseAction; 29 import com.blandware.atleap.webapp.form.ContentFieldForm; 30 import com.blandware.atleap.webapp.util.core.WebappConstants; 31 import com.blandware.atleap.webapp.util.core.WebappUtil; 32 import org.apache.commons.validator.GenericValidator; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 import org.apache.struts.action.ActionMessage; 37 import org.apache.struts.action.ActionMessages; 38 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import java.util.HashSet ; 42 import java.util.Set ; 43 import java.util.TreeSet ; 44 45 71 public final class CreateContentFieldAction extends BaseAction { 72 81 public ActionForward execute(ActionMapping mapping, ActionForm form, 82 HttpServletRequest request, HttpServletResponse response) throws Exception { 83 84 ContentFieldForm contentFieldForm = (ContentFieldForm) form; 85 Long ownerId = null; 86 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) { 87 ownerId = Long.valueOf(contentFieldForm.getOwnerId()); 88 } else { 89 if ( log.isWarnEnabled() ) { 90 log.warn("Missing owner ID. Returning to index..."); 91 } 92 return mapping.findForward("admin"); 93 } 94 95 request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId); 96 97 if ( !isCancelled(request) ) { 98 LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN); 99 Localizable owner = lookupManager.retrieveLocalizable(ownerId); 100 101 if ( owner == null ) { 102 ActionMessages errors = new ActionMessages(); 103 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound")); 104 saveErrors(request, errors); 105 return mapping.findForward("admin"); 106 } 107 108 String fieldIdentifier = contentFieldForm.getIdentifier(); 109 110 if (owner instanceof ContentPage || owner instanceof ActionPage) { 112 Set indexedFieldIdentifiers = WebappUtil.getIndexedOverridableFieldIdentifiers(owner, request); 113 Set unIndexedFieldIdentifiers = WebappUtil.getUnIndexedOverridableFieldIdentifiers(owner, request); 114 Set intersection = new HashSet (indexedFieldIdentifiers); 115 intersection.retainAll(unIndexedFieldIdentifiers); 116 if ( !intersection.contains(fieldIdentifier) ) { 117 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 118 if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) { 119 ActionMessages errors = new ActionMessages(); 120 errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex")); 121 saveErrors(request, errors); 122 saveToken(request); 123 return mapping.getInputForward(); 124 } 125 } else { 126 if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) { 127 ActionMessages errors = new ActionMessages(); 128 errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected")); 129 saveErrors(request, errors); 130 saveToken(request); 131 return mapping.getInputForward(); 132 } 133 } 134 } 135 } else if (owner instanceof Layout) { 136 Layout ownerLayout = (Layout) owner; 137 Set indexedFieldIdentifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 138 Set unIndexedFieldIdentifiers = WebappUtil.getUnIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 139 Set intersection = new HashSet (indexedFieldIdentifiers); 140 intersection.retainAll(unIndexedFieldIdentifiers); 141 if ( !intersection.contains(fieldIdentifier) ) { 142 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 143 if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) { 144 ActionMessages errors = new ActionMessages(); 145 errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex")); 146 saveErrors(request, errors); 147 saveToken(request); 148 return mapping.getInputForward(); 149 } 150 } else { 151 if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) { 152 ActionMessages errors = new ActionMessages(); 153 errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected")); 154 saveErrors(request, errors); 155 saveToken(request); 156 return mapping.getInputForward(); 157 } 158 } 159 } 160 } 161 162 if ( owner instanceof ContentPage || (owner instanceof Layout && !request.isUserInRole("core-contentField-create") || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-create"))) ) { 163 Set identifiers = null; 164 if ( owner instanceof ContentPage || owner instanceof ActionPage ) { 165 if ( !request.isUserInRole("core-contentField-override") ) { 167 response.sendError(HttpServletResponse.SC_FORBIDDEN); 168 return null; 169 } 170 identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 171 172 if ( !identifiers.contains(fieldIdentifier) ) { 173 ActionMessages errors = new ActionMessages(); 174 errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier")); 175 saveErrors(request, errors); 176 saveToken(request); 177 return mapping.getInputForward(); 178 } 179 } else { 180 boolean canCreateIndexed = request.isUserInRole("core-contentField-createIndexed"); 182 boolean canOverride = request.isUserInRole("core-contentField-override"); 183 if ( !canCreateIndexed && !canOverride ) { 184 response.sendError(HttpServletResponse.SC_FORBIDDEN); 185 return null; 186 } 187 Layout ownerLayout = (Layout) owner; 188 if (canCreateIndexed) { 189 identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 190 if (identifiers == null) { 191 identifiers = new TreeSet (); 192 } 193 } 194 if (canOverride) { 195 identifiers.addAll(WebappUtil.getOverridableFieldIdentifiers(ownerLayout, request)); 196 } 197 if ( !identifiers.contains(fieldIdentifier) ) { 198 ActionMessages errors = new ActionMessages(); 199 errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier")); 200 saveErrors(request, errors); 201 saveToken(request); 202 return mapping.getInputForward(); 203 } 204 213 } 214 } else { 215 if ( !request.isUserInRole("core-contentField-create") ) { 218 response.sendError(HttpServletResponse.SC_FORBIDDEN); 219 return null; 220 } 221 } 222 223 ContentField contentField = new ContentField(); 224 WebappUtil.copyProperties(contentField, contentFieldForm, request); 225 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 226 fieldIdentifier += "[" + contentFieldForm.getIndex() + "]"; 227 contentField.setIdentifier(fieldIdentifier); 228 } 229 contentField.setInternal(Boolean.FALSE); 230 231 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 232 233 Long contentFieldId = null; 234 try { 235 contentFieldId = contentFieldManager.createContentField(contentField, ownerId); 236 } catch ( OwnerNotFoundException e ) { 237 ActionMessages errors = new ActionMessages(); 238 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound")); 239 saveErrors(request, errors); 240 return mapping.findForward("admin"); 241 } catch ( BeanAlreadyExistsException e ) { 242 ActionMessages errors = new ActionMessages(); 244 errors.add("contentFieldAlreadyExists", new ActionMessage("core.contentField.errors.alreadyExists")); 245 saveErrors(request, errors); 246 saveToken(request); 247 return mapping.getInputForward(); 248 } 249 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, contentFieldId); 250 return mapping.findForward("viewContentField"); 251 } else { 252 return mapping.findForward("listContentFields"); 254 } 255 } 256 } | Popular Tags |