View Javadoc

1   package org.kuali.student.datadictionary.mojo;
2   
3   import java.io.File;
4   import java.util.Collection;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Set;
8   
9   import org.apache.maven.plugin.AbstractMojo;
10  import org.apache.maven.plugin.MojoExecutionException;
11  
12  import org.kuali.student.contract.model.ServiceContractModel;
13  import org.kuali.student.contract.model.XmlType;
14  import org.kuali.student.contract.model.impl.ServiceContractModelCache;
15  import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
16  import org.kuali.student.datadictionary.util.KradDictionaryCreator;
17  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  /**
22   * The plugin entrypoint which is used to generate dictionary files based on the contract
23   * @phase generate-sources
24   * @goal ksdictionarycreator
25   */
26  public class KSDictionaryCreatorMojo extends AbstractMojo {
27  
28  	private static final Logger log = LoggerFactory.getLogger(KSDictionaryCreatorMojo.class);
29  	
30      /**
31       * @parameter expression=true
32       **/
33      private boolean throwExceptionIfNotAllFilesProcessed;
34      /**
35       * @parameter
36       **/
37      private List<String> sourceDirs;
38      /**
39       * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/datadictionary"
40       */
41      private File outputDirectory;
42      /**
43       * @parameter expression=false
44       */
45      private boolean writeManual;
46      /**
47       * @parameter expression=true
48       */
49      private boolean writeGenerated;
50      
51      public boolean isThrowExceptionIfNotAllFilesProcessed() {
52          return throwExceptionIfNotAllFilesProcessed;
53      }
54  
55      public void setThrowExceptionIfNotAllFilesProcessed(boolean throwExceptionIfNotAllFilesProcessed) {
56          this.throwExceptionIfNotAllFilesProcessed = throwExceptionIfNotAllFilesProcessed;
57      }
58  
59      public File getOutputDirectory() {
60          return outputDirectory;
61      }
62  
63      public List<String> getSourceDirs() {
64          return sourceDirs;
65      }
66  
67      public boolean isWriteManual() {
68          return writeManual;
69      }
70  
71      public boolean isWriteGenerated() {
72          return writeGenerated;
73      }
74  
75      public void setWriteManual(boolean writeManual) {
76          this.writeManual = writeManual;
77      }
78  
79      public void setWriteGenerated(boolean writeGenerated) {
80          this.writeGenerated = writeGenerated;
81      }
82  
83      public void setOutputDirectory(File htmlDirectory) {
84          this.outputDirectory = htmlDirectory;
85      }
86  
87      public void setSourceDirs(List<String> sourceDirs) {
88          this.sourceDirs = sourceDirs;
89      }
90  
91      private ServiceContractModel getModel() {
92          ServiceContractModel instance = new ServiceContractModelQDoxLoader(
93                  sourceDirs);
94          return new ServiceContractModelCache(instance);
95      }
96  
97      private boolean validate(ServiceContractModel model) {
98          Collection<String> errors = new ServiceContractModelValidator(model).validate();
99          if (errors.size() > 0) {
100             StringBuilder buf = new StringBuilder();
101             buf.append(errors.size()).append(" errors found while validating the data.");
102             return false;
103         }
104         return true;
105     }
106 
107     @Override
108     public void execute() throws MojoExecutionException {
109         getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
110         getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
111         ServiceContractModel model = this.getModel();
112         this.validate(model);
113 
114         // build the list of expected files types to generate the dictionary files for.
115         Set<String> lowerClasses = new HashSet<String>();
116         
117         for (XmlType type : model.getXmlTypes()) {
118 			lowerClasses.add(type.getName().toLowerCase());
119 		}
120 
121         String dictionaryDirectory = this.outputDirectory.toString();
122         for (XmlType xmlType : model.getXmlTypes()) {
123             if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
124                 lowerClasses.remove(xmlType.getName().toLowerCase());
125                 String xmlObject = xmlType.getName();
126                 KradDictionaryCreator writer =
127 				        new KradDictionaryCreator(dictionaryDirectory,
128 				        model,
129 				        xmlObject,
130 				        writeManual,
131 				        writeGenerated);
132                 try {
133 					
134 					writer.write();
135 				} catch (Exception e) {
136 					log.warn("Generate Failed for: " + xmlObject, e);
137 					writer.delete();
138 					
139 				}
140             }
141         }
142         if (!lowerClasses.isEmpty()) {
143             StringBuilder buf = new StringBuilder();
144             buf.append(lowerClasses.size());
145             buf.append(" classes were not processed: ");
146             String comma = "";
147             for (String className : lowerClasses) {
148                 buf.append(comma);
149                 buf.append(className);
150                 comma = ", ";
151             }
152             if (throwExceptionIfNotAllFilesProcessed) {
153                 throw new MojoExecutionException(buf.toString());
154             }
155             else
156             {
157                log.info(buf.toString());
158             }
159         }
160     }
161 }