KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > authoring > DocumentCreator


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
18 /* $Id: DocumentCreator.java 160148 2005-04-05 09:40:13Z michi $ */
19
20 package org.apache.lenya.cms.authoring;
21
22 import java.io.File JavaDoc;
23 import java.util.Collections JavaDoc;
24
25 import org.apache.lenya.cms.publication.SiteTree;
26 import org.apache.lenya.cms.publication.DocumentType;
27 import org.apache.lenya.cms.publication.DocumentTypeBuildException;
28 import org.apache.lenya.cms.publication.DocumentTypeBuilder;
29 import org.apache.lenya.cms.publication.Label;
30 import org.apache.lenya.cms.publication.Publication;
31
32 import org.apache.log4j.Category;
33
34 /**
35  *
36  */

37 public class DocumentCreator {
38     private static Category log = Category.getInstance(DocumentCreator.class);
39
40     /**
41      * DOCUMENT ME!
42      *
43      * @param publication DOCUMENT ME!
44      * @param authoringDirectory DOCUMENT ME!
45      * @param area the area
46      * @param parentId DOCUMENT ME!
47      * @param childId DOCUMENT ME!
48      * @param childName DOCUMENT ME!
49      * @param childTypeString DOCUMENT ME!
50      * @param documentTypeName DOCUMENT ME!
51      * @param language the language of the document to be created.
52      * @param visibleInNav boolean determines wether the node.
53      *
54      * @throws CreatorException DOCUMENT ME!
55      */

56     public void create(
57         Publication publication,
58         File JavaDoc authoringDirectory,
59         String JavaDoc area,
60         String JavaDoc parentId,
61         String JavaDoc childId,
62         String JavaDoc childName,
63         String JavaDoc childTypeString,
64         String JavaDoc documentTypeName,
65         String JavaDoc language,
66         boolean visibleInNav)
67         throws CreatorException {
68         short childType;
69
70         if (childTypeString.equals("branch")) {
71             childType = ParentChildCreatorInterface.BRANCH_NODE;
72         } else if (childTypeString.equals("leaf")) {
73             childType = ParentChildCreatorInterface.LEAF_NODE;
74         } else {
75             throw new CreatorException(
76                 "No such child type: " + childTypeString);
77         }
78
79         if (!validate(parentId,
80             childId,
81             childName,
82             childTypeString,
83             documentTypeName)) {
84             throw new CreatorException("Exception: Validation of parameters failed");
85         }
86
87         // Get creator
88
DocumentType type;
89
90         try {
91             type =
92                 DocumentTypeBuilder.buildDocumentType(
93                     documentTypeName,
94                     publication);
95         } catch (DocumentTypeBuildException e) {
96             throw new CreatorException(e);
97         }
98
99         ParentChildCreatorInterface creator = type.getCreator();
100
101         SiteTree siteTree;
102
103         try {
104             log.debug("Get sitetree of area: " + area);
105             siteTree = publication.getTree(area);
106         } catch (Exception JavaDoc e) {
107             throw new CreatorException(e);
108         }
109
110         Label[] labels = new Label[1];
111         labels[0] = new Label(childName, language);
112
113         try {
114             siteTree.addNode(
115                 parentId,
116                 creator.generateTreeId(childId, childType),
117                 labels,
118                 visibleInNav);
119         } catch (Exception JavaDoc e) {
120             throw new CreatorException(e);
121         }
122
123         File JavaDoc doctypesDirectory =
124             new File JavaDoc(
125                 publication.getDirectory(),
126                 DocumentTypeBuilder.DOCTYPE_DIRECTORY);
127
128         try {
129             creator.create(
130                 new File JavaDoc(doctypesDirectory, "samples"),
131                 new File JavaDoc(authoringDirectory, parentId),
132                 childId,
133                 childType,
134                 childName,
135                 language,
136                 Collections.EMPTY_MAP);
137         } catch (Exception JavaDoc e) {
138             throw new CreatorException(e);
139         }
140
141         // commit (sort of)
142
try {
143             siteTree.save();
144         } catch (Exception JavaDoc e) {
145             throw new CreatorException(e);
146         }
147     }
148
149     /**
150      * DOCUMENT ME!
151      *
152      * @param parentid DOCUMENT ME!
153      * @param childid DOCUMENT ME!
154      * @param childname DOCUMENT ME!
155      * @param childtype DOCUMENT ME!
156      * @param doctype DOCUMENT ME!
157      *
158      * @return DOCUMENT ME!
159      */

160     public boolean validate(
161         String JavaDoc parentid,
162         String JavaDoc childid,
163         String JavaDoc childname,
164         String JavaDoc childtype,
165         String JavaDoc doctype) {
166         return (childid.indexOf(" ") == -1)
167             && (childid.length() > 0)
168             && (childname.length() > 0);
169     }
170 }
171
Popular Tags