001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.student.datadictionary.mojo;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashSet;
022    import java.util.List;
023    import java.util.Map;
024    import java.util.Set;
025    
026    import org.apache.maven.plugin.AbstractMojo;
027    import org.apache.maven.plugin.MojoExecutionException;
028    
029    import org.kuali.student.common.mojo.AbstractKSMojo;
030    import org.kuali.student.contract.model.ServiceContractModel;
031    import org.kuali.student.contract.model.XmlType;
032    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
033    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
034    import org.kuali.student.datadictionary.util.KradDictionaryCreator;
035    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
036    import org.slf4j.Logger;
037    import org.slf4j.LoggerFactory;
038    
039    /**
040     * The plugin entrypoint which is used to generate dictionary files based on the contract
041     * @phase generate-sources
042     * @goal ksdictionarycreator
043     */
044    public class KSDictionaryCreatorMojo extends AbstractKSMojo {
045    
046            private static final Logger log = LoggerFactory.getLogger(KSDictionaryCreatorMojo.class);
047            
048        /**
049         * @parameter default-value=true
050         **/
051        private boolean throwExceptionIfNotAllFilesProcessed;
052       
053        /**
054         * @parameter property="outputDirectory" default-value="${project.build.directory}/generated-sources/datadictionary"
055         */
056        private File outputDirectory;
057        /**
058         * @parameter default-value=false
059         */
060        private boolean writeManual;
061        /**
062         * @parameter default-value=true
063         */
064        private boolean writeGenerated;
065        
066        /**
067         * @parameter 
068         */
069        private Map<String, String>typeOverrides;
070        
071        public boolean isThrowExceptionIfNotAllFilesProcessed() {
072            return throwExceptionIfNotAllFilesProcessed;
073        }
074    
075        public void setThrowExceptionIfNotAllFilesProcessed(boolean throwExceptionIfNotAllFilesProcessed) {
076            this.throwExceptionIfNotAllFilesProcessed = throwExceptionIfNotAllFilesProcessed;
077        }
078    
079        public File getOutputDirectory() {
080            return outputDirectory;
081        }
082    
083    
084        public boolean isWriteManual() {
085            return writeManual;
086        }
087    
088        public boolean isWriteGenerated() {
089            return writeGenerated;
090        }
091    
092        public void setWriteManual(boolean writeManual) {
093            this.writeManual = writeManual;
094        }
095    
096        public void setWriteGenerated(boolean writeGenerated) {
097            this.writeGenerated = writeGenerated;
098        }
099    
100        public void setOutputDirectory(File htmlDirectory) {
101            this.outputDirectory = htmlDirectory;
102        }
103    
104        @Override
105        public void execute() throws MojoExecutionException {
106            getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual);
107            getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated);
108            ServiceContractModel model = this.getModel();
109            this.validate(model);
110    
111            // build the list of expected files types to generate the dictionary files for.
112            Set<String> lowerClasses = new HashSet<String>();
113            
114            for (XmlType type : model.getXmlTypes()) {
115                    
116                    String className = type.getName().toLowerCase();
117                    
118                    // skip non Info classes and r1 services
119                    if (!className.endsWith("info") || className.matches("\\.r1\\."))
120                            continue;
121                    
122                    // exclude things in packages that are not local
123                    if (this.localPackages.contains(type.getJavaPackage())) {
124                            lowerClasses.add(className);
125                    }
126                    }
127    
128            String dictionaryDirectory = this.outputDirectory.toString();
129            
130            
131            for (XmlType xmlType : model.getXmlTypes()) {
132                if (lowerClasses.contains(xmlType.getName().toLowerCase())) {
133                    lowerClasses.remove(xmlType.getName().toLowerCase());
134                    String xmlObject = xmlType.getName();
135                    KradDictionaryCreator writer =
136                                            new KradDictionaryCreator(dictionaryDirectory,
137                                            model,
138                                            xmlObject,
139                                            writeManual,
140                                            writeGenerated, 
141                                            typeOverrides);
142                    try {
143                                            
144                                            writer.write();
145                                    } catch (Exception e) {
146                                            log.warn("Generate Failed for: " + xmlObject, e);
147                                            writer.delete();
148                                            
149                                    }
150                    
151                }
152            }
153            if (!lowerClasses.isEmpty()) {
154                StringBuilder buf = new StringBuilder();
155                buf.append(lowerClasses.size());
156                buf.append(" classes were not processed: ");
157                String comma = "";
158                for (String className : lowerClasses) {
159                    buf.append(comma);
160                    buf.append(className);
161                    comma = ", ";
162                }
163                if (throwExceptionIfNotAllFilesProcessed) {
164                    throw new MojoExecutionException(buf.toString());
165                }
166                else
167                {
168                   log.info(buf.toString());
169                }
170            }
171        }
172    }