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.admin.ui.mojo;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import javax.xml.namespace.QName;
23  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24  import org.kuali.student.contract.model.MessageStructure;
25  import org.kuali.student.contract.model.Service;
26  
27  import org.kuali.student.contract.model.ServiceContractModel;
28  import org.kuali.student.contract.model.ServiceMethod;
29  import org.kuali.student.contract.model.XmlType;
30  import org.kuali.student.contract.model.util.ModelFinder;
31  import org.kuali.student.contract.writer.JavaClassWriter;
32  import org.kuali.student.contract.writer.service.GetterSetterNameCalculator;
33  
34  /**
35   *
36   * @author nwright
37   */
38  public class AdminUiInquirableWriter extends JavaClassWriter {
39  
40      private ServiceContractModel model;
41      private ModelFinder finder;
42      private String directory;
43      private String rootPackage;
44      private String servKey;
45      private Service service;
46      private XmlType xmlType;
47      private List<ServiceMethod> methods;
48  
49      public AdminUiInquirableWriter(ServiceContractModel model,
50              String directory,
51              String rootPackage,
52              String servKey,
53              XmlType xmlType,
54              List<ServiceMethod> methods) {
55          super(directory + "/" + "java", calcPackage(servKey, rootPackage, xmlType), calcClassName(servKey, xmlType));
56          this.model = model;
57          this.finder = new ModelFinder(model);
58          this.directory = directory;
59          this.rootPackage = rootPackage;
60          this.servKey = servKey;
61          service = finder.findService(servKey);
62          this.xmlType = xmlType;
63          this.methods = methods;
64      }
65  
66      public static String calcPackage(String servKey, String rootPackage, XmlType xmlType) {
67          String pack = rootPackage + "." + servKey.toLowerCase();
68  //  StringBuffer buf = new StringBuffer (service.getVersion ().length ());
69  //  for (int i = 0; i < service.getVersion ().length (); i ++)
70  //  {
71  //   char c = service.getVersion ().charAt (i);
72  //   c = Character.toLowerCase (c);
73  //   if (Character.isLetter (c))
74  //   {
75  //    buf.append (c);
76  //    continue;
77  //   }
78  //   if (Character.isDigit (c))
79  //   {
80  //    buf.append (c);
81  //   }
82  //  }
83  //  pack = pack + buf.toString ();
84  //        pack = pack + "service.decorators";
85          return pack;
86  //        return rootPackage;
87      }
88  
89      public static String calcClassName(String servKey, String xmlTypeName) {
90          return GetterSetterNameCalculator.calcInitUpper(xmlTypeName) + "AdminInquirableImpl";
91      }
92  
93      public static String calcClassName(String servKey, XmlType xmlType) {
94          return calcClassName(servKey, xmlType.getName());
95      }
96  
97      public static String calcDecoratorClassName(String servKey) {
98          return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceDecorator");
99      }
100 
101     private static enum MethodType {
102 
103         VALIDATE, CREATE, UPDATE
104     };
105 
106     private MethodType calcMethodType(ServiceMethod method) {
107         if (method.getName().startsWith("validate")) {
108             return MethodType.VALIDATE;
109         }
110         if (method.getName().startsWith("create")) {
111             return MethodType.CREATE;
112         }
113         if (method.getName().startsWith("update")) {
114             return MethodType.UPDATE;
115         }
116         return null;
117     }
118 
119     /**
120      * Write out the entire file
121      *
122      * @param out
123      */
124     public void write() {
125         indentPrint("public class " + calcClassName(servKey, xmlType));
126         println(" extends InquirableImpl");
127         importsAdd("org.kuali.rice.krad.inquiry.InquirableImpl");
128         openBrace();
129         writeLogic();
130         closeBrace();
131 
132         this.writeJavaClassAndImportsOutToFile();
133         this.getOut().close();
134     }
135 
136     private void writeLogic() {
137 
138         String initUpper = GetterSetterNameCalculator.calcInitUpper(servKey);
139         String initLower = GetterSetterNameCalculator.calcInitLower(servKey);
140         String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
141         String serviceClass = GetterSetterNameCalculator.calcInitUpper(service.getName());
142         String serviceVar = GetterSetterNameCalculator.calcInitLower(service.getName());
143         importsAdd(service.getImplProject() + "." + service.getName());
144         importsAdd("org.apache.log4j.Logger");
145         indentPrintln("private static final Logger LOG = Logger.getLogger(" + calcClassName(servKey, xmlType) + ".class);");
146         indentPrintln("private transient " + serviceClass + " " + serviceVar + ";");
147         MessageStructure pk = this.getPrimaryKey(xmlType.getName());
148         indentPrintln("private final static String PRIMARY_KEY = \"" + pk.getShortName() + "\";");
149 
150         println("");
151         indentPrintln("@Override");
152         importsAdd(xmlType.getJavaPackage() + "." + infoClass);
153         importsAdd(List.class.getName());
154         importsAdd(Map.class.getName());
155 //        importsAdd(LookupForm.class.getName());
156 //        importsAdd(QueryByCriteria.class.getName());
157 //        importsAdd(Predicate.class.getName());
158 //        importsAdd(PredicateFactory.class.getName());
159         importsAdd(GlobalResourceLoader.class.getName());
160         XmlType contextInfo = finder.findXmlType("contextInfo");
161         importsAdd(contextInfo.getJavaPackage() + "." + contextInfo.getName());
162         importsAdd("org.kuali.student.enrollment.common.util.ContextBuilder");
163 //        importsAdd(PredicateFactory.class.getName());
164         importsAdd(ArrayList.class.getName());
165         importsAdd(QName.class.getName());
166 //        importsAdd (PredicateFactory.class.getName());
167         indentPrintln("public " + infoClass + " retrieveDataObject(Map<String, String> parameters)");
168         openBrace();
169         indentPrintln("String key = parameters.get(PRIMARY_KEY);");
170         indentPrintln("try");
171         openBrace();
172         String getMethod = calcGetMethod();
173         if (getMethod == null) {
174             indentPrintln("// WARNING: Missing get method please add it to the service contract: " + servKey + "." + xmlType.getName());
175             getMethod = "get" + GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
176         }
177         indentPrintln("" + infoClass + " info = this.get" + serviceClass + "()." + getMethod + "(key, getContextInfo());");
178         indentPrintln("return info;");
179         closeBrace();
180         indentPrintln("catch (Exception ex) {");
181         indentPrintln("    throw new RuntimeException(ex);");
182         indentPrintln("}");
183         closeBrace();
184         writeServiceGetterAndSetter (this, serviceClass, serviceVar, xmlType);
185     }
186     
187     private MessageStructure getPrimaryKey(String xmlTypeName) {
188         for (MessageStructure ms : finder.findMessageStructures(xmlTypeName)) {
189           if (ms.isPrimaryKey()) {
190               return ms;
191           }
192         }
193         return null;
194     }
195     public static void writeServiceGetterAndSetter (JavaClassWriter out, String serviceClass, String serviceVar, XmlType xmlType) {
196         
197         out.println ("");
198         out.indentPrintln("public void set" + serviceClass + "(" + serviceClass + " " + serviceVar + ")");
199         out.openBrace();
200         out.indentPrintln("    this." + serviceVar + " = " + serviceVar + ";");
201         out.closeBrace();
202         out.println("");
203         out.indentPrintln("public " + serviceClass + " get" + serviceClass + "()");
204         out.openBrace();
205         out.indentPrintln("if (" + serviceVar + " == null)");
206         out.openBrace();
207         String serviceConstants = calcServiceContantsName(serviceClass);
208         out.importsAdd(calcServiceContantsPackage(xmlType) + "." + serviceConstants);
209         out.indentPrintln("QName qname = new QName(" + serviceConstants + ".NAMESPACE," + serviceConstants + ".SERVICE_NAME_LOCAL_PART);");
210         out.indentPrintln(serviceVar + " = (" + serviceClass + ") GlobalResourceLoader.getService(qname);");
211         out.closeBrace();
212         out.indentPrintln("return this." + serviceVar + ";");
213         out.closeBrace();
214         out.println("");
215         out.indentPrintln("private ContextInfo getContextInfo() {");
216         out.indentPrintln("    return ContextBuilder.loadContextInfo();");
217         out.indentPrintln("}");
218     }
219 
220     public static String calcServiceContantsName(String serviceClass) {
221         if (serviceClass.equals("LRCService")) {
222             return "LrcServiceConstants";
223         }
224         return serviceClass + "Constants";
225     }
226     private static Map<String, String> SERVICE_CLASS_PACKAGE;
227 
228     {
229         SERVICE_CLASS_PACKAGE = new HashMap<String, String>();
230         SERVICE_CLASS_PACKAGE.put("lum", "org.kuali.student.r2.lum.util.constants");
231         SERVICE_CLASS_PACKAGE.put("lum", "org.kuali.student.r2.lum.util.constants");
232         SERVICE_CLASS_PACKAGE.put("core", "org.kuali.student.r2.core.constants");
233         SERVICE_CLASS_PACKAGE.put("enrollment", "org.kuali.student.r2.common.util.constants");
234     }
235 
236     public static String calcServiceContantsPackage(XmlType xmlType) {
237         if (xmlType.getJavaPackage().contains(".enrollment.")) {
238             return SERVICE_CLASS_PACKAGE.get("enrollment");
239         }
240         if (xmlType.getJavaPackage().contains(".core.")) {
241             return SERVICE_CLASS_PACKAGE.get("core");
242         }
243         if (xmlType.getJavaPackage().contains(".lum.")) {
244             return SERVICE_CLASS_PACKAGE.get("lum");
245         }
246         return "org.kuali.student.r2.common.util.constants";
247     }
248 
249     private String calcGetMethod() {
250         ServiceMethod method = this.findGetMethod();
251         if (method != null) {
252             return method.getName();
253         }
254         return null;
255     }
256 
257     private ServiceMethod findGetMethod() {
258         String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
259         for (ServiceMethod method : methods) {
260             if (method.getName().startsWith("get")) {
261                 if (method.getReturnValue().getType().equalsIgnoreCase(infoClass)) {
262                     if (method.getParameters().size() == 2) {
263                         if (method.getParameters().get(0).getType().equalsIgnoreCase("String")) {
264                             return method;
265                         }
266                     }
267                 }
268             }
269         }
270         return null;
271     }
272 }