View Javadoc

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