001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend;
017:
018: import org.apache.cocoon.components.flow.apples.AppleRequest;
019: import org.apache.cocoon.components.flow.apples.AppleResponse;
020: import org.apache.cocoon.forms.formmodel.Form;
021: import org.apache.cocoon.forms.formmodel.Repeater;
022: import org.apache.cocoon.forms.FormContext;
023: import org.outerj.daisy.frontend.util.FormHelper;
024: import org.outerj.daisy.frontend.util.HttpMethodNotAllowedException;
025: import org.outerj.daisy.frontend.util.EncodingUtil;
026: import org.outerj.daisy.repository.variant.VariantManager;
027: import org.outerj.daisy.repository.RepositoryException;
028: import org.outerj.daisy.repository.AvailableVariants;
029: import org.outerj.daisy.repository.query.QueryHelper;
030: import org.outerx.daisy.x10.SearchResultDocument;
031:
032: import java.util.Locale;
033: import java.util.Map;
034: import java.util.HashMap;
035: import java.util.List;
036:
037: /**
038: * This apple allows the user to create a new document variant for one document.
039: */
040: public class CreateDocumentVariantApple extends AbstractDocumentApple {
041: private boolean init = false;
042: private Form form;
043: private Map<String, Object> viewDataTemplate;
044: private Locale locale;
045:
046: protected void processDocumentRequest(AppleRequest appleRequest,
047: AppleResponse appleResponse) throws Exception {
048: if (!init) {
049: locale = frontEndContext.getLocale();
050: form = FormHelper.createForm(getServiceManager(),
051: "resources/form/createdocvariant_definition.xml");
052: form.getChild("copyContent").setValue(Boolean.TRUE);
053: form.getChild("goToEditor").setValue(Boolean.TRUE);
054: form.getChild("startVersion").setValue("last");
055:
056: String imagesQuery = "select name where LinksFromVariant("
057: + QueryHelper.formatString(getDocumentId()) + ","
058: + getBranchId() + "," + getLanguageId()
059: + ",1,0,'image') and branchId = " + getBranchId()
060: + " and languageId = " + getLanguageId();
061: List<SearchResultDocument.SearchResult.Rows.Row> images = getRepository()
062: .getQueryManager()
063: .performQuery(imagesQuery, locale)
064: .getSearchResult().getRows().getRowList();
065: Repeater repeater = (Repeater) form.getChild("resources");
066: for (SearchResultDocument.SearchResult.Rows.Row image : images) {
067: Repeater.RepeaterRow row = repeater.addRow();
068: row.getChild("id").setValue(image.getDocumentId());
069: row.getChild("name").setValue(image.getValueArray(0));
070: row.getChild("createvariant").setValue(Boolean.TRUE);
071: }
072:
073: VariantManager variantManager = getRepository()
074: .getVariantManager();
075: viewDataTemplate = new HashMap<String, Object>();
076: viewDataTemplate.put("branchesArray", variantManager
077: .getAllBranches(false).getArray());
078: viewDataTemplate.put("languagesArray", variantManager
079: .getAllLanguages(false).getArray());
080: viewDataTemplate.put("startBranchName", variantManager
081: .getBranch(getBranchId(), false).getName());
082: viewDataTemplate.put("startLanguageName", variantManager
083: .getLanguage(getLanguageId(), false).getName());
084: viewDataTemplate.put("availableVariants", getRepository()
085: .getAvailableVariants(getDocumentId()).getArray());
086: viewDataTemplate.put("documentPath", getMountPoint() + "/"
087: + getSiteConf().getName() + "/"
088: + getRequestedNavigationPath());
089: viewDataTemplate.put("CocoonFormsInstance", form);
090: viewDataTemplate.put("variantParams", getVariantParams());
091: viewDataTemplate.put("variantQueryString",
092: getVariantQueryString());
093: viewDataTemplate.put("locale", locale);
094:
095: init = true;
096:
097: appleResponse.redirectTo(EncodingUtil
098: .encodePath(getMountPoint() + "/"
099: + getSiteConf().getName() + "/"
100: + getRequestedNavigationPath()
101: + "/createVariant/" + getContinuationId()));
102: } else {
103: if (request.getMethod().equals("POST")) {
104: FormContext formContext = new FormContext(request,
105: locale);
106: boolean finished = form.process(formContext);
107: if (finished) {
108: long newBranchId = ((Long) form.getChild(
109: "newBranchId").getValue()).longValue();
110: long newLanguageId = ((Long) form.getChild(
111: "newLanguageId").getValue()).longValue();
112: createImageVariants(newBranchId, newLanguageId);
113: boolean copyContent = ((Boolean) form.getChild(
114: "copyContent").getValue()).booleanValue();
115: if (!copyContent) {
116: String redirectURL = getMountPoint() + "/"
117: + getSiteConf().getName()
118: + "/new/edit?variantOf="
119: + getDocumentId() + "&startBranchId="
120: + getBranchId() + "&startLanguageId="
121: + getLanguageId() + "&newBranchId="
122: + newBranchId + "&newLanguageId="
123: + newLanguageId + "&startWithGet=true";
124: appleResponse.redirectTo(EncodingUtil
125: .encodePathQuery(redirectURL));
126: } else {
127: String startVersion = (String) form.getChild(
128: "startVersion").getValue();
129: long startVersionId;
130: if (startVersion.equals("last"))
131: startVersionId = -1;
132: else if (startVersion.equals("live"))
133: startVersionId = -2;
134: else
135: startVersionId = Long
136: .parseLong(startVersion);
137: getRepository().createVariant(getDocumentId(),
138: getBranchId(), getLanguageId(),
139: startVersionId, newBranchId,
140: newLanguageId, true);
141: boolean goToEditor = ((Boolean) form.getChild(
142: "goToEditor").getValue())
143: .booleanValue();
144: if (goToEditor) {
145: String redirectURL = getMountPoint() + "/"
146: + getSiteConf().getName() + "/"
147: + getDocumentId() + "/edit"
148: + "?branch=" + newBranchId
149: + "&language=" + newLanguageId
150: + "&startWithGet=true";
151: appleResponse.redirectTo(EncodingUtil
152: .encodePathQuery(redirectURL));
153: } else {
154: setBranchAndLanguage(newBranchId,
155: newLanguageId);
156: String redirectURL = getMountPoint() + "/"
157: + getSiteConf().getName() + "/"
158: + getDocumentId() + ".html"
159: + getVariantQueryString();
160: appleResponse.redirectTo(EncodingUtil
161: .encodePathQuery(redirectURL));
162: }
163: }
164: } else {
165: appleResponse.sendPage(
166: "Form-createdocvariant-Pipe",
167: getViewData(frontEndContext));
168: }
169: } else if (request.getMethod().equals("GET")) {
170: appleResponse.sendPage("Form-createdocvariant-Pipe",
171: getViewData(frontEndContext));
172: } else {
173: throw new HttpMethodNotAllowedException(request
174: .getMethod());
175: }
176: }
177: }
178:
179: protected boolean needsInitialisation() {
180: return !init;
181: }
182:
183: private void createImageVariants(long newBranchId,
184: long newLanguageId) throws Exception {
185: Repeater repeater = (Repeater) form.getChild("resources");
186: for (int i = 0; i < repeater.getSize(); i++) {
187: Repeater.RepeaterRow row = repeater.getRow(i);
188: if (Boolean.TRUE.equals(row.getChild("createvariant")
189: .getValue())) {
190: String id = (String) row.getChild("id").getValue();
191: try {
192: AvailableVariants availableVariants = getRepository()
193: .getAvailableVariants(id);
194: if (!availableVariants.hasVariant(newBranchId,
195: newLanguageId))
196: getRepository().createVariant(id,
197: getBranchId(), getLanguageId(), -1,
198: newBranchId, newLanguageId, true);
199: } catch (RepositoryException e) {
200: throw new Exception(
201: "Error branching embedded image " + id);
202: }
203: }
204: }
205: }
206:
207: private Map<String, Object> getViewData(
208: FrontEndContext frontEndContext) {
209: Map<String, Object> viewData = new HashMap<String, Object>(
210: viewDataTemplate);
211: viewData.put("pageContext", frontEndContext.getPageContext());
212: return viewData;
213: }
214: }
|