001    /**
002     * 
003     */
004    package org.kuali.student.common.mojo;
005    
006    import java.util.ArrayList;
007    import java.util.Collection;
008    import java.util.LinkedHashSet;
009    import java.util.List;
010    import java.util.Set;
011    
012    import org.apache.maven.plugin.AbstractMojo;
013    import org.apache.maven.project.MavenProject;
014    import org.kuali.student.contract.model.ServiceContractModel;
015    import org.kuali.student.contract.model.impl.ServiceContractModelCache;
016    import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader;
017    import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
018    
019    /**
020     * 
021     * The basic Kuali Student Mojo that defines things like the source lookup path.
022     * 
023     * @author ocleirig
024     * 
025     */
026    public abstract class AbstractKSMojo extends AbstractMojo {
027    
028            /**
029             * @parameter
030             **/
031            private List<String> sourceDirs;
032    
033            public AbstractKSMojo() {
034                    super();
035            }
036    
037            public List<String> getSourceDirs() {
038                    return sourceDirs;
039            }
040    
041            public void setSourceDirs(List<String> sourceDirs) {
042                    this.sourceDirs = sourceDirs;
043            }
044    
045            protected final ServiceContractModel getModel() {
046    
047                    Set<String> modelSourceDirectories = new LinkedHashSet<String>();
048    
049                    MavenProject project = (MavenProject) getPluginContext().get("project");
050                    /*
051                     * Default to the source directory that the plugin is run from.
052                     */
053    
054                    if (sourceDirs == null) {
055                            modelSourceDirectories.add(project.getBuild().getSourceDirectory());
056                    } else {
057                                    modelSourceDirectories.addAll(sourceDirs);
058                    }
059                    if (modelSourceDirectories.size() == 0)
060                            throw new RuntimeException("No Source Directories are defined");
061    
062                    ServiceContractModel instance = new ServiceContractModelQDoxLoader(
063                                    new ArrayList<String>(modelSourceDirectories));
064                    return new ServiceContractModelCache(instance);
065            }
066    
067            protected final boolean validate(ServiceContractModel model) {
068                    Collection<String> errors = new ServiceContractModelValidator(model)
069                                    .validate();
070                    if (errors.size() > 0) {
071    
072                            StringBuilder buf = new StringBuilder();
073                            buf.append(errors.size()).append(
074                                            " errors found while validating the data.");
075                            int cnt = 0;
076                            for (String msg : errors) {
077                                    cnt++;
078                                    buf.append("\n");
079                                    buf.append("*error*").append(cnt).append(":").append(msg);
080                            }
081    
082                            buf.append(errors.size()).append(
083                                            " errors found while validating the data.");
084                            return false;
085                    }
086                    return true;
087            }
088    
089    }