KickJava   Java API By Example, From Geeks To Geeks.

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


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.webapp.action.core.BaseAction;
20 import com.blandware.atleap.webapp.form.ContentDocumentForm;
21 import com.blandware.atleap.webapp.form.core.UploadContentResourceForm;
22 import com.blandware.atleap.webapp.util.core.LocaleUtil;
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.Globals;
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 import java.util.List JavaDoc;
37 import java.util.Locale JavaDoc;
38
39 /**
40  * <p>Returns page with form to create new document
41  * </p>
42  * <p><a HREF="CallCreateDocumentAction.java.htm"><i>View Source</i></a></p>
43  * <p/>
44  *
45  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
46  * @version $Revision: 1.7 $ $Date: 2006/03/10 17:10:22 $
47  * @struts.action name="contentDocumentForm"
48  * path="/core/contentResource/document/callCreate"
49  * validate="false"
50  * roles="core-contentResource-create"
51  * input="callCreateContentResource"
52  * @struts.action-forward name="createDocument"
53  * path=".core.contentResource.document.create"
54  * @struts.action-forward name="callCreateContentResource"
55  * path="/core/contentResource/callCreate.do"
56  */

57 public final class CallCreateDocumentAction 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         UploadContentResourceForm uploadContentResourceForm = (UploadContentResourceForm) request.getSession().getAttribute(WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY);
71         if ( uploadContentResourceForm == null || uploadContentResourceForm.getFile() == null ) {
72             if ( log.isWarnEnabled() ) {
73                 log.warn("Missing required attribute in session '" + WebappConstants.UPLOAD_CONTENT_RESOURCE_FORM_KEY + "'");
74             }
75             ActionMessages errors = new ActionMessages();
76             errors.add("missingResource", new ActionMessage("core.contentResource.errors.missingResource"));
77             saveErrors(request, errors);
78             saveToken(request);
79             return mapping.findForward("callCreateContentResource");
80         }
81
82         FormFile formFile = uploadContentResourceForm.getFile();
83         String JavaDoc fileName = formFile.getFileName();
84
85         ContentDocumentForm documentForm = (ContentDocumentForm) form;
86         String JavaDoc uri = "/";
87         if ( GenericValidator.isBlankOrNull(documentForm.getUri()) && request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY) != null ) {
88             uri = (String JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_RESOURCES_CURRENT_PATH_KEY);
89             if ( !uri.endsWith("/") ) {
90                 uri += "/";
91             }
92             // TODO: this is a patch!! Fix the reason!
93
if (!"/".equals(uri) && !"".equals(uri)) {
94                 if (!uri.startsWith("/")) {
95                     uri = "/" + uri;
96                 }
97             }
98         }
99         documentForm.setUri(uri + fileName);
100
101         String JavaDoc encoding = (String JavaDoc) request.getSession().getAttribute(WebappConstants.CONTENT_DOCUMENT_ENCODING_KEY);
102         if (GenericValidator.isBlankOrNull(encoding)) {
103             encoding = Constants.DEFAULT_ENCODING;
104         }
105         documentForm.setCharset(encoding);
106
107         Locale JavaDoc currentLocale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
108         if ( currentLocale != null ) {
109             documentForm.setContentLocaleId(currentLocale.getLanguage());
110         }
111
112         ResourceTypesManager resourceTypesManager = ResourceTypesManager.getInstance(request.getSession().getServletContext());
113         String JavaDoc mimeType = resourceTypesManager.getMimeTypeByFileName(fileName);
114         documentForm.setMimeType(mimeType);
115         documentForm.setSize(String.valueOf(formFile.getFileData().length));
116         request.getSession().setAttribute(WebappConstants.CONTENT_DOCUMENT_NEW_FILENAME_KEY, formFile.getFileName());
117
118         // Populate content locale drop-down list
119
List JavaDoc availableContentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
120         request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, availableContentLocales);
121
122         saveToken(request);
123         return mapping.findForward("createDocument");
124     }
125 }
Popular Tags