Clover Coverage Report - Kuali Student 1.2-M5-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Aug 29 2011 05:04:27 EDT
../../../../../../img/srcFileCovDistChart6.png 35% of files have more coverage
353   611   108   12.61
144   548   0.31   28
28     3.86  
1    
 
  MetadataFormatter       Line # 29 353 0% 108 220 58.1% 0.5809524
 
  (1)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15    package org.kuali.student.common.assembly.dictionary;
16   
17    import java.util.ArrayList;
18    import java.util.Collections;
19    import java.util.List;
20    import java.util.Set;
21   
22    import org.kuali.student.common.assembly.data.ConstraintMetadata;
23    import org.kuali.student.common.assembly.data.Data;
24    import org.kuali.student.common.assembly.data.LookupMetadata;
25    import org.kuali.student.common.assembly.data.LookupParamMetadata;
26    import org.kuali.student.common.assembly.data.Metadata;
27    import org.kuali.student.common.assembly.data.MetadataInterrogator;
28   
 
29    public class MetadataFormatter {
30   
31    private StringBuilder builder = new StringBuilder(5000);
32    private Metadata structureMetadata;
33    private String type;
34    private String rowSeperator = "\n";
35    private String colSeperator = "|";
36    private String structureName;
37    private int level;
38    // private Map<String, Metadata> subStructuresToProcess = new LinkedHashMap<String, Metadata>();
39    private Set<Metadata> structuresAlreadyProcessed;
40    private MetadataFormatter parent;
41   
 
42  4 toggle public MetadataFormatter(String structureName, Metadata structureMetadata,
43    String type, MetadataFormatter parent,
44    Set<Metadata> structuresAlreadyProcessed, int level) {
45  4 this.structureName = structureName;
46  4 this.structureMetadata = structureMetadata;
47  4 this.type = type;
48  4 this.parent = parent;
49  4 this.structuresAlreadyProcessed = structuresAlreadyProcessed;
50  4 this.level = level;
51    }
52   
 
53  0 toggle public String getStructureName() {
54  0 return structureName;
55    }
56   
 
57  0 toggle public MetadataFormatter getParent() {
58  0 return parent;
59    }
60   
 
61  0 toggle public String getRowSeperator() {
62  0 return rowSeperator;
63    }
64   
 
65  0 toggle public void setRowSeperator(String rowSeperator) {
66  0 this.rowSeperator = rowSeperator;
67    }
68   
 
69  0 toggle public String getColSeparator() {
70  0 return colSeperator;
71    }
72   
 
73  0 toggle public void setColSeparator(String separator) {
74  0 this.colSeperator = separator;
75    }
76   
 
77  150 toggle private String pad(String str, int size) {
78  150 StringBuilder padStr = new StringBuilder(size);
79  150 padStr.append(str);
80  2058 while (padStr.length() < size) {
81  1908 padStr.append(' ');
82    }
83  150 return padStr.toString();
84    }
85   
 
86  4 toggle public String formatForWiki() {
87  4 if (!this.structuresAlreadyProcessed.add(structureMetadata)) {
88  0 return "";
89    }
90  4 if (level == 1) {
91  4 builder.append(rowSeperator);
92    // builder.append
93    // ("======= start dump of object structure definition ========");
94  4 builder.append(rowSeperator);
95  4 String header = "h1. " + this.calcSimpleName(structureName);
96  4 if (type != null) {
97  2 header = "h2. " + type;
98    }
99  4 builder.append(header);
100  4 builder.append("{anchor:" + structureName + "}");
101  4 builder.append(rowSeperator);
102  4 builder.append("The object key is " + structureName);
103  4 builder.append(rowSeperator);
104  4 builder.append("The type is " + type);
105  4 builder.append(rowSeperator);
106  4 builder.append(colSeperator);
107  4 builder.append(colSeperator);
108  4 builder.append("Field");
109  4 builder.append(colSeperator);
110  4 builder.append(colSeperator);
111  4 builder.append("Required?");
112  4 builder.append(colSeperator);
113  4 builder.append(colSeperator);
114  4 builder.append("DataType");
115  4 builder.append(colSeperator);
116  4 builder.append(colSeperator);
117  4 builder.append("Length");
118  4 builder.append(colSeperator);
119  4 builder.append(colSeperator);
120  4 builder.append("Dynamic");
121  4 builder.append(colSeperator);
122  4 builder.append(colSeperator);
123  4 builder.append("Default");
124  4 builder.append(colSeperator);
125  4 builder.append(colSeperator);
126  4 builder.append("Repeats?");
127  4 builder.append(colSeperator);
128  4 builder.append(colSeperator);
129  4 builder.append("Valid Characters");
130  4 builder.append(colSeperator);
131  4 builder.append(colSeperator);
132  4 builder.append("Lookup Widget");
133  4 builder.append(colSeperator);
134  4 builder.append(colSeperator);
135  4 builder.append("Lookup");
136  4 builder.append(colSeperator);
137  4 builder.append(colSeperator);
138  4 builder.append(rowSeperator);
139    }
140   
141  4 List<String> keys = new ArrayList<String>();
142  4 keys.addAll(structureMetadata.getProperties().keySet());
143  4 Collections.sort(keys);
144  4 for (String key : keys) {
145  25 if (key.equalsIgnoreCase ("_runtimeData"))
146    {
147  0 continue;
148    }
149  25 Metadata fieldMeta = structureMetadata.getProperties().get(key);
150  25 builder.append(colSeperator);
151  25 builder.append(calcFieldInfo (key, fieldMeta));
152  25 builder.append(colSeperator);
153  25 builder.append(pad(calcRequired(fieldMeta), 10));
154  25 builder.append(colSeperator);
155  25 builder.append(pad(calcDataType(fieldMeta), 25));
156  25 builder.append(colSeperator);
157  25 builder.append(pad(calcLength(fieldMeta), 15));
158  25 builder.append(colSeperator);
159  25 builder.append(pad(calcDynamic(fieldMeta), 7));
160  25 builder.append(colSeperator);
161  25 builder.append(pad(calcDefaultValue(fieldMeta), 15));
162  25 builder.append(colSeperator);
163  25 builder.append(calcRepeating(fieldMeta));
164  25 builder.append(colSeperator);
165  25 builder.append(calcValidChars(fieldMeta));
166  25 builder.append(colSeperator);
167  25 builder.append(calcWidget(fieldMeta));
168  25 builder.append(colSeperator);
169  25 builder.append(calcLookup(fieldMeta));
170  25 builder.append(colSeperator);
171  25 builder.append(rowSeperator);
172  25 if (fieldMeta.getDataType().equals(Data.DataType.DATA)
173    || fieldMeta.getDataType().equals(Data.DataType.LIST)) {
174  0 if (fieldMeta.getProperties() == null) {
175  0 throw new IllegalArgumentException(
176    fieldMeta.getName()
177    + " is DATA but does not have a sub-structure defined");
178    }
179  0 MetadataFormatter formatter = new MetadataFormatter(key,
180    fieldMeta, null, this, structuresAlreadyProcessed,
181    level + 1);
182  0 builder.append(formatter.formatForWiki());
183    }
184    }
185   
186  4 return builder.toString();
187    }
188   
 
189  25 toggle private String calcDataType(Metadata fieldMeta) {
190  25 if (fieldMeta.getDataType().equals(Data.DataType.LIST)) {
191  0 StringBuilder type = new StringBuilder();
192  0 type.append("LIST of ");
193  0 String comma = "";
194  0 for (String key : fieldMeta.getProperties().keySet()) {
195  0 type.append(comma);
196  0 type.append(fieldMeta.getProperties().get(key).getDataType()
197    .toString());
198  0 comma = ", ";
199    }
200  0 return type.toString();
201    }
202  25 return fieldMeta.getDataType().toString();
203    }
204   
 
205  25 toggle private String calcDefaultValue(Metadata fieldMeta) {
206  25 if (fieldMeta.getDefaultValue() != null) {
207  0 return fieldMeta.getDefaultValue().toString();
208    }
209  25 return " ";
210    }
211   
 
212  25 toggle private String calcDynamic(Metadata meta) {
213  25 if (meta.isDynamic()) {
214  0 return "dynamic";
215    }
216  25 return " ";
217    }
218   
 
219  25 toggle private String calcFieldInfo (String key, Metadata fieldMeta)
220    {
221  25 StringBuilder bldr = new StringBuilder (40);
222  25 bldr.append (pad(calcFullyQualifiedFieldName(key), 30));
223  25 if (fieldMeta.getLabelKey () != null)
224    {
225  0 bldr.append ("\\\\\n");
226  0 bldr.append ("Label: ");
227  0 bldr.append (fieldMeta.getLabelKey ());
228    }
229  25 return bldr.toString ();
230    }
231   
 
232  25 toggle public String calcFullyQualifiedFieldName(String fieldName) {
233  25 if (parent == null) {
234  25 return escapeWiki(fieldName);
235    }
236  0 return parent.calcFullyQualifiedFieldName(structureName) + "."
237    + escapeWiki(fieldName);
238    }
239   
 
240  4 toggle private String calcSimpleName(String name) {
241  4 if (name.lastIndexOf(".") != -1) {
242  0 name = name.substring(name.lastIndexOf(".") + 1);
243    }
244  4 return name;
245    }
246   
 
247  0 toggle private String calcNotSoSimpleName(String name) {
248  0 if (name.lastIndexOf(".") == -1) {
249  0 return name;
250    }
251  0 String simpleName = calcSimpleName(name);
252  0 String fieldName = calcSimpleName(name.substring(0, name.length()
253    - simpleName.length() - 1));
254  0 return fieldName + "." + simpleName;
255    }
256   
 
257  25 toggle private String calcRequired(Metadata fieldMeta) {
258  25 for (ConstraintMetadata cons : fieldMeta.getConstraints()) {
259  25 if (cons.getMaxOccurs() != null) {
260  12 if (cons.getMaxOccurs() == 0) {
261  0 return "NOT USED";
262    }
263    }
264   
265  25 if (cons.getMinOccurs() != null) {
266  24 if (cons.getMinOccurs() >= 1) {
267  11 return "required";
268    }
269    }
270    }
271  14 return " ";
272    // return "optional";
273    }
274   
275    private static final String LINK_TO_DEFINITIONS = "KULSTG:Formatted View of Base Dictionary#Valid Character Definitions";
276   
 
277  25 toggle private String calcValidChars(Metadata fieldMeta) {
278  25 for (ConstraintMetadata cons : fieldMeta.getConstraints()) {
279  25 if (cons.getValidChars() == null) {
280  18 continue;
281    }
282  7 String validChars = escapeWiki(cons.getValidChars());
283  7 String descr = "[" + "See" + "|" + LINK_TO_DEFINITIONS + "]"
284    + "\\\\\n" + validChars;
285  7 return descr;
286    }
287  18 return " ";
288    }
289   
 
290  32 toggle private String escapeWiki(String str) {
291  32 StringBuilder bldr = new StringBuilder(str.length());
292  32 boolean precededByBackSlash = false;
293  266 for (int i = 0; i < str.length(); i++) {
294  234 char c = str.charAt(i);
295  234 switch (c) {
296  0 case '\\':
297  7 case '[':
298  7 case '*':
299  7 case ']':
300  0 case '|':
301  21 if (!precededByBackSlash) {
302  21 bldr.append('\\');
303    }
304  21 break;
305  213 default:
306  213 break;
307    }
308  234 bldr.append(c);
309  234 if (c == '\\') {
310  0 precededByBackSlash = true;
311    } else {
312  234 precededByBackSlash = false;
313    }
314    }
315  32 return bldr.toString();
316    }
317   
 
318  25 toggle private String calcWidget(Metadata fieldMeta) {
319  25 StringBuilder bldr = new StringBuilder();
320  25 String comma = "";
321  25 if (!fieldMeta.isCanEdit()) {
322  2 bldr.append(comma);
323  2 bldr.append("not editable");
324  2 comma = ", ";
325    }
326  25 if (!fieldMeta.isCanView()) {
327  0 bldr.append(comma);
328  0 bldr.append("not viewable");
329  0 comma = ", ";
330    }
331  25 if (!fieldMeta.isCanUnmask()) {
332  1 bldr.append(comma);
333  1 bldr.append("Not unmaskable");
334  1 comma = ", ";
335    }
336  25 if (fieldMeta.getInitialLookup() != null) {
337  2 bldr.append(comma);
338  2 bldr.append(fieldMeta.getInitialLookup().getWidget());
339  2 comma = ", ";
340    }
341  25 if (bldr.length() == 0) {
342  21 bldr.append(" ");
343    }
344  25 return bldr.toString();
345    }
346   
 
347  25 toggle private String calcLookup(Metadata fieldMeta) {
348  25 StringBuilder bldr = new StringBuilder ();
349  25 if (fieldMeta.getInitialLookup() != null) {
350  2 bldr.append (calcLookup (fieldMeta.getInitialLookup ()));
351    }
352   
353  25 if (fieldMeta.getAdditionalLookups () != null) {
354  25 if (fieldMeta.getAdditionalLookups ().size () > 0) {
355  0 if (fieldMeta.getInitialLookup() == null) {
356  0 bldr.append ("No initial lookup but...");
357    }
358  0 bldr.append("\\\\");
359  0 bldr.append("\n");
360  0 bldr.append("\\\\");
361  0 bldr.append("\n");
362  0 bldr.append ("Additional Lookups:");
363  0 bldr.append("\\\\");
364  0 bldr.append("\n");
365    }
366  25 for (LookupMetadata lm : fieldMeta.getAdditionalLookups ()) {
367  0 bldr.append("\\\\");
368  0 bldr.append("\n");
369  0 bldr.append (calcLookup (lm));
370  0 bldr.append("\\\\");
371  0 bldr.append("\n");
372    }
373    }
374  25 if (bldr.length () == 0)
375    {
376  23 bldr.append (" ");
377    }
378  25 return bldr.toString ();
379    }
380   
 
381  2 toggle private String calcLookup(LookupMetadata lm) {
382  2 StringBuilder bldr = new StringBuilder();
383  2 bldr.append(lm.getId());
384    // if (lm.getUsage () != null) {
385    // bldr.append (" usage " + lm.getUsage ());
386    // }
387    // this is the search description not the lookup description
388  2 bldr.append (" - ");
389  2 bldr.append (lm.getName());
390  2 bldr.append (" " + lm.getWidget ());
391  2 String and = " with option ";
392  2 if (lm.getWidgetOptions () != null) {
393  0 for (LookupMetadata.WidgetOption wo: lm.getWidgetOptions ().keySet ())
394    {
395  0 bldr.append (" and ");
396  0 bldr.append (wo);
397  0 bldr.append ("=");
398  0 bldr.append (lm.getWidgetOptions ().get (wo));
399    }
400    }
401  2 and = "";
402  2 bldr.append("\\\\\n");
403  2 bldr.append("Implemented using search: ");
404  2 String searchPage = calcWikiSearchPage(lm.getSearchTypeId());
405  2 bldr.append("[" + lm.getSearchTypeId() + "|" + searchPage + "#"
406    + lm.getSearchTypeId() + "]");
407  2 List<LookupParamMetadata> configuredParameters = filterConfiguredParams(lm
408    .getParams());
409  2 if (configuredParameters.size() > 0) {
410  2 bldr.append("\\\\");
411  2 bldr.append("\n");
412  2 bldr.append(" where ");
413  2 and = "";
414  2 for (LookupParamMetadata param : configuredParameters) {
415  2 bldr.append(and);
416  2 and = " and ";
417  2 bldr.append(param.getName());
418  2 bldr.append (" (" + param.getDataType () + ") ");
419  2 bldr.append("=");
420  2 if (param.getDefaultValueString() != null) {
421  2 bldr.append(param.getDefaultValueString());
422    }
423  2 if (param.getDefaultValueList() != null) {
424  0 String comma = "";
425  0 for (String defValue : param.getDefaultValueList()) {
426  0 bldr.append(comma);
427  0 comma = ", ";
428  0 bldr.append(defValue);
429    }
430    }
431    }
432    }
433  2 List<LookupParamMetadata> userEnterableParameters = this.filterUserEnterableParams (lm
434    .getParams());
435  2 if (userEnterableParameters.size() > 0) {
436  0 bldr.append("\\\\");
437  0 bldr.append("\n");
438  0 bldr.append(" and the user can enter: ");
439  0 for (LookupParamMetadata param : userEnterableParameters) {
440  0 bldr.append ("\\\\\n");
441  0 bldr.append(param.getName());
442  0 bldr.append (" (" + param.getDataType () + ")");
443  0 if (param.getWidget () != null) {
444  0 bldr.append(" using widget ");
445  0 bldr.append (param.getWidget ());
446    }
447  0 if (param.getDefaultValueString() != null) {
448  0 bldr.append("defaulted to " + param.getDefaultValueString());
449    }
450  0 if (param.getDefaultValueList() != null) {
451  0 String comma = "defaulted to ";
452  0 for (String defValue : param.getDefaultValueList()) {
453  0 bldr.append(comma);
454  0 comma = ", ";
455  0 bldr.append(defValue);
456    }
457    }
458  0 if (param.getChildLookup () != null)
459    {
460  0 bldr.append("\\\\");
461  0 bldr.append("\n");
462  0 bldr.append ("using a child lookup: ");
463  0 bldr.append("\\\\");
464  0 bldr.append("\n");
465  0 bldr.append (calcLookup (param.getChildLookup ()));
466    }
467    }
468    }
469  2 return bldr.toString();
470    }
471   
472    private static final String PAGE_PREFIX = "Formatted View of ";
473    private static final String PAGE_SUFFIX = " Searches";
474   
 
475  2 toggle private String calcWikiSearchPage(String searchType) {
476  2 return PAGE_PREFIX + calcWikigPageAbbrev(searchType) + PAGE_SUFFIX;
477    }
478   
 
479  2 toggle private String calcWikigPageAbbrev(String searchType) {
480  2 if (searchType == null) {
481  0 return null;
482    }
483  2 if (searchType.equals("enumeration.management.search")) {
484  2 return "EM";
485    }
486  0 if (searchType.startsWith("lu.")) {
487  0 return "LU";
488    }
489  0 if (searchType.startsWith("cluset.")) {
490  0 return "LU";
491    }
492  0 if (searchType.startsWith("lo.")) {
493  0 return "LO";
494    }
495  0 if (searchType.startsWith("lrc.")) {
496  0 return "LRC";
497    }
498  0 if (searchType.startsWith("comment.")) {
499  0 return "Comment";
500    }
501  0 if (searchType.startsWith("org.")) {
502  0 return "Organization";
503    }
504  0 if (searchType.startsWith("atp.")) {
505  0 return "ATP";
506    }
507  0 if (searchType.startsWith("person.")) {
508  0 return "Person";
509    }
510  0 if (searchType.startsWith("proposal.")) {
511  0 return "Proposal";
512    }
513  0 if (searchType.startsWith("subjectCode.")) {
514  0 return "SC";
515    }
516  0 throw new IllegalArgumentException("Unknown type of search: "
517    + searchType);
518    }
519   
 
520  2 toggle private List<LookupParamMetadata> filterConfiguredParams(
521    List<LookupParamMetadata> params) {
522  2 List<LookupParamMetadata> list = new ArrayList<LookupParamMetadata>();
523  2 if (params == null) {
524  0 return list;
525    }
526  2 if (params.size() == 0) {
527  0 return list;
528    }
529  2 for (LookupParamMetadata param : params) {
530  10 if (param.getDefaultValueString() != null) {
531  2 list.add(param);
532  2 continue;
533    }
534  8 if (param.getDefaultValueList() != null) {
535  0 list.add(param);
536  0 continue;
537    }
538    }
539  2 return list;
540    }
541   
 
542  2 toggle private List<LookupParamMetadata> filterUserEnterableParams (
543    List<LookupParamMetadata> params) {
544  2 List<LookupParamMetadata> list = new ArrayList<LookupParamMetadata>();
545  2 if (params == null) {
546  0 return list;
547    }
548  2 if (params.size() == 0) {
549  0 return list;
550    }
551  2 for (LookupParamMetadata param : params) {
552  10 if (param.getWriteAccess () != null) {
553  8 if ( ! param.getWriteAccess ().equals (Metadata.WriteAccess.NEVER)) {
554  0 list.add(param);
555  0 continue;
556    }
557    }
558    }
559  2 return list;
560    }
561   
 
562  25 toggle private String calcRepeating(Metadata fieldMeta) {
563  25 if (!fieldMeta.getDataType().equals(Data.DataType.LIST)) {
564  25 return " ";
565    }
566    // return "repeating";
567  0 MetadataInterrogator mi = new MetadataInterrogator(fieldMeta);
568  0 if (mi.getSmallestMaxOccurs() == null) {
569  0 if (mi.getLargestMinOccurs() != null
570    && mi.getLargestMinOccurs() > 1) {
571  0 return "repeating: minimum " + mi.getLargestMinOccurs()
572    + " times";
573    }
574  0 return "repeating: unlimited";
575    }
576  0 if (mi.getSmallestMaxOccurs() == 0) {
577  0 return "NOT USED";
578    }
579  0 if (mi.getSmallestMaxOccurs() == 1) {
580  0 return " ";
581    // return "single";
582    }
583   
584  0 if (mi.getLargestMinOccurs() != null) {
585  0 if (mi.getLargestMinOccurs() > 1) {
586  0 return "repeating: " + mi.getLargestMinOccurs() + " to "
587    + mi.getSmallestMaxOccurs() + " times";
588    }
589    }
590  0 return "repeating: maximum " + mi.getSmallestMaxOccurs() + " times";
591    }
592   
 
593  25 toggle private String calcLength(Metadata fieldMeta) {
594  25 MetadataInterrogator mi = new MetadataInterrogator(fieldMeta);
595  25 if (mi.getSmallestMaxLength() != null) {
596  22 if (mi.getLargestMinLength() != null
597    && mi.getLargestMinLength() != 0) {
598  22 if (mi.getSmallestMaxLength() == mi.getLargestMinLength()) {
599  0 return ("(must be " + mi.getSmallestMaxLength() + ")");
600    }
601  22 return "(" + mi.getLargestMinLength() + " to "
602    + mi.getSmallestMaxLength() + ")";
603    }
604  0 return "(up to " + mi.getSmallestMaxLength() + ")";
605    }
606  3 if (mi.getLargestMinLength() != null) {
607  0 return "(over " + mi.getLargestMinLength() + ")";
608    }
609  3 return " ";
610    }
611    }