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