KickJava   Java API By Example, From Geeks To Geeks.

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


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.document;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.parsers.PlainTextExtractor;
20 import com.blandware.atleap.common.util.StringUtil;
21 import com.blandware.atleap.model.core.ContentDocument;
22 import com.blandware.atleap.service.core.ContentResourceManager;
23 import com.blandware.atleap.webapp.action.core.BaseAction;
24 import com.blandware.atleap.webapp.form.ContentDocumentForm;
25 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
26 import com.blandware.atleap.webapp.util.core.GlobalProperties;
27 import com.blandware.atleap.webapp.util.core.LocaleUtil;
28 import com.blandware.atleap.webapp.util.core.ResourceTypesManager;
29 import com.blandware.atleap.webapp.util.core.WebappConstants;
30 import com.blandware.atleap.webapp.util.core.WebappUtil;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36 import org.apache.struts.upload.FormFile;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.util.List JavaDoc;
42
43 /**
44  * <p>Prepares form bean to update document
45  * </p>
46  * <p><a HREF="CallUpdateDocumentAction.java.htm"><i>View Source</i></a></p>
47  * <p/>
48  *
49  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
50  * @version $Revision: 1.7 $ $Date: 2006/03/10 17:10:22 $
51  * @struts.action path="/core/contentResource/document/callUpdate"
52  * name="contentDocumentForm"
53  * scope="request"
54  * validate="false"
55  * roles="core-contentResource-update"
56  * @struts.action-forward name="updateDocument"
57  * path=".core.contentResource.document.update"
58  * @struts.action-forward name="callUpdateContentResource"
59  * path="/core/contentResource/callUpdate.do"
60  * @struts.action-forward name="listContentResources"
61  * path="/core/contentResource/list.do"
62  * redirect="false"
63  */

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

74     public ActionForward execute(ActionMapping mapping, ActionForm form,
75                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76
77         Long JavaDoc contentResourceId = null;
78         if ( ((ContentDocumentForm) form).getId() != null ) {
79             contentResourceId = Long.valueOf(((ContentDocumentForm) form).getId());
80         } else if ( request.getParameter("contentResourceId") != null ) {
81             contentResourceId = Long.valueOf(request.getParameter("contentResourceId"));
82         } else if ( request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY) != null ) {
83             contentResourceId = (Long JavaDoc) request.getAttribute(WebappConstants.CONTENT_RESOURCE_ID_KEY);
84         } else {
85             if ( log.isWarnEnabled() ) {
86                 log.warn("Missing required parameter 'contentResourceId'");
87             }
88             return mapping.getInputForward();
89         }
90
91         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
92         ContentDocument document = contentResourceManager.retrieveContentDocument(contentResourceId);
93         if ( document == null ) {
94             // document not found. it might be deleted by someone else
95
ActionMessages errors = new ActionMessages();
96             errors.add("documentNotFound", new ActionMessage("core.contentResource.document.errors.notFound"));
97             saveErrors(request, errors);
98             return mapping.findForward("listContentResources");
99         }
100
101         ContentDocumentForm documentForm = (ContentDocumentForm) form;
102         WebappUtil.copyProperties(documentForm, document, request);
103         documentForm.setContentLocaleId(document.getContentLocale().getIdentifier());
104
105         //setup uri
106
String JavaDoc uri = document.getUri();
107         if ( uri.startsWith(Constants.RESOURCES_URI_PREFIX) ) {
108             uri = uri.substring(Constants.RESOURCES_URI_PREFIX.length());
109         }
110         documentForm.setUri(uri);
111
112         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
113
114         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
115         FormFile formFile = null;
116         if ( uploadContentResourceForm != null ) {
117             formFile = uploadContentResourceForm.getFile();
118         }
119         if ( formFile != null && formFile.getFileName() != null && formFile.getFileName().trim().length() > 0 && formFile.getFileSize() > 0 ) {
120             String JavaDoc fileName = formFile.getFileName();
121             String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
122             byte[] newDocumentData = formFile.getFileData();
123
124
125             //summary
126
String JavaDoc plainText;
127             try {
128                 plainText = new PlainTextExtractor().extract(new ByteArrayInputStream JavaDoc(newDocumentData), mimeType);
129             } catch ( Exception JavaDoc ex ) {
130                 if ( formFile != null ) {
131                     formFile.destroy();
132                 }
133                 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
134
135                 ActionMessages errors = new ActionMessages();
136                 errors.add("contentResourceCorruptedData", new ActionMessage("core.contentResource.errors.corruptedData"));
137                 saveErrors(request, errors);
138                 return mapping.findForward("callUpdateContentResource");
139             }
140             Integer JavaDoc summarySize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.DOCUMENT_SUMMARY_SIZE_KEY, new Integer JavaDoc(400));
141             plainText = StringUtil.htmlEncode(StringUtil.shortenString(plainText, Math.min(plainText.length(), summarySize.intValue())));
142             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_SUMMARY_KEY, plainText);
143             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_MIMETYPE_KEY, mimeType);
144             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_SIZE_KEY, new Long JavaDoc(newDocumentData.length));
145             request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_FILENAME_KEY, formFile.getFileName());
146         } else {
147             request.getSession().removeAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_MIMETYPE_KEY);
148             request.getSession().removeAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_SIZE_KEY);
149             request.getSession().removeAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_FILENAME_KEY);
150         }
151
152         // Populate content locale drop-down list
153
List JavaDoc availableContentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
154         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, availableContentLocales);
155
156         saveToken(request);
157         return mapping.findForward("updateDocument");
158     }
159
160 }
Popular Tags