001    package org.kuali.student.datadictionary.mojo;
002    
003    import java.io.File;
004    import java.util.Collection;
005    import java.util.HashSet;
006    import java.util.List;
007    import java.util.Set;
008    
009    import org.apache.maven.plugin.AbstractMojo;
010    import org.apache.maven.plugin.MojoExecutionException;
011    
012    import org.kuali.student.common.mojo.AbstractKSMojo;
013    import org.kuali.student.contract.model.ServiceContractModel;
014    import org.kuali.student.contract.model.XmlType;
015    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
016    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
017    import org.kuali.student.datadictionary.util.KradDictionaryCreator;
018    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
019    import org.slf4j.Logger;
020    import org.slf4j.LoggerFactory;
021    
022    /**
023     * The plugin entrypoint which is used to generate dictionary files based on the contract
024     * @phase generate-sources
025     * @goal ksdictionarycreator
026     */
027    public class KSDictionaryCreatorMojo extends AbstractKSMojo {
028    
029            private static final Logger log = LoggerFactory.getLogger(KSDictionaryCreatorMojo.class);
030            
031        /**
032         * @parameter expression=true
033         **/
034        private boolean throwExceptionIfNotAllFilesProcessed;
035       
036        /**
037         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/datadictionary"
038         */
039        private File outputDirectory;
040        /**
041         * @parameter expression=false
042         */
043        private boolean writeManual;
044        /**
045         * @parameter expression=true
046         */
047        private boolean writeGenerated;
048        
049        public boolean isThrowExceptionIfNotAllFilesProcessed() {
050            return throwExceptionIfNotAllFilesProcessed;
051        }
052    
053        public void setThrowExceptionIfNotAllFilesProcessed(boolean throwExceptionIfNotAllFilesProcessed) {
054            this.throwExceptionIfNotAllFilesProcessed = throwExceptionIfNotAllFilesProcessed;
055        }
056    
057        public File getOutputDirectory() {
058            return outputDirectory;
059        }
060    
061    
062        public boolean isWriteManual() {
063            return writeManual;
064        }
065    
066        public boolean isWriteGenerated() {
067            return writeGenerated;
068        }
069    
070        public void setWriteManual(boolean writeManual) {
071            this.writeManual = writeManual;
072        }
073    
074        public void setWriteGenerated(boolean writeGenerated) {
075            this.writeGenerated = writeGenerated;
076        }
077    
078        public void setOutputDirectory(File htmlDirectory) {
079            this.outputDirectory = htmlDirectory;
080        }
081    
082        @Override
083        public void execute() throws MojoExecutionException {
084            getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
085            getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
086            ServiceContractModel model = this.getModel();
087            this.validate(model);
088    
089            // build the list of expected files types to generate the dictionary files for.
090            Set<String> lowerClasses = new HashSet<String>();
091            
092            for (XmlType type : model.getXmlTypes()) {
093                    
094                    String className = type.getName().toLowerCase();
095                    
096                    // skip non Info classes and r1 services
097                    if (!className.endsWith("info") || className.matches("\\.r1\\."))
098                            continue;
099                    
100                            lowerClasses.add(className);
101                    }
102    
103            String dictionaryDirectory = this.outputDirectory.toString();
104            
105            
106            for (XmlType xmlType : model.getXmlTypes()) {
107                if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
108                    lowerClasses.remove(xmlType.getName().toLowerCase());
109                    String xmlObject = xmlType.getName();
110                    KradDictionaryCreator writer =
111                                            new KradDictionaryCreator(dictionaryDirectory,
112                                            model,
113                                            xmlObject,
114                                            writeManual,
115                                            writeGenerated);
116                    try {
117                                            
118                                            writer.write();
119                                    } catch (Exception e) {
120                                            log.warn("Generate Failed for: " + xmlObject, e);
121                                            writer.delete();
122                                            
123                                    }
124                    
125                }
126            }
127            if (!lowerClasses.isEmpty()) {
128                StringBuilder buf = new StringBuilder();
129                buf.append(lowerClasses.size());
130                buf.append(" classes were not processed: ");
131                String comma = "";
132                for (String className : lowerClasses) {
133                    buf.append(comma);
134                    buf.append(className);
135                    comma = ", ";
136                }
137                if (throwExceptionIfNotAllFilesProcessed) {
138                    throw new MojoExecutionException(buf.toString());
139                }
140                else
141                {
142                   log.info(buf.toString());
143                }
144            }
145        }
146    }