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.impl.ServiceContractModelPescXsdLoader; 031 import org.kuali.student.contract.model.util.ServicesFilter; 032 import org.kuali.student.contract.model.validation.DictionaryValidationException; 033 import org.kuali.student.contract.model.validation.ServiceContractModelValidator; 034 import org.slf4j.Logger; 035 import org.slf4j.LoggerFactory; 036 037 /** 038 * 039 * @author nwright 040 */ 041 public class PureJavaInfcWriter { 042 043 private static Logger log = LoggerFactory.getLogger(PureJavaInfcWriter.class); 044 045 private ServiceContractModel model; 046 private String directory; 047 private String rootPackage; 048 public static final String DEFAULT_ROOT_PACKAGE = "org.kuali.student.service"; 049 private ServicesFilter filter; 050 051 public PureJavaInfcWriter(ServiceContractModel model, 052 String directory, 053 String rootPackage, 054 ServicesFilter filter) { 055 this.model = model; 056 this.directory = directory; 057 this.rootPackage = rootPackage; 058 this.filter = filter; 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 PureJavaInfcWriterForOneService(model, directory, rootPackage, service.getKey()).write(); 070 } 071 072 // the Info interfaces's 073 log.info("Generating common Info interfaces"); 074 for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) { 075 log.info("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 // log.info ("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 log.info(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 }