View Javadoc

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