| 1 | |
package org.kuali.student.contract.mojo; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.util.Collection; |
| 5 | |
import java.util.HashSet; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Set; |
| 8 | |
|
| 9 | |
import org.apache.maven.plugin.AbstractMojo; |
| 10 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 11 | |
|
| 12 | |
import org.kuali.student.contract.model.ServiceContractModel; |
| 13 | |
import org.kuali.student.contract.model.XmlType; |
| 14 | |
import org.kuali.student.contract.model.impl.ServiceContractModelCache; |
| 15 | |
import org.kuali.student.contract.model.impl.ServiceContractModelQDoxLoader; |
| 16 | |
import org.kuali.student.contract.model.util.KradDictionaryCreator; |
| 17 | |
import org.kuali.student.contract.model.validation.ServiceContractModelValidator; |
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 0 | public class KSDictionaryCreatorMojo extends AbstractMojo { |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
private List<String> sourceDirs; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
private File outputDirectory; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
private boolean writeManual; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
private boolean writeGenerated; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private List<String> classNames; |
| 46 | |
|
| 47 | |
public File getOutputDirectory() { |
| 48 | 0 | return outputDirectory; |
| 49 | |
} |
| 50 | |
|
| 51 | |
public List<String> getSourceDirs() { |
| 52 | 0 | return sourceDirs; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public boolean isWriteManual() { |
| 56 | 0 | return writeManual; |
| 57 | |
} |
| 58 | |
|
| 59 | |
public boolean isWriteGenerated() { |
| 60 | 0 | return writeGenerated; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public List<String> getClassNames() { |
| 64 | 0 | return classNames; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public void setClassNames(List<String> classNames) { |
| 68 | 0 | this.classNames = classNames; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public void setWriteManual(boolean writeManual) { |
| 72 | 0 | this.writeManual = writeManual; |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
public void setWriteGenerated(boolean writeGenerated) { |
| 76 | 0 | this.writeGenerated = writeGenerated; |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
public void setOutputDirectory(File htmlDirectory) { |
| 80 | 0 | this.outputDirectory = htmlDirectory; |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
public void setSourceDirs(List<String> sourceDirs) { |
| 84 | 0 | this.sourceDirs = sourceDirs; |
| 85 | 0 | } |
| 86 | |
|
| 87 | |
private ServiceContractModel getModel() { |
| 88 | 0 | ServiceContractModel instance = new ServiceContractModelQDoxLoader( |
| 89 | |
sourceDirs); |
| 90 | 0 | return new ServiceContractModelCache(instance); |
| 91 | |
} |
| 92 | |
|
| 93 | |
private boolean validate(ServiceContractModel model) { |
| 94 | 0 | Collection<String> errors = new ServiceContractModelValidator(model).validate(); |
| 95 | 0 | if (errors.size() > 0) { |
| 96 | 0 | StringBuilder buf = new StringBuilder(); |
| 97 | 0 | buf.append(errors.size()).append(" errors found while validating the data."); |
| 98 | 0 | return false; |
| 99 | |
} |
| 100 | 0 | return true; |
| 101 | |
} |
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public void execute() throws MojoExecutionException { |
| 105 | 0 | getLog().info("generating ks-XXX-dictionary.xml files=" + this.writeManual); |
| 106 | 0 | getLog().info("generating ks-XXX-dictionary-generated.xml files=" + this.writeGenerated); |
| 107 | 0 | ServiceContractModel model = this.getModel(); |
| 108 | 0 | this.validate(model); |
| 109 | |
|
| 110 | 0 | Set<String> lowerClasses = new HashSet(); |
| 111 | 0 | for (String className : classNames) { |
| 112 | 0 | lowerClasses.add(className.toLowerCase()); |
| 113 | |
} |
| 114 | |
|
| 115 | 0 | String dictionaryDirectory = this.outputDirectory.toString(); |
| 116 | 0 | for (XmlType xmlType : model.getXmlTypes()) { |
| 117 | 0 | if (lowerClasses.contains(xmlType.getName().toLowerCase())) { |
| 118 | 0 | lowerClasses.remove(xmlType.getName().toLowerCase()); |
| 119 | 0 | String xmlObject = xmlType.getName(); |
| 120 | 0 | KradDictionaryCreator writer = |
| 121 | |
new KradDictionaryCreator(dictionaryDirectory, |
| 122 | |
model, |
| 123 | |
xmlObject, |
| 124 | |
writeManual, |
| 125 | |
writeGenerated); |
| 126 | 0 | writer.write(); |
| 127 | 0 | } |
| 128 | |
} |
| 129 | 0 | if (!lowerClasses.isEmpty()) { |
| 130 | 0 | StringBuilder buf = new StringBuilder(); |
| 131 | 0 | buf.append(lowerClasses.size()); |
| 132 | 0 | buf.append(" classes were not processed: "); |
| 133 | 0 | String comma = ""; |
| 134 | 0 | for (String className : lowerClasses) { |
| 135 | 0 | buf.append(comma); |
| 136 | 0 | buf.append(className); |
| 137 | 0 | comma = ", "; |
| 138 | |
} |
| 139 | 0 | throw new MojoExecutionException(buf.toString()); |
| 140 | |
} |
| 141 | 0 | } |
| 142 | |
} |