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                            lowerClasses.add(type.getName().toLowerCase());
094                    }
095    
096            String dictionaryDirectory = this.outputDirectory.toString();
097            for (XmlType xmlType : model.getXmlTypes()) {
098                if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
099                    lowerClasses.remove(xmlType.getName().toLowerCase());
100                    String xmlObject = xmlType.getName();
101                    KradDictionaryCreator writer =
102                                            new KradDictionaryCreator(dictionaryDirectory,
103                                            model,
104                                            xmlObject,
105                                            writeManual,
106                                            writeGenerated);
107                    try {
108                                            
109                                            writer.write();
110                                    } catch (Exception e) {
111                                            log.warn("Generate Failed for: " + xmlObject, e);
112                                            writer.delete();
113                                            
114                                    }
115                    
116                }
117            }
118            if (!lowerClasses.isEmpty()) {
119                StringBuilder buf = new StringBuilder();
120                buf.append(lowerClasses.size());
121                buf.append(" classes were not processed: ");
122                String comma = "";
123                for (String className : lowerClasses) {
124                    buf.append(comma);
125                    buf.append(className);
126                    comma = ", ";
127                }
128                if (throwExceptionIfNotAllFilesProcessed) {
129                    throw new MojoExecutionException(buf.toString());
130                }
131                else
132                {
133                   log.info(buf.toString());
134                }
135            }
136        }
137    }