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.contract.model.ServiceContractModel;
013    import org.kuali.student.contract.model.XmlType;
014    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
015    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
016    import org.kuali.student.datadictionary.util.KradDictionaryCreator;
017    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
018    
019    /**
020     * The plugin entrypoint which is used to generate dictionary files based on the contract
021     * @phase generate-sources
022     * @goal ksdictionarycreator
023     */
024    public class KSDictionaryCreatorMojo extends AbstractMojo {
025    
026        /**
027         * @parameter expression=true
028         **/
029        private boolean throwExceptionIfNotAllFilesProcessed;
030        /**
031         * @parameter
032         **/
033        private List<String> sourceDirs;
034        /**
035         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/datadictionary"
036         */
037        private File outputDirectory;
038        /**
039         * @parameter expression=false
040         */
041        private boolean writeManual;
042        /**
043         * @parameter expression=true
044         */
045        private boolean writeGenerated;
046        /**
047         * @parameter
048         */
049        private List<String> classNames;
050    
051        public boolean isThrowExceptionIfNotAllFilesProcessed() {
052            return throwExceptionIfNotAllFilesProcessed;
053        }
054    
055        public void setThrowExceptionIfNotAllFilesProcessed(boolean throwExceptionIfNotAllFilesProcessed) {
056            this.throwExceptionIfNotAllFilesProcessed = throwExceptionIfNotAllFilesProcessed;
057        }
058    
059        public File getOutputDirectory() {
060            return outputDirectory;
061        }
062    
063        public List<String> getSourceDirs() {
064            return sourceDirs;
065        }
066    
067        public boolean isWriteManual() {
068            return writeManual;
069        }
070    
071        public boolean isWriteGenerated() {
072            return writeGenerated;
073        }
074    
075        public List<String> getClassNames() {
076            return classNames;
077        }
078    
079        public void setClassNames(List<String> classNames) {
080            this.classNames = classNames;
081        }
082    
083        public void setWriteManual(boolean writeManual) {
084            this.writeManual = writeManual;
085        }
086    
087        public void setWriteGenerated(boolean writeGenerated) {
088            this.writeGenerated = writeGenerated;
089        }
090    
091        public void setOutputDirectory(File htmlDirectory) {
092            this.outputDirectory = htmlDirectory;
093        }
094    
095        public void setSourceDirs(List<String> sourceDirs) {
096            this.sourceDirs = sourceDirs;
097        }
098    
099        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    }