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.site;
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.ProjectMetadata;
32  import org.springframework.roo.project.ProjectOperations;
33  import org.springframework.roo.support.lifecycle.ScopeDevelopment;
34  import org.springframework.roo.support.util.Assert;
35  import org.springframework.roo.support.util.TemplateUtils;
36  import org.springframework.roo.support.util.XmlUtils;
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  
40  /**
41   * Provides Surf Site operations.
42   *
43   * @author Yong Qu
44   * @since 1.0
45   */
46  @ScopeDevelopment
47  public class SiteOperations {
48  
49  	Logger logger = Logger.getLogger(SiteOperations.class.getName());
50  
51  	private FileManager fileManager;
52  	private PathResolver pathResolver;
53  	private MetadataService metadataService;
54  
55  	private SurfOperations surfOperations;
56  
57  	/**
58  	 * @param fileManager
59  	 * @param pathResolver
60  	 * @param metadataService
61  	 * @param projectOperations
62  	 */
63  	public SiteOperations(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.surfOperations = new SurfOperations(fileManager, pathResolver, metadataService, projectOperations);
73  	}
74  
75  	/**
76  	 * Checks if a new site configuration can be created.
77  	 * @return true if a new site configuration can be created.
78  	 */
79  	public boolean isSiteOperationAvailable() {
80  		return surfOperations.isManageSurfAvailable();
81  	}
82  
83  	/**
84  	 * Creates a new site configuration.
85  	 * @param siteName Site name
86  	 * @param rootPageId Root page id.
87  	 */
88  	public void newSite(String siteName,String rootPageId) {
89  		Assert.notNull(rootPageId, "Root page id required");
90  		if (siteName == null || siteName.equals("")) {
91  			ProjectMetadata projectMetadata = (ProjectMetadata) metadataService.get(ProjectMetadata.getProjectIdentifier());
92  			siteName = projectMetadata.getProjectName();
93  		}
94  		// Check if the site configuration file exists.
95  		String siteXmlFilename = Constants.SURF_SITEDATA_SITE_CP+"default.site.configuration.xml";
96  
97  		if (!fileManager.exists(pathResolver.getIdentifier(Path.SRC_MAIN_WEBAPP, siteXmlFilename))) {
98  			InputStream templateInputStream = TemplateUtils.getTemplate(getClass(), "site-template.xml");
99  			Document pom;
100 			try {
101 				pom = XmlUtils.getDocumentBuilder().parse(templateInputStream);
102 			} catch (Exception ex) {
103 				throw new IllegalStateException(ex);
104 			}
105 
106 			Element rootElement = (Element) pom.getFirstChild();
107 			XmlUtils.findFirstElementByName("root-page", rootElement).setTextContent(rootPageId);
108 			XmlUtils.findFirstElementByName("title", rootElement).setTextContent(siteName);
109 			XmlUtils.findFirstElementByName("description", rootElement).setTextContent(siteName);
110 
111 			MutableFile mutableFile = fileManager.createFile(pathResolver.getIdentifier(Path.SRC_MAIN_WEBAPP, siteXmlFilename));
112 			XmlUtils.writeXml(mutableFile.getOutputStream(), pom);
113 			fileManager.scan();
114 
115 			try {
116 				templateInputStream.close();
117 			} catch (Exception ex) {
118 				throw new IllegalStateException(ex);
119 			}
120 		}
121 	}
122 
123 }