KickJava   Java API By Example, From Geeks To Geeks.

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


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.HttpMethodNotAllowedException;
20 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
21 import org.outerj.daisy.frontend.components.docbasket.DocumentBasketEntry;
22 import org.outerj.daisy.frontend.components.docbasket.DocumentBasketHelper;
23 import org.outerj.daisy.repository.Repository;
24 import org.outerj.daisy.repository.variant.VariantManager;
25 import org.outerj.daisy.navigation.NavigationManager;
26 import org.outerj.daisy.navigation.NavigationParams;
27 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
28 import org.apache.cocoon.components.flow.apples.AppleRequest;
29 import org.apache.cocoon.components.flow.apples.AppleResponse;
30 import org.apache.cocoon.environment.Request;
31 import org.apache.cocoon.xml.SaxBuffer;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.service.ServiceManager;
34 import org.apache.avalon.framework.service.ServiceException;
35
36 import java.util.Map JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.regex.Pattern JavaDoc;
39 import java.util.regex.Matcher JavaDoc;
40
41 /**
42  * Apple that lets the user select documents from the current site's navigation
43  * tree to add them to the document basket.
44  */

45 public class NavigationToDocumentBasketApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
46     private static Pattern JavaDoc DOC_PATTERN = Pattern.compile("([0-9]+)\\.([0-9]+)\\.([0-9]+)");
47     private ServiceManager serviceManager;
48
49     public void service(ServiceManager serviceManager) throws ServiceException {
50         this.serviceManager = serviceManager;
51     }
52
53     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
54         Request request = appleRequest.getCocoonRequest();
55         Repository repository = WikiHelper.getRepository(request, serviceManager);
56         SiteConf siteConf = WikiHelper.getSiteConf(request);
57         WikiVersionMode versionMode = WikiHelper.getVersionMode(request);
58
59         if (request.getMethod().equals("GET")) {
60             // GET = show the navigation tree
61

62             NavigationManager navigationManager = (NavigationManager)repository.getExtension("NavigationManager");
63             SaxBuffer buffer = new SaxBuffer();
64             NavigationParams navParams = new NavigationParams(siteConf.getNavigationDoc(), versionMode.getNavigationVersionMode(), null, false);
65             navigationManager.generateNavigationTree(buffer, navParams, null, false);
66
67             Map JavaDoc viewData = new HashMap JavaDoc();
68             viewData.put("pageXml", buffer);
69             viewData.put("pageContext", new PageContext(getMountPoint(), siteConf, repository, getLayoutType(), getSkin(), getContext()));
70             appleResponse.sendPage("internal/documentBasket/selectFromNavigation", viewData);
71
72         } else if (request.getMethod().equals("POST")) {
73             // POST = selected nodes have been submitted
74

75             VariantManager variantManager = repository.getVariantManager();
76             String JavaDoc[] values = request.getParameterValues("document");
77             if (values == null)
78                 values = new String JavaDoc[0];
79             DocumentBasketEntry[] entries = new DocumentBasketEntry[values.length];
80
81             for (int i = 0; i < values.length; i++) {
82                 Matcher JavaDoc docMatcher = DOC_PATTERN.matcher(values[i]);
83                 if (docMatcher.matches()) {
84                     long documentId = Long.parseLong(docMatcher.group(1));
85                     long branchId = Long.parseLong(docMatcher.group(2));
86                     long languageId = Long.parseLong(docMatcher.group(3));
87
88                     String JavaDoc branch = variantManager.getBranch(branchId, false).getName();
89                     String JavaDoc language = variantManager.getLanguage(languageId, false).getName();
90
91                     entries[i] = new DocumentBasketEntry(documentId, branch, language, -3, "");
92                 } else {
93                     throw new Exception JavaDoc("Unexpected error in adding documents from navigation to document basket: input value did not match: \"" + values[i] + "\".");
94                 }
95             }
96
97             DocumentBasketHelper.updateDocumentNames(entries, request, repository);
98             DocumentBasketHelper.getDocumentBasket(request, true).appendEntries(entries);
99
100             appleResponse.redirectTo(getMountPoint() + "/" + siteConf.getName() + "/documentBasket");
101         } else {
102             throw new HttpMethodNotAllowedException(request.getMethod());
103         }
104     }
105
106 }
107
Popular Tags