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                String realType = stripList(calcClassName(ms.getType()));
089                String type = this.calcFieldTypeToUse(ms.getType(), realType);
090                indentPrintln("");
091                indentPrintln("/**");
092                indentPrintWrappedComment("Set " + ms.getName());
093                indentPrintln("*");
094                indentPrintln("* Type: " + ms.getType());
095                indentPrintln("*");
096                indentPrintWrappedComment(ms.getDescription());
097                indentPrintln("*/");
098                indentPrintln("public void " + calcSetter(ms) + "(" + type + " " + initLower(
099                        ms.getShortName()) + ");");
100    
101    
102                indentPrintln("");
103                indentPrintln("/**");
104                indentPrintWrappedComment("Get " + ms.getName());
105                indentPrintln("*");
106                indentPrintln("* Type: " + ms.getType());
107                indentPrintln("*");
108                indentPrintWrappedComment(ms.getDescription());
109                indentPrintln("*/");
110                indentPrintln("public " + type + " " + calcGetter(ms) + "();");
111                indentPrintln("");
112    
113                indentPrintln("");
114            }
115            indentPrintln("");
116            closeBrace();
117    
118            this.writeJavaClassAndImportsOutToFile();
119            this.getOut().close();
120        }
121    
122        private String stripList(String str) {
123            return GetterSetterNameCalculator.stripList(str);
124        }
125    
126        private String initLower(String str) {
127            return GetterSetterNameCalculator.calcInitLower(str);
128        }
129    
130        private String calcGetter(MessageStructure ms) {
131            return new GetterSetterNameCalculator(ms, this, model).calcGetter();
132        }
133    
134        private String calcSetter(MessageStructure ms) {
135            return new GetterSetterNameCalculator(ms, this, model).calcSetter();
136        }
137    
138        private String calcFieldTypeToUse(String type, String realType) {
139            XmlType t = finder.findXmlType(this.stripList(type));
140            String pckName = calcPackage(t.getService(), rootPackage);
141            return MessageStructureTypeCalculator.calculate(this, model, type, realType,
142                    pckName);
143        }
144    }