View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.datadictionary.mojo;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  
29  import org.kuali.student.common.mojo.AbstractKSMojo;
30  import org.kuali.student.contract.model.ServiceContractModel;
31  import org.kuali.student.contract.model.XmlType;
32  import org.kuali.student.contract.model.impl.ServiceContractModelCache;
33  import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
34  import org.kuali.student.datadictionary.util.KradDictionaryCreator;
35  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * The plugin entrypoint which is used to generate dictionary files based on the contract
41   * @phase generate-sources
42   * @goal ksdictionarycreator
43   */
44  public class KSDictionaryCreatorMojo extends AbstractKSMojo {
45  
46  	private static final Logger log = LoggerFactory.getLogger(KSDictionaryCreatorMojo.class);
47  	
48      /**
49       * @parameter default-value=true
50       **/
51      private boolean throwExceptionIfNotAllFilesProcessed;
52     
53      /**
54       * @parameter property="outputDirectory" default-value="${project.build.directory}/generated-sources/datadictionary"
55       */
56      private File outputDirectory;
57      /**
58       * @parameter default-value=false
59       */
60      private boolean writeManual;
61      /**
62       * @parameter default-value=true
63       */
64      private boolean writeGenerated;
65      
66      /**
67       * @parameter 
68       */
69      private Map<String, String>typeOverrides;
70      
71      public boolean isThrowExceptionIfNotAllFilesProcessed() {
72          return throwExceptionIfNotAllFilesProcessed;
73      }
74  
75      public void setThrowExceptionIfNotAllFilesProcessed(boolean throwExceptionIfNotAllFilesProcessed) {
76          this.throwExceptionIfNotAllFilesProcessed = throwExceptionIfNotAllFilesProcessed;
77      }
78  
79      public File getOutputDirectory() {
80          return outputDirectory;
81      }
82  
83  
84      public boolean isWriteManual() {
85          return writeManual;
86      }
87  
88      public boolean isWriteGenerated() {
89          return writeGenerated;
90      }
91  
92      public void setWriteManual(boolean writeManual) {
93          this.writeManual = writeManual;
94      }
95  
96      public void setWriteGenerated(boolean writeGenerated) {
97          this.writeGenerated = writeGenerated;
98      }
99  
100     public void setOutputDirectory(File htmlDirectory) {
101         this.outputDirectory = htmlDirectory;
102     }
103 
104     @Override
105     public void execute() throws MojoExecutionException {
106         getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
107         getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
108         ServiceContractModel model = this.getModel();
109         this.validate(model);
110 
111         // build the list of expected files types to generate the dictionary files for.
112         Set<String> lowerClasses = new HashSet<String>();
113         
114         for (XmlType type : model.getXmlTypes()) {
115         	
116         	String className = type.getName().toLowerCase();
117         	
118         	// skip non Info classes and r1 services
119         	if (!className.endsWith("info") || className.matches("\\.r1\\."))
120         		continue;
121         	
122         	// exclude things in packages that are not local
123         	if (this.localPackages.contains(type.getJavaPackage())) {
124         		lowerClasses.add(className);
125         	}
126 		}
127 
128         String dictionaryDirectory = this.outputDirectory.toString();
129         
130         
131         for (XmlType xmlType : model.getXmlTypes()) {
132             if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
133                 lowerClasses.remove(xmlType.getName().toLowerCase());
134                 String xmlObject = xmlType.getName();
135                 KradDictionaryCreator writer =
136 				        new KradDictionaryCreator(dictionaryDirectory,
137 				        model,
138 				        xmlObject,
139 				        writeManual,
140 				        writeGenerated, 
141 				        typeOverrides);
142                 try {
143 					
144 					writer.write();
145 				} catch (Exception e) {
146 					log.warn("Generate Failed for: " + xmlObject, e);
147 					writer.delete();
148 					
149 				}
150                 
151             }
152         }
153         if (!lowerClasses.isEmpty()) {
154             StringBuilder buf = new StringBuilder();
155             buf.append(lowerClasses.size());
156             buf.append(" classes were not processed: ");
157             String comma = "";
158             for (String className : lowerClasses) {
159                 buf.append(comma);
160                 buf.append(className);
161                 comma = ", ";
162             }
163             if (throwExceptionIfNotAllFilesProcessed) {
164                 throw new MojoExecutionException(buf.toString());
165             }
166             else
167             {
168                log.info(buf.toString());
169             }
170         }
171     }
172 }