KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentResource > UpdateContentResourceAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.contentResource;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentResource;
20 import com.blandware.atleap.service.core.ContentResourceManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
23 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import org.apache.commons.validator.GenericValidator;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionMessage;
30 import org.apache.struts.action.ActionMessages;
31 import org.apache.struts.upload.FormFile;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * <p>Updates content resource
39  * </p>
40  * <p><a HREF="UpdateContentResourceAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.24 $ $Date: 2006/03/26 11:52:07 $
46  * @struts.action path="/core/contentResource/update"
47  * name="uploadContentResourceForm"
48  * scope="request"
49  * input="inputForward"
50  * validate="true"
51  * roles="core-contentResource-update"
52  * @struts.action-forward name="inputForward"
53  * path="/core/contentResource/callUpdate.do"
54  * redirect="true"
55  * @struts.action-forward name="callUpdateImage"
56  * path="/core/contentResource/image/callUpdate.do"
57  * @struts.action-forward name="callUpdateFile"
58  * path="/core/contentResource/file/callUpdate.do"
59  * @struts.action-forward name="callUpdateDocument"
60  * path="/core/contentResource/document/callUpdate.do"
61  * @struts.action-forward name="listContentResources"
62  * path="/core/contentResource/list.do"
63  * redirect="true"
64  */

65 public final class UpdateContentResourceAction extends BaseAction {
66
67     /**
68      * @param mapping The ActionMapping used to select this instance
69      * @param form The optional ActionForm bean for this request (if any)
70      * @param request The HTTP request we are proceeding
71      * @param response The HTTP response we are creating
72      * @return an ActionForward instance describing where and how
73      * control should be forwarded, or null if response
74      * has already been completed
75      */

76     public ActionForward execute(ActionMapping mapping, ActionForm form,
77                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
78         if ( isCancelled(request) ) {
79             resetToken(request);
80             request.getSession().removeAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
81             return mapping.findForward("listContentResources");
82         }
83
84         Long JavaDoc contentResourceId = null;
85         if ( !GenericValidator.isBlankOrNull(request.getParameter("contentResourceId")) ) {
86             contentResourceId = Long.valueOf(request.getParameter("contentResourceId"));
87         } else if ( request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
88             contentResourceId = (Long JavaDoc) request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
89         } else if ( request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
90             contentResourceId = (Long JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
91         } else {
92             return mapping.findForward("listContentResources");
93         }
94         request.setAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY, contentResourceId);
95
96         if ( log.isDebugEnabled() ) {
97             log.debug("Updating content resource with ID=" + contentResourceId);
98         }
99
100         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) form;
101
102         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
103         ContentResource contentResource = contentResourceManager.retrieveContentResource(contentResourceId);
104
105         if ( contentResource == null ) {
106             // content resource not found. it might be deleted by someone else
107
ActionMessages errors = new ActionMessages();
108             errors.add("contentResourceNotFound", new ActionMessage("core.contentResource.errors.notFound"));
109             saveErrors(request, errors);
110             return mapping.findForward("listContentResources");
111         }
112
113         FormFile file = uploadContentResourceForm.getFile();
114         String JavaDoc resourceType = contentResource.getType();
115
116         //if some file was selected
117
if ( file != null && file.getFileName() != null && file.getFileName().trim().length() > 0 && file.getFileSize() > 0 ) {
118             String JavaDoc fileName = file.getFileName();
119             ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
120             String JavaDoc newResourceType = resourceTypesManager.getResourceTypeByFileName(fileName);
121             if (newResourceType == null) {
122                 newResourceType = Constants.RESOURCE_TYPE_FILE;
123             }
124             if ( !resourceType.equalsIgnoreCase(newResourceType) ) {
125                 file.destroy();
126                 request.getSession().removeAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
127                 List JavaDoc supportedExtensions = resourceTypesManager.getResourceTypeExtensions(resourceType);
128                 ActionMessages errors = new ActionMessages();
129                 errors.add("contentResourceDifferentType", new ActionMessage("core.contentResource.errors.differentType", supportedExtensions.toString()));
130                 request.getSession().setAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY, contentResourceId);
131                 saveErrors(request, errors);
132                 saveToken(request);
133                 return mapping.getInputForward();
134             }
135
136             request.getSession().setAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY, uploadContentResourceForm);
137         } else {
138             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
139         }
140
141         if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_IMAGE) ) {
142             return mapping.findForward("callUpdateImage");
143         } else if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_FILE) ) {
144             return mapping.findForward("callUpdateFile");
145         } else if ( resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_DOCUMENT) ) {
146             return mapping.findForward("callUpdateDocument");
147         } else {
148             if ( file != null ) {
149                 file.destroy();
150             }
151             throw new Exception JavaDoc("Unexpected error");
152         }
153
154     }
155 }
Popular Tags