KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HttpMethodNotAllowedException;
21 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
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.schema.DocumentType;
26 import org.outerj.daisy.repository.schema.PartTypeUse;
27 import org.apache.cocoon.components.flow.apples.AppleRequest;
28 import org.apache.cocoon.components.flow.apples.AppleResponse;
29 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
30 import org.apache.cocoon.environment.Request;
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.datatype.SelectionList;
35 import org.apache.cocoon.forms.datatype.StaticSelectionList;
36 import org.apache.cocoon.forms.datatype.Datatype;
37 import org.apache.cocoon.forms.util.StringMessage;
38 import org.apache.cocoon.forms.FormContext;
39 import org.apache.avalon.framework.service.Serviceable;
40 import org.apache.avalon.framework.service.ServiceManager;
41 import org.apache.avalon.framework.service.ServiceException;
42
43 import java.util.Locale JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.HashMap JavaDoc;
46
47 public class CreatePlaceholderDocApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
48     private ServiceManager serviceManager;
49
50     public void service(ServiceManager serviceManager) throws ServiceException {
51         this.serviceManager = serviceManager;
52     }
53
54     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
55         Request request = appleRequest.getCocoonRequest();
56         Repository repository = WikiHelper.getRepository(request, serviceManager);
57         Locale JavaDoc locale = WikiHelper.getLocale(request);
58         SiteConf siteConf = WikiHelper.getSiteConf(request);
59         Form form = createForm(repository, locale, siteConf);
60
61         if (request.getMethod().equals("POST")) {
62             boolean success = form.process(new FormContext(request, locale));
63             if (success) {
64                 String JavaDoc name = (String JavaDoc)form.getChild("name").getValue();
65                 long documentTypeId = ((Long JavaDoc)form.getChild("documentType").getValue()).longValue();
66                 long branchId = ((Long JavaDoc)form.getChild("branchId").getValue()).longValue();
67                 long languageId = ((Long JavaDoc)form.getChild("languageId").getValue()).longValue();
68                 Document document = repository.createDocument(name, documentTypeId, branchId, languageId);
69
70                 Object JavaDoc[] collectionIds = (Object JavaDoc[])form.getChild("collections").getValue();
71                 CollectionManager collectionManager = repository.getCollectionManager();
72                 for (int i = 0; i < collectionIds.length; i++) {
73                     long collectionId = ((Long JavaDoc)collectionIds[i]).longValue();
74                     document.addToCollection(collectionManager.getCollection(collectionId, false));
75                 }
76
77                 DocumentType documentType = repository.getRepositorySchema().getDocumentTypeById(documentTypeId, false);
78                 PartTypeUse[] partTypeUses = documentType.getPartTypeUses();
79                 for (int i = 0; i < partTypeUses.length; i++) {
80                     if (partTypeUses[i].isRequired() && partTypeUses[i].getPartType().isDaisyHtml()) {
81                         document.setPart(partTypeUses[i].getPartType().getName(), "text/xml", "<html><body><p>TODO</p></body></html>".getBytes("UTF-8"));
82                     }
83                 }
84
85                 // do not validate, required parts and fields might not have a value
86
document.save(false);
87
88                 Map JavaDoc viewData = new HashMap JavaDoc();
89                 viewData.put("documentId", String.valueOf(document.getId()));
90                 viewData.put("mountPoint", WikiHelper.getMountPoint(request));
91                 viewData.put("siteName", siteConf.getName());
92                 viewData.put("name", name);
93                 viewData.put("branchId", String.valueOf(branchId));
94                 viewData.put("branch", repository.getVariantManager().getBranch(branchId, false).getName());
95                 viewData.put("languageId", String.valueOf(languageId));
96                 viewData.put("language", repository.getVariantManager().getLanguage(languageId, false).getName());
97                 appleResponse.sendPage("Template-placeholder_finish-Pipe", viewData);
98                 return;
99             }
100         } else if (request.getMethod().equals("GET")) {
101             long branchId = RequestUtil.getBranchId(request, siteConf.getBranchId(), repository);
102             long languageId = RequestUtil.getLanguageId(request, siteConf.getLanguageId(), repository);
103             form.getChild("branchId").setValue(new Long JavaDoc(branchId));
104             form.getChild("languageId").setValue(new Long JavaDoc(languageId));
105
106             long documentTypeId;
107             if (request.getParameter("documentType") == null) {
108                 documentTypeId = siteConf.getDefaultDocumentTypeId();
109                 if (documentTypeId == -1)
110                     documentTypeId = repository.getRepositorySchema().getDocumentTypeByName("SimpleDocument", false).getId();
111             } else {
112                 documentTypeId = RequestUtil.getLongParameter(request, "documentType");
113             }
114             form.getChild("documentType").setValue(new Long JavaDoc(documentTypeId));
115         } else {
116             throw new HttpMethodNotAllowedException(request.getMethod());
117         }
118
119         Map JavaDoc viewData = new HashMap JavaDoc();
120         viewData.put("CocoonFormsInstance", form);
121         viewData.put("locale", locale);
122         viewData.put("mountPoint", WikiHelper.getMountPoint(request));
123         viewData.put("siteName", siteConf.getName());
124         viewData.put("branchesArray", repository.getVariantManager().getAllBranches(false).getArray());
125         viewData.put("languagesArray", repository.getVariantManager().getAllLanguages(false).getArray());
126         appleResponse.sendPage("DialogForm-placeholder_doc-Pipe", viewData);
127     }
128
129     private Form createForm(Repository repository, Locale JavaDoc locale, SiteConf siteConf) throws Exception JavaDoc {
130         Form form = FormHelper.createForm(serviceManager, "resources/form/placeholder_doc_definition.xml");
131
132         MultiValueField collectionsField = (MultiValueField)form.getChild("collections");
133         collectionsField.setSelectionList(repository.getCollectionManager().getCollections(false).getArray(), "id", "name");
134         collectionsField.setValues(new Long JavaDoc[] {new Long JavaDoc(siteConf.getCollectionId())});
135
136         Field documentTypeField = (Field)form.getChild("documentType");
137         DocumentType[] documentTypes = DocumentTypeSelectionApple.getFilteredDocumentTypes(repository, -1);
138         documentTypeField.setSelectionList(createDocumentTypeSelectionList(documentTypes, locale, documentTypeField.getDatatype()));
139
140         return form;
141     }
142
143
144     private SelectionList createDocumentTypeSelectionList(DocumentType[] types, Locale JavaDoc locale, Datatype datatype) {
145         StaticSelectionList selectionList = new StaticSelectionList(datatype);
146         for (int i = 0; i < types.length; i++) {
147             selectionList.addItem(new Long JavaDoc(types[i].getId()), new StringMessage(types[i].getLabel(locale)));
148         }
149         return selectionList;
150     }
151 }
152
Popular Tags