1 17 18 19 20 package org.apache.lenya.cms.authoring; 21 22 import java.io.File ; 23 import java.util.Collections ; 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 37 public class DocumentCreator { 38 private static Category log = Category.getInstance(DocumentCreator.class); 39 40 56 public void create( 57 Publication publication, 58 File authoringDirectory, 59 String area, 60 String parentId, 61 String childId, 62 String childName, 63 String childTypeString, 64 String documentTypeName, 65 String 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 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 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 e) { 120 throw new CreatorException(e); 121 } 122 123 File doctypesDirectory = 124 new File ( 125 publication.getDirectory(), 126 DocumentTypeBuilder.DOCTYPE_DIRECTORY); 127 128 try { 129 creator.create( 130 new File (doctypesDirectory, "samples"), 131 new File (authoringDirectory, parentId), 132 childId, 133 childType, 134 childName, 135 language, 136 Collections.EMPTY_MAP); 137 } catch (Exception e) { 138 throw new CreatorException(e); 139 } 140 141 try { 143 siteTree.save(); 144 } catch (Exception e) { 145 throw new CreatorException(e); 146 } 147 } 148 149 160 public boolean validate( 161 String parentid, 162 String childid, 163 String childname, 164 String childtype, 165 String doctype) { 166 return (childid.indexOf(" ") == -1) 167 && (childid.length() > 0) 168 && (childname.length() > 0); 169 } 170 } 171 | Popular Tags |