1 16 package com.blandware.atleap.webapp.action.core.contentResource.file; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.ConvertUtil; 20 import com.blandware.atleap.model.core.*; 21 import com.blandware.atleap.service.core.ContentResourceManager; 22 import com.blandware.atleap.service.exception.BeanAlreadyExistsException; 23 import com.blandware.atleap.webapp.action.core.BaseAction; 24 import com.blandware.atleap.webapp.form.ContentResourceForm; 25 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm; 26 import com.blandware.atleap.webapp.util.core.CacheUtil; 27 import com.blandware.atleap.webapp.util.core.ResourceTypesManager; 28 import com.blandware.atleap.webapp.util.core.WebappConstants; 29 import com.blandware.atleap.webapp.util.core.WebappUtil; 30 import com.blandware.atleap.search.SearchManager; 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 import org.apache.struts.upload.FormFile; 38 import org.springframework.orm.ObjectOptimisticLockingFailureException; 39 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Set ; 45 import java.util.HashSet ; 46 47 77 public final class UpdateFileAction extends BaseAction { 78 79 88 public ActionForward execute(ActionMapping mapping, ActionForm form, 89 HttpServletRequest request, HttpServletResponse response) throws Exception { 90 boolean force = "true".equalsIgnoreCase(request.getParameter("force")); 93 94 ContentResourceForm contentResourceForm; 95 if (force) { 96 contentResourceForm = (ContentResourceForm) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCE_FORM_KEY); 97 } else { 98 contentResourceForm = (ContentResourceForm) form; 99 } 100 101 UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 102 FormFile formFile = null; 103 if ( uploadContentResourceForm != null ) { 104 formFile = uploadContentResourceForm.getFile(); 105 } 106 107 Long fileId = null; 108 if ( !GenericValidator.isBlankOrNull(contentResourceForm.getId()) ) { 109 fileId = Long.valueOf(contentResourceForm.getId()); 110 } else { 111 if ( log.isWarnEnabled() ) { 112 log.warn("Missing file ID. Returning to list"); 113 } 114 return mapping.findForward("listContentResources"); 115 } 116 117 if (isBackPressed(request)) { 118 if ( formFile != null ) { 119 formFile.destroy(); 120 } 121 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 122 request.setAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY, fileId); 123 return mapping.findForward("updateContentResource"); 124 } 125 126 if ( isCancelled(request) ) { 127 if ( formFile != null ) { 128 formFile.destroy(); 129 } 130 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 131 return mapping.findForward("listContentResources"); 132 } 133 134 ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN); 135 ContentResource file = contentResourceManager.retrieveContentResource(fileId); 136 137 if ( file == null ) { 138 ActionMessages errors = new ActionMessages(); 140 errors.add("fileNotFound", new ActionMessage("core.contentResource.file.errors.notFound")); 141 saveErrors(request, errors); 142 return mapping.findForward("listContentResources"); 143 } 144 145 if ( !file.getType().equalsIgnoreCase(Constants.RESOURCE_TYPE_FILE) ) { 146 throw new Exception ("Resource must have " + Constants.RESOURCE_TYPE_FILE + " type"); 147 } 148 149 String uri = Constants.RESOURCES_URI_PREFIX + contentResourceForm.getUri(); 150 151 String oldUri = file.getUri(); 152 153 if (!oldUri.equals(uri)) { 154 ContentResource oldResource = contentResourceManager.findContentResourceByUri(uri); 156 if (oldResource != null) { 157 ActionMessages errors = new ActionMessages(); 159 errors.add("contentResourceAlreadyExists", new ActionMessage("core.contentResource.errors.alreadyExists")); 160 saveErrors(request, errors); 161 saveToken(request); 162 return mapping.getInputForward(); 163 } 164 } 165 166 boolean uriChanged = !oldUri.equals(uri); 167 if (!force && file.isInUse() && uriChanged ) { 168 String action = mapping.findForward("listContentResources").getPath(); 170 String redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE); 171 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl); 172 request.getSession().setAttribute(WebappConstants.CONTENT_RESOURCE_FORM_KEY, contentResourceForm); 173 request.getSession().setAttribute(WebappConstants.LINKED_OBJECT_NEW_URI_KEY, uri); 174 ActionMessages messages = new ActionMessages(); 175 messages.add("changeUriWarning", new ActionMessage("core.contentResource.messages.changeUriWarning")); 176 if (file.doUnmodifiableLinkedObjectsExist()) { 177 messages.add("unmodifiableLinkedObjectsWarning", new ActionMessage("core.contentResource.messages.unmodifiableLinkedObjectsWarning")); 178 } 179 saveMessages(request, messages); 180 request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY); 181 request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY, "core/contentResource/file/update"); 182 request.setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, file.getId()); 183 request.setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, "resource"); 184 return mapping.findForward("viewLinkedObjects"); 185 } 186 187 WebappUtil.copyProperties(file, contentResourceForm, request); 188 189 file.setUri(uri); 191 192 if (uriChanged) { 194 for (Iterator i = file.getLinkedMenuItems().iterator(); i.hasNext();) { 195 MenuItem menuItem = (MenuItem) i.next(); 196 if (!menuItem.isDynamic()) { 197 i.remove(); 198 } 199 } 200 } 201 202 ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext()); 203 204 byte[] fileData; 205 String mimeType = null; 206 207 if ( formFile != null && formFile.getFileName() != null && formFile.getFileName().trim().length() > 0 && formFile.getFileSize() > 0 ) { 208 fileData = formFile.getFileData(); 210 mimeType = resourceTypesManager.getMimeTypeByFileName(formFile.getFileName()); 211 212 file.setSize(new Long (fileData.length)); 213 214 } else { 215 fileData = file.getResourceData().getData(); 216 mimeType = file.getMimeType(); 217 } 218 219 CacheUtil cacheUtil = CacheUtil.getInstance(request); 220 try { 221 ResourceData resourceData = file.getResourceData(); 222 if ( resourceData != null ) { 223 resourceData.setData(fileData); 224 } else { 225 resourceData = new ResourceData(fileData); 226 } 227 file.setMimeType(mimeType); 228 contentResourceManager.updateContentResource(file, resourceData); 229 230 cacheUtil.flushResourceCache(oldUri); 232 233 CacheUtil.ResourceData rd = new CacheUtil.ResourceData(fileData, mimeType, WebappUtil.rolesToString(file.getRoles()), file.getLastUpdatedDatetime().getTime()); 235 cacheUtil.putResourceInCache(rd, uri); 236 237 if (force) { 238 SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext()); 241 242 cacheUtil.flushMenuCache(); 244 245 List linkedContentFieldValues = file.getLinkedContentFieldValues(); 247 Set owners = new HashSet (); 248 for (Iterator i = linkedContentFieldValues.iterator(); i.hasNext();) { 249 ContentFieldValue fieldValue = (ContentFieldValue) i.next(); 250 ContentField field = fieldValue.getContentField(); 251 Localizable owner = field.getOwner(); 252 owners.add(owner); 254 255 String localeIdentifier = fieldValue.getContentLocale().getIdentifier(); 256 257 byte fieldType = field.getType(); 258 String content = null; 259 260 if (fieldType == ContentField.LINE_TYPE) { 261 content = fieldValue.getSimpleValue(); 262 } else { 263 content = ConvertUtil.convertToString(fieldValue.getValue()); 264 } 265 if ( owner instanceof Page ) { 266 Page page = (Page) owner; 267 CacheUtil.CFVData cfvData = new CacheUtil.CFVData(content, field.getId(), field.getType(), fieldValue.getId()); 268 cacheUtil.putPageFieldValueInCache(cfvData, page.getUri(), field.getIdentifier(), localeIdentifier); 269 } 270 } 271 272 for (Iterator i = owners.iterator(); i.hasNext();) { 274 Localizable owner = (Localizable) i.next(); 275 if ( owner instanceof Layout ) { 276 Layout layout = (Layout) owner; 277 cacheUtil.flushLayoutFieldValueCache(layout.getDefinition()); 278 279 List contentPages = layout.getContentPages(); 282 for ( int j = 0; j < contentPages.size(); j++ ) { 283 ContentPage cp = (ContentPage) contentPages.get(j); 284 searchManager.reIndexPage(cp, request); 285 cacheUtil.updateContentPageLastModifiedInCache(cp.getUri()); 286 } 287 288 } else if ( owner instanceof Page ) { 289 Page page = (Page) owner; 290 searchManager.reIndexPage(page, request); 291 if ( page instanceof ContentPage ) { 292 cacheUtil.updateContentPageLastModifiedInCache(page.getUri()); 293 } 294 } 295 } 296 } 297 298 } catch ( BeanAlreadyExistsException e ) { 299 if ( formFile != null ) { 301 formFile.destroy(); 302 } 303 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 304 ActionMessages errors = new ActionMessages(); 305 errors.add("contentResourceAlreadyExists", new ActionMessage("core.contentResource.errors.alreadyExists")); 306 saveErrors(request, errors); 307 saveToken(request); 308 return mapping.getInputForward(); 309 } catch ( ObjectOptimisticLockingFailureException e ) { 310 ActionMessages errors = new ActionMessages(); 312 errors.add("updateFailed", new ActionMessage("core.contentResource.file.errors.updateFailed")); 313 saveErrors(request, errors); 314 return mapping.findForward("callUpdateFile"); 315 } 316 317 if ( formFile != null ) { 318 formFile.destroy(); 319 } 320 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY); 321 322 return mapping.findForward("listContentResources"); 323 } 324 } | Popular Tags |