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