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  package org.springframework.roo.addon.surf.report;
19  
20  import java.io.InputStream;
21  import java.util.Hashtable;
22  import java.util.SortedSet;
23  import java.util.logging.Logger;
24  
25  import org.springframework.roo.addon.surf.SurfOperations;
26  import org.springframework.roo.addon.surf.component.ComponentOperations;
27  import org.springframework.roo.addon.surf.page.PageOperations;
28  import org.springframework.roo.addon.surf.region.RegionConfig;
29  import org.springframework.roo.addon.surf.template.TemplateOperations;
30  import org.springframework.roo.addon.surf.util.ComponentConfigUtils;
31  import org.springframework.roo.file.monitor.event.FileDetails;
32  import org.springframework.roo.metadata.MetadataService;
33  import org.springframework.roo.process.manager.FileManager;
34  import org.springframework.roo.project.PathResolver;
35  import org.springframework.roo.project.ProjectOperations;
36  import org.springframework.roo.support.lifecycle.ScopeDevelopment;
37  import org.springframework.roo.support.util.Assert;
38  import org.springframework.roo.support.util.XmlUtils;
39  import org.w3c.dom.Document;
40  
41  /**
42   * Provides Surf report related operations.
43   * @author Yong Qu
44   * @since 1.0
45   */
46  @ScopeDevelopment
47  public class ReportOperations {
48  
49  	Logger logger = Logger.getLogger(PageOperations.class.getName());
50  
51  	private FileManager fileManager;
52  	private PathResolver pathResolver;
53  	private MetadataService metadataService;
54  	private ProjectOperations projectOperations;
55  	private PageOperations pageOperations;
56  
57  	private SurfOperations surfOperations;
58  	/**
59  	 * @param fileManager
60  	 * @param pathResolver
61  	 * @param metadataService
62  	 * @param projectOperations
63  	 */
64  	public ReportOperations(FileManager fileManager, PathResolver pathResolver,
65  			MetadataService metadataService, ProjectOperations projectOperations) {
66  		Assert.notNull(fileManager, "File manager required");
67  		Assert.notNull(pathResolver, "Path resolver required");
68  		Assert.notNull(metadataService, "Metadata service required");
69  		Assert.notNull(projectOperations, "Project operations required");
70  		this.fileManager = fileManager;
71  		this.pathResolver = pathResolver;
72  		this.metadataService = metadataService;
73  		this.projectOperations = projectOperations;
74  		this.pageOperations = new PageOperations(fileManager, pathResolver, metadataService, projectOperations);
75  		this.surfOperations = new SurfOperations(fileManager, pathResolver, metadataService, projectOperations);
76  	}
77  
78  	/**
79  	 * Checks if a new Surf report can be generated.
80  	 * @return true if a new Surf report can be generated.
81  	 */
82  	public boolean isReportOperationAvailable() {
83  		return surfOperations.isManageSurfAvailable();
84  	}
85  
86  	/**
87  	 * Returns detailed configurations of a specified Surf page.
88  	 * @param pageId Page Id.
89  	 * @return Detailed configurations.
90  	 */
91  	public String showSurfPageConfig(String pageId) {
92  
93  		FileDetails pageXmlFile = pageOperations.findSurfPageById(pageId);
94  
95  		StringBuffer pageConfig = new StringBuffer();
96  
97  		if (pageXmlFile == null) {
98  			logger.warning("Page "+pageId+" doesn't exist");
99  			return pageConfig.toString();
100 		}
101 
102 		String newSeparateLine = "-----------------------------------------------------------\n";
103 		pageConfig.append(newSeparateLine);
104 		pageConfig.append("Report on Page "+pageId).append("\n\n");
105 		pageConfig.append(newSeparateLine);
106 		pageConfig.append("Basic Information").append("\n\n");
107 
108 		pageConfig.append("Id: ").append(pageId).append("\n");
109 		pageConfig.append("Name: ").append(pageId).append("\n");
110 		pageConfig.append("Path: ").append(pageXmlFile.getCanonicalPath().substring(pageOperations.getPageXmlRootPath().length())).append("\n");
111 
112 		//Find page template instance name.
113 		String instanceName = pageOperations.getTemplateInstance(pageId);
114 		pageConfig.append("Instance: ").append(instanceName).append("\n");
115 
116 		//Find page template path.
117 		TemplateOperations templateOperations = new TemplateOperations(fileManager, pathResolver, metadataService, projectOperations);
118 		String templateName = templateOperations.getTemplatePath(instanceName);
119 
120 		if ( templateName == null || templateName.equals("") ) {
121 			logger.warning("Template "+templateName+" doesn't exist");
122 			return pageConfig.toString();
123 		}
124 		pageConfig.append("Template: ").append(templateName).append("\n");
125 
126 		InputStream pageInputStream = fileManager.getInputStream(pageXmlFile.getCanonicalPath());
127 		Document pageDoc;
128 		try {
129 			pageDoc = XmlUtils.getDocumentBuilder().parse(pageInputStream);
130 		} catch (Exception ex) {
131 			throw new IllegalStateException(ex);
132 		}
133 		pageConfig.append(newSeparateLine);
134 		pageConfig.append("Page Scoped Components").append("\n\n").append(ComponentConfigUtils.componentsToString(pageDoc));
135 
136 		FileDetails templateInstanceXmlFile=templateOperations.findSurfTemplateInstanceByName(instanceName);
137 		if ( templateInstanceXmlFile == null) {
138 			logger.warning("Template instance "+instanceName+" doesn't exist");
139 			return pageConfig.toString();
140 		}
141 		InputStream templateInstanceInputStream = fileManager.getInputStream(templateInstanceXmlFile.getCanonicalPath());
142 		Document templateDoc;
143 		try {
144 			templateDoc = XmlUtils.getDocumentBuilder().parse(templateInstanceInputStream);
145 		} catch (Exception ex) {
146 			throw new IllegalStateException(ex);
147 		}
148 
149 		pageConfig.append("\n").append(newSeparateLine);
150 		pageConfig.append("Template Scoped Components").append("\n\n").append(ComponentConfigUtils.componentsToString(templateDoc));
151 
152 		pageConfig.append("\n").append(newSeparateLine);
153 		pageConfig.append("Other Components").append("\n\n");
154 
155 		// Inspect the template and list all components.
156 		Hashtable<String,RegionConfig> regionConfigList = templateOperations.listSurfTemplateRegions(templateName);
157 
158 		ComponentOperations componentOperations = new ComponentOperations(fileManager, pathResolver, metadataService, projectOperations);
159 		SortedSet<String> componentList = componentOperations.listSurfComponents(null,null,null);
160 
161 		for (RegionConfig regionConfig : regionConfigList.values()){
162 			String componentInstanceName = componentOperations.getComponentInstanceName(instanceName, templateName,regionConfig.getId(), regionConfig.getScope());
163 			if (regionConfig.getScope().equals("page")) {
164 				if (ComponentConfigUtils.getComponentConfigByRegionId(pageDoc, regionConfig.getId())== null) {
165 					if ( componentList.contains(componentInstanceName)) {
166 						pageConfig.append(regionConfig.toString()+" Component:"+componentInstanceName+" Url:").append(componentOperations.getComponentUrl(componentInstanceName)).append("\n");
167 					} else {
168 						pageConfig.append(regionConfig.toString()+" Configued:false").append("\n");
169 					}
170 				}
171 			} else if (regionConfig.getScope().equals("template")) {
172 				if (ComponentConfigUtils.getComponentConfigByRegionId(templateDoc, regionConfig.getId())== null) {
173 					if ( componentList.contains(componentInstanceName)) {
174 						pageConfig.append(regionConfig.toString()+" Component:"+componentInstanceName+" Url:").append(componentOperations.getComponentUrl(componentInstanceName)).append("\n");
175 					} else {
176 						pageConfig.append(regionConfig.toString()+" Configued:false").append("\n");
177 					}
178 				}
179 			} else if (regionConfig.getScope().equals("global")) {
180 				if ( componentList.contains(componentInstanceName)) {
181 					pageConfig.append(regionConfig.toString()+" Component:"+componentInstanceName+" Url:").append(componentOperations.getComponentUrl(componentInstanceName)).append("\n");
182 				} else {
183 					pageConfig.append(regionConfig.toString()+" Configued:false").append("\n");
184 				}
185 			} else {
186 				pageConfig.append(regionConfig.toString()+" Unsupported Scope Type.").append("\n");
187 			}
188 		}
189 		// Inspect page associations.
190 		pageConfig.append(newSeparateLine);
191 		pageConfig.append("Page Associations").append("\n\n");
192 		for (String assocPage : pageOperations.listSurfPageAssociations(pageId)) {
193 			pageConfig.append(assocPage).append("\n");
194 		}
195 		pageConfig.append(newSeparateLine);
196 		return pageConfig.toString().trim();
197 	}
198 
199 }