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.remote.impl.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.MessageStructure;
23  import org.kuali.student.contract.model.ServiceContractModel;
24  import org.kuali.student.contract.model.ServiceMethod;
25  import org.kuali.student.contract.model.ServiceMethodParameter;
26  import org.kuali.student.contract.model.ServiceMethodReturnValue;
27  import org.kuali.student.contract.model.XmlType;
28  import org.kuali.student.contract.model.util.ModelFinder;
29  import org.kuali.student.contract.model.validation.DictionaryValidationException;
30  
31  /**
32   *
33   * @author nwright
34   */
35  public class RemoteImplWriterForOneService {
36  
37      private ServiceContractModel model;
38      private ModelFinder finder;
39      private String directory;
40      private String rootPackage;
41      private String servKey;
42  
43      public RemoteImplWriterForOneService(ServiceContractModel model,
44              String directory,
45              String rootPackage,
46              String servKey) {
47          this.model = model;
48          this.finder = new ModelFinder(model);
49          this.directory = directory;
50          this.rootPackage = rootPackage;
51          this.servKey = servKey;
52      }
53  
54      /**
55       * Write out the entire file
56       * @param out
57       */
58      public void write() {
59          List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
60          if (methods.size() == 0) {
61              System.out.println("No methods defined for servKey: " + servKey);
62              return;
63          }
64  
65          // the main servKey
66          System.out.println("Generating remote impl and search unit test for " + servKey + " directory = " + directory);
67  //        new RemoteImplServiceWriter(model, directory, rootPackage, servKey, methods).write();
68          new RemoteImplServiceTestWriter(model, directory, rootPackage, servKey, methods).write();
69          
70      }
71  
72      private Set<XmlType> getXmlTypesUsedJustByService() {
73          Set<XmlType> set = new HashSet();
74          for (XmlType type : model.getXmlTypes()) {
75              if (type.getService().equalsIgnoreCase(servKey)) {
76                  if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
77                      set.add(type);
78                  }
79              }
80          }
81          return set;
82      }
83  
84      private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
85          Set<XmlType> set = new HashSet();
86          for (ServiceMethod method : methods) {
87              if (method.getReturnValue() != null) {
88                  ServiceMethodReturnValue ret = method.getReturnValue();
89                  XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
90                  if (xmlType == null) {
91                      throw new DictionaryValidationException("Method " + method.getService()
92                              + "." + method.getName()
93                              + "returns an unknown type, "
94                              + ret.getType());
95                  }
96                  addTypeAndAllSubTypes(set, xmlType);
97              }
98              for (ServiceMethodParameter param : method.getParameters()) {
99                  XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
100                 if (xmlType == null) {
101                     throw new DictionaryValidationException("Parameter "
102                             + method.getService() + "."
103                             + method.getName() + "."
104                             + param.getName()
105                             + "has an unknown type, "
106                             + param.getType());
107                 }
108                 addTypeAndAllSubTypes(set, xmlType);
109             }
110         }
111         return set;
112     }
113 
114     private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
115         if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
116             if (set.add(xmlType)) {
117                 addXmlTypesUsedByMessageStructure(set, xmlType);
118             }
119         }
120     }
121 
122     private String stripListFromType(String type) {
123         if (type.endsWith("List")) {
124             type = type.substring(0, type.length() - "List".length());
125         }
126         return type;
127     }
128 
129     private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
130             XmlType xmlType) {
131         ModelFinder finder = new ModelFinder(model);
132         for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
133             XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
134             if (subType == null) {
135                 throw new DictionaryValidationException("MessageStructure field "
136                         + ms.getId()
137                         + " has an unknown type, "
138                         + ms.getType());
139             }
140             addTypeAndAllSubTypes(set, subType);
141         }
142     }
143 }