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