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.service.core.LookupManager; 23 import com.blandware.atleap.service.exception.BeanAlreadyExistsException; 24 import com.blandware.atleap.webapp.action.core.BaseAction; 25 import com.blandware.atleap.webapp.form.ContentFieldForm; 26 import com.blandware.atleap.webapp.util.core.CacheUtil; 27 import com.blandware.atleap.webapp.util.core.WebappConstants; 28 import com.blandware.atleap.webapp.util.core.WebappUtil; 29 import org.apache.commons.validator.GenericValidator; 30 import org.apache.struts.action.*; 31 import org.springframework.orm.ObjectOptimisticLockingFailureException; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import java.util.HashSet ; 36 import java.util.List ; 37 import java.util.Set ; 38 import java.util.TreeSet ; 39 40 69 public final class UpdateContentFieldAction extends BaseAction { 70 79 public ActionForward execute(ActionMapping mapping, ActionForm form, 80 HttpServletRequest request, HttpServletResponse response) throws Exception { 81 ContentFieldForm contentFieldForm = (ContentFieldForm) form; 82 83 Long ownerId = null; 84 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getOwnerId()) ) { 85 ownerId = Long.valueOf(contentFieldForm.getOwnerId()); 86 } else { 87 if ( log.isWarnEnabled() ) { 88 log.warn("Missing owner ID. Returning to index..."); 89 } 90 return mapping.findForward("admin"); 91 } 92 93 request.getSession().setAttribute(WebappConstants.OWNER_ID_KEY, ownerId); 94 95 Long contentFieldId = null; 96 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getId()) ) { 97 contentFieldId = Long.valueOf(contentFieldForm.getId()); 98 } else { 99 if ( log.isWarnEnabled() ) { 100 log.warn("Missing field ID. Returning to list"); 101 } 102 return mapping.findForward("listContentFields"); 103 } 104 105 if ( !isCancelled(request) ) { 106 LookupManager lookupManager = (LookupManager) getBean(Constants.LOOKUP_MANAGER_BEAN); 107 Localizable owner = lookupManager.retrieveLocalizable(ownerId); 108 109 if ( owner == null ) { 110 ActionMessages errors = new ActionMessages(); 111 errors.add("ownerNotFound", new ActionMessage("core.contentField.errors.ownerNotFound")); 112 saveErrors(request, errors); 113 return mapping.findForward("admin"); 114 } 115 116 String fieldIdentifier = contentFieldForm.getIdentifier(); 117 118 if (owner instanceof ContentPage || owner instanceof ActionPage) { 119 Set indexedFieldIdentifiers = WebappUtil.getIndexedOverridableFieldIdentifiers(owner, request); 120 Set unIndexedFieldIdentifiers = WebappUtil.getUnIndexedOverridableFieldIdentifiers(owner, request); 121 Set intersection = new HashSet (indexedFieldIdentifiers); 122 intersection.retainAll(unIndexedFieldIdentifiers); 123 if ( !intersection.contains(fieldIdentifier) ) { 124 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 125 if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) { 126 ActionMessages errors = new ActionMessages(); 127 errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex")); 128 saveErrors(request, errors); 129 saveToken(request); 130 return mapping.getInputForward(); 131 } 132 } else { 133 if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) { 134 ActionMessages errors = new ActionMessages(); 135 errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected")); 136 saveErrors(request, errors); 137 saveToken(request); 138 return mapping.getInputForward(); 139 } 140 } 141 } 142 } else if (owner instanceof Layout) { 143 Layout ownerLayout = (Layout) owner; 144 Set indexedFieldIdentifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 145 Set unIndexedFieldIdentifiers = WebappUtil.getUnIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 146 Set intersection = new HashSet (indexedFieldIdentifiers); 147 intersection.retainAll(unIndexedFieldIdentifiers); 148 if ( !intersection.contains(fieldIdentifier) ) { 149 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 150 if ( unIndexedFieldIdentifiers.contains(fieldIdentifier) ) { 151 ActionMessages errors = new ActionMessages(); 152 errors.add("contentFieldUnexpectedIndex", new ActionMessage("core.contentField.errors.unexpectedIndex")); 153 saveErrors(request, errors); 154 saveToken(request); 155 return mapping.getInputForward(); 156 } 157 } else { 158 if ( indexedFieldIdentifiers.contains(fieldIdentifier) ) { 159 ActionMessages errors = new ActionMessages(); 160 errors.add("contentFieldIndexExpected", new ActionMessage("core.contentField.errors.indexExpected")); 161 saveErrors(request, errors); 162 saveToken(request); 163 return mapping.getInputForward(); 164 } 165 } 166 } 167 } 168 169 if ( owner instanceof ContentPage || (owner instanceof Layout && !request.isUserInRole("core-contentField-update") || (owner instanceof ActionPage && !request.isUserInRole("core-contentField-update"))) ) { 170 Set identifiers = null; 171 if ( owner instanceof ContentPage || owner instanceof ActionPage ) { 172 if ( !request.isUserInRole("core-contentField-updateOverriden") ) { 174 response.sendError(HttpServletResponse.SC_FORBIDDEN); 175 return null; 176 } 177 identifiers = WebappUtil.getOverridableFieldIdentifiers(owner, request); 178 179 if ( !identifiers.contains(fieldIdentifier) ) { 180 ActionMessages errors = new ActionMessages(); 181 errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier")); 182 saveErrors(request, errors); 183 saveToken(request); 184 return mapping.getInputForward(); 185 } 186 } else { 187 boolean canUpdateIndexed = request.isUserInRole("core-contentField-updateIndexed"); 189 boolean canUpdateOverriden = request.isUserInRole("core-contentField-updateOverriden"); 190 if ( !canUpdateIndexed && !canUpdateOverriden ) { 191 response.sendError(HttpServletResponse.SC_FORBIDDEN); 192 return null; 193 } 194 Layout ownerLayout = (Layout) owner; 195 if (canUpdateIndexed) { 196 identifiers = WebappUtil.getIndexedFieldIdentifiers(ownerLayout.getDefinition(), request); 197 if (identifiers == null) { 198 identifiers = new TreeSet (); 199 } 200 } 201 if (canUpdateOverriden) { 202 identifiers.addAll(WebappUtil.getOverridableFieldIdentifiers(ownerLayout, request)); 203 } 204 if ( !identifiers.contains(fieldIdentifier) ) { 205 ActionMessages errors = new ActionMessages(); 206 errors.add("contentFieldIncorrectIdentifier", new ActionMessage("core.contentField.errors.incorrectIdentifier")); 207 saveErrors(request, errors); 208 saveToken(request); 209 return mapping.getInputForward(); 210 } 211 220 } 221 } else { 222 if ( !request.isUserInRole("core-contentField-update") ) { 225 response.sendError(HttpServletResponse.SC_FORBIDDEN); 226 return null; 227 } 228 } 229 230 ContentFieldManager contentFieldManager = (ContentFieldManager) getBean(Constants.CONTENT_FIELD_MANAGER_BEAN); 231 ContentField contentField = contentFieldManager.retrieveContentField(contentFieldId); 232 233 if ( contentField == null ) { 234 ActionMessages errors = new ActionMessages(); 236 errors.add("contentFieldNotFound", new ActionMessage("core.contentField.errors.notFound")); 237 saveErrors(request, errors); 238 return mapping.findForward("listContentFields"); 239 } 240 241 byte type = contentField.getType(); 243 244 WebappUtil.copyProperties(contentField, contentFieldForm, request); 245 if ( !GenericValidator.isBlankOrNull(contentFieldForm.getIndex()) ) { 246 fieldIdentifier += "[" + contentFieldForm.getIndex() + "]"; 247 contentField.setIdentifier(fieldIdentifier); 248 } 249 250 contentField.setType(type); 252 253 try { 254 255 contentFieldManager.updateContentField(contentField, ownerId); 256 257 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 259 260 CacheUtil cacheUtil = CacheUtil.getInstance(request); 262 cacheUtil.flushFieldIndices(); 263 264 if ( owner instanceof Layout ) { 265 Layout layout = (Layout) owner; 266 267 List contentPages = layout.getContentPages(); 269 for ( int i = 0; i < contentPages.size(); i++ ) { 270 ContentPage contentPage = (ContentPage) contentPages.get(i); 271 searchManager.reIndexPage(contentPage, request); 272 cacheUtil.updateContentPageLastModifiedInCache(contentPage.getUri()); 273 } 274 cacheUtil.flushLayoutFieldValueCache(layout.getDefinition()); 275 276 } else if ( owner instanceof Page ) { 277 Page page = (Page) owner; 278 searchManager.reIndexPage(page, request); 279 cacheUtil.flushPageFieldValueCache(page.getUri()); 280 if ( page instanceof ContentPage ) { 281 cacheUtil.updateContentPageLastModifiedInCache(page.getUri()); 282 } 283 } 284 285 } catch ( BeanAlreadyExistsException e ) { 286 ActionMessages errors = new ActionMessages(); 288 errors.add("contentFieldAlreadyExists", new ActionMessage("core.contentField.errors.alreadyExists")); 289 saveErrors(request, errors); 290 saveToken(request); 291 return mapping.getInputForward(); 292 } catch ( ObjectOptimisticLockingFailureException e ) { 293 ActionMessages errors = new ActionMessages(); 295 errors.add("updateFailed", new ActionMessage("core.contentField.errors.updateFailed")); 296 saveErrors(request, errors); 297 return mapping.findForward("callUpdateContentField"); 298 } 299 request.getSession().setAttribute(WebappConstants.CONTENT_FIELD_ID_KEY, Long.valueOf(contentFieldForm.getId())); 300 return mapping.findForward("viewContentField"); 301 } else { 302 String requestUrl = (String ) request.getSession().getAttribute("beforeEditedFieldUrl"); 304 if ( !GenericValidator.isBlankOrNull(requestUrl) ) { 305 request.getSession().removeAttribute("beforeEditedFieldUrl"); 306 return new ActionForward(requestUrl, true); 307 } else { 308 return mapping.findForward("listContentFields"); 309 } 310 } 311 312 } 313 } | Popular Tags |