001 /* 002 * Copyright 2009 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.osedu.org/licenses/ECL-2.0 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.contract.writer.service; 017 018 import java.io.Serializable; 019 import java.util.List; 020 021 import org.kuali.student.contract.model.MessageStructure; 022 import org.kuali.student.contract.model.ServiceContractModel; 023 import org.kuali.student.contract.model.XmlType; 024 import org.kuali.student.contract.model.util.ModelFinder; 025 import org.kuali.student.contract.model.validation.DictionaryValidationException; 026 import org.kuali.student.contract.writer.JavaClassWriter; 027 028 /** 029 * 030 * @author nwright 031 */ 032 public class PureJavaInfcBeanWriter extends JavaClassWriter { 033 034 private ServiceContractModel model; 035 private String directory; 036 private String rootPackage; 037 private String service; 038 private XmlType type; 039 private ModelFinder finder; 040 041 public PureJavaInfcBeanWriter(ServiceContractModel model, 042 String directory, 043 String rootPackage, 044 String service, 045 XmlType type) { 046 super(directory, calcPackage(service, rootPackage), calcClassName(type.getName())); 047 this.model = model; 048 this.finder = new ModelFinder(model); 049 this.directory = directory; 050 this.rootPackage = rootPackage; 051 this.service = service; 052 this.type = type; 053 } 054 055 public static String calcPackage(String service, String rootPackage) { 056 if (service.contains(",")) { 057 service = "common"; 058 } 059 return PureJavaInfcServiceWriter.calcPackage(service, rootPackage); 060 } 061 062 public static String calcClassName(String name) { 063 if (name.endsWith("Info")) { 064 name = name.substring(0, name.length() - "Info".length()); 065 } 066 name = name + "Bean"; 067 return GetterSetterNameCalculator.calcInitUpper(name); 068 069 } 070 071 /** 072 * Write out the entire file 073 * @param out 074 */ 075 public void write() { 076 indentPrintln("public class " + calcClassName(type.getName())); 077 incrementIndent(); 078 indentPrint(" implements " 079 + PureJavaInfcInfcWriter.calcClassName(type.getName())); 080 importsAdd(PureJavaInfcInfcWriter.calcPackage(service, rootPackage) 081 + "." + PureJavaInfcInfcWriter.calcClassName(type.getName())); 082 this.importsAdd(Serializable.class.getName()); 083 indentPrintln(", Serializable"); 084 openBrace(); 085 086 indentPrintln(""); 087 indentPrintln("private static final long serialVersionUID = 1L;"); 088 089 List<MessageStructure> list = 090 finder.findMessageStructures(type.getName()); 091 // if (list.size() == 0) { 092 // throw new DictionaryExecutionException("xmlType " + type.getName() 093 // + " has no fields defined in the message structure tab"); 094 // } 095 for (MessageStructure ms : list) { 096 String realType = stripList(PureJavaInfcInfcWriter.calcClassName(ms.getType())); 097 String fieldType = null; 098 if (ms.getId().equals("RegistrationGroupTemplateInfo.activityOfferingCombinations")) { 099 continue; 100 } 101 try { 102 fieldType = this.calcFieldTypeToUse(ms.getType(), realType); 103 } catch (DictionaryValidationException ex) { 104 throw new DictionaryValidationException (ms.getId(), ex); 105 } 106 String name = initLower(ms.getShortName()); 107 indentPrintln(""); 108 indentPrintln("private " + fieldType + " " + name + ";"); 109 indentPrintln(""); 110 indentPrintln("/**"); 111 indentPrintWrappedComment("Set " + ms.getName()); 112 indentPrintln("*"); 113 indentPrintln("* Type: " + ms.getType()); 114 indentPrintln("*"); 115 indentPrintWrappedComment(ms.getDescription()); 116 indentPrintln("*/"); 117 indentPrintln("@Override"); 118 indentPrintln("public void " + calcSetter(ms) + "(" + fieldType + " " 119 + name + ")"); 120 openBrace(); 121 indentPrintln("this." + name + " = " + name + ";"); 122 closeBrace(); 123 124 indentPrintln(""); 125 indentPrintln("/**"); 126 indentPrintWrappedComment("Get " + ms.getName()); 127 indentPrintln("*"); 128 indentPrintln("* Type: " + ms.getType()); 129 indentPrintln("*"); 130 indentPrintWrappedComment(ms.getDescription()); 131 indentPrintln("*/"); 132 indentPrintln("@Override"); 133 indentPrintln("public " + fieldType + " " + calcGetter(ms) + "()"); 134 openBrace(); 135 indentPrintln("return this." + name + ";"); 136 closeBrace(); 137 indentPrint(""); 138 139 indentPrint(""); 140 } 141 indentPrintln(""); 142 closeBrace(); 143 144 this.writeJavaClassAndImportsOutToFile(); 145 this.getOut().close(); 146 } 147 148 private String stripList(String str) { 149 return GetterSetterNameCalculator.stripList(str); 150 } 151 152 private String initLower(String str) { 153 if (str == null) { 154 return null; 155 } 156 if (str.isEmpty()) { 157 return str; 158 } 159 if (str.length() == 1) { 160 return str.toLowerCase(); 161 } 162 return str.substring(0, 1).toLowerCase() + str.substring(1); 163 } 164 165 private String calcGetter(MessageStructure ms) { 166 return new GetterSetterNameCalculator(ms, this, model).calcGetter(); 167 } 168 169 private String calcSetter(MessageStructure ms) { 170 return new GetterSetterNameCalculator(ms, this, model).calcSetter(); 171 } 172 173 private String calcFieldTypeToUse(String type, String realType) { 174 XmlType t = finder.findXmlType(this.stripList(type)); 175 String pckName = calcPackage(t.getService(), rootPackage); 176 return MessageStructureTypeCalculator.calculate(this, model, type, realType, 177 pckName); 178 } 179 }