KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > admin > TestAclApple


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.admin;
17
18 import org.outerj.daisy.frontend.util.*;
19 import org.outerj.daisy.frontend.PageContext;
20 import org.outerj.daisy.frontend.SkinConfHelper;
21 import org.outerj.daisy.frontend.WikiHelper;
22 import org.outerj.daisy.repository.Repository;
23 import org.outerj.daisy.repository.acl.AccessManager;
24 import org.outerj.daisy.repository.acl.AclResultInfo;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.cocoon.components.flow.apples.AppleRequest;
29 import org.apache.cocoon.components.flow.apples.AppleResponse;
30 import org.apache.cocoon.ResourceNotFoundException;
31 import org.apache.cocoon.forms.formmodel.Form;
32 import org.apache.cocoon.forms.FormContext;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Locale JavaDoc;
37
38 public class TestAclApple extends AbstractDaisyApple implements Serviceable {
39     private ServiceManager serviceManager;
40     private AccessManager accessManager;
41     private Repository repository;
42     private Form form;
43     private boolean init = false;
44     private Locale JavaDoc locale;
45     private Map JavaDoc viewData;
46     private String JavaDoc aclName;
47     private String JavaDoc path;
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         if (!init) {
55             aclName = appleRequest.getSitemapParameter("name");
56
57             if (!"live".equals(aclName) && !"staging".equals(aclName))
58                 throw new ResourceNotFoundException("ACL does not exist: " + aclName);
59
60             repository = WikiHelper.getRepository(appleRequest.getCocoonRequest(), serviceManager);
61             accessManager = repository.getAccessManager();
62
63             form = FormHelper.createForm(serviceManager, "resources/form/testacl_definition.xml");
64             locale = WikiHelper.getLocale(appleRequest.getCocoonRequest());
65
66             path = getMountPoint() + "/admin/testacl/" + aclName + "/" + getContinuationId();
67
68             form.getChild("branchId").setValue(new Long JavaDoc(1));
69             form.getChild("languageId").setValue(new Long JavaDoc(1));
70
71             viewData = new HashMap JavaDoc();
72             viewData.put("submitPath", path);
73             viewData.put("locale", locale);
74             viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
75             viewData.put("CocoonFormsInstance", form);
76             viewData.put("branchesArray", repository.getVariantManager().getAllBranches(false).getArray());
77             viewData.put("languagesArray", repository.getVariantManager().getAllLanguages(false).getArray());
78
79             init = true;
80
81             appleResponse.redirectTo(path);
82         } else {
83             String JavaDoc resource = appleRequest.getSitemapParameter("resource", null);
84
85             if ("result".equals(resource)) {
86                 if (!form.isValid()) {
87                     appleResponse.redirectTo(path);
88                 } else {
89                     long userId = ((Long JavaDoc)form.getChild("userId").getValue()).longValue();
90                     long roleId = ((Long JavaDoc)form.getChild("roleId").getValue()).longValue();
91                     long documentId = ((Long JavaDoc)form.getChild("documentId").getValue()).longValue();
92                     long branchId = ((Long JavaDoc)form.getChild("branchId").getValue()).longValue();
93                     long languageId = ((Long JavaDoc)form.getChild("languageId").getValue()).longValue();
94
95                     AclResultInfo result;
96                     if (aclName.equals("live"))
97                         result = accessManager.getAclInfoOnLive(userId, new long[] {roleId}, documentId, branchId, languageId);
98                     else
99                         result = accessManager.getAclInfoOnStaging(userId, new long[] {roleId}, documentId, branchId, languageId);
100
101                     Map JavaDoc viewData = new HashMap JavaDoc();
102                     viewData.put("pageXml", new XmlObjectXMLizable(result.getXml()));
103                     viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
104
105                     appleResponse.sendPage("TestAclResultPipe", viewData);
106                 }
107             } else if (resource == null) {
108                 String JavaDoc methodName = appleRequest.getCocoonRequest().getMethod();
109                 if (methodName.equals("GET")) {
110                     // display the form
111
appleResponse.sendPage("TestAclPipe", viewData);
112                 } else if (methodName.equals("POST")) {
113                     // handle a form submit
114
boolean endProcessing = form.process(new FormContext(appleRequest.getCocoonRequest(), locale));
115
116                     if (!endProcessing) {
117                         appleResponse.sendPage("TestAclPipe", viewData);
118                     } else {
119                         appleResponse.redirectTo(path + "/result");
120                     }
121                 }
122             } else {
123                 throw new ResourceNotFoundException("Unexisting resource: " + resource);
124             }
125         }
126     }
127 }
128
Popular Tags