View Javadoc
1   /**
2    * Copyright 2004-2014 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.opensource.org/licenses/ecl2.php
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.jpa.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  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   *
35   * @author nwright
36   */
37  public class JpaImplWriterForOneService {
38  
39      private static Logger log = LoggerFactory.getLogger(JpaImplWriterForOneService.class);
40      
41      protected ServiceContractModel model;
42      protected ModelFinder finder;
43      protected String directory;
44      protected String rootPackage;
45      protected String servKey;
46      protected boolean isR1;
47  
48      public JpaImplWriterForOneService(ServiceContractModel model,
49              String directory,
50              String rootPackage,
51              String servKey,
52              boolean isR1) {
53          this.model = model;
54          this.finder = new ModelFinder(model);
55          this.directory = directory;
56          this.rootPackage = rootPackage;
57          this.servKey = servKey;
58          this.isR1 = isR1;
59      }
60  
61      /**
62       * Write out the entire file
63       * @param out
64       */
65      public void write() {
66          List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
67          if (methods.size() == 0) {
68              log.warn("No methods defined for servKey: " + servKey);
69              return;
70          }
71  
72          // the main servKey
73          log.info("Generating jpa impls for " + servKey);
74          new JpaImplServiceWriter(model, directory, rootPackage, servKey, methods, isR1).write();
75          
76  //        for (XmlType xmlType : getXmlTypesUsedJustByService()) {
77  //            new JpaEntityWriter(model, directory, rootPackage, servKey, methods, xmlType, isR1).write();
78  //        }
79  
80      }
81  
82      private Set<XmlType> getXmlTypesUsedJustByService() {
83          Set<XmlType> set = new HashSet();
84          for (XmlType type : model.getXmlTypes()) {
85              if (type.getService().equalsIgnoreCase(servKey)) {
86                  if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
87                      set.add(type);
88                  }
89              }
90          }
91          return set;
92      }
93  
94      private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
95          Set<XmlType> set = new HashSet();
96          for (ServiceMethod method : methods) {
97              if (method.getReturnValue() != null) {
98                  ServiceMethodReturnValue ret = method.getReturnValue();
99                  XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
100                 if (xmlType == null) {
101                     throw new DictionaryValidationException("Method " + method.getService()
102                             + "." + method.getName()
103                             + "returns an unknown type, "
104                             + ret.getType());
105                 }
106                 addTypeAndAllSubTypes(set, xmlType);
107             }
108             for (ServiceMethodParameter param : method.getParameters()) {
109                 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
110                 if (xmlType == null) {
111                     throw new DictionaryValidationException("Parameter "
112                             + method.getService() + "."
113                             + method.getName() + "."
114                             + param.getName()
115                             + "has an unknown type, "
116                             + param.getType());
117                 }
118                 addTypeAndAllSubTypes(set, xmlType);
119             }
120         }
121         return set;
122     }
123 
124     private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
125         if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
126             if (set.add(xmlType)) {
127                 addXmlTypesUsedByMessageStructure(set, xmlType);
128             }
129         }
130     }
131 
132     private String stripListFromType(String type) {
133         if (type.endsWith("List")) {
134             type = type.substring(0, type.length() - "List".length());
135         }
136         return type;
137     }
138 
139     private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
140             XmlType xmlType) {
141         ModelFinder finder = new ModelFinder(model);
142         for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
143             XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
144             if (subType == null) {
145                 throw new DictionaryValidationException("MessageStructure field "
146                         + ms.getId()
147                         + " has an unknown type, "
148                         + ms.getType());
149             }
150             addTypeAndAllSubTypes(set, subType);
151         }
152     }
153 }