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     */
016    package org.kuali.student.jpa.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    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    /**
037     *
038     * @author nwright
039     */
040    public class JpaImplWriter {
041    
042        private static Logger log = LoggerFactory.getLogger(JpaImplWriter.class);
043        
044        protected ServiceContractModel model;
045        protected String directory;
046        protected String rootPackage;
047        public static final String ROOT_PACKAGE = "org.kuali.student";
048        public static final String LUM_ROOT_PACKAGE = "org.kuali.student.lum";
049        public static final String ENROLLMENT_ROOT_PACKAGE = "org.kuali.student.enrollment";
050        protected ServicesFilter filter;
051        protected boolean isR1;
052    
053        public JpaImplWriter(ServiceContractModel model,
054                String directory,
055                String rootPackage,
056                ServicesFilter filter,
057                boolean isR1) {
058            this.model = model;
059            this.directory = directory;
060            this.rootPackage = rootPackage;
061            this.filter = filter;
062            this.isR1 = isR1;
063        }
064    
065        /**
066         * Write out the entire file
067         * @param out
068         */
069        public void write() {
070            this.validate();
071    
072            for (Service service : filterServices()) {
073                log.info ("**************writing service=" + service.getKey());
074                new JpaImplWriterForOneService(model, directory, rootPackage, service.getKey(), isR1).write();
075            }
076    
077    //        // the Info interfaces's
078    //        System.out.println("Generating common Info interfaces");
079    //        for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService()) {
080    //            System.out.println("Generating info interface for " + xmlType.getName());
081    //            new PureJavaInfcInfcWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
082    //            new PureJavaInfcBeanWriter(model, directory, rootPackage, xmlType.getService(), xmlType).write();
083    //        }
084    
085    //  exceptions
086            // Decided to just use the exisiting exceptions that are hand crafted
087            // no need to generate
088    //  for (ServiceMethodError error : getServiceMethodErrors ().values ())
089    //  {
090    //   System.out.println ("generating exception class: " + error.getType ());
091    //   new ServiceExceptionWriter (model, directory, rootPackage, error).write ();
092    //  }
093    
094        }
095    
096        private Set<XmlType> getXmlTypesUsedByMoreThanOneByService() {
097            Set<XmlType> set = new HashSet();
098            for (XmlType type : model.getXmlTypes()) {
099                if (type.getService().contains(",")) {
100                    if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
101                        System.out.println(type.getName() + "==>" + type.getService());
102                        set.add(type);
103                    }
104                }
105            }
106            return set;
107        }
108    
109        private Map<String, ServiceMethodError> getServiceMethodErrors() {
110            Map<String, ServiceMethodError> errors = new HashMap();
111            for (ServiceMethod method : model.getServiceMethods()) {
112                for (ServiceMethodError error : method.getErrors()) {
113                    errors.put(error.getType(), error);
114                }
115            }
116            return errors;
117        }
118    
119        protected List<Service> filterServices() {
120            if (filter == null) {
121                return model.getServices();
122            }
123            return filter.filter(model.getServices());
124        }
125    
126        protected void validate() {
127            Collection<String> errors =
128                    new ServiceContractModelValidator(model).validate();
129            if (errors.size() > 0) {
130                StringBuffer buf = new StringBuffer();
131                buf.append(errors.size() + " errors found while validating the data.");
132                int cnt = 0;
133                for (String msg : errors) {
134                    cnt++;
135                    buf.append("\n");
136                    buf.append("*error*" + cnt + ":" + msg);
137                }
138    
139                throw new DictionaryValidationException(buf.toString());
140            }
141        }
142    }