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.admin.ui.mojo;
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 AdminUiWriter {
039    
040        private ServiceContractModel model;
041        private String directory;
042        private String rootPackage;
043        public static final String DEFAULT_ROOT_PACKAGE = "org.kuali.student.ui.admin";
044        private ServicesFilter filter;
045    
046        public AdminUiWriter(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 AdminUiWriterForOneService(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    }