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.Collection;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.kuali.student.contract.model.Service;
26  import org.kuali.student.contract.model.ServiceContractModel;
27  import org.kuali.student.contract.model.ServiceMethod;
28  import org.kuali.student.contract.model.ServiceMethodError;
29  import org.kuali.student.contract.model.XmlType;
30  import org.kuali.student.contract.model.util.ServicesFilter;
31  import org.kuali.student.contract.model.validation.DictionaryValidationException;
32  import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   *
38   * @author nwright
39   */
40  public class JpaImplWriter {
41  
42      private static Logger log = LoggerFactory.getLogger(JpaImplWriter.class);
43      
44      protected ServiceContractModel model;
45      protected String directory;
46      protected String rootPackage;
47      public static final String ROOT_PACKAGE = "org.kuali.student";
48      public static final String LUM_ROOT_PACKAGE = "org.kuali.student.lum";
49      public static final String ENROLLMENT_ROOT_PACKAGE = "org.kuali.student.enrollment";
50      protected ServicesFilter filter;
51      protected boolean isR1;
52  
53      public JpaImplWriter(ServiceContractModel model,
54              String directory,
55              String rootPackage,
56              ServicesFilter filter,
57              boolean isR1) {
58          this.model = model;
59          this.directory = directory;
60          this.rootPackage = rootPackage;
61          this.filter = filter;
62          this.isR1 = isR1;
63      }
64  
65      /**
66       * Write out the entire file
67       * @param out
68       */
69      public void write() {
70          this.validate();
71  
72          for (Service service : filterServices()) {
73              log.info ("**************writing service=" + service.getKey());
74              new JpaImplWriterForOneService(model, directory, rootPackage, service.getKey(), isR1).write();
75          }
76  
77  //        // the Info interfaces's
78  //        System.out.println("Generating common Info interfaces");
79  //        for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) {
80  //            System.out.println("Generating info interface for " + xmlType.getName());
81  //            new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
82  //            new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
83  //        }
84  
85  //  exceptions
86          // Decided to just use the exisiting exceptions that are hand crafted
87          // no need to generate
88  //  for (ServiceMethodError error : getServiceMethodErrors ().values ())
89  //  {
90  //   System.out.println ("generating exception class: " + error.getType ());
91  //   new ServiceExceptionWriter (model, directory, rootPackage, error).write ();
92  //  }
93  
94      }
95  
96      private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() {
97          Set<XmlType> set = new HashSet();
98          for (XmlType type : model.getXmlTypes()) {
99              if (type.getService().contains(",")) {
100                 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
101                     System.out.println(type.getName() + "==>" + type.getService());
102                     set.add(type);
103                 }
104             }
105         }
106         return set;
107     }
108 
109     private Map<String, ServiceMethodError> getServiceMethodErrors() {
110         Map<String, ServiceMethodError> errors = new HashMap();
111         for (ServiceMethod method : model.getServiceMethods()) {
112             for (ServiceMethodError error : method.getErrors()) {
113                 errors.put(error.getType(), error);
114             }
115         }
116         return errors;
117     }
118 
119     protected List<Service> filterServices() {
120         if (filter == null) {
121             return model.getServices();
122         }
123         return filter.filter(model.getServices());
124     }
125 
126     protected void validate() {
127         Collection<String> errors =
128                 new ServiceContractModelValidator(model).validate();
129         if (errors.size() > 0) {
130             StringBuffer buf = new StringBuffer();
131             buf.append(errors.size() + " errors found while validating the data.");
132             int cnt = 0;
133             for (String msg : errors) {
134                 cnt++;
135                 buf.append("\n");
136                 buf.append("*error*" + cnt + ":" + msg);
137             }
138 
139             throw new DictionaryValidationException(buf.toString());
140         }
141     }
142 }