KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.core.ContentDocument;
20 import com.blandware.atleap.model.core.ResourceData;
21 import com.blandware.atleap.search.SearchManager;
22 import com.blandware.atleap.service.core.ContentResourceManager;
23 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentDocumentForm;
26 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
27 import com.blandware.atleap.webapp.util.core.CacheUtil;
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
41 /**
42  * <p>Creates new content resource with type 'document'
43  * </p>
44  * <p><a HREF="CreateDocumentAction.java.htm"><i>View Source</i></a></p>
45  * <p/>
46  *
47  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
48  * @version $Revision: 1.12 $ $Date: 2006/03/10 17:10:22 $
49  * @struts.action path="/core/contentResource/document/create"
50  * name="contentDocumentForm"
51  * scope="request"
52  * input="inputForward"
53  * validate="true"
54  * roles="core-contentResource-create"
55  * @struts.action-forward name="inputForward"
56  * path=".core.contentResource.document.create"
57  * @struts.action-forward name="listContentResources"
58  * path="/core/contentResource/list.do"
59  * redirect="true"
60  * @struts.action-forward name="createContentResource"
61  * path=".core.contentResource.create"
62  * @struts.action-forward name="unsatisfiable"
63  * path="/core/contentResource/list.do"
64  */

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

75     public ActionForward execute(ActionMapping mapping, ActionForm form,
76                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77
78         ContentDocumentForm documentForm = (ContentDocumentForm) form;
79
80         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
81         if ( uploadContentResourceForm == null || uploadContentResourceForm.getFile() == null ) {
82             // Maybe session was invalidated and after that user pressed 'back'
83
// or 'cancel'?
84
if (isBackPressed(request)) {
85                 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
86                 saveToken(request);
87                 return mapping.findForward("createContentResource");
88             }
89             if ( isCancelled(request) ) {
90                 request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
91                 return mapping.findForward("listContentResources");
92             }
93             if ( log.isWarnEnabled() ) {
94                 log.warn("Missing required attribute in session '" + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + "'");
95             }
96             ActionMessages errors = new ActionMessages();
97             errors.add("missingResource", new ActionMessage("core.contentResource.errors.missingResource"));
98             saveErrors(request, errors);
99             saveToken(request);
100             return mapping.getInputForward();
101         }
102         FormFile formFile = uploadContentResourceForm.getFile();
103         String JavaDoc fileName = formFile.getFileName();
104
105         if (isBackPressed(request)) {
106             if ( formFile != null ) {
107                 formFile.destroy();
108             }
109             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
110             saveToken(request);
111             return mapping.findForward("createContentResource");
112         }
113
114         if ( isCancelled(request) ) {
115             if ( formFile != null ) {
116                 formFile.destroy();
117             }
118             request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
119             return mapping.findForward("listContentResources");
120         }
121
122         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
123         String JavaDoc resourceType = resourceTypesManager.getResourceTypeByFileName(fileName);
124
125         if ( !resourceType.equalsIgnoreCase(Constants.RESOURCE_TYPE_DOCUMENT) ) {
126             throw new Exception JavaDoc("Resource must have " + Constants.RESOURCE_TYPE_DOCUMENT + " type");
127         }
128
129         if ( !WebappUtil.isCharsetSupported(documentForm.getCharset()) ) {
130             // specified encoding in not supported
131
ActionMessages errors = new ActionMessages();
132             errors.add("notSupportedCharset", new ActionMessage("core.commons.errors.notSupportedCharset", documentForm.getCharset()));
133             saveErrors(request, errors);
134             saveToken(request);
135             return mapping.getInputForward();
136         }
137
138         ContentDocument document = new ContentDocument();
139         WebappUtil.copyProperties(document, documentForm, request);
140
141         //setup uri
142
String JavaDoc uri = Constants.RESOURCES_URI_PREFIX + documentForm.getUri();
143         document.setUri(uri);
144
145         String JavaDoc type = resourceTypesManager.getResourceTypeByFileName(fileName);
146         document.setType(type);
147
148         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
149
150         byte[] documentData = formFile.getFileData();
151
152         document.setSize(new Long JavaDoc(documentData.length));
153
154         document.setMimeType(mimeType);
155
156         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
157         try {
158             Long JavaDoc contentResourceId = contentResourceManager.createContentDocument(document, new ResourceData(documentData), documentForm.getContentLocaleId());
159
160             //put into cache
161
document = contentResourceManager.retrieveContentDocument(contentResourceId);
162             CacheUtil cacheUtil = CacheUtil.getInstance(request);
163             CacheUtil.ResourceData rd = new CacheUtil.ResourceData(documentData, mimeType, document.getCharset(), null, document.getLastUpdatedDatetime().getTime());
164             cacheUtil.putResourceInCache(rd, document.getUri());
165
166             //index
167
SearchManager searchManager = SearchManager.getInstance(servlet.getServletContext());
168             searchManager.reIndexDocument(document, request);
169         } catch ( BeanAlreadyExistsException e ) {
170             // content resource already exists
171
ActionMessages errors = new ActionMessages();
172             errors.add("contentResourceAlreadyExists", new ActionMessage("core.contentResource.errors.alreadyExists"));
173             saveErrors(request, errors);
174             saveToken(request);
175             return mapping.getInputForward();
176         }
177
178         if ( formFile != null ) {
179             formFile.destroy();
180         }
181         request.getSession().removeAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
182
183         return mapping.findForward("listContentResources");
184
185
186     }
187 }
Popular Tags