View Javadoc

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