001    package org.kuali.student.contract.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.contract.model.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
028         **/
029        private List<String> sourceDirs;
030        /**
031         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/classes"
032         */
033        private File outputDirectory;
034        /**
035         * @parameter expression=false
036         */
037        private boolean writeManual;
038        /**
039         * @parameter expression=true
040         */
041        private boolean writeGenerated;
042        /**
043         * @parameter
044         */
045        private List<String> classNames;
046    
047        public File getOutputDirectory() {
048            return outputDirectory;
049        }
050    
051        public List<String> getSourceDirs() {
052            return sourceDirs;
053        }
054    
055        public boolean isWriteManual() {
056            return writeManual;
057        }
058    
059        public boolean isWriteGenerated() {
060            return writeGenerated;
061        }
062    
063        public List<String> getClassNames() {
064            return classNames;
065        }
066    
067        public void setClassNames(List<String> classNames) {
068            this.classNames = classNames;
069        }
070    
071        public void setWriteManual(boolean writeManual) {
072            this.writeManual = writeManual;
073        }
074    
075        public void setWriteGenerated(boolean writeGenerated) {
076            this.writeGenerated = writeGenerated;
077        }
078    
079        public void setOutputDirectory(File htmlDirectory) {
080            this.outputDirectory = htmlDirectory;
081        }
082    
083        public void setSourceDirs(List<String> sourceDirs) {
084            this.sourceDirs = sourceDirs;
085        }
086    
087        private ServiceContractModel getModel() {
088            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
089                    sourceDirs);
090            return new ServiceContractModelCache(instance);
091        }
092    
093        private boolean validate(ServiceContractModel model) {
094            Collection<String> errors = new ServiceContractModelValidator(model).validate();
095            if (errors.size() > 0) {
096                StringBuilder buf = new StringBuilder();
097                buf.append(errors.size()).append(" errors found while validating the data.");
098                return false;
099            }
100            return true;
101        }
102    
103        @Override
104        public void execute() throws MojoExecutionException {
105            getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
106            getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
107            ServiceContractModel model = this.getModel();
108            this.validate(model);
109    
110            Set<String> lowerClasses = new HashSet();
111            for (String className : classNames) {
112                lowerClasses.add(className.toLowerCase());
113            }
114    
115            String dictionaryDirectory = this.outputDirectory.toString();
116            for (XmlType xmlType : model.getXmlTypes()) {
117                if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
118                    lowerClasses.remove(xmlType.getName().toLowerCase());
119                    String xmlObject = xmlType.getName();
120                    KradDictionaryCreator writer =
121                            new KradDictionaryCreator(dictionaryDirectory,
122                            model,
123                            xmlObject,
124                            writeManual,
125                            writeGenerated);
126                    writer.write();
127                }
128            }
129            if (!lowerClasses.isEmpty()) {
130                StringBuilder buf = new StringBuilder();
131                buf.append(lowerClasses.size());
132                buf.append(" classes were not processed: ");
133                String comma = "";
134                for (String className : lowerClasses) {
135                    buf.append(comma);
136                    buf.append(className);
137                    comma = ", ";
138                }
139                throw new MojoExecutionException(buf.toString());
140            }
141        }
142    }