KickJava   Java API By Example, From Geeks To Geeks.

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


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.docbasket.DocumentBasket;
21 import org.outerj.daisy.frontend.components.docbasket.DocumentBasketHelper;
22 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
23 import org.outerj.daisy.repository.Repository;
24 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
25 import org.apache.cocoon.components.flow.apples.AppleRequest;
26 import org.apache.cocoon.components.flow.apples.AppleResponse;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.forms.util.StringMessage;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.excalibur.xml.sax.XMLizable;
33
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 public class DocumentBasketApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
40     private ServiceManager serviceManager;
41     private Repository repository;
42     private SiteConf siteConf;
43
44     public void service(ServiceManager serviceManager) throws ServiceException {
45         this.serviceManager = serviceManager;
46     }
47
48     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
49         Request request = appleRequest.getCocoonRequest();
50         siteConf = WikiHelper.getSiteConf(request);
51         repository = WikiHelper.getRepository(request, serviceManager);
52
53         // Note: guest users are allowed to use the document basket too
54

55         if (request.getMethod().equals("GET")) {
56             // GET request: show basket content
57

58             DocumentBasket documentBasket = DocumentBasketHelper.getDocumentBasket(request, false);
59             if (documentBasket == null)
60                 documentBasket = new DocumentBasket(); // not stored in session on purpose: avoids session creation
61

62             showDocumentBasket(appleResponse, documentBasket, null);
63         } else if (request.getMethod().equals("POST")) {
64             String JavaDoc action = RequestUtil.getStringParameter(request, "basketAction");
65
66             DocumentBasket documentBasket = DocumentBasketHelper.getDocumentBasket(request, true);
67
68             if (action.equals("clear")) {
69                 documentBasket.clear();
70                 appleResponse.redirectTo("documentBasket");
71             } else if (action.equals("removeSelected")) {
72                 List JavaDoc entries = getEntriesAndCheckConcurrentUpdates(documentBasket, request, appleResponse);
73                 if (entries == null)
74                     return;
75
76                 // Read out selected items
77
boolean[] selected = getSelectStatus(request, entries.size());
78
79                 List JavaDoc newEntries = new ArrayList JavaDoc(entries);
80                 for (int i = selected.length - 1; i >=0 ; i--) {
81                     if (selected[i])
82                         newEntries.remove(i);
83                 }
84                 documentBasket.setEntries(newEntries);
85
86                 appleResponse.redirectTo("documentBasket");
87             } else {
88                 throw new Exception JavaDoc("Invalid value for action parameter: \"" + action + "\".");
89             }
90             
91         } else {
92             throw new HttpMethodNotAllowedException(request.getMethod());
93         }
94     }
95
96     private void showDocumentBasket(AppleResponse appleResponse, DocumentBasket documentBasket, XMLizable message) {
97         Map JavaDoc viewData = new HashMap JavaDoc();
98         viewData.put("documentBasket", documentBasket);
99         if (message != null)
100             viewData.put("basketMessage", message);
101         viewData.put("pageContext", new PageContext(getMountPoint(), siteConf, repository, getLayoutType(), getSkin(), getContext()));
102
103         appleResponse.sendPage("internal/documentBasket/content", viewData);
104     }
105
106     /**
107      * Gets the list of entries from the basket, but also checks the basket hasn't changed
108      * since this request. If this is the case, this method will set an appropriate apple
109      * response and return null.
110      */

111     private List JavaDoc getEntriesAndCheckConcurrentUpdates(DocumentBasket documentBasket, Request request, AppleResponse appleResponse) throws Exception JavaDoc {
112         List JavaDoc entries = documentBasket.getEntries();
113         long currentUpdateCount = documentBasket.getUpdateCount();
114         long requestUpdateCount = RequestUtil.getLongParameter(request, "basketUpdateCount");
115         if (currentUpdateCount != requestUpdateCount) {
116             showDocumentBasket(appleResponse, documentBasket, new StringMessage("Your basket was updated since you requested this operation, so the operation has not been performed."));
117             return null;
118         }
119         return entries;
120     }
121
122     private boolean[] getSelectStatus(Request request, int size) {
123         boolean[] selected = new boolean[size];
124         for (int i = 0; i < size; i++) {
125             String JavaDoc checked = request.getParameter("entry." + (i + 1));
126             selected[i] = checked != null && checked.equals("true");
127         }
128         return selected;
129     }
130
131 }
132
Popular Tags