KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.cocoon.components.flow.apples.AppleRequest;
19 import org.apache.cocoon.components.flow.apples.AppleResponse;
20 import org.apache.cocoon.forms.formmodel.Form;
21 import org.apache.cocoon.forms.formmodel.Repeater;
22 import org.apache.cocoon.forms.FormContext;
23 import org.apache.cocoon.environment.Request;
24 import org.outerj.daisy.frontend.util.FormHelper;
25 import org.outerj.daisy.frontend.util.HttpMethodNotAllowedException;
26 import org.outerj.daisy.frontend.util.EncodingUtil;
27 import org.outerj.daisy.repository.variant.VariantManager;
28 import org.outerj.daisy.repository.RepositoryException;
29 import org.outerj.daisy.repository.AvailableVariants;
30 import org.outerx.daisy.x10.SearchResultDocument;
31
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 /**
37  * This apple allows the user to create a new document variant for one document.
38  */

39 public class CreateDocumentVariantApple extends AbstractDocumentApple {
40     private boolean init = false;
41     private Form form;
42     private Map JavaDoc viewData;
43     private Locale JavaDoc locale;
44
45     protected void processDocumentRequest(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
46         Request request = appleRequest.getCocoonRequest();
47
48         if (!init) {
49             locale = WikiHelper.getLocale(request);
50             form = FormHelper.createForm(getServiceManager(), "resources/form/createdocvariant_definition.xml");
51             form.getChild("copyContent").setValue(Boolean.TRUE);
52             form.getChild("goToEditor").setValue(Boolean.TRUE);
53             form.getChild("startVersion").setValue("last");
54
55             SearchResultDocument.SearchResult.Rows.Row[] images = getRepository().getQueryManager().performQuery("select name where LinksFromVariant(" + getDocumentId() + "," + getBranchId() + "," + getLanguageId() + ",1,0,'image') and branchId = " + getBranchId() + " and languageId = " + getLanguageId(), locale).getSearchResult().getRows().getRowArray();
56             Repeater repeater = (Repeater)form.getChild("resources");
57             for (int i = 0; i < images.length; i++) {
58                 Repeater.RepeaterRow row = repeater.addRow();
59                 row.getChild("id").setValue(new Long JavaDoc(images[i].getDocumentId()));
60                 row.getChild("name").setValue(images[i].getValueArray()[0]);
61                 row.getChild("createvariant").setValue(Boolean.TRUE);
62             }
63
64             VariantManager variantManager = getRepository().getVariantManager();
65             viewData = new HashMap JavaDoc();
66             viewData.put("pageContext", new PageContext(getMountPoint(), getSiteConf(), getRepository(), getLayoutType(), getSkin(), getContext()));
67             viewData.put("branchesArray", variantManager.getAllBranches(false).getArray());
68             viewData.put("languagesArray", variantManager.getAllLanguages(false).getArray());
69             viewData.put("startBranchName", variantManager.getBranch(getBranchId(), false).getName());
70             viewData.put("startLanguageName", variantManager.getLanguage(getLanguageId(), false).getName());
71             viewData.put("availableVariants", getRepository().getAvailableVariants(getDocumentId()).getArray());
72             viewData.put("documentPath", getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath());
73             viewData.put("CocoonFormsInstance", form);
74             viewData.put("variantParams", getVariantParams());
75             viewData.put("variantQueryString", getVariantQueryString());
76             viewData.put("locale", locale);
77
78             init = true;
79
80             appleResponse.redirectTo(EncodingUtil.encodePath(getMountPoint() + "/" + getSiteConf().getName() + "/" + getRequestedNavigationPath() + "/createVariant/" + getContinuationId()));
81         } else {
82             if (request.getMethod().equals("POST")) {
83                 FormContext formContext = new FormContext(request, locale);
84                 boolean finished = form.process(formContext);
85                 if (finished) {
86                     long newBranchId = ((Long JavaDoc)form.getChild("newBranchId").getValue()).longValue();
87                     long newLanguageId = ((Long JavaDoc)form.getChild("newLanguageId").getValue()).longValue();
88                     createImageVariants(newBranchId, newLanguageId);
89                     boolean copyContent = ((Boolean JavaDoc)form.getChild("copyContent").getValue()).booleanValue();
90                     if (!copyContent) {
91                         String JavaDoc redirectURL = getMountPoint() + "/" + getSiteConf().getName()
92                                 + "/new/edit?variantOf=" + getDocumentId() + "&startBranchId=" + getBranchId()
93                                 + "&startLanguageId=" + getLanguageId() + "&newBranchId=" + newBranchId
94                                 + "&newLanguageId=" + newLanguageId + "&startWithGet=true";
95                         appleResponse.redirectTo(EncodingUtil.encodePathQuery(redirectURL));
96                     } else {
97                         String JavaDoc startVersion = (String JavaDoc)form.getChild("startVersion").getValue();
98                         long startVersionId;
99                         if (startVersion.equals("last"))
100                             startVersionId = -1;
101                         else if (startVersion.equals("live"))
102                             startVersionId = -2;
103                         else
104                             startVersionId = Long.parseLong(startVersion);
105                         getRepository().createVariant(getDocumentId(), getBranchId(), getLanguageId(), startVersionId, newBranchId, newLanguageId, true);
106                         boolean goToEditor = ((Boolean JavaDoc)form.getChild("goToEditor").getValue()).booleanValue();
107                         if (goToEditor) {
108                             String JavaDoc redirectURL = getMountPoint() + "/" + getSiteConf().getName() + "/" + getDocumentId() + "/edit"
109                                     + "?branch=" + newBranchId + "&language=" + newLanguageId + "&startWithGet=true";
110                             appleResponse.redirectTo(EncodingUtil.encodePathQuery(redirectURL));
111                         } else {
112                             setBranchAndLanguage(newBranchId, newLanguageId);
113                             String JavaDoc redirectURL = getMountPoint() + "/" + getSiteConf().getName() + "/" + getDocumentId() + ".html" + getVariantQueryString();
114                             appleResponse.redirectTo(EncodingUtil.encodePathQuery(redirectURL));
115                         }
116                     }
117                 } else {
118                     appleResponse.sendPage("Form-createdocvariant-Pipe", viewData);
119                 }
120             } else if (request.getMethod().equals("GET")) {
121                 appleResponse.sendPage("Form-createdocvariant-Pipe", viewData);
122             } else {
123                 throw new HttpMethodNotAllowedException(request.getMethod());
124             }
125         }
126     }
127
128     protected boolean needsInitialisation() {
129         return !init;
130     }
131
132     private void createImageVariants(long newBranchId, long newLanguageId) throws Exception JavaDoc {
133         Repeater repeater = (Repeater)form.getChild("resources");
134         for (int i = 0; i < repeater.getSize(); i++) {
135             Repeater.RepeaterRow row = repeater.getRow(i);
136             long id = ((Long JavaDoc)row.getChild("id").getValue()).longValue();
137             try {
138                 AvailableVariants availableVariants = getRepository().getAvailableVariants(id);
139                 if (!availableVariants.hasVariant(newBranchId, newLanguageId))
140                     getRepository().createVariant(id, getBranchId(), getLanguageId(), -1, newBranchId, newLanguageId, true);
141             } catch (RepositoryException e) {
142                 throw new Exception JavaDoc("Error branching embedded image " + id);
143             }
144         }
145     }
146 }
147
Popular Tags