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  
19  import org.kuali.student.contract.model.ServiceContractModel;
20  import org.kuali.student.contract.model.ServiceMethod;
21  import org.kuali.student.contract.model.ServiceMethodError;
22  import org.kuali.student.contract.model.ServiceMethodParameter;
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 EachMethodServiceWriterForOneMethod extends JavaClassWriter {
32  
33      private ServiceContractModel model;
34      private ModelFinder finder;
35      private String directory;
36      private String rootPackage;
37      private String servKey;
38      private ServiceMethod method;
39  
40      public EachMethodServiceWriterForOneMethod(ServiceContractModel model,
41              String directory,
42              String rootPackage,
43              String servKey,
44              ServiceMethod method) {
45          super(directory, calcPackage(servKey, rootPackage), calcClassName(servKey, method));
46          this.model = model;
47          this.finder = new ModelFinder(model);
48          this.directory = directory;
49          this.rootPackage = rootPackage;
50          this.servKey = servKey;
51          this.method = method;
52      }
53  
54      public static String calcPackage(String servKey, String rootPackage) {
55          String pack = rootPackage + "." + servKey.toLowerCase() + ".";
56  //  StringBuffer buf = new StringBuffer (service.getVersion ().length ());
57  //  for (int i = 0; i < service.getVersion ().length (); i ++)
58  //  {
59  //   char c = service.getVersion ().charAt (i);
60  //   c = Character.toLowerCase (c);
61  //   if (Character.isLetter (c))
62  //   {
63  //    buf.append (c);
64  //    continue;
65  //   }
66  //   if (Character.isDigit (c))
67  //   {
68  //    buf.append (c);
69  //   }
70  //  }
71  //  pack = pack + buf.toString ();
72          pack = pack + "service.methods";
73          return pack;
74      }
75      
76      public static String initUpper (String str) {
77          return GetterSetterNameCalculator.calcInitUpper(str);
78      }
79  
80      public static String calcClassName(String servKey, ServiceMethod method) {
81  //        initUpper (servKey) + "Service" + 
82          return initUpper(method.getName() + "ServiceMethod");
83      }
84  
85      /**
86       * Write out the entire file
87       * @param out
88       */
89      public void write() {
90          indentPrintln("public interface " + calcClassName(servKey, method));
91          openBrace();
92  
93  
94          indentPrintln("");
95          indentPrintln("/**");
96          indentPrintWrappedComment(method.getDescription());
97          indentPrintln("* ");
98          for (ServiceMethodParameter param : method.getParameters()) {
99              indentPrintWrappedComment("@param " + param.getName() + " - "
100                     + param.getType() + " - "
101                     + param.getDescription());
102         }
103         indentPrintWrappedComment("@return " + method.getReturnValue().
104                 getDescription());
105         indentPrintln("*/");
106         String type = method.getReturnValue().getType();
107         String realType = stripList(type);
108         indentPrint("public " + calcType(type, realType) + " " + method.getName()
109                 + "(");
110         // now do parameters
111         String comma = "";
112         for (ServiceMethodParameter param : method.getParameters()) {
113             type = param.getType();
114             realType = stripList(type);
115             print(comma);
116             print(calcType(type, realType));
117             print(" ");
118             print(param.getName());
119             comma = ", ";
120         }
121         println(")");
122         // now do exceptions
123         comma = "throws ";
124         incrementIndent();
125         for (ServiceMethodError error : method.getErrors()) {
126             indentPrint(comma);
127             String exceptionClassName = calcExceptionClassName(error);
128             String exceptionPackageName = this.calcExceptionPackageName(error);
129             println(exceptionClassName);
130             this.importsAdd(exceptionPackageName + "." + exceptionClassName);
131             comma = "      ,";
132         }
133         decrementIndent();
134         indentPrintln(";");
135 
136 
137         closeBrace();
138 
139         this.writeJavaClassAndImportsOutToFile();
140         this.getOut().close();
141     }
142 
143     private String stripList(String str) {
144         return GetterSetterNameCalculator.stripList(str);
145     }
146 
147     private String calcExceptionClassName(ServiceMethodError error) {
148         if (error.getClassName() == null) {
149             return ServiceExceptionWriter.calcClassName(error.getType());
150         }
151         return error.getClassName();
152     }
153 
154     private String calcExceptionPackageName(ServiceMethodError error) {
155         if (error.getClassName() == null) {
156             return ServiceExceptionWriter.calcPackage(rootPackage);
157         }
158         return error.getPackageName();
159     }
160 
161     private String calcType(String type, String realType) {
162         XmlType t = finder.findXmlType(this.stripList(type));
163         String pckName = t.getJavaPackage();
164         return MessageStructureTypeCalculator.calculate(this, model, type, realType,
165                 pckName);
166     }
167 }