View Javadoc

1   /*
2    * Copyright 2010 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.HashSet;
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.kuali.student.contract.model.ServiceContractModel;
23  import org.kuali.student.contract.model.ServiceMethod;
24  import org.kuali.student.contract.model.XmlType;
25  import org.kuali.student.contract.model.util.ModelFinder;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   *
31   * @author nwright
32   */
33  public class AdminUiWriterForOneService {
34  
35  
36  	private static final Logger log = LoggerFactory.getLogger(AdminUiWriterForOneService.class);
37      
38      private ServiceContractModel model;
39      private ModelFinder finder;
40      private String directory;
41      private String rootPackage;
42      private String servKey;
43  
44      public AdminUiWriterForOneService(ServiceContractModel model,
45              String directory,
46              String rootPackage,
47              String servKey) {
48          this.model = model;
49          this.finder = new ModelFinder(model);
50          this.directory = directory;
51          this.rootPackage = rootPackage;
52          this.servKey = servKey;
53      }
54  
55      /**
56       * Write out the entire file
57       *
58       * @param out
59       */
60      public void write() {
61          List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
62          if (methods.isEmpty ()) {
63              log.warn("No methods defined for servKey: " + servKey);
64              return;
65          }
66          Set<XmlType> types = this.getMainXmlTypesUsedByService(methods);
67          if (types.isEmpty()) {
68              log.warn("No types defined for servKey: " + servKey);
69              return;
70          }
71          // the main servKey
72          log.info("Generating admin UI for " + types.size() + " in " + servKey);
73          for (XmlType type : types) {
74              new AdminUiInquirableWriter(model, directory, rootPackage, servKey, type, methods).write();
75              new AdminUiLookupableWriter(model, directory, rootPackage, servKey, type, methods).write();
76              new AdminUiLookupViewBeanWriter(model, directory, rootPackage, servKey, type, methods).write();
77              new AdminUiInquiryViewBeanWriter(model, directory, rootPackage, servKey, type, methods).write();
78          }
79      }
80  
81      private Set<XmlType> getMainXmlTypesUsedByService(List<ServiceMethod> methods) {
82          Set<XmlType> set = new HashSet();
83          for (ServiceMethod method : methods) {
84              if (method.getReturnValue().getType().endsWith("List")) {
85                  continue;
86              }
87              XmlType returnType = finder.findXmlType(method.getReturnValue().getType());
88              if (returnType == null) {
89                  continue;
90              }
91              if (!returnType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
92                  continue;
93              }
94              // TYPE only should show up on type service
95              if (returnType.getName().equalsIgnoreCase("TypeInfo")) {
96                  if (!servKey.equalsIgnoreCase("type")) {
97                      continue;
98                  }
99              }
100             // State only should show up on type service
101             if (returnType.getName().equalsIgnoreCase("StateInfo")) {
102                 if (!servKey.equalsIgnoreCase("state")) {
103                     continue;
104                 }
105             }
106 //            if (method.getName().startsWith("create")) {
107 //                set.add(returnType);
108 //                continue;
109 //            }
110 //            if (method.getName().startsWith("update")) {
111 //                set.add(returnType);
112 //                continue;
113 //            }
114             if (method.getName().startsWith("get")) {
115                 if (method.getParameters().size() == 2) {
116                     if (method.getParameters().get(0).getType().equalsIgnoreCase("String")) {
117                         set.add(returnType);
118                         continue;
119                     }
120                 }
121             }
122         }
123         return set;
124     }
125 }