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