001    /**
002     * Copyright 2010 The Kuali Foundation Licensed under the
003     * Educational Community License, Version 2.0 (the "License"); you may
004     * not use this file except in compliance with the License. You may
005     * obtain a copy of the License at
006     *
007     * http://www.osedu.org/licenses/ECL-2.0
008     *
009     * Unless required by applicable law or agreed to in writing,
010     * software distributed under the License is distributed on an "AS IS"
011     * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012     * or implied. See the License for the specific language governing
013     * permissions and limitations under the License.
014     */
015    package org.kuali.student.lum.ui;
016    
017    import java.io.File;
018    import java.io.FileNotFoundException;
019    import java.io.FileOutputStream;
020    import java.io.OutputStream;
021    import java.io.PrintStream;
022    import java.util.ArrayList;
023    import java.util.Date;
024    import java.util.HashSet;
025    import java.util.LinkedHashMap;
026    import java.util.LinkedHashSet;
027    import java.util.List;
028    import java.util.Map;
029    import java.util.Set;
030    
031    import org.junit.Test;
032    import static org.junit.Assert.*;
033    
034    import org.kuali.student.common.assembly.data.Metadata;
035    import org.kuali.student.common.assembly.dictionary.MetadataFormatter;
036    import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl;
037    import org.kuali.student.common.dictionary.service.DictionaryService;
038    import org.kuali.student.common.dictionary.service.impl.DictionaryServiceImpl;
039    import org.kuali.student.core.proposal.dto.ProposalInfo;
040    import org.kuali.student.core.statement.dto.ReqCompFieldInfo;
041    import org.kuali.student.core.statement.dto.ReqComponentInfo;
042    import org.kuali.student.core.statement.dto.StatementInfo;
043    import org.kuali.student.lum.course.dto.CourseInfo;
044    import org.kuali.student.lum.program.dto.MajorDisciplineInfo;
045    import org.kuali.student.lum.program.dto.ProgramRequirementInfo;
046    
047    public class TestMetadataServiceDictionary {
048    
049            @Test
050            public void testMetadataService() {
051                    Set<String> startingClasses = new LinkedHashSet<String>();
052                    Map<String, Set<String>> types = new LinkedHashMap<String, Set<String>>();
053                    startingClasses.add(CourseInfo.class.getName());
054                    startingClasses.add(MajorDisciplineInfo.class.getName());
055                    startingClasses.add(ProgramRequirementInfo.class.getName());
056                    startingClasses.add(ProposalInfo.class.getName());
057                    startingClasses.add(StatementInfo.class.getName());
058                    startingClasses.add(ReqComponentInfo.class.getName());
059                    startingClasses.add(ReqCompFieldInfo.class.getName());
060                    startingClasses.add("cluset");
061                    startingClasses.add("courseSet");
062                    startingClasses.add("programSet");
063                    startingClasses.add("testSet");
064                    startingClasses.add("search");
065                    startingClasses.add("browse");
066    
067                    // startingClasses.add (StatementTreeViewInfo.class);
068    
069                    Set<String> typesForClass = new LinkedHashSet<String>();
070                    types.put(ReqCompFieldInfo.class.getName(), typesForClass);
071                    typesForClass.add("kuali.reqComponent.field.type.gpa");
072                    typesForClass.add("kuali.reqComponent.field.type.operator");
073                    typesForClass.add("kuali.reqComponent.field.type.clu.id");
074                    typesForClass.add("kuali.reqComponent.field.type.course.clu.id");
075                    typesForClass.add("kuali.reqComponent.field.type.program.clu.id");
076                    typesForClass.add("kuali.reqComponent.field.type.test.clu.id");
077                    typesForClass.add("kuali.reqComponent.field.type.test.score");
078                    typesForClass.add("kuali.reqComponent.field.type.cluSet.id");
079                    typesForClass.add("kuali.reqComponent.field.type.course.cluSet.id");
080                    typesForClass.add("kuali.reqComponent.field.type.program.cluSet.id");
081                    typesForClass.add("kuali.reqComponent.field.type.test.cluSet.id");
082                    typesForClass.add("kuali.reqComponent.field.type.person.id");
083                    typesForClass.add("kuali.reqComponent.field.type.org.id");
084                    typesForClass
085                                    .add("kuali.reqComponent.field.type.value.positive.integer");
086                    typesForClass.add("kuali.reqComponent.field.type.gradeType.id");
087                    typesForClass.add("kuali.reqComponent.field.type.grade.id");
088                    typesForClass.add("kuali.reqComponent.field.type.durationType.id");
089                    typesForClass.add("kuali.reqComponent.field.type.duration");
090    
091                    DictionaryService courseDictService = new DictionaryServiceImpl(
092                                    "classpath:ks-courseInfo-dictionary-context.xml");
093                    DictionaryService progDictService = new DictionaryServiceImpl(
094                                    "classpath:ks-programInfo-dictionary-context.xml");
095                    DictionaryService cluSetDictService = new DictionaryServiceImpl(
096                                    "classpath:ks-cluSetInfo-dictionary-context.xml");
097                    
098                    for (String objType : cluSetDictService.getObjectTypes()) {
099                            System.out.println("Cluset has object type=" + objType);
100                    }
101                    
102                    DictionaryService proposalDictService = new DictionaryServiceImpl(
103                                    "classpath:ks-proposalInfo-dictionary-context.xml");
104                    DictionaryService statementDictService = new DictionaryServiceImpl(
105                                    "classpath:ks-statement-dictionary-context.xml");
106                    MetadataServiceImpl metadataService = new MetadataServiceImpl(
107                                    courseDictService, progDictService, cluSetDictService,
108                                    proposalDictService, statementDictService);
109                    metadataService
110                                    .setUiLookupContext("classpath:lum-ui-lookup-context.xml");
111                    
112                    List<String> errors = new ArrayList<String>();
113                    for (String className : startingClasses) {
114                            String outFile = "target/metadata-for-" + className + ".txt";
115                            File file = new File(outFile);
116                            file.getParentFile().mkdirs();
117                            OutputStream outputStream = null;
118                            try {
119                                    outputStream = new FileOutputStream(file, false);
120                            } catch (FileNotFoundException ex) {
121                                    throw new RuntimeException(ex);
122                            }
123                            PrintStream out = new PrintStream(outputStream);
124                            out.println("(!) This page was automatically generated on "     + new Date());
125                            out.println("DO NOT UPDATE MANUALLY!");
126                            out.println("");
127                            out.println("This page represents a formatted view of the lum ui dictionary for " + className);
128                            out.println("");
129                            out.println("----");
130                            out.println("{toc}");
131                            out.println("----");
132                            // out.println ("getting meta data for " + className);
133                            Metadata metadata = metadataService.getMetadata(className);
134                            assertNotNull(metadata);
135                            errors.addAll(this.validateMetadata(metadata, className, null));
136                            MetadataFormatter formatter = new MetadataFormatter(className,
137                                            metadata, null, null, new HashSet<Metadata>(), 1);
138                            out.println(formatter.formatForWiki());
139                            if (types.get(className) == null) {
140                                    continue;
141                            }
142                            for (String type : types.get(className)) {
143                                    System.out.println("*** Generating formatted version for " + type);
144                                    metadata = metadataService.getMetadata(className, type,
145                                                    (String) null);
146                                    assertNotNull(metadata);
147                                    errors.addAll(this.validateMetadata(metadata, className, type));
148                                    formatter = new MetadataFormatter(className, metadata, type,
149                                                    null, new HashSet<Metadata>(), 1);
150                                    out.println(formatter.formatForWiki());
151                            }
152                            out.close();
153                    }
154                    if (errors.size() > 0) {
155                            for (String error : errors) {
156                                    System.out.println("error: " + error);
157                            }
158                            System.out.println(errors.size() + " errors found when validating metadata");
159    
160                            // These first 6 are because the recursion stops but the final field still is flagged as DATA even though it cannot have sub fields
161    
162                            //error: org.kuali.student.lum.course.dto.CourseInfo.courseSpecificLOs.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
163                            //error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.variations.*.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
164                            //error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.orgCoreProgram.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
165                            //error: org.kuali.student.lum.program.dto.MajorDisciplineInfo.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
166                            //error: org.kuali.student.lum.program.dto.ProgramRequirementInfo.statement.statements.*.statements.*.statements.* is of type DATA but it has no properties
167                            //error: org.kuali.student.lum.program.dto.ProgramRequirementInfo.learningObjectives.*.loDisplayInfoList.*.loDisplayInfoList.*.loDisplayInfoList.* is of type DATA but it has no properties
168    
169                            
170                            // 33 errors found when validating metadata
171                            if (errors.size() != 33) {
172                                    fail(errors.size() + " errors found when validating metadata");
173                            }
174                    }
175            }
176    
177            private MetadataServiceDictionaryValidator validator = null;
178    
179            private MetadataServiceDictionaryValidator getValidator() {
180                    if (validator == null) {
181                            validator = new MetadataServiceDictionaryValidator();
182                    }
183                    return validator;
184            }
185    
186            private List<String> validateMetadata(Metadata md, String name, String type) {
187                    return getValidator().validateMetadata(md, name, type);
188            }
189    }