View Javadoc

1   /*
2    * Copyright 2009 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.admin.ui.mojo;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.PrintStream;
22  import java.util.List;
23  import java.util.Stack;
24  import org.kuali.student.contract.model.Lookup;
25  import org.kuali.student.contract.model.MessageStructure;
26  import org.kuali.student.contract.model.Service;
27  
28  import org.kuali.student.contract.model.ServiceContractModel;
29  import org.kuali.student.contract.model.ServiceMethod;
30  import org.kuali.student.contract.model.XmlType;
31  import org.kuali.student.contract.model.util.ModelFinder;
32  import org.kuali.student.contract.writer.XmlWriter;
33  import org.kuali.student.contract.writer.service.GetterSetterNameCalculator;
34  
35  /**
36   *
37   * @author nwright
38   */
39  public class AdminUiInquiryViewBeanWriter {
40  
41      private ServiceContractModel model;
42      private ModelFinder finder;
43      private String directory;
44      private String rootPackage;
45      private String servKey;
46      private Service service;
47      private XmlType xmlType;
48      private List<ServiceMethod> methods;
49      private XmlWriter out;
50      String fileName;
51      String fullDirectoryPath;
52  
53      public AdminUiInquiryViewBeanWriter(ServiceContractModel model,
54              String directory,
55              String rootPackage,
56              String servKey,
57              XmlType xmlType,
58              List<ServiceMethod> methods) {
59          this.model = model;
60          this.finder = new ModelFinder(model);
61          this.directory = directory;
62          this.rootPackage = rootPackage;
63          this.servKey = servKey;
64          service = finder.findService(servKey);
65          this.xmlType = xmlType;
66          this.methods = methods;
67      }
68  
69      /**
70       * Write out the entire file
71       *
72       * @param out
73       */
74      public void write() {
75          initXmlWriter();
76          writeBoilerPlate();
77          writeBean();
78      }
79  
80      private void writeBean() {
81          String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
82          String serviceClass = GetterSetterNameCalculator.calcInitUpper(service.getName());
83          String serviceVar = GetterSetterNameCalculator.calcInitLower(service.getName());
84          String serviceContract = service.getImplProject() + "." + service.getName();
85          String inquirable = AdminUiInquirableWriter.calcPackage(servKey, rootPackage, xmlType) + "."
86                  + AdminUiInquirableWriter.calcClassName(servKey, xmlType);
87          out.println("");
88          out.incrementIndent();
89          out.indentPrintln("<import resource=\"classpath:ks-" + infoClass + "-dictionary.xml\"/>");
90          out.indentPrintln("<import resource=\"classpath:UifKSDefinitions.xml\"/>");
91          out.indentPrintln("<!-- **********************************************");
92          out.indentPrintln("Paste bean definition below into the list of dataDictionaryPackages of org.kuali.rice.krad.bo.ModuleConfiguration ");
93          out.indentPrintln ("<value>classpath:" + fullDirectoryPath + "/" + fileName + "</value>");
94          out.indentPrintln("********************************************** -->");
95          out.indentPrintln("<!-- InquiryView -->");
96          out.indentPrintln("<bean id=\"KS-" + infoClass + "-AdminInquiryView\" parent=\"KS-Uif-InquiryView\"");
97          out.incrementIndent();
98          out.indentPrintln("p:title=\"" + xmlType.getName() + " Inquiry\"");
99          out.indentPrintln("p:dataObjectClassName=\"" + xmlType.getJavaPackage() + "." + infoClass + "\"");
100         out.indentPrintln("p:viewHelperServiceClass=\"" + inquirable + "\">");
101         out.indentPrintln("");
102         out.indentPrintln("<property name=\"Items\">");
103         out.indentPrintln("    <list>");
104         out.indentPrintln("        <bean parent=\"Uif-Disclosure-GridSection\">");
105         out.indentPrintln("            <property name=\"layoutManager.numberOfColumns\" value=\"2\"/>");
106         out.indentPrintln("            <property name=\"headerText\" value=\"" + xmlType.getName() + " Inquiry\"/>");
107         out.indentPrintln("            <property name=\"items\">");
108         out.indentPrintln("                <list>");
109         this.writeFields(xmlType, new Stack<XmlType>(), "");
110         out.indentPrintln("                </list>");
111         out.indentPrintln("            </property>");
112         out.indentPrintln("        </bean>");
113         out.indentPrintln("    </list>");
114         out.indentPrintln("</property>");
115         out.decrementIndent();
116         out.indentPrintln("</bean>");
117         out.indentPrintln("");
118         out.decrementIndent();
119         out.indentPrintln("</beans>");
120 
121     }
122 
123     private void writeFields(XmlType type, Stack<XmlType> parents, String prefix) {
124         // avoid recursion
125         if (parents.contains(type)) {
126             return;
127         }
128         parents.push(type);
129         for (MessageStructure ms : finder.findMessageStructures(type.getName())) {
130             String fieldName = GetterSetterNameCalculator.calcInitLower(ms.getShortName());
131             if (!prefix.isEmpty()) {
132                 fieldName = prefix + "." + fieldName;
133             }
134             if (ms.getType().equalsIgnoreCase("AttributeInfoList")) {
135                 out.indentPrintln("                    <!-- TODO: deal with dynamic attributes -->");
136                 continue;
137             }
138             if (ms.getType().endsWith("List")) {
139                 out.indentPrintln("                    <!-- TODO: deal with " + fieldName + " which is a list -->");
140                 continue;
141             }
142             XmlType fieldType = finder.findXmlType(ms.getType());
143             if (fieldType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
144                 // complex sub-types such as rich text 
145                 this.writeFields(fieldType, parents, fieldName);
146                 continue;
147             }
148             out.indentPrint("                    <bean parent=\"Uif-DataField\" p:propertyName=\"" + fieldName + "\"");
149             if (!shouldDoLookup (ms.getLookup())) {
150                 out.println(" />");
151                 continue;
152             }
153             // process lookup
154             out.println(">");
155             Lookup lookup = ms.getLookup();
156             XmlType msType = finder.findXmlType(lookup.getXmlTypeName());
157             MessageStructure pk = this.getPrimaryKey(ms.getLookup().getXmlTypeName());
158             out.indentPrintln("                        <property name=\"inquiry\">");
159             out.indentPrintln("                            <bean parent=\"Uif-Inquiry\" p:dataObjectClassName=\"" 
160                     + msType.getJavaPackage() + "." + msType.getName() 
161                     + "\" p:inquiryParameters=\"" + fieldName + ":" + pk.getShortName() + "\" />");
162             out.indentPrintln("                        </property>");
163             out.indentPrintln("                    </bean>");
164         }
165         parents.pop();
166     }
167 
168         
169     public static boolean shouldDoLookup (Lookup lookup) {
170         if (lookup == null) {
171             return false;
172         }
173         
174         // can't do lookups on things we don't have inquirables on yet        
175         if (lookup.getXmlTypeName().equals("OrgInfo")) {
176             return false;
177         }
178         if (lookup.getXmlTypeName().equals("Principal")) {
179             return false;
180         }
181         if (lookup.getXmlTypeName().equals("Agenda")) {
182             return false;
183         }
184         return true;
185     }
186     
187     private MessageStructure getPrimaryKey(String xmlTypeName) {
188         for (MessageStructure ms : finder.findMessageStructures(xmlTypeName)) {
189             if (ms.isPrimaryKey()) {
190                 return ms;
191             }
192         }
193         throw new NullPointerException ("could not find primary key for " + xmlTypeName);
194     }
195     private void initXmlWriter() {
196         String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
197         String serviceClass = GetterSetterNameCalculator.calcInitUpper(service.getName());
198         String serviceVar = GetterSetterNameCalculator.calcInitLower(service.getName());
199         String serviceContract = service.getImplProject() + "." + service.getName();
200 
201         fullDirectoryPath = AdminUiInquirableWriter.calcPackage(servKey, rootPackage, xmlType).replace('.', '/');
202         fileName = infoClass + "AdminInquiryView.xml";
203 
204         File dir = new File(this.directory);
205 
206         if (!dir.exists()) {
207             if (!dir.mkdirs()) {
208                 throw new IllegalStateException("Could not create directory "
209                         + this.directory);
210             }
211         }
212 
213         String dirStr = this.directory + "/" + "resources" + "/" + fullDirectoryPath;
214         File dirFile = new File(dirStr);
215         if (!dirFile.exists()) {
216             if (!dirFile.mkdirs()) {
217                 throw new IllegalStateException(
218                         "Could not create directory " + dirStr);
219             }
220         }
221         try {
222             PrintStream out = new PrintStream(new FileOutputStream(
223                     dirStr + "/" + fileName, false));
224             this.out = new XmlWriter(out, 0);
225         } catch (FileNotFoundException ex) {
226             throw new IllegalStateException(ex);
227         }
228     }
229 
230     private void writeBoilerPlate() {
231         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
232         out.println("<beans xmlns=\"http://www.springframework.org/schema/beans\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
233         out.println("       xmlns:p=\"http://www.springframework.org/schema/p\"");
234         out.println("       xsi:schemaLocation=\"http://www.springframework.org/schema/beans");
235         out.println("                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\">");
236         out.println("    <!--");
237         out.println("     Copyright 2007-2012 The Kuali Foundation");
238         out.println("");
239         out.println("     Licensed under the Educational Community License, Version 2.0 (the \"License\");");
240         out.println("     you may not use this file except in compliance with the License.");
241         out.println("     You may obtain a copy of the License at");
242         out.println("");
243         out.println("     http://www.opensource.org/licenses/ecl2.php");
244         out.println("");
245         out.println("     Unless required by applicable law or agreed to in writing, software");
246         out.println("     distributed under the License is distributed on an \"AS IS\" BASIS,");
247         out.println("     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
248         out.println("     See the License for the specific language governing permissions and");
249         out.println("     limitations under the License.");
250         out.println("    -->");
251 
252     }
253 }