View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.common.impex.schema.execute;
17  
18  import java.io.File;
19  
20  import org.kuali.common.impex.model.Schema;
21  import org.kuali.common.impex.model.util.ModelUtils;
22  import org.kuali.common.impex.schema.DefaultDumpSchemaService;
23  import org.kuali.common.impex.schema.DumpSchemaService;
24  import org.kuali.common.util.Assert;
25  import org.kuali.common.util.FileSystemUtils;
26  import org.kuali.common.util.Project;
27  import org.kuali.common.util.ProjectUtils;
28  import org.kuali.common.util.StringFilter;
29  import org.kuali.common.util.execute.Executable;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public class ProjectSchemaExportExecutable implements Executable {
34  
35  	private static final Logger logger = LoggerFactory.getLogger(ProjectSchemaExportExecutable.class);
36  
37  	public static final boolean DEFAULT_EXECUTION_SKIP = false;
38  	public static final DumpSchemaService DEFAULT_EXPORT_SCHEMA_SERVICE = new DefaultDumpSchemaService();
39  	public static final String DEFAULT_SCHEMA_FILENAME = "schema.xml";
40  
41  	boolean skip = DEFAULT_EXECUTION_SKIP;
42  	DumpSchemaService service = DEFAULT_EXPORT_SCHEMA_SERVICE;
43  	String schemaFilename = DEFAULT_SCHEMA_FILENAME;
44  
45  	Project project;
46  	Schema schema;
47  	StringFilter nameFilter;
48  	File stagingDir;
49  	File basedir;
50  
51  	@Override
52  	public void execute() {
53  
54  		// Might have nothing to do
55  		if (skip) {
56  			return;
57  		}
58  
59  		// Make sure we are configured correctly
60  		Assert.notNull(service, "service is null");
61  		Assert.notNull(schema, "schema is null");
62  		Assert.notNull(project, "project is null");
63  		Assert.notNull(stagingDir, "stagingDir is null");
64  		Assert.notNull(basedir, "basedir is null");
65  
66  		// Clone the existing schema
67  		Schema clone = new Schema(schema);
68  
69  		// Filter out model objects not relevant to this export
70  		ModelUtils.filter(clone, nameFilter);
71  
72  		// The location of the schema file is always based on groupId + artifactId
73  		File schemaFile = ProjectUtils.getResourceFile(stagingDir, project, schemaFilename);
74  
75  		// Log the name of the file we are creating
76  		logger.info("Creating - [{}]", FileSystemUtils.getRelativePathQuietly(basedir, schemaFile));
77  
78  		// Persist the cloned and filtered schema to disk as XML
79  		service.dumpSchema(clone, schemaFile);
80  
81  	}
82  
83  	public boolean isSkip() {
84  		return skip;
85  	}
86  
87  	public void setSkip(boolean skip) {
88  		this.skip = skip;
89  	}
90  
91  	public DumpSchemaService getService() {
92  		return service;
93  	}
94  
95  	public void setService(DumpSchemaService service) {
96  		this.service = service;
97  	}
98  
99  	public StringFilter getNameFilter() {
100 		return nameFilter;
101 	}
102 
103 	public void setNameFilter(StringFilter nameFilter) {
104 		this.nameFilter = nameFilter;
105 	}
106 
107 	public Project getProject() {
108 		return project;
109 	}
110 
111 	public void setProject(Project project) {
112 		this.project = project;
113 	}
114 
115 	public Schema getSchema() {
116 		return schema;
117 	}
118 
119 	public void setSchema(Schema schema) {
120 		this.schema = schema;
121 	}
122 
123 	public File getStagingDir() {
124 		return stagingDir;
125 	}
126 
127 	public void setStagingDir(File stagingDir) {
128 		this.stagingDir = stagingDir;
129 	}
130 
131 	public File getBasedir() {
132 		return basedir;
133 	}
134 
135 	public void setBasedir(File basedir) {
136 		this.basedir = basedir;
137 	}
138 
139 }