View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.roo.addon.surf.content;
20  
21  import java.io.InputStream;
22  import java.util.logging.Logger;
23  
24  import org.springframework.roo.addon.surf.Constants;
25  import org.springframework.roo.addon.surf.SurfOperations;
26  import org.springframework.roo.metadata.MetadataService;
27  import org.springframework.roo.process.manager.FileManager;
28  import org.springframework.roo.process.manager.MutableFile;
29  import org.springframework.roo.project.Path;
30  import org.springframework.roo.project.PathResolver;
31  import org.springframework.roo.project.ProjectOperations;
32  import org.springframework.roo.support.lifecycle.ScopeDevelopment;
33  import org.springframework.roo.support.util.Assert;
34  import org.springframework.roo.support.util.TemplateUtils;
35  import org.springframework.roo.support.util.XmlUtils;
36  import org.w3c.dom.Document;
37  import org.w3c.dom.Element;
38  
39  /**
40   * Provides Surf Content operations.
41   *
42   * @author Yong Qu
43   * @since 1.0
44   */
45  @ScopeDevelopment
46  public class ContentOperations {
47  
48  	Logger logger = Logger.getLogger(ContentOperations.class.getName());
49  
50  	private FileManager fileManager;
51  	private PathResolver pathResolver;
52  	private MetadataService metadataService;
53  	private ProjectOperations projectOperations;
54  
55  	private SurfOperations surfOperations;
56  
57  	/**
58  	 * @param fileManager
59  	 * @param pathResolver
60  	 * @param metadataService
61  	 * @param projectOperations
62  	 */
63  	public ContentOperations(FileManager fileManager, PathResolver pathResolver, MetadataService metadataService, ProjectOperations projectOperations) {
64  		Assert.notNull(fileManager, "File manager required");
65  		Assert.notNull(pathResolver, "Path resolver required");
66  		Assert.notNull(metadataService, "Metadata service required");
67  		Assert.notNull(projectOperations, "Project operations required");
68  
69  		this.fileManager = fileManager;
70  		this.pathResolver = pathResolver;
71  		this.metadataService = metadataService;
72  		this.projectOperations = projectOperations;
73  		this.surfOperations = new SurfOperations(fileManager, pathResolver, metadataService, projectOperations);
74  	}
75  
76  	/**
77  	 * Checks if a new Surf content association can be added.
78  	 * @return true if a new Surf page can be added.
79  	 */
80  	public boolean isNewContentAssociationAvailable() {
81  		return surfOperations.isManageSurfAvailable();
82  	}
83  
84  	/**
85  	 * Creates a new Surf content association.
86  	 * @param id Association id.
87  	 * @param contentTypeName Content type.
88  	 * @param desName Page or component id.
89  	 * @param type Association type.
90  	 */
91  	public void createContentAssociation(String id, String contentTypeName, String desName,String type) {
92  
93  		Assert.hasText(contentTypeName, "Content type required");
94  		Assert.hasText(desName, "Page or component name required");
95  
96  		if ( id==null || id.equals("")) {
97  			if (contentTypeName.indexOf("}") != -1) {
98  				id = contentTypeName.substring(contentTypeName.indexOf("}")+1)+"-"+desName;
99  			} else {
100 				id = contentTypeName+"-"+desName;
101 			}
102 		}
103 
104 		String contentAssocXmlFilename = Constants.SURF_SITEDATA_CONTENT_ASSOC_CP+id+".xml";
105 
106 		if (fileManager.exists(pathResolver.getIdentifier(Path.SRC_MAIN_WEBAPP, contentAssocXmlFilename))) {
107 			//file exists, so nothing to do
108 			logger.warning("Content association "+contentAssocXmlFilename+" exists.");
109 			return;
110 		}
111 
112 		InputStream templateInstanceInputStream = TemplateUtils.getTemplate(getClass(), "content-association-template.xml");
113 		Document pom;
114 		try {
115 			pom = XmlUtils.getDocumentBuilder().parse(templateInstanceInputStream);
116 		} catch (Exception ex) {
117 			throw new IllegalStateException(ex);
118 		}
119 
120 		Element rootElement = (Element) pom.getFirstChild();
121 		XmlUtils.findFirstElementByName("source-id", rootElement).setTextContent(contentTypeName);
122 		XmlUtils.findFirstElementByName("dest-id", rootElement).setTextContent(desName);
123 		XmlUtils.findFirstElementByName("assoc-type", rootElement).setTextContent(type);
124 
125 		MutableFile mutableFile = fileManager.createFile(pathResolver.getIdentifier(Path.SRC_MAIN_WEBAPP, contentAssocXmlFilename));
126 		XmlUtils.writeXml(mutableFile.getOutputStream(), pom);
127 
128 		fileManager.scan();
129 
130 		try {
131 			templateInstanceInputStream.close();
132 		} catch (Exception ex) {
133 			throw new IllegalStateException(ex);
134 		}
135 	}
136 
137 }