001/** 002 * Copyright 2004-2014 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.student.mock.mojo; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.kuali.student.contract.model.Service; 026import org.kuali.student.contract.model.ServiceContractModel; 027import org.kuali.student.contract.model.ServiceMethod; 028import org.kuali.student.contract.model.ServiceMethodError; 029import org.kuali.student.contract.model.XmlType; 030import org.kuali.student.contract.model.impl.ServiceContractModelPescXsdLoader; 031import org.kuali.student.contract.model.util.ServicesFilter; 032import org.kuali.student.contract.model.validation.DictionaryValidationException; 033import org.kuali.student.contract.model.validation.ServiceContractModelValidator; 034import org.slf4j.Logger; 035import org.slf4j.LoggerFactory; 036 037/** 038 * 039 * @author nwright 040 */ 041public class MockImplWriter { 042 043 private static Logger log = LoggerFactory.getLogger(MockImplWriter.class); 044 045 protected ServiceContractModel model; 046 protected String directory; 047 protected String rootPackage; 048 public static final String ROOT_PACKAGE = "org.kuali.student"; 049 public static final String LUM_ROOT_PACKAGE = "org.kuali.student.lum"; 050 public static final String ENROLLMENT_ROOT_PACKAGE = "org.kuali.student.enrollment"; 051 protected ServicesFilter filter; 052 protected boolean isR1; 053 054 public MockImplWriter(ServiceContractModel model, 055 String directory, 056 String rootPackage, 057 ServicesFilter filter, 058 boolean isR1) { 059 this.model = model; 060 this.directory = directory; 061 this.rootPackage = rootPackage; 062 this.filter = filter; 063 this.isR1 = isR1; 064 } 065 066 /** 067 * Write out the entire file 068 * @param out 069 */ 070 public void write() { 071 this.validate(); 072 073 for (Service service : filterServices()) { 074 log.info ("**************writing service=" + service.getKey()); 075 new MockImplWriterForOneService(model, directory, rootPackage, service.getKey(), isR1).write(); 076 } 077 078// // the Info interfaces's 079// System.out.println("Generating common Info interfaces"); 080// for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) { 081// System.out.println("Generating info interface for " + xmlType.getName()); 082// new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 083// new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write(); 084// } 085 086// exceptions 087 // Decided to just use the exisiting exceptions that are hand crafted 088 // no need to generate 089// for (ServiceMethodError error : getServiceMethodErrors ().values ()) 090// { 091// System.out.println ("generating exception class: " + error.getType ()); 092// new ServiceExceptionWriter (model, directory, rootPackage, error).write (); 093// } 094 095 } 096 097 private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() { 098 Set<XmlType> set = new HashSet(); 099 for (XmlType type : model.getXmlTypes()) { 100 if (type.getService().contains(",")) { 101 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) { 102 System.out.println(type.getName() + "==>" + type.getService()); 103 set.add(type); 104 } 105 } 106 } 107 return set; 108 } 109 110 private Map<String, ServiceMethodError> getServiceMethodErrors() { 111 Map<String, ServiceMethodError> errors = new HashMap(); 112 for (ServiceMethod method : model.getServiceMethods()) { 113 for (ServiceMethodError error : method.getErrors()) { 114 errors.put(error.getType(), error); 115 } 116 } 117 return errors; 118 } 119 120 protected List<Service> filterServices() { 121 if (filter == null) { 122 return model.getServices(); 123 } 124 return filter.filter(model.getServices()); 125 } 126 127 protected void validate() { 128 Collection<String> errors = 129 new ServiceContractModelValidator(model).validate(); 130 if (errors.size() > 0) { 131 StringBuffer buf = new StringBuffer(); 132 buf.append(errors.size() + " errors found while validating the data."); 133 int cnt = 0; 134 for (String msg : errors) { 135 cnt++; 136 buf.append("\n"); 137 buf.append("*error*" + cnt + ":" + msg); 138 } 139 140 throw new DictionaryValidationException(buf.toString()); 141 } 142 } 143}