KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > DefaultCreatorAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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
17 /* $Id: DefaultCreatorAction.java 160148 2005-04-05 09:40:13Z michi $ */
18
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
31 import org.apache.avalon.framework.parameters.ParameterException;
32 import org.apache.avalon.framework.parameters.Parameters;
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.acting.AbstractComplementaryConfigurableAction;
35 import org.apache.cocoon.environment.ObjectModelHelper;
36 import org.apache.cocoon.environment.Redirector;
37 import org.apache.cocoon.environment.Request;
38 import org.apache.cocoon.environment.Session;
39 import org.apache.cocoon.environment.SourceResolver;
40 import org.apache.lenya.cms.authoring.ParentChildCreatorInterface;
41 import org.apache.lenya.cms.publication.SiteTree;
42 import org.apache.lenya.cms.publication.Label;
43 import org.apache.lenya.cms.publication.Publication;
44 import org.apache.lenya.cms.publication.PublicationFactory;
45 import org.apache.log4j.Category;
46
47
48 import org.dom4j.Attribute;
49 import org.dom4j.Document;
50 import org.dom4j.io.SAXReader;
51
52
53 /**
54  * DOCUMENT ME!
55  */

56 public class DefaultCreatorAction extends AbstractComplementaryConfigurableAction implements Configurable {
57     Category log = Category.getInstance(DefaultCreatorAction.class);
58
59     private String JavaDoc docsPath = null;
60     private String JavaDoc doctypesPath = null;
61
62     /**
63      * DOCUMENT ME!
64      *
65      * @param conf DOCUMENT ME!
66      *
67      * @throws ConfigurationException DOCUMENT ME!
68      */

69     public void configure(Configuration conf) throws ConfigurationException {
70         super.configure(conf);
71
72         docsPath = conf.getChild("docs").getAttribute("href");
73         doctypesPath = conf.getChild("doctypes").getAttribute("href");
74     }
75
76     /**
77      * DOCUMENT ME!
78      *
79      * @param redirector DOCUMENT ME!
80      * @param resolver DOCUMENT ME!
81      * @param objectModel DOCUMENT ME!
82      * @param src DOCUMENT ME!
83      * @param parameters DOCUMENT ME!
84      *
85      * @return DOCUMENT ME!
86      *
87      * @throws Exception DOCUMENT ME!
88      */

89     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src,
90         Parameters parameters) throws Exception JavaDoc {
91         Publication publication = PublicationFactory.getPublication(objectModel);
92
93         // Get request object
94
Request request = ObjectModelHelper.getRequest(objectModel);
95
96         if (request == null) {
97             getLogger().error("No request object");
98
99             return null;
100         }
101
102         // Get parameters
103
//String parentid = request.getParameter("parentid");
104
String JavaDoc parentid = request.getParameter("properties.create.parent-id");
105         log.debug("properties.create.parent-id = " + parentid);
106
107         //String childid = request.getParameter("childid");
108
String JavaDoc childid = request.getParameter("properties.create.child-id");
109         log.debug("properties.create.child-id = " + childid);
110
111         //String childname = request.getParameter("childname");
112
String JavaDoc childname = request.getParameter("properties.create.child-name");
113         log.debug("properties.create.child-name = " + childname);
114
115         //String childtype = request.getParameter("childtype");
116
String JavaDoc childtype = request.getParameter("properties.create.child-type");
117         log.debug("properties.create.childtype = " + childtype);
118         
119         //String visibleInNav = request.getParameter("visible");
120
String JavaDoc visible = request.getParameter("properties.create.visible");
121
122         boolean visibleInNav = true;
123         if (visible.equals("no")){
124             visibleInNav = false;
125         }
126         
127         short childType;
128         if (childtype.equals("branch")) {
129             childType = ParentChildCreatorInterface.BRANCH_NODE;
130         } else if (childtype.equals("leaf")) {
131             childType = ParentChildCreatorInterface.LEAF_NODE;
132         } else {
133             log.error("No such child type: " + childtype);
134             return null;
135         }
136
137
138
139         //String doctype = request.getParameter("doctype");
140
String JavaDoc doctype = request.getParameter("properties.create.doctype");
141         log.debug("poperties.create.doctype = " + doctype);
142
143         //String language = request.getParameter("language");
144
String JavaDoc language = request.getParameter("properties.create.language");
145         log.debug("poperties.create.language = " + language);
146         
147
148
149
150         if (!validate(parentid, childid, childname, childtype, doctype)) {
151             getLogger().error("Exception: Validation of parameters failed");
152
153             return null;
154         }
155
156         // Get session
157
Session session = request.getSession(true);
158
159         if (session == null) {
160             getLogger().error("No session object");
161
162             return null;
163         }
164
165         // Get creator
166
ParentChildCreatorInterface creator = null;
167         String JavaDoc absoluteDoctypesPath = publication.getDirectory() + File.separator + doctypesPath;
168         Document doctypesDoc = new SAXReader().read("file:" + absoluteDoctypesPath +
169                 "doctypes.xconf");
170         Attribute creator_src = (Attribute) doctypesDoc.selectSingleNode("/doctypes/doc[@type='" +
171                 doctype + "']/creator/@src");
172
173         if (creator_src != null) {
174             log.info("Creator found for \"" + doctype + "\": " + creator_src.getName() + " " + creator_src.getPath() + " " + creator_src.getValue());
175
176             // now get the constructor that accepts the configuration
177
Class JavaDoc creatorClass = Class.forName(creator_src.getValue());
178             creator = (ParentChildCreatorInterface) creatorClass.newInstance();
179         } else {
180             log.warn("No creator found for \"" + doctype + "\". DefaultBranchCreator will be taken.");
181             creator = new org.apache.lenya.cms.authoring.DefaultBranchCreator();
182         }
183
184         getLogger().debug(".act(): Creator : " + creator.getClass().getName());
185
186         // Init creator
187
// "Read" the configuration from the DOM node
188
DefaultConfigurationBuilder defaultConfigBuilder = new DefaultConfigurationBuilder();
189         Configuration[] docTypeConfigs = defaultConfigBuilder.buildFromFile(absoluteDoctypesPath +
190                 "doctypes.xconf").getChildren();
191
192         Configuration doctypeConf = null;
193
194         for (int i = 0; i < docTypeConfigs.length; i++) {
195             String JavaDoc typeName = docTypeConfigs[i].getAttribute("type");
196
197             if (typeName.equals(doctype)) {
198                 doctypeConf = docTypeConfigs[i].getChild("creator", false);
199             }
200         }
201
202         creator.init(doctypeConf);
203
204         // add a node to the tree
205
SiteTree siteTree = publication.getTree(Publication.AUTHORING_AREA);
206         Label[] labels = new Label[1];
207         labels[0] = new Label(childname, language);
208         siteTree.addNode(parentid, creator.generateTreeId(childid, childType), labels, visibleInNav);
209
210         // Transaction should actually be finished here!
211
// Create actual document
212
// grab all the parameters from session, request params and
213
// sitemap params
214
HashMap JavaDoc allParameters = new HashMap JavaDoc();
215         String JavaDoc[] names = parameters.getNames();
216
217         for (int i = 0; i < names.length; i++) {
218             String JavaDoc name = names[i];
219             String JavaDoc value = null;
220
221             try {
222                 value = parameters.getParameter(name);
223             } catch (ParameterException pe) {
224                 value = null;
225             }
226
227             allParameters.put(name, value);
228         }
229
230         Enumeration JavaDoc requestParameters = request.getParameterNames();
231
232         while (requestParameters.hasMoreElements()) {
233             String JavaDoc requestParameterName = (String JavaDoc) requestParameters.nextElement();
234
235             if (allParameters.containsKey(requestParameterName)) {
236                 // we do not allow name clashes
237
throw new ProcessingException("Name clash in request parameter " +
238                     "and sitemap parameter: " + requestParameterName);
239             }
240
241             allParameters.put(requestParameterName, request.getParameter(requestParameterName));
242         }
243
244         Enumeration JavaDoc sessionAttributeNames = session.getAttributeNames();
245
246         while (sessionAttributeNames.hasMoreElements()) {
247             String JavaDoc sessionAttributeName = (String JavaDoc) sessionAttributeNames.nextElement();
248
249             if (allParameters.containsKey(sessionAttributeName)) {
250                 // we do not allow name clashes
251
throw new ProcessingException("Name clash in session attribute " +
252                     "and request parameter or sitemap parameter: " + sessionAttributeName);
253             }
254
255             allParameters.put(sessionAttributeName, session.getAttribute(sessionAttributeName));
256         }
257
258         try {
259             creator.create(new File JavaDoc(absoluteDoctypesPath + "samples"),
260                 new File JavaDoc(publication.getDirectory(), docsPath + parentid), childid, childType,
261                 childname, language, allParameters);
262         } catch (Exception JavaDoc e) {
263             log.error("Creator threw exception: " + e);
264             return null;
265         }
266
267         // commit (sort of)
268
siteTree.save();
269
270         HashMap JavaDoc actionMap = new HashMap JavaDoc();
271
272         return actionMap;
273     }
274
275     /**
276      * DOCUMENT ME!
277      *
278      * @param parentid DOCUMENT ME!
279      * @param childid DOCUMENT ME!
280      * @param childname DOCUMENT ME!
281      * @param childtype DOCUMENT ME!
282      * @param doctype DOCUMENT ME!
283      *
284      * @return DOCUMENT ME!
285      */

286     public boolean validate(String JavaDoc parentid, String JavaDoc childid, String JavaDoc childname, String JavaDoc childtype,
287         String JavaDoc doctype) {
288         getLogger().debug(".validate(): parentid=" + parentid + " ; childid=" + childid +
289             " ; childname=" + childname + " ; childtype=" + childtype + " ; doctype=" + doctype);
290
291         if ((childid.indexOf(" ") >= 0) || (childid.length() == 0)) {
292             return false;
293         }
294
295         if (childname.length() == 0) {
296             return false;
297         }
298
299         return true;
300     }
301 }
302
Popular Tags