1 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 ; 33 import java.util.Map ; 34 import java.util.HashMap ; 35 36 39 public class CreateDocumentVariantApple extends AbstractDocumentApple { 40 private boolean init = false; 41 private Form form; 42 private Map viewData; 43 private Locale locale; 44 45 protected void processDocumentRequest(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception { 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 (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 (); 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 )form.getChild("newBranchId").getValue()).longValue(); 87 long newLanguageId = ((Long )form.getChild("newLanguageId").getValue()).longValue(); 88 createImageVariants(newBranchId, newLanguageId); 89 boolean copyContent = ((Boolean )form.getChild("copyContent").getValue()).booleanValue(); 90 if (!copyContent) { 91 String 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 startVersion = (String )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 )form.getChild("goToEditor").getValue()).booleanValue(); 107 if (goToEditor) { 108 String 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 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 { 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 )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 ("Error branching embedded image " + id); 143 } 144 } 145 } 146 } 147 | Popular Tags |