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.contract.writer.service;
017    
018    import java.util.List;
019    
020    import org.kuali.student.contract.exception.DictionaryExecutionException;
021    import org.kuali.student.contract.model.MessageStructure;
022    import org.kuali.student.contract.model.ServiceContractModel;
023    import org.kuali.student.contract.model.XmlType;
024    import org.kuali.student.contract.model.util.ModelFinder;
025    import org.kuali.student.contract.writer.JavaClassWriter;
026    
027    /**
028     *
029     * @author nwright
030     */
031    public class PureJavaInfcInfcWriter extends JavaClassWriter {
032    
033        private ServiceContractModel model;
034        private ModelFinder finder;
035        private String directory;
036        private String rootPackage;
037        private String service;
038        private XmlType xmlType;
039    
040        public PureJavaInfcInfcWriter(ServiceContractModel model,
041                String directory,
042                String rootPackage,
043                String service,
044                XmlType xmlType) {
045            super(directory, calcPackage(service, rootPackage), calcClassName(
046                    xmlType.getName()));
047            this.model = model;
048            this.finder = new ModelFinder(model);
049            this.directory = directory;
050            this.rootPackage = rootPackage;
051            this.service = service;
052            this.xmlType = xmlType;
053        }
054    
055        public static String calcPackage(String service, String rootPackage) {
056            if (service.contains(",")) {
057                service = "common";
058            }
059            return PureJavaInfcServiceWriter.calcPackage(service, rootPackage);
060        }
061    
062        public static String calcClassName(String name) {
063            if (name.endsWith("Info")) {
064                name = name.substring(0, name.length() - "Info".length());
065                name = name + "Infc";
066            } else if (name.endsWith("InfoList")) {
067                name = name.substring(0, name.length() - "InfoList".length());
068                name = name + "InfcList";
069            }
070            return GetterSetterNameCalculator.calcInitUpper(name);
071        }
072    
073        /**
074         * Write out the entire file
075         * @param out
076         */
077        public void write() {
078            indentPrintln("public interface " + calcClassName(xmlType.getName()));
079            openBrace();
080    
081            List<MessageStructure> list = finder.findMessageStructures(xmlType.getName());
082    //        if (list.size() == 0) {
083    //            throw new DictionaryExecutionException(
084    //                    "xmlType " + xmlType.getName()
085    //                    + " has no fields defined in the message structure tab");
086    //        }
087            for (MessageStructure ms : list) {
088                if (ms.getId().equals ("RegistrationGroupTemplateInfo.activityOfferingCombinations")) {
089                    continue;
090                }
091                String realType = stripList(calcClassName(ms.getType()));
092                String type = this.calcFieldTypeToUse(ms.getType(), realType);
093                indentPrintln("");
094                indentPrintln("/**");
095                indentPrintWrappedComment("Set " + ms.getName());
096                indentPrintln("*");
097                indentPrintln("* Type: " + ms.getType());
098                indentPrintln("*");
099                indentPrintWrappedComment(ms.getDescription());
100                indentPrintln("*/");
101                indentPrintln("public void " + calcSetter(ms) + "(" + type + " " + initLower(
102                        ms.getShortName()) + ");");
103    
104    
105                indentPrintln("");
106                indentPrintln("/**");
107                indentPrintWrappedComment("Get " + ms.getName());
108                indentPrintln("*");
109                indentPrintln("* Type: " + ms.getType());
110                indentPrintln("*");
111                indentPrintWrappedComment(ms.getDescription());
112                indentPrintln("*/");
113                indentPrintln("public " + type + " " + calcGetter(ms) + "();");
114                indentPrintln("");
115    
116                indentPrintln("");
117            }
118            indentPrintln("");
119            closeBrace();
120    
121            this.writeJavaClassAndImportsOutToFile();
122            this.getOut().close();
123        }
124    
125        private String stripList(String str) {
126            return GetterSetterNameCalculator.stripList(str);
127        }
128    
129        private String initLower(String str) {
130            return GetterSetterNameCalculator.calcInitLower(str);
131        }
132    
133        private String calcGetter(MessageStructure ms) {
134            return new GetterSetterNameCalculator(ms, this, model).calcGetter();
135        }
136    
137        private String calcSetter(MessageStructure ms) {
138            return new GetterSetterNameCalculator(ms, this, model).calcSetter();
139        }
140    
141        private String calcFieldTypeToUse(String type, String realType) {
142            XmlType t = finder.findXmlType(this.stripList(type));
143            String pckName = calcPackage(t.getService(), rootPackage);
144            return MessageStructureTypeCalculator.calculate(this, model, type, realType,
145                    pckName);
146        }
147    }