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.remote.impl.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 RemoteImplWriter {
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.remote.impl";
044        private ServicesFilter filter;
045    
046        public RemoteImplWriter(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 RemoteImplWriterForOneService(model, directory, rootPackage, service.getKey()).write();
065            }
066            
067    //        new RemoteImplServiceSpringBeanWriter (model, directory, rootPackage).write();
068    
069    //        // the Info interfaces's
070    //        System.out.println("Generating common Info interfaces");
071    //        for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) {
072    //            System.out.println("Generating info interface for " + xmlType.getName());
073    //            new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
074    //            new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
075    //        }
076    
077    //  exceptions
078            // Decided to just use the exisiting exceptions that are hand crafted
079            // no need to generate
080    //  for (ServiceMethodError error : getServiceMethodErrors ().values ())
081    //  {
082    //   System.out.println ("generating exception class: " + error.getType ());
083    //   new ServiceExceptionWriter (model, directory, rootPackage, error).write ();
084    //  }
085    
086        }
087    
088        private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() {
089            Set<XmlType> set = new HashSet();
090            for (XmlType type : model.getXmlTypes()) {
091                if (type.getService().contains(",")) {
092                    if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
093                        System.out.println(type.getName() + "==>" + type.getService());
094                        set.add(type);
095                    }
096                }
097            }
098            return set;
099        }
100    
101        private Map<String, ServiceMethodError> getServiceMethodErrors() {
102            Map<String, ServiceMethodError> errors = new HashMap();
103            for (ServiceMethod method : model.getServiceMethods()) {
104                for (ServiceMethodError error : method.getErrors()) {
105                    errors.put(error.getType(), error);
106                }
107            }
108            return errors;
109        }
110    
111        private List<Service> filterServices() {
112            if (filter == null) {
113                return model.getServices();
114            }
115            return filter.filter(model.getServices());
116        }
117    
118        private void validate() {
119            Collection<String> errors =
120                    new ServiceContractModelValidator(model).validate();
121            if (errors.size() > 0) {
122                StringBuffer buf = new StringBuffer();
123                buf.append(errors.size() + " errors found while validating the data.");
124                int cnt = 0;
125                for (String msg : errors) {
126                    cnt++;
127                    buf.append("\n");
128                    buf.append("*error*" + cnt + ":" + msg);
129                }
130    
131                throw new DictionaryValidationException(buf.toString());
132            }
133        }
134    }