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.remote.impl.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 RemoteImplWriter {
042
043    private static Logger log = LoggerFactory.getLogger(RemoteImplWriter.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.remote.impl";
049    private ServicesFilter filter;
050
051    public RemoteImplWriter(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     *
064     * @param out
065     */
066    public void write() {
067        this.validate();
068
069        for (Service service : filterServices()) {
070            new RemoteImplWriterForOneService(model, directory, rootPackage, service.getKey()).write();
071            new RemoteImplServiceSpringBeanWriter(model, directory, rootPackage, service.getKey()).write();
072        }
073//        // the Info interfaces's
074//        System.out.println("Generating common Info interfaces");
075//        for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) {
076//            System.out.println("Generating info interface for " + xmlType.getName());
077//            new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
078//            new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
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    private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() {
091        Set<XmlType> set = new HashSet();
092        for (XmlType type : model.getXmlTypes()) {
093            if (type.getService().contains(",")) {
094                if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
095                    log.info(type.getName() + "==>" + type.getService());
096                    set.add(type);
097                }
098            }
099        }
100        return set;
101    }
102
103    private Map<String, ServiceMethodError> getServiceMethodErrors() {
104        Map<String, ServiceMethodError> errors = new HashMap();
105        for (ServiceMethod method : model.getServiceMethods()) {
106            for (ServiceMethodError error : method.getErrors()) {
107                errors.put(error.getType(), error);
108            }
109        }
110        return errors;
111    }
112
113    private List<Service> filterServices() {
114        if (filter == null) {
115            return model.getServices();
116        }
117        return filter.filter(model.getServices());
118    }
119
120    private void validate() {
121        Collection<String> errors
122                = new ServiceContractModelValidator(model).validate();
123        if (errors.size() > 0) {
124            StringBuffer buf = new StringBuffer();
125            buf.append(errors.size() + " errors found while validating the data.");
126            int cnt = 0;
127            for (String msg : errors) {
128                cnt++;
129                buf.append("\n");
130                buf.append("*error*" + cnt + ":" + msg);
131            }
132
133            throw new DictionaryValidationException(buf.toString());
134        }
135    }
136}