001 /* 002 * Copyright 2009 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.osedu.org/licenses/ECL-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.student.contract.writer.service; 017 018 import java.util.Collection; 019 import java.util.HashMap; 020 import java.util.HashSet; 021 import java.util.List; 022 import java.util.Map; 023 import java.util.Set; 024 025 import org.kuali.student.contract.model.Service; 026 import org.kuali.student.contract.model.ServiceContractModel; 027 import org.kuali.student.contract.model.ServiceMethod; 028 import org.kuali.student.contract.model.ServiceMethodError; 029 import org.kuali.student.contract.model.XmlType; 030 import org.kuali.student.contract.model.util.ServicesFilter; 031 import org.kuali.student.contract.model.validation.DictionaryValidationException; 032 import org.kuali.student.contract.model.validation.ServiceContractModelValidator; 033 034 /** 035 * 036 * @author nwright 037 */ 038 public class MockImplWriter { 039 040 private ServiceContractModel model; 041 private String directory; 042 private String rootPackage; 043 public static final String ROOT_PACKAGE = "org.kuali.student"; 044 public static final String LUM_ROOT_PACKAGE = "org.kuali.student.lum"; 045 public static final String ENROLLMENT_ROOT_PACKAGE = "org.kuali.student.enrollment"; 046 private ServicesFilter filter; 047 private boolean isR1; 048 049 public MockImplWriter(ServiceContractModel model, 050 String directory, 051 String rootPackage, 052 ServicesFilter filter, 053 boolean isR1) { 054 this.model = model; 055 this.directory = directory; 056 this.rootPackage = rootPackage; 057 this.filter = filter; 058 this.isR1 = isR1; 059 } 060 061 /** 062 * Write out the entire file 063 * @param out 064 */ 065 public void write() { 066 this.validate(); 067 068 for (Service service : filterServices()) { 069 new MockImplWriterForOneService(model, directory, rootPackage, service.getKey(), isR1).write(); 070 } 071 072 // // the Info interfaces's 073 // System.out.println("Generating common Info interfaces"); 074 // for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) { 075 // System.out.println("Generating info interface for " + xmlType.getName()); 076 // new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 077 // new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 078 // } 079 080 // exceptions 081 // Decided to just use the exisiting exceptions that are hand crafted 082 // no need to generate 083 // for (ServiceMethodError error : getServiceMethodErrors ().values ()) 084 // { 085 // System.out.println ("generating exception class: " + error.getType ()); 086 // new ServiceExceptionWriter (model, directory, rootPackage, error).write (); 087 // } 088 089 } 090 091 private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() { 092 Set<XmlType> set = new HashSet(); 093 for (XmlType type : model.getXmlTypes()) { 094 if (type.getService().contains(",")) { 095 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { 096 System.out.println(type.getName() + "==>" + type.getService()); 097 set.add(type); 098 } 099 } 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 }