KickJava   Java API By Example, From Geeks To Geeks.

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


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.file;
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.ContentResourceForm;
23 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
24 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
25 import com.blandware.atleap.webapp.util.core.WebappConstants;
26 import com.blandware.atleap.webapp.util.core.WebappUtil;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.upload.FormFile;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * <p>Prepares form bean to update file
39  * </p>
40  * <p><a HREF="CallUpdateFileAction.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.9 $ $Date: 2006/03/10 17:10:22 $
46  * @struts.action path="/core/contentResource/file/callUpdate"
47  * name="contentResourceForm"
48  * scope="request"
49  * validate="false"
50  * roles="core-contentResource-update"
51  * @struts.action-forward name="updateFile"
52  * path=".core.contentResource.file.update"
53  * @struts.action-forward name="listContentResources"
54  * path="/core/contentResource/list.do"
55  * redirect="false"
56  */

57 public final class CallUpdateFileAction extends BaseAction {
58     /**
59      * @param mapping The ActionMapping used to select this instance
60      * @param form The optional ActionForm bean for this request (if any)
61      * @param request The HTTP request we are proceeding
62      * @param response The HTTP response we are creating
63      * @return an ActionForward instance describing where and how
64      * control should be forwarded, or null if response
65      * has already been completed
66      */

67     public ActionForward execute(ActionMapping mapping, ActionForm form,
68                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         Long JavaDoc contentResourceId = null;
71         if ( ((ContentResourceForm) form).getId() != null ) {
72             contentResourceId = Long.valueOf(((ContentResourceForm) form).getId());
73         } else if ( request.getParameter("contentResourceId") != null ) {
74             contentResourceId = Long.valueOf(request.getParameter("contentResourceId"));
75         } else if ( request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
76             contentResourceId = (Long JavaDoc) request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
77         } else {
78             if ( log.isWarnEnabled() ) {
79                 log.warn("Missing required parameter 'contentResourceId'");
80             }
81             return mapping.findForward("listContentResources");
82         }
83
84         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
85         ContentResource file = contentResourceManager.retrieveContentResource(contentResourceId);
86         if ( file == null ) {
87             // file not found. it might be deleted by someone else
88
ActionMessages errors = new ActionMessages();
89             errors.add("fileNotFound", new ActionMessage("core.contentResource.file.errors.notFound"));
90             saveErrors(request, errors);
91             return mapping.findForward("listContentResources");
92         }
93
94         ContentResourceForm contentResourceForm = (ContentResourceForm) form;
95         WebappUtil.copyProperties(contentResourceForm, file, request);
96
97         //setup uri
98
String JavaDoc uri = file.getUri();
99         if ( uri.startsWith(Constants.RESOURCES_URI_PREFIX) ) {
100             uri = uri.substring(Constants.RESOURCES_URI_PREFIX.length());
101         }
102         contentResourceForm.setUri(uri);
103
104         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
105
106         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
107         FormFile formFile = null;
108         if ( uploadContentResourceForm != null ) {
109             formFile = uploadContentResourceForm.getFile();
110         }
111         if ( formFile != null && formFile.getFileName() != null && formFile.getFileName().trim().length() > 0 && formFile.getFileSize() > 0 ) {
112             String JavaDoc fileName = formFile.getFileName();
113             String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
114             request.getSession().setAttribute(WebappConstants.CONTENT_FILE_NEW_MIMETYPE_KEY, mimeType);
115             request.getSession().setAttribute(WebappConstants.CONTENT_FILE_NEW_SIZE_KEY, new Long JavaDoc(formFile.getFileData().length));
116             request.getSession().setAttribute(WebappConstants.CONTENT_FILE_NEW_FILENAME_KEY, formFile.getFileName());
117             request.getSession().setAttribute(WebappConstants.CONTENT_FILE_NEW_MIMETYPE_IS_SET_KEY, Boolean.TRUE);
118         } else {
119             request.getSession().removeAttribute(WebappConstants.CONTENT_FILE_NEW_MIMETYPE_KEY);
120             request.getSession().removeAttribute(WebappConstants.CONTENT_FILE_NEW_SIZE_KEY);
121             request.getSession().removeAttribute(WebappConstants.CONTENT_FILE_NEW_FILENAME_KEY);
122             request.getSession().removeAttribute(WebappConstants.CONTENT_FILE_NEW_MIMETYPE_IS_SET_KEY);
123         }
124
125         saveToken(request);
126         return mapping.findForward("updateFile");
127     }
128
129 }
Popular Tags