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.io.Serializable;
019    import java.util.List;
020    
021    import org.kuali.student.contract.exception.DictionaryExecutionException;
022    import org.kuali.student.contract.model.MessageStructure;
023    import org.kuali.student.contract.model.ServiceContractModel;
024    import org.kuali.student.contract.model.XmlType;
025    import org.kuali.student.contract.model.util.ModelFinder;
026    import org.kuali.student.contract.writer.JavaClassWriter;
027    
028    /**
029     *
030     * @author nwright
031     */
032    public class PureJavaInfcBeanWriter extends JavaClassWriter {
033    
034        private ServiceContractModel model;
035        private String directory;
036        private String rootPackage;
037        private String service;
038        private XmlType type;
039        private ModelFinder finder;
040    
041        public PureJavaInfcBeanWriter(ServiceContractModel model,
042                String directory,
043                String rootPackage,
044                String service,
045                XmlType type) {
046            super(directory, calcPackage(service, rootPackage), calcClassName(type.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.type = type;
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            }
066            name = name + "Bean";
067            return GetterSetterNameCalculator.calcInitUpper(name);
068    
069        }
070    
071        /**
072         * Write out the entire file
073         * @param out
074         */
075        public void write() {
076            indentPrintln("public class " + calcClassName(type.getName()));
077            incrementIndent();
078            indentPrint(" implements "
079                    + PureJavaInfcInfcWriter.calcClassName(type.getName()));
080            importsAdd(PureJavaInfcInfcWriter.calcPackage(service, rootPackage)
081                    + "." + PureJavaInfcInfcWriter.calcClassName(type.getName()));
082            this.importsAdd(Serializable.class.getName());
083            indentPrintln(", Serializable");
084            openBrace();
085    
086            indentPrintln("");
087            indentPrintln("private static final long serialVersionUID = 1L;");
088    
089            List<MessageStructure> list =
090                    finder.findMessageStructures(type.getName());
091            if (list.size() == 0) {
092                throw new DictionaryExecutionException("xmlType " + type.getName()
093                        + " has no fields defined in the message structure tab");
094            }
095            for (MessageStructure ms : list) {
096                String realType = stripList(PureJavaInfcInfcWriter.calcClassName(ms.getType()));
097                String fieldType = this.calcFieldTypeToUse(ms.getType(), realType);
098                String name = initLower(ms.getShortName());
099                indentPrintln("");
100                indentPrintln("private " + fieldType + " " + name + ";");
101                indentPrintln("");
102                indentPrintln("/**");
103                indentPrintWrappedComment("Set " + ms.getName());
104                indentPrintln("*");
105                indentPrintln("* Type: " + ms.getType());
106                indentPrintln("*");
107                indentPrintWrappedComment(ms.getDescription());
108                indentPrintln("*/");
109                indentPrintln("@Override");
110                indentPrintln("public void " + calcSetter(ms) + "(" + fieldType + " "
111                        + name + ")");
112                openBrace();
113                indentPrintln("this." + name + " = " + name + ";");
114                closeBrace();
115    
116                indentPrintln("");
117                indentPrintln("/**");
118                indentPrintWrappedComment("Get " + ms.getName());
119                indentPrintln("*");
120                indentPrintln("* Type: " + ms.getType());
121                indentPrintln("*");
122                indentPrintWrappedComment(ms.getDescription());
123                indentPrintln("*/");
124                indentPrintln("@Override");
125                indentPrintln("public " + fieldType + " " + calcGetter(ms) + "()");
126                openBrace();
127                indentPrintln("return this." + name + ";");
128                closeBrace();
129                indentPrint("");
130    
131                indentPrint("");
132            }
133            indentPrintln("");
134            closeBrace();
135    
136            this.writeJavaClassAndImportsOutToFile();
137            this.getOut().close();
138        }
139    
140        private String stripList(String str) {
141            return GetterSetterNameCalculator.stripList(str);
142        }
143    
144        private String initLower(String str) {
145            if (str == null) {
146                return null;
147            }
148            if (str.isEmpty()) {
149                return str;
150            }
151            if (str.length() == 1) {
152                return str.toLowerCase();
153            }
154            return str.substring(0, 1).toLowerCase() + str.substring(1);
155        }
156    
157        private String calcGetter(MessageStructure ms) {
158            return new GetterSetterNameCalculator(ms, this, model).calcGetter();
159        }
160    
161        private String calcSetter(MessageStructure ms) {
162            return new GetterSetterNameCalculator(ms, this, model).calcSetter();
163        }
164    
165        private String calcFieldTypeToUse(String type, String realType) {
166            XmlType t = finder.findXmlType(this.stripList(type));
167            String pckName = calcPackage(t.getService(), rootPackage);
168            return MessageStructureTypeCalculator.calculate(this, model, type, realType,
169                    pckName);
170        }
171    }