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 */
016package org.kuali.student.contract.writer.service;
017
018import java.util.List;
019
020import org.kuali.student.contract.model.ServiceContractModel;
021import org.kuali.student.contract.model.ServiceMethod;
022import org.kuali.student.contract.model.ServiceMethodError;
023import org.kuali.student.contract.model.ServiceMethodParameter;
024import org.kuali.student.contract.model.XmlType;
025import org.kuali.student.contract.model.util.ModelFinder;
026import org.kuali.student.contract.writer.JavaClassWriter;
027
028/**
029 *
030 * @author nwright
031 */
032public class PureJavaInfcServiceWriter extends JavaClassWriter {
033
034    private ServiceContractModel model;
035    private ModelFinder finder;
036    private String directory;
037    private String rootPackage;
038    private String servKey;
039    private List<ServiceMethod> methods;
040
041    public PureJavaInfcServiceWriter(ServiceContractModel model,
042            String directory,
043            String rootPackage,
044            String servKey,
045            List<ServiceMethod> methods) {
046        super(directory, calcPackage(servKey, rootPackage), calcClassName(servKey));
047        this.model = model;
048        this.finder = new ModelFinder(model);
049        this.directory = directory;
050        this.rootPackage = rootPackage;
051        this.servKey = servKey;
052        this.methods = methods;
053    }
054
055    public static String calcPackage(String servKey, String rootPackage) {
056        String pack = rootPackage + "." + servKey.toLowerCase() + ".";
057//  StringBuffer buf = new StringBuffer (service.getVersion ().length ());
058//  for (int i = 0; i < service.getVersion ().length (); i ++)
059//  {
060//   char c = service.getVersion ().charAt (i);
061//   c = Character.toLowerCase (c);
062//   if (Character.isLetter (c))
063//   {
064//    buf.append (c);
065//    continue;
066//   }
067//   if (Character.isDigit (c))
068//   {
069//    buf.append (c);
070//   }
071//  }
072//  pack = pack + buf.toString ();
073        pack = pack + "api";
074        return pack;
075    }
076
077    public static String calcClassName(String servKey) {
078        return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceInfc");
079    }
080
081    /**
082     * Write out the entire file
083     * @param out
084     */
085    public void write() {
086        indentPrintln("public interface " + calcClassName(servKey));
087        openBrace();
088
089        for (ServiceMethod method : methods) {
090            indentPrintln("");
091            indentPrintln("/**");
092            indentPrintWrappedComment(method.getDescription());
093            indentPrintln("* ");
094            for (ServiceMethodParameter param : method.getParameters()) {
095                indentPrintWrappedComment("@param " + param.getName() + " - "
096                        + param.getType() + " - "
097                        + param.getDescription());
098            }
099            indentPrintWrappedComment("@return " + method.getReturnValue().
100                    getDescription());
101            indentPrintln("*/");
102            String type = method.getReturnValue().getType();
103            String realType = stripList(PureJavaInfcInfcWriter.calcClassName(type));
104            indentPrint("public " + calcType(type, realType) + " " + method.getName()
105                    + "(");
106            // now do parameters
107            String comma = "";
108            for (ServiceMethodParameter param : method.getParameters()) {
109                type = param.getType();
110                realType = stripList(PureJavaInfcInfcWriter.calcClassName(type));
111                print(comma);
112                print(calcType(type, realType));
113                print(" ");
114                print(param.getName());
115                comma = ", ";
116            }
117            println(")");
118            // now do exceptions
119            comma = "throws ";
120            incrementIndent();
121            for (ServiceMethodError error : method.getErrors()) {
122                indentPrint(comma);
123                String exceptionClassName = calcExceptionClassName(error);
124                String exceptionPackageName = this.calcExceptionPackageName(error);
125                println(exceptionClassName);
126                this.importsAdd(exceptionPackageName + "." + exceptionClassName);
127                comma = "      ,";
128            }
129            decrementIndent();
130            indentPrintln(";");
131
132        }
133
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 calcExceptionClassName(ServiceMethodError error) {
145        if (error.getClassName() == null) {
146            return ServiceExceptionWriter.calcClassName(error.getType());
147        }
148        return error.getClassName();
149    }
150
151    private String calcExceptionPackageName(ServiceMethodError error) {
152        if (error.getClassName() == null) {
153            return ServiceExceptionWriter.calcPackage(rootPackage);
154        }
155        return error.getPackageName();
156    }
157
158    private String calcType(String type, String realType) {
159        XmlType t = finder.findXmlType(this.stripList(type));
160        String pckName = PureJavaInfcInfcWriter.calcPackage(t.getService(), rootPackage);
161        return MessageStructureTypeCalculator.calculate(this, model, type, realType,
162                pckName);
163    }
164}