KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > UploadApple


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 org.outerj.daisy.frontend;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.util.FormHelper;
20 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
21 import org.outerj.daisy.frontend.editor.UploadPartDataSource;
22 import org.outerj.daisy.repository.Repository;
23 import org.outerj.daisy.repository.Document;
24 import org.outerj.daisy.repository.CollectionManager;
25 import org.outerj.daisy.repository.PartDataSource;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.cocoon.components.flow.apples.AppleRequest;
30 import org.apache.cocoon.components.flow.apples.AppleResponse;
31 import org.apache.cocoon.forms.formmodel.Form;
32 import org.apache.cocoon.forms.formmodel.MultiValueField;
33 import org.apache.cocoon.forms.formmodel.Field;
34 import org.apache.cocoon.forms.FormContext;
35 import org.apache.cocoon.environment.Request;
36
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 /**
42  * Apple used when uploading a new attachment or image.
43  */

44 public class UploadApple extends AbstractDaisyApple implements Serviceable {
45     private ServiceManager serviceManager;
46     private Form form;
47     private boolean init = false;
48     private Map JavaDoc viewData;
49     private Locale JavaDoc locale;
50     private SiteConf siteConf;
51     private Repository repository;
52     private String JavaDoc documentTypeName;
53     private String JavaDoc partTypeName;
54
55     public void service(ServiceManager serviceManager) throws ServiceException {
56         this.serviceManager = serviceManager;
57     }
58
59     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
60         Request request = appleRequest.getCocoonRequest();
61
62         if (!init) {
63             documentTypeName = RequestUtil.getStringParameter(request, "documentType");
64             partTypeName = RequestUtil.getStringParameter(request, "partType");
65
66             form = FormHelper.createForm(serviceManager, "resources/form/upload_definition.xml");
67
68             locale = WikiHelper.getLocale(request);
69             siteConf = WikiHelper.getSiteConf(request);
70             repository = WikiHelper.getRepository(request, serviceManager);
71             long branchId = RequestUtil.getBranchId(request, siteConf.getBranchId(), repository);
72             long languageId = RequestUtil.getLanguageId(request, siteConf.getLanguageId(), repository);
73
74             MultiValueField collectionsField = (MultiValueField)form.getChild("collections");
75             collectionsField.setSelectionList(repository.getCollectionManager().getCollections(false).getArray(), "id", "name");
76             collectionsField.setValue(new Long JavaDoc[] { new Long JavaDoc(siteConf.getCollectionId()) });
77
78             form.getChild("branchId").setValue(new Long JavaDoc(branchId));
79             form.getChild("languageId").setValue(new Long JavaDoc(languageId));
80
81             viewData = new HashMap JavaDoc();
82             viewData.put("CocoonFormsInstance", form);
83             viewData.put("locale", locale);
84             viewData.put("submitPath", getPath());
85             viewData.put("mountPoint", getMountPoint());
86             viewData.put("branchesArray", repository.getVariantManager().getAllBranches(false).getArray());
87             viewData.put("languagesArray", repository.getVariantManager().getAllLanguages(false).getArray());
88
89             init = true;
90             appleResponse.redirectTo(getPath());
91         } else {
92             String JavaDoc method = request.getMethod();
93             if (method.equals("GET")) {
94                 appleResponse.sendPage("DialogForm-upload-Pipe", viewData);
95             } else if (method.equals("POST")) {
96                 boolean endProcessing = form.process(new FormContext(request, locale));
97                 if (endProcessing) {
98                     String JavaDoc name = (String JavaDoc)form.getChild("name").getValue();
99                     long branchId = ((Long JavaDoc)form.getChild("branchId").getValue()).longValue();
100                     long languageId = ((Long JavaDoc)form.getChild("languageId").getValue()).longValue();
101                     String JavaDoc mimeType = (String JavaDoc)form.getChild("mimetype").getValue();
102                     String JavaDoc fileName = (String JavaDoc)form.getChild("filename").getValue();
103
104                     long uploadDocTypeId = repository.getRepositorySchema().getDocumentTypeByName(documentTypeName, false).getId();
105                     Document document = repository.createDocument(name, uploadDocTypeId, branchId, languageId);
106                     document.setPart(partTypeName, mimeType, getUploadData());
107                     document.setPartFileName(partTypeName, fileName);
108
109                     // save the collections
110
MultiValueField collectionsField = (MultiValueField)form.getChild("collections");
111                     Object JavaDoc[] collections = (Object JavaDoc[])collectionsField.getValue();
112                     CollectionManager collectionManager = repository.getCollectionManager();
113                     document.clearCollections();
114                     for (int i = 0; i < collections.length; i++) {
115                         document.addToCollection(collectionManager.getCollection(((Long JavaDoc)collections[i]).longValue(), false));
116                     }
117                     document.save();
118
119                     Map JavaDoc viewData = new HashMap JavaDoc();
120                     viewData.put("mountPoint", getMountPoint());
121                     viewData.put("uploadDocId", String.valueOf(document.getId()));
122                     viewData.put("uploadName", document.getName());
123                     viewData.put("branch", repository.getVariantManager().getBranch(branchId, false).getName());
124                     viewData.put("language", repository.getVariantManager().getLanguage(languageId, false).getName());
125                     viewData.put("branchId", String.valueOf(branchId));
126                     viewData.put("languageId", String.valueOf(languageId));
127                     appleResponse.sendPage("Template-upload_finish-Pipe", viewData);
128                 } else {
129                     if (form.getSubmitWidget().getId().equals("file")) {
130                         org.apache.cocoon.servlet.multipart.Part docUploadPart = (org.apache.cocoon.servlet.multipart.Part)form.getChild("file").getValue();
131                         if (docUploadPart != null) {
132                             form.getChild("mimetype").setValue(docUploadPart.getMimeType());
133                             String JavaDoc fileName = RequestUtil.removePathFromUploadFileName(docUploadPart.getUploadName());
134                             form.getChild("filename").setValue(fileName);
135                             Field docName = (Field)form.getChild("name");
136                             if (docName.getValue() == null) {
137                                 int pos = fileName.lastIndexOf('.');
138                                 if (pos != -1)
139                                     fileName = fileName.substring(0, pos);
140                                 docName.setValue(fileName);
141                             }
142                         }
143                     }
144                     appleResponse.sendPage("DialogForm-upload-Pipe", viewData);
145                 }
146             } else {
147                 throw new Exception JavaDoc("Unexpected HTML method: " + method);
148             }
149         }
150     }
151
152     private PartDataSource getUploadData() throws Exception JavaDoc {
153         org.apache.cocoon.servlet.multipart.Part docUploadPart = (org.apache.cocoon.servlet.multipart.Part)form.getChild("file").getValue();
154         return new UploadPartDataSource(docUploadPart);
155     }
156
157     private String JavaDoc getPath() {
158         return getMountPoint() + "/" + siteConf.getName() + "/editing/upload/" + getContinuationId();
159     }
160 }
161
162
Popular Tags