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.util.List;
19  
20  import org.kuali.student.contract.exception.DictionaryExecutionException;
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.writer.JavaClassWriter;
26  
27  /**
28   *
29   * @author nwright
30   */
31  public class PureJavaInfcInfcWriter extends JavaClassWriter {
32  
33      private ServiceContractModel model;
34      private ModelFinder finder;
35      private String directory;
36      private String rootPackage;
37      private String service;
38      private XmlType xmlType;
39  
40      public PureJavaInfcInfcWriter(ServiceContractModel model,
41              String directory,
42              String rootPackage,
43              String service,
44              XmlType xmlType) {
45          super(directory, calcPackage(service, rootPackage), calcClassName(
46                  xmlType.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.xmlType = xmlType;
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              name = name + "Infc";
66          } else if (name.endsWith("InfoList")) {
67              name = name.substring(0, name.length() - "InfoList".length());
68              name = name + "InfcList";
69          }
70          return GetterSetterNameCalculator.calcInitUpper(name);
71      }
72  
73      /**
74       * Write out the entire file
75       * @param out
76       */
77      public void write() {
78          indentPrintln("public interface " + calcClassName(xmlType.getName()));
79          openBrace();
80  
81          List<MessageStructure> list = finder.findMessageStructures(xmlType.getName());
82          if (list.size() == 0) {
83              throw new DictionaryExecutionException(
84                      "xmlType " + xmlType.getName()
85                      + " has no fields defined in the message structure tab");
86          }
87          for (MessageStructure ms : list) {
88              String realType = stripList(calcClassName(ms.getType()));
89              String type = this.calcFieldTypeToUse(ms.getType(), realType);
90              indentPrintln("");
91              indentPrintln("/**");
92              indentPrintWrappedComment("Set " + ms.getName());
93              indentPrintln("*");
94              indentPrintln("* Type: " + ms.getType());
95              indentPrintln("*");
96              indentPrintWrappedComment(ms.getDescription());
97              indentPrintln("*/");
98              indentPrintln("public void " + calcSetter(ms) + "(" + type + " " + initLower(
99                      ms.getShortName()) + ");");
100 
101 
102             indentPrintln("");
103             indentPrintln("/**");
104             indentPrintWrappedComment("Get " + ms.getName());
105             indentPrintln("*");
106             indentPrintln("* Type: " + ms.getType());
107             indentPrintln("*");
108             indentPrintWrappedComment(ms.getDescription());
109             indentPrintln("*/");
110             indentPrintln("public " + type + " " + calcGetter(ms) + "();");
111             indentPrintln("");
112 
113             indentPrintln("");
114         }
115         indentPrintln("");
116         closeBrace();
117 
118         this.writeJavaClassAndImportsOutToFile();
119         this.getOut().close();
120     }
121 
122     private String stripList(String str) {
123         return GetterSetterNameCalculator.stripList(str);
124     }
125 
126     private String initLower(String str) {
127         return GetterSetterNameCalculator.calcInitLower(str);
128     }
129 
130     private String calcGetter(MessageStructure ms) {
131         return new GetterSetterNameCalculator(ms, this, model).calcGetter();
132     }
133 
134     private String calcSetter(MessageStructure ms) {
135         return new GetterSetterNameCalculator(ms, this, model).calcSetter();
136     }
137 
138     private String calcFieldTypeToUse(String type, String realType) {
139         XmlType t = finder.findXmlType(this.stripList(type));
140         String pckName = calcPackage(t.getService(), rootPackage);
141         return MessageStructureTypeCalculator.calculate(this, model, type, realType,
142                 pckName);
143     }
144 }