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.contract.writer.service;
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.util.ServicesFilter;
031import org.kuali.student.contract.model.validation.DictionaryValidationException;
032import org.kuali.student.contract.model.validation.ServiceContractModelValidator;
033
034/**
035 *
036 * @author nwright
037 */
038public class EachMethodServiceWriter {
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 EachMethodServiceWriter(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 EachMethodServiceWriterForOneService(model, directory, rootPackage, service.getKey()).write();
065        }
066    }
067
068    private List<Service> filterServices() {
069        if (filter == null) {
070            return model.getServices();
071        }
072        return filter.filter(model.getServices());
073    }
074
075    private void validate() {
076        Collection<String> errors =
077                new ServiceContractModelValidator(model).validate();
078        if (errors.size() > 0) {
079            StringBuffer buf = new StringBuffer();
080            buf.append(errors.size() + " errors found while validating the data.");
081            int cnt = 0;
082            for (String msg : errors) {
083                cnt++;
084                buf.append("\n");
085                buf.append("*error*" + cnt + ":" + msg);
086            }
087
088            throw new DictionaryValidationException(buf.toString());
089        }
090    }
091}