View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.writer.service;
17  
18  import java.io.Serializable;
19  import java.util.List;
20  
21  import org.kuali.student.contract.exception.DictionaryExecutionException;
22  import org.kuali.student.contract.model.MessageStructure;
23  import org.kuali.student.contract.model.ServiceContractModel;
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   * @author nwright
31   */
32  public class PureJavaInfcBeanWriter extends JavaClassWriter {
33  
34      private ServiceContractModel model;
35      private String directory;
36      private String rootPackage;
37      private String service;
38      private XmlType type;
39      private ModelFinder finder;
40  
41      public PureJavaInfcBeanWriter(ServiceContractModel model,
42              String directory,
43              String rootPackage,
44              String service,
45              XmlType type) {
46          super(directory, calcPackage(service, rootPackage), calcClassName(type.getName()));
47          this.model = model;
48          this.finder = new ModelFinder(model);
49          this.directory = directory;
50          this.rootPackage = rootPackage;
51          this.service = service;
52          this.type = type;
53      }
54  
55      public static String calcPackage(String service, String rootPackage) {
56          if (service.contains(",")) {
57              service = "common";
58          }
59          return PureJavaInfcServiceWriter.calcPackage(service, rootPackage);
60      }
61  
62      public static String calcClassName(String name) {
63          if (name.endsWith("Info")) {
64              name = name.substring(0, name.length() - "Info".length());
65          }
66          name = name + "Bean";
67          return GetterSetterNameCalculator.calcInitUpper(name);
68  
69      }
70  
71      /**
72       * Write out the entire file
73       * @param out
74       */
75      public void write() {
76          indentPrintln("public class " + calcClassName(type.getName()));
77          incrementIndent();
78          indentPrint(" implements "
79                  + PureJavaInfcInfcWriter.calcClassName(type.getName()));
80          importsAdd(PureJavaInfcInfcWriter.calcPackage(service, rootPackage)
81                  + "." + PureJavaInfcInfcWriter.calcClassName(type.getName()));
82          this.importsAdd(Serializable.class.getName());
83          indentPrintln(", Serializable");
84          openBrace();
85  
86          indentPrintln("");
87          indentPrintln("private static final long serialVersionUID = 1L;");
88  
89          List<MessageStructure> list =
90                  finder.findMessageStructures(type.getName());
91          if (list.size() == 0) {
92              throw new DictionaryExecutionException("xmlType " + type.getName()
93                      + " has no fields defined in the message structure tab");
94          }
95          for (MessageStructure ms : list) {
96              String realType = stripList(PureJavaInfcInfcWriter.calcClassName(ms.getType()));
97              String fieldType = this.calcFieldTypeToUse(ms.getType(), realType);
98              String name = initLower(ms.getShortName());
99              indentPrintln("");
100             indentPrintln("private " + fieldType + " " + name + ";");
101             indentPrintln("");
102             indentPrintln("/**");
103             indentPrintWrappedComment("Set " + ms.getName());
104             indentPrintln("*");
105             indentPrintln("* Type: " + ms.getType());
106             indentPrintln("*");
107             indentPrintWrappedComment(ms.getDescription());
108             indentPrintln("*/");
109             indentPrintln("@Override");
110             indentPrintln("public void " + calcSetter(ms) + "(" + fieldType + " "
111                     + name + ")");
112             openBrace();
113             indentPrintln("this." + name + " = " + name + ";");
114             closeBrace();
115 
116             indentPrintln("");
117             indentPrintln("/**");
118             indentPrintWrappedComment("Get " + ms.getName());
119             indentPrintln("*");
120             indentPrintln("* Type: " + ms.getType());
121             indentPrintln("*");
122             indentPrintWrappedComment(ms.getDescription());
123             indentPrintln("*/");
124             indentPrintln("@Override");
125             indentPrintln("public " + fieldType + " " + calcGetter(ms) + "()");
126             openBrace();
127             indentPrintln("return this." + name + ";");
128             closeBrace();
129             indentPrint("");
130 
131             indentPrint("");
132         }
133         indentPrintln("");
134         closeBrace();
135 
136         this.writeJavaClassAndImportsOutToFile();
137         this.getOut().close();
138     }
139 
140     private String stripList(String str) {
141         return GetterSetterNameCalculator.stripList(str);
142     }
143 
144     private String initLower(String str) {
145         if (str == null) {
146             return null;
147         }
148         if (str.isEmpty()) {
149             return str;
150         }
151         if (str.length() == 1) {
152             return str.toLowerCase();
153         }
154         return str.substring(0, 1).toLowerCase() + str.substring(1);
155     }
156 
157     private String calcGetter(MessageStructure ms) {
158         return new GetterSetterNameCalculator(ms, this, model).calcGetter();
159     }
160 
161     private String calcSetter(MessageStructure ms) {
162         return new GetterSetterNameCalculator(ms, this, model).calcSetter();
163     }
164 
165     private String calcFieldTypeToUse(String type, String realType) {
166         XmlType t = finder.findXmlType(this.stripList(type));
167         String pckName = calcPackage(t.getService(), rootPackage);
168         return MessageStructureTypeCalculator.calculate(this, model, type, realType,
169                 pckName);
170     }
171 }