View Javadoc

1   /**
2    * Copyright 2004-2013 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.opensource.org/licenses/ecl2.php
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.model.MessageStructure;
22  import org.kuali.student.contract.model.ServiceContractModel;
23  import org.kuali.student.contract.model.XmlType;
24  import org.kuali.student.contract.model.util.ModelFinder;
25  import org.kuali.student.contract.model.validation.DictionaryValidationException;
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 = null;
98              if (ms.getId().equals("RegistrationGroupTemplateInfo.activityOfferingCombinations")) {
99                  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 }