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 PureJavaInfcWriter { 039 040 private ServiceContractModel model; 041 private String directory; 042 private String rootPackage; 043 public static final String DEFAULT_ROOT_PACKAGE = "org.kuali.student.service"; 044 private ServicesFilter filter; 045 046 public PureJavaInfcWriter(ServiceContractModel model, 047 String directory, 048 String rootPackage, 049 ServicesFilter filter) { 050 this.model = model; 051 this.directory = directory; 052 this.rootPackage = rootPackage; 053 this.filter = filter; 054 } 055 056 /** 057 * Write out the entire file 058 * @param out 059 */ 060 public void write() { 061 this.validate(); 062 063 for (Service service : filterServices()) { 064 new PureJavaInfcWriterForOneService(model, directory, rootPackage, service.getKey()).write(); 065 } 066 067 // the Info interfaces's 068 System.out.println("Generating common Info interfaces"); 069 for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) { 070 System.out.println("Generating info interface for " + xmlType.getName()); 071 new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 072 new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 073 } 074 075 // exceptions 076 // Decided to just use the exisiting exceptions that are hand crafted 077 // no need to generate 078 // for (ServiceMethodError error : getServiceMethodErrors ().values ()) 079 // { 080 // System.out.println ("generating exception class: " + error.getType ()); 081 // new ServiceExceptionWriter (model, directory, rootPackage, error).write (); 082 // } 083 084 } 085 086 private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() { 087 Set<XmlType> set = new HashSet(); 088 for (XmlType type : model.getXmlTypes()) { 089 if (type.getService().contains(",")) { 090 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { 091 System.out.println(type.getName() + "==>" + type.getService()); 092 set.add(type); 093 } 094 } 095 } 096 return set; 097 } 098 099 private Map<String, ServiceMethodError> getServiceMethodErrors() { 100 Map<String, ServiceMethodError> errors = new HashMap(); 101 for (ServiceMethod method : model.getServiceMethods()) { 102 for (ServiceMethodError error : method.getErrors()) { 103 errors.put(error.getType(), error); 104 } 105 } 106 return errors; 107 } 108 109 private List<Service> filterServices() { 110 if (filter == null) { 111 return model.getServices(); 112 } 113 return filter.filter(model.getServices()); 114 } 115 116 private void validate() { 117 Collection<String> errors = 118 new ServiceContractModelValidator(model).validate(); 119 if (errors.size() > 0) { 120 StringBuffer buf = new StringBuffer(); 121 buf.append(errors.size() + " errors found while validating the data."); 122 int cnt = 0; 123 for (String msg : errors) { 124 cnt++; 125 buf.append("\n"); 126 buf.append("*error*" + cnt + ":" + msg); 127 } 128 129 throw new DictionaryValidationException(buf.toString()); 130 } 131 } 132 }