| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.contract.writer.service; |
| 17 | |
|
| 18 | |
import java.util.List; |
| 19 | |
|
| 20 | |
import org.kuali.student.contract.model.ServiceContractModel; |
| 21 | |
import org.kuali.student.contract.model.ServiceMethod; |
| 22 | |
import org.kuali.student.contract.model.ServiceMethodError; |
| 23 | |
import org.kuali.student.contract.model.ServiceMethodParameter; |
| 24 | |
import org.kuali.student.contract.model.XmlType; |
| 25 | |
import org.kuali.student.contract.model.util.ModelFinder; |
| 26 | |
import org.kuali.student.contract.writer.JavaClassWriter; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class PureJavaInfcServiceWriter extends JavaClassWriter { |
| 33 | |
|
| 34 | |
private ServiceContractModel model; |
| 35 | |
private ModelFinder finder; |
| 36 | |
private String directory; |
| 37 | |
private String rootPackage; |
| 38 | |
private String servKey; |
| 39 | |
private List<ServiceMethod> methods; |
| 40 | |
|
| 41 | |
public PureJavaInfcServiceWriter(ServiceContractModel model, |
| 42 | |
String directory, |
| 43 | |
String rootPackage, |
| 44 | |
String servKey, |
| 45 | |
List<ServiceMethod> methods) { |
| 46 | 0 | super(directory, calcPackage(servKey, rootPackage), calcClassName(servKey)); |
| 47 | 0 | this.model = model; |
| 48 | 0 | this.finder = new ModelFinder(model); |
| 49 | 0 | this.directory = directory; |
| 50 | 0 | this.rootPackage = rootPackage; |
| 51 | 0 | this.servKey = servKey; |
| 52 | 0 | this.methods = methods; |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
public static String calcPackage(String servKey, String rootPackage) { |
| 56 | 0 | String pack = rootPackage + "." + servKey.toLowerCase() + "."; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 0 | pack = pack + "api"; |
| 74 | 0 | return pack; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public static String calcClassName(String servKey) { |
| 78 | 0 | return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceInfc"); |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
public void write() { |
| 86 | 0 | indentPrintln("public interface " + calcClassName(servKey)); |
| 87 | 0 | openBrace(); |
| 88 | |
|
| 89 | 0 | for (ServiceMethod method : methods) { |
| 90 | 0 | indentPrintln(""); |
| 91 | 0 | indentPrintln("/**"); |
| 92 | 0 | indentPrintWrappedComment(method.getDescription()); |
| 93 | 0 | indentPrintln("* "); |
| 94 | 0 | for (ServiceMethodParameter param : method.getParameters()) { |
| 95 | 0 | indentPrintWrappedComment("@param " + param.getName() + " - " |
| 96 | |
+ param.getType() + " - " |
| 97 | |
+ param.getDescription()); |
| 98 | |
} |
| 99 | 0 | indentPrintWrappedComment("@return " + method.getReturnValue(). |
| 100 | |
getDescription()); |
| 101 | 0 | indentPrintln("*/"); |
| 102 | 0 | String type = method.getReturnValue().getType(); |
| 103 | 0 | String realType = stripList(PureJavaInfcInfcWriter.calcClassName(type)); |
| 104 | 0 | indentPrint("public " + calcType(type, realType) + " " + method.getName() |
| 105 | |
+ "("); |
| 106 | |
|
| 107 | 0 | String comma = ""; |
| 108 | 0 | for (ServiceMethodParameter param : method.getParameters()) { |
| 109 | 0 | type = param.getType(); |
| 110 | 0 | realType = stripList(PureJavaInfcInfcWriter.calcClassName(type)); |
| 111 | 0 | print(comma); |
| 112 | 0 | print(calcType(type, realType)); |
| 113 | 0 | print(" "); |
| 114 | 0 | print(param.getName()); |
| 115 | 0 | comma = ", "; |
| 116 | |
} |
| 117 | 0 | println(")"); |
| 118 | |
|
| 119 | 0 | comma = "throws "; |
| 120 | 0 | incrementIndent(); |
| 121 | 0 | for (ServiceMethodError error : method.getErrors()) { |
| 122 | 0 | indentPrint(comma); |
| 123 | 0 | String exceptionClassName = calcExceptionClassName(error); |
| 124 | 0 | String exceptionPackageName = this.calcExceptionPackageName(error); |
| 125 | 0 | println(exceptionClassName); |
| 126 | 0 | this.importsAdd(exceptionPackageName + "." + exceptionClassName); |
| 127 | 0 | comma = " ,"; |
| 128 | 0 | } |
| 129 | 0 | decrementIndent(); |
| 130 | 0 | indentPrintln(";"); |
| 131 | |
|
| 132 | 0 | } |
| 133 | |
|
| 134 | 0 | closeBrace(); |
| 135 | |
|
| 136 | 0 | this.writeJavaClassAndImportsOutToFile(); |
| 137 | 0 | this.getOut().close(); |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
private String stripList(String str) { |
| 141 | 0 | return GetterSetterNameCalculator.stripList(str); |
| 142 | |
} |
| 143 | |
|
| 144 | |
private String calcExceptionClassName(ServiceMethodError error) { |
| 145 | 0 | if (error.getClassName() == null) { |
| 146 | 0 | return ServiceExceptionWriter.calcClassName(error.getType()); |
| 147 | |
} |
| 148 | 0 | return error.getClassName(); |
| 149 | |
} |
| 150 | |
|
| 151 | |
private String calcExceptionPackageName(ServiceMethodError error) { |
| 152 | 0 | if (error.getClassName() == null) { |
| 153 | 0 | return ServiceExceptionWriter.calcPackage(rootPackage); |
| 154 | |
} |
| 155 | 0 | return error.getPackageName(); |
| 156 | |
} |
| 157 | |
|
| 158 | |
private String calcType(String type, String realType) { |
| 159 | 0 | XmlType t = finder.findXmlType(this.stripList(type)); |
| 160 | 0 | String pckName = PureJavaInfcInfcWriter.calcPackage(t.getService(), rootPackage); |
| 161 | 0 | return MessageStructureTypeCalculator.calculate(this, model, type, realType, |
| 162 | |
pckName); |
| 163 | |
} |
| 164 | |
} |