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.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   * @author nwright
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          super(directory, calcPackage(servKey, rootPackage), calcClassName(servKey));
47          this.model = model;
48          this.finder = new ModelFinder(model);
49          this.directory = directory;
50          this.rootPackage = rootPackage;
51          this.servKey = servKey;
52          this.methods = methods;
53      }
54  
55      public static String calcPackage(String servKey, String rootPackage) {
56          String pack = rootPackage + "." + servKey.toLowerCase() + ".";
57  //  StringBuffer buf = new StringBuffer (service.getVersion ().length ());
58  //  for (int i = 0; i < service.getVersion ().length (); i ++)
59  //  {
60  //   char c = service.getVersion ().charAt (i);
61  //   c = Character.toLowerCase (c);
62  //   if (Character.isLetter (c))
63  //   {
64  //    buf.append (c);
65  //    continue;
66  //   }
67  //   if (Character.isDigit (c))
68  //   {
69  //    buf.append (c);
70  //   }
71  //  }
72  //  pack = pack + buf.toString ();
73          pack = pack + "api";
74          return pack;
75      }
76  
77      public static String calcClassName(String servKey) {
78          return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceInfc");
79      }
80  
81      /**
82       * Write out the entire file
83       * @param out
84       */
85      public void write() {
86          indentPrintln("public interface " + calcClassName(servKey));
87          openBrace();
88  
89          for (ServiceMethod method : methods) {
90              indentPrintln("");
91              indentPrintln("/**");
92              indentPrintWrappedComment(method.getDescription());
93              indentPrintln("* ");
94              for (ServiceMethodParameter param : method.getParameters()) {
95                  indentPrintWrappedComment("@param " + param.getName() + " - "
96                          + param.getType() + " - "
97                          + param.getDescription());
98              }
99              indentPrintWrappedComment("@return " + method.getReturnValue().
100                     getDescription());
101             indentPrintln("*/");
102             String type = method.getReturnValue().getType();
103             String realType = stripList(PureJavaInfcInfcWriter.calcClassName(type));
104             indentPrint("public " + calcType(type, realType) + " " + method.getName()
105                     + "(");
106             // now do parameters
107             String comma = "";
108             for (ServiceMethodParameter param : method.getParameters()) {
109                 type = param.getType();
110                 realType = stripList(PureJavaInfcInfcWriter.calcClassName(type));
111                 print(comma);
112                 print(calcType(type, realType));
113                 print(" ");
114                 print(param.getName());
115                 comma = ", ";
116             }
117             println(")");
118             // now do exceptions
119             comma = "throws ";
120             incrementIndent();
121             for (ServiceMethodError error : method.getErrors()) {
122                 indentPrint(comma);
123                 String exceptionClassName = calcExceptionClassName(error);
124                 String exceptionPackageName = this.calcExceptionPackageName(error);
125                 println(exceptionClassName);
126                 this.importsAdd(exceptionPackageName + "." + exceptionClassName);
127                 comma = "      ,";
128             }
129             decrementIndent();
130             indentPrintln(";");
131 
132         }
133 
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 calcExceptionClassName(ServiceMethodError error) {
145         if (error.getClassName() == null) {
146             return ServiceExceptionWriter.calcClassName(error.getType());
147         }
148         return error.getClassName();
149     }
150 
151     private String calcExceptionPackageName(ServiceMethodError error) {
152         if (error.getClassName() == null) {
153             return ServiceExceptionWriter.calcPackage(rootPackage);
154         }
155         return error.getPackageName();
156     }
157 
158     private String calcType(String type, String realType) {
159         XmlType t = finder.findXmlType(this.stripList(type));
160         String pckName = PureJavaInfcInfcWriter.calcPackage(t.getService(), rootPackage);
161         return MessageStructureTypeCalculator.calculate(this, model, type, realType,
162                 pckName);
163     }
164 }