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  
19  /**
20   * The plugin entrypoint which is used to generate dictionary files based on the contract
21   * @phase generate-sources
22   * @goal ksdictionarycreator
23   */
24  public class KSDictionaryCreatorMojo extends AbstractMojo {
25  
26      /**
27       * @parameter expression=true
28       **/
29      private boolean throwExceptionIfNotAllFilesProcessed;
30      /**
31       * @parameter
32       **/
33      private List<String> sourceDirs;
34      /**
35       * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/datadictionary"
36       */
37      private File outputDirectory;
38      /**
39       * @parameter expression=false
40       */
41      private boolean writeManual;
42      /**
43       * @parameter expression=true
44       */
45      private boolean writeGenerated;
46      /**
47       * @parameter
48       */
49      private List<String> classNames;
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 List<String> getClassNames() {
76          return classNames;
77      }
78  
79      public void setClassNames(List<String> classNames) {
80          this.classNames = classNames;
81      }
82  
83      public void setWriteManual(boolean writeManual) {
84          this.writeManual = writeManual;
85      }
86  
87      public void setWriteGenerated(boolean writeGenerated) {
88          this.writeGenerated = writeGenerated;
89      }
90  
91      public void setOutputDirectory(File htmlDirectory) {
92          this.outputDirectory = htmlDirectory;
93      }
94  
95      public void setSourceDirs(List<String> sourceDirs) {
96          this.sourceDirs = sourceDirs;
97      }
98  
99      private ServiceContractModel getModel() {
100         ServiceContractModel instance = new ServiceContractModelQDoxLoader(
101                 sourceDirs);
102         return new ServiceContractModelCache(instance);
103     }
104 
105     private boolean validate(ServiceContractModel model) {
106         Collection<String> errors = new ServiceContractModelValidator(model).validate();
107         if (errors.size() > 0) {
108             StringBuilder buf = new StringBuilder();
109             buf.append(errors.size()).append(" errors found while validating the data.");
110             return false;
111         }
112         return true;
113     }
114 
115     @Override
116     public void execute() throws MojoExecutionException {
117         getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
118         getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
119         ServiceContractModel model = this.getModel();
120         this.validate(model);
121 
122         Set<String> lowerClasses = new HashSet();
123         for (String className : classNames) {
124             lowerClasses.add(className.toLowerCase());
125         }
126 
127         String dictionaryDirectory = this.outputDirectory.toString();
128         for (XmlType xmlType : model.getXmlTypes()) {
129             if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
130                 lowerClasses.remove(xmlType.getName().toLowerCase());
131                 String xmlObject = xmlType.getName();
132                 KradDictionaryCreator writer =
133                         new KradDictionaryCreator(dictionaryDirectory,
134                         model,
135                         xmlObject,
136                         writeManual,
137                         writeGenerated);
138                 writer.write();
139             }
140         }
141         if (!lowerClasses.isEmpty()) {
142             StringBuilder buf = new StringBuilder();
143             buf.append(lowerClasses.size());
144             buf.append(" classes were not processed: ");
145             String comma = "";
146             for (String className : lowerClasses) {
147                 buf.append(comma);
148                 buf.append(className);
149                 comma = ", ";
150             }
151             if (throwExceptionIfNotAllFilesProcessed) {
152                 throw new MojoExecutionException(buf.toString());
153             }
154             else
155             {
156                 getLog ().info(buf);
157             }
158         }
159     }
160 }