Coverage Report - org.kuali.student.contract.writer.service.PureJavaInfcWriterForOneService
 
Classes in this File Line Coverage Branch Coverage Complexity
PureJavaInfcWriterForOneService
0%
0/58
0%
0/32
3.857
 
 1  
 /*
 2  
  * Copyright 2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may        obtain a copy of the License at
 7  
  *
 8  
  *         http://www.osedu.org/licenses/ECL-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.student.contract.writer.service;
 17  
 
 18  
 import java.util.HashSet;
 19  
 import java.util.List;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.kuali.student.contract.model.MessageStructure;
 23  
 import org.kuali.student.contract.model.ServiceContractModel;
 24  
 import org.kuali.student.contract.model.ServiceMethod;
 25  
 import org.kuali.student.contract.model.ServiceMethodParameter;
 26  
 import org.kuali.student.contract.model.ServiceMethodReturnValue;
 27  
 import org.kuali.student.contract.model.XmlType;
 28  
 import org.kuali.student.contract.model.util.ModelFinder;
 29  
 import org.kuali.student.contract.model.validation.DictionaryValidationException;
 30  
 
 31  
 /**
 32  
  *
 33  
  * @author nwright
 34  
  */
 35  
 public class PureJavaInfcWriterForOneService
 36  
 {
 37  
 
 38  
  private ServiceContractModel model;
 39  
  private ModelFinder finder;
 40  
  private String directory;
 41  
  private String rootPackage;
 42  
  private String servKey;
 43  
 
 44  
  public PureJavaInfcWriterForOneService (ServiceContractModel model,
 45  
                        String directory,
 46  
                        String rootPackage,
 47  
                        String servKey)
 48  0
  {
 49  0
   this.model = model;
 50  0
   this.finder = new ModelFinder (model);
 51  0
   this.directory = directory;
 52  0
   this.rootPackage = rootPackage;
 53  0
   this.servKey = servKey;
 54  0
  }
 55  
 
 56  
  /**
 57  
   * Write out the entire file
 58  
   * @param out
 59  
   */
 60  
  public void write ()
 61  
  {
 62  0
   List<ServiceMethod> methods = finder.getServiceMethodsInService (servKey);
 63  0
   if (methods.size () == 0)
 64  
   {
 65  0
    System.out.println ("No methods defined for servKey: " + servKey);
 66  0
    return;
 67  
   }
 68  
 
 69  
   // the main servKey
 70  0
   System.out.println ("Generating servKeys API's for " + servKey);
 71  0
   new PureJavaInfcServiceWriter (model, directory, rootPackage, servKey, methods).write ();
 72  
 
 73  
   // the beans's
 74  0
   System.out.println ("Generating info interfaces");
 75  0
   for (XmlType xmlType : getXmlTypesUsedJustByService ())
 76  
   {
 77  0
    System.out.println ("Generating Beans for " + xmlType.getName ());
 78  0
    new PureJavaInfcBeanWriter (model, directory, rootPackage, servKey, xmlType).write ();
 79  
   }
 80  
 
 81  
   // the Info interfaces's
 82  0
   System.out.println ("Generating Info interfaces");
 83  0
   for (XmlType xmlType : getXmlTypesUsedJustByService ())
 84  
   {
 85  0
    System.out.println ("Generating info interface for " + xmlType.getName ());
 86  0
    new PureJavaInfcInfcWriter (model, directory, rootPackage, servKey, xmlType).write ();
 87  
   }
 88  0
  }
 89  
 
 90  
  private Set<XmlType> getXmlTypesUsedJustByService ()
 91  
  {
 92  0
   Set<XmlType> set = new HashSet ();
 93  0
   for (XmlType type : model.getXmlTypes ())
 94  
   {
 95  0
    if (type.getService ().equalsIgnoreCase (servKey))
 96  
    {
 97  0
     if (type.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX))
 98  
     {
 99  0
      set.add (type);
 100  
     }
 101  
    }
 102  
   }
 103  0
   return set;
 104  
  }
 105  
 
 106  
  private Set<XmlType> getXmlTypesUsedByService (List<ServiceMethod> methods)
 107  
  {
 108  0
   Set<XmlType> set = new HashSet ();
 109  0
   for (ServiceMethod method : methods)
 110  
   {
 111  0
    if (method.getReturnValue () != null)
 112  
    {
 113  0
     ServiceMethodReturnValue ret = method.getReturnValue ();
 114  0
     XmlType xmlType = finder.findXmlType (stripListFromType (ret.getType ()));
 115  0
     if (xmlType == null)
 116  
     {
 117  0
      throw new DictionaryValidationException ("Method " + method.getService ()
 118  
                                               + "." + method.getName ()
 119  
                                               + "returns an unknown type, "
 120  
                                               + ret.getType ());
 121  
     }
 122  0
     addTypeAndAllSubTypes (set, xmlType);
 123  
    }
 124  0
    for (ServiceMethodParameter param : method.getParameters ())
 125  
    {
 126  0
     XmlType xmlType = finder.findXmlType (stripListFromType (param.getType ()));
 127  0
     if (xmlType == null)
 128  
     {
 129  0
      throw new DictionaryValidationException ("Parameter "
 130  
                                               + method.getService () + "."
 131  
                                               + method.getName () + "."
 132  
                                               + param.getName ()
 133  
                                               + "has an unknown type, "
 134  
                                               + param.getType ());
 135  
     }
 136  0
     addTypeAndAllSubTypes (set, xmlType);
 137  0
    }
 138  
   }
 139  0
   return set;
 140  
  }
 141  
 
 142  
  private void addTypeAndAllSubTypes (Set<XmlType> set, XmlType xmlType)
 143  
  {
 144  0
   if (xmlType.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX))
 145  
   {
 146  0
    if (set.add (xmlType))
 147  
    {
 148  0
     addXmlTypesUsedByMessageStructure (set, xmlType);
 149  
    }
 150  
   }
 151  0
  }
 152  
 
 153  
  private String stripListFromType (String type)
 154  
  {
 155  0
   if (type.endsWith ("List"))
 156  
   {
 157  0
    type = type.substring (0, type.length () - "List".length ());
 158  
   }
 159  0
   return type;
 160  
  }
 161  
 
 162  
  private void addXmlTypesUsedByMessageStructure (Set<XmlType> set,
 163  
                                                  XmlType xmlType)
 164  
  {
 165  0
   ModelFinder finder = new ModelFinder (model);
 166  0
   for (MessageStructure ms : finder.findMessageStructures (xmlType.getName ()))
 167  
   {
 168  0
    XmlType subType = finder.findXmlType (stripListFromType (ms.getType ()));
 169  0
    if (subType == null)
 170  
    {
 171  0
     throw new DictionaryValidationException ("MessageStructure field "
 172  
                                              + ms.getId ()
 173  
                                              + " has an unknown type, "
 174  
                                              + ms.getType ());
 175  
    }
 176  0
    addTypeAndAllSubTypes (set, subType);
 177  0
   }
 178  0
  }
 179  
 }