001    package org.kuali.student.datadictionary.mojo;
002    
003    import java.io.File;
004    import java.util.Collection;
005    import java.util.List;
006    
007    import org.apache.maven.plugin.AbstractMojo;
008    import org.apache.maven.plugin.MojoExecutionException;
009    
010    import org.kuali.student.contract.model.ServiceContractModel;
011    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
012    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
013    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
014    import org.kuali.student.contract.writer.service.EachMethodServiceWriter;
015    
016    /**
017     * The plugin entrypoint which is used to generate a java interface file for each
018     * service method in the source files based on the contract definition
019     * @phase generate-sources
020     * @goal kseachmethodservicegenerator
021     */
022    public class EachMethodServiceGeneratorrMojo extends AbstractMojo {
023    
024        /**
025         * @parameter
026         **/
027        private List<String> sourceDirs;
028        /**
029         * @parameter expression="${outputDirectory}" default-value="${project.build.directory}/generated-sources"
030         */
031        private File outputDirectory;
032    
033        public File getOutputDirectory() {
034            return outputDirectory;
035        }
036    
037        public List<String> getSourceDirs() {
038            return sourceDirs;
039        }
040    
041        public void setOutputDirectory(File htmlDirectory) {
042            this.outputDirectory = htmlDirectory;
043        }
044    
045        public void setSourceDirs(List<String> sourceDirs) {
046            this.sourceDirs = sourceDirs;
047        }
048        /**
049         * @parameter expression="${rootPackage}" default-value="org.kuali.student.enrollment"
050         */
051        private String rootPackage;
052    
053        public String getRootPackage() {
054            return rootPackage;
055        }
056    
057        public void setRootPackage(String rootPackage) {
058            this.rootPackage = rootPackage;
059        }
060    
061        private ServiceContractModel getModel() {
062            ServiceContractModel instance = new ServiceContractModelQDoxLoader(
063                    sourceDirs);
064            return new ServiceContractModelCache(instance);
065        }
066    
067        private boolean validate(ServiceContractModel model) {
068            Collection<String> errors = new ServiceContractModelValidator(model).validate();
069            if (errors.size() > 0) {
070                StringBuilder buf = new StringBuilder();
071                buf.append(errors.size()).append(" errors found while validating the data.");
072                return false;
073            }
074            return true;
075        }
076    
077        @Override
078        public void execute() throws MojoExecutionException {
079            getLog().info("generating separate java interface files for each method in the service contracts");
080            ServiceContractModel model = this.getModel();
081            this.validate(model);
082            if (!outputDirectory.exists()) {
083                if (!outputDirectory.mkdirs()) {
084    //                throw new MojoExecutionException("Could not create directory "
085                    throw new IllegalArgumentException("Could not create directory "
086                            + this.outputDirectory.getPath());
087                }
088            }
089            String targetDirectory = this.outputDirectory.toString();
090            EachMethodServiceWriter instance =
091                    new EachMethodServiceWriter(model,
092                    targetDirectory,
093                    rootPackage,
094                    null);
095            instance.write();
096    
097        }
098    }