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    import org.slf4j.Logger;
019    import org.slf4j.LoggerFactory;
020    
021    /**
022     * The plugin entrypoint which is used to generate dictionary files based on the contract
023     * @phase generate-sources
024     * @goal ksdictionarycreator
025     */
026    public class KSDictionaryCreatorMojo extends AbstractMojo {
027    
028            private static final Logger log = LoggerFactory.getLogger(KSDictionaryCreatorMojo.class);
029            
030        /**
031         * @parameter expression=true
032         **/
033        private boolean throwExceptionIfNotAllFilesProcessed;
034        /**
035         * @parameter
036         **/
037        private List<String> sourceDirs;
038        /**
039         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources/datadictionary"
040         */
041        private File outputDirectory;
042        /**
043         * @parameter expression=false
044         */
045        private boolean writeManual;
046        /**
047         * @parameter expression=true
048         */
049        private boolean writeGenerated;
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 void setWriteManual(boolean writeManual) {
076            this.writeManual = writeManual;
077        }
078    
079        public void setWriteGenerated(boolean writeGenerated) {
080            this.writeGenerated = writeGenerated;
081        }
082    
083        public void setOutputDirectory(File htmlDirectory) {
084            this.outputDirectory = htmlDirectory;
085        }
086    
087        public void setSourceDirs(List<String> sourceDirs) {
088            this.sourceDirs = sourceDirs;
089        }
090    
091        private ServiceContractModel getModel() {
092            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
093                    sourceDirs);
094            return new ServiceContractModelCache(instance);
095        }
096    
097        private boolean validate(ServiceContractModel model) {
098            Collection<String> errors = new ServiceContractModelValidator(model).validate();
099            if (errors.size() > 0) {
100                StringBuilder buf = new StringBuilder();
101                buf.append(errors.size()).append(" errors found while validating the data.");
102                return false;
103            }
104            return true;
105        }
106    
107        @Override
108        public void execute() throws MojoExecutionException {
109            getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
110            getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
111            ServiceContractModel model = this.getModel();
112            this.validate(model);
113    
114            // build the list of expected files types to generate the dictionary files for.
115            Set<String> lowerClasses = new HashSet<String>();
116            
117            for (XmlType type : model.getXmlTypes()) {
118                            lowerClasses.add(type.getName().toLowerCase());
119                    }
120    
121            String dictionaryDirectory = this.outputDirectory.toString();
122            for (XmlType xmlType : model.getXmlTypes()) {
123                if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
124                    lowerClasses.remove(xmlType.getName().toLowerCase());
125                    String xmlObject = xmlType.getName();
126                    KradDictionaryCreator writer =
127                                            new KradDictionaryCreator(dictionaryDirectory,
128                                            model,
129                                            xmlObject,
130                                            writeManual,
131                                            writeGenerated);
132                    try {
133                                            
134                                            writer.write();
135                                    } catch (Exception e) {
136                                            log.warn("Generate Failed for: " + xmlObject, e);
137                                            writer.delete();
138                                            
139                                    }
140                }
141            }
142            if (!lowerClasses.isEmpty()) {
143                StringBuilder buf = new StringBuilder();
144                buf.append(lowerClasses.size());
145                buf.append(" classes were not processed: ");
146                String comma = "";
147                for (String className : lowerClasses) {
148                    buf.append(comma);
149                    buf.append(className);
150                    comma = ", ";
151                }
152                if (throwExceptionIfNotAllFilesProcessed) {
153                    throw new MojoExecutionException(buf.toString());
154                }
155                else
156                {
157                   log.info(buf.toString());
158                }
159            }
160        }
161    }