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.List;
20  import java.util.Map;
21  import javax.xml.namespace.QName;
22  import org.kuali.rice.core.api.criteria.Predicate;
23  import org.kuali.rice.core.api.criteria.PredicateFactory;
24  import org.kuali.rice.core.api.criteria.QueryByCriteria;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.krad.web.form.LookupForm;
27  import org.kuali.student.contract.model.Service;
28  
29  import org.kuali.student.contract.model.ServiceContractModel;
30  import org.kuali.student.contract.model.ServiceMethod;
31  import org.kuali.student.contract.model.XmlType;
32  import org.kuali.student.contract.model.util.ModelFinder;
33  import org.kuali.student.contract.writer.JavaClassWriter;
34  import org.kuali.student.contract.writer.service.GetterSetterNameCalculator;
35  
36  /**
37   *
38   * @author nwright
39   */
40  public class AdminUiLookupableWriter extends JavaClassWriter {
41  
42      private ServiceContractModel model;
43      private ModelFinder finder;
44      private String directory;
45      private String rootPackage;
46      private String servKey;
47      private Service service;
48      private XmlType xmlType;
49      private List<ServiceMethod> methods;
50  
51      public AdminUiLookupableWriter(ServiceContractModel model,
52              String directory,
53              String rootPackage,
54              String servKey,
55              XmlType xmlType,
56              List<ServiceMethod> methods) {
57          super(directory + "/" + "java", calcPackage(servKey, rootPackage, xmlType), calcClassName(servKey, xmlType));
58          this.model = model;
59          this.finder = new ModelFinder(model);
60          this.directory = directory;
61          this.rootPackage = rootPackage;
62          this.servKey = servKey;
63          service = finder.findService(servKey);
64          this.xmlType = xmlType;
65          this.methods = methods;
66      }
67  
68      public static String calcPackage(String servKey, String rootPackage, XmlType xmlType) {
69          String pack = rootPackage + "." + servKey.toLowerCase();
70  //  StringBuffer buf = new StringBuffer (service.getVersion ().length ());
71  //  for (int i = 0; i < service.getVersion ().length (); i ++)
72  //  {
73  //   char c = service.getVersion ().charAt (i);
74  //   c = Character.toLowerCase (c);
75  //   if (Character.isLetter (c))
76  //   {
77  //    buf.append (c);
78  //    continue;
79  //   }
80  //   if (Character.isDigit (c))
81  //   {
82  //    buf.append (c);
83  //   }
84  //  }
85  //  pack = pack + buf.toString ();
86  //        pack = pack + "service.decorators";
87          return pack;
88  //        return rootPackage;
89      }
90  
91      public static String calcClassName(String servKey, String xmlTypeName) {
92          return GetterSetterNameCalculator.calcInitUpper(xmlTypeName) + "AdminLookupableImpl";
93      }
94  
95      public static String calcClassName(String servKey, XmlType xmlType) {
96          return calcClassName(servKey, xmlType.getName());
97      }
98  
99      public static String calcDecoratorClassName(String servKey) {
100         return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceDecorator");
101     }
102 
103     private static enum MethodType {
104 
105         VALIDATE, CREATE, UPDATE
106     };
107 
108     private MethodType calcMethodType(ServiceMethod method) {
109         if (method.getName().startsWith("validate")) {
110             return MethodType.VALIDATE;
111         }
112         if (method.getName().startsWith("create")) {
113             return MethodType.CREATE;
114         }
115         if (method.getName().startsWith("update")) {
116             return MethodType.UPDATE;
117         }
118         return null;
119     }
120 
121     /**
122      * Write out the entire file
123      *
124      * @param out
125      */
126     public void write() {
127         indentPrint("public class " + calcClassName(servKey, xmlType));
128         println(" extends LookupableImpl");
129         importsAdd("org.kuali.rice.krad.lookup.LookupableImpl");
130         openBrace();
131         writeLogic();
132         closeBrace();
133 
134         this.writeJavaClassAndImportsOutToFile();
135         this.getOut().close();
136     }
137 
138     private void writeLogic() {
139 
140         String initUpper = GetterSetterNameCalculator.calcInitUpper(servKey);
141         String initLower = GetterSetterNameCalculator.calcInitLower(servKey);
142         String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
143         String serviceClass = GetterSetterNameCalculator.calcInitUpper(service.getName());
144         String serviceVar = GetterSetterNameCalculator.calcInitLower(service.getName());
145         importsAdd(service.getImplProject() + "." + service.getName());
146         importsAdd("org.apache.log4j.Logger");
147         indentPrintln("private static final Logger LOG = Logger.getLogger(" + calcClassName(servKey, xmlType) + ".class);");
148         indentPrintln("private transient " + serviceClass + " " + serviceVar + ";");
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("protected List<" + infoClass + "> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded)");
168         openBrace();
169         indentPrintln("QueryByCriteria.Builder qBuilder = QueryByCriteria.Builder.create();");
170         indentPrintln("List<Predicate> pList = new ArrayList<Predicate>();");
171         indentPrintln("for (String fieldName : fieldValues.keySet())");
172         openBrace();
173         indentPrintln("String value = fieldValues.get(fieldName);");
174         indentPrintln("if (value != null && !value.isEmpty())");
175         openBrace();
176         indentPrintln("if (fieldName.equals(\"maxResultsToReturn\"))");
177         openBrace();
178         indentPrintln("qBuilder.setMaxResults (Integer.parseInt(value));");
179         indentPrintln("continue;");
180         closeBrace();
181         indentPrintln("pList.add(PredicateFactory.equal(fieldName, value));");
182         closeBrace();
183         closeBrace();
184         indentPrintln("if (!pList.isEmpty())");
185         openBrace();
186         indentPrintln("qBuilder.setPredicates(PredicateFactory.and(pList.toArray(new Predicate[pList.size()])));");
187         closeBrace();
188         indentPrintln("try");
189         openBrace();
190         String searchMethodName = calcSearchMethodName();
191         if (searchMethodName == null) {
192             indentPrintln("// WARNING: Missing searchMethod please add it to the service contract: " + servKey + "." + xmlType.getName());
193             searchMethodName = "searchFor" + GetterSetterNameCalculator.calcInitUpper(xmlType.getName()) + "s";
194         }
195         indentPrintln("List<" + infoClass + "> list = this.get" + serviceClass + "()." + searchMethodName + "(qBuilder.build(), getContextInfo());");
196         indentPrintln("return list;");
197         closeBrace();
198         indentPrintln("catch (Exception ex) {");
199         indentPrintln("    throw new RuntimeException(ex);");
200         indentPrintln("}");
201         closeBrace();
202 
203         AdminUiInquirableWriter.writeServiceGetterAndSetter(this, serviceClass, serviceVar, xmlType);
204     }
205 
206     private String calcSearchMethodName() {
207         ServiceMethod method = this.findSearchMethod();
208         if (method != null) {
209             return method.getName();
210         }
211         return null;
212     }
213 
214     private ServiceMethod findSearchMethod() {
215         return findSearchMethod(xmlType, methods);
216     }
217 
218     public static ServiceMethod findSearchMethod(XmlType xmlType, List<ServiceMethod> methods) {
219         String infoClassList = GetterSetterNameCalculator.calcInitUpper(xmlType.getName()) + "List";
220         for (ServiceMethod method : methods) {
221             if (method.getReturnValue().getType().equalsIgnoreCase(infoClassList)) {
222                 if (!method.getParameters().isEmpty()) {
223                     if (method.getParameters().get(0).getType().equalsIgnoreCase("QueryByCriteria")) {
224                         return method;
225                     }
226                 }
227             }
228         }
229         return null;
230     }
231 }