KickJava   Java API By Example, From Geeks To Geeks.

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


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.components.siteconf.SiteConf;
20 import org.outerj.daisy.repository.schema.DocumentType;
21 import org.outerj.daisy.repository.Repository;
22 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
23 import org.apache.cocoon.components.flow.apples.AppleRequest;
24 import org.apache.cocoon.components.flow.apples.AppleResponse;
25 import org.apache.cocoon.environment.Request;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.ServiceException;
29
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32
33 public class DocumentTypeSelectionApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
34     private ServiceManager serviceManager;
35
36     public void service(ServiceManager serviceManager) throws ServiceException {
37         this.serviceManager = serviceManager;
38     }
39
40     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
41         Request request = appleRequest.getCocoonRequest();
42         Repository repository = WikiHelper.getRepository(request, serviceManager);
43         SiteConf siteConf = WikiHelper.getSiteConf(request);
44         String JavaDoc requestedNavigationPath = appleRequest.getSitemapParameter("requestedNavigationPath");
45
46         DocumentType[] filteredDocumentTypes = getFilteredDocumentTypes(repository, siteConf.getCollectionId());
47
48         String JavaDoc returnTo = request.getParameter("returnTo");
49         if (returnTo == null)
50             returnTo = getMountPoint() + "/" + siteConf.getName() + "/" + requestedNavigationPath + "/edit";
51
52         Map JavaDoc viewData = new HashMap JavaDoc();
53         viewData.put("pageContext", new PageContext(getMountPoint(), siteConf, repository, getLayoutType(), getSkin(), getContext()));
54         viewData.put("locale", WikiHelper.getLocale(request));
55         viewData.put("documentTypes", filteredDocumentTypes);
56         viewData.put("requestedNavigationPath", requestedNavigationPath);
57         viewData.put("goto", returnTo);
58         viewData.put("branches", repository.getVariantManager().getAllBranches(false).getArray());
59         viewData.put("languages", repository.getVariantManager().getAllLanguages(false).getArray());
60
61         appleResponse.sendPage("SelectDocumentTypePipe", viewData);
62     }
63
64     public static DocumentType[] getFilteredDocumentTypes(Repository repository, long collectionId) throws Exception JavaDoc {
65         DocumentType[] documentTypes = repository.getRepositorySchema().getAllDocumentTypes(false).getArray();
66         long[] documentTypeIds = new long[documentTypes.length];
67         for (int i = 0; i < documentTypes.length; i++) {
68             documentTypeIds[i] = documentTypes[i].getId();
69         }
70         long[] filteredIds = repository.getAccessManager().filterDocumentTypes(documentTypeIds, collectionId);
71
72
73         DocumentType[] filteredDocumentTypes = new DocumentType[filteredIds.length];
74         for (int i = 0; i < filteredIds.length; i++) {
75             for (int k = 0; i < documentTypes.length; k++) {
76                 if (documentTypes[k].getId() == filteredIds[i]) {
77                     filteredDocumentTypes[i] = documentTypes[k];
78                     break;
79                 }
80             }
81             if (filteredDocumentTypes[i] == null) {
82                 throw new Exception JavaDoc("Encountered a situation which should be impossible, documentType " + filteredIds[i] + " not found.");
83             }
84         }
85         return filteredDocumentTypes;
86     }
87 }
88
Popular Tags