001    /*
002     * Copyright 2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 1.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/ecl1.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     */
016    package org.kuali.student.datadictionary.util;
017    
018    import java.util.ArrayList;
019    import java.util.HashSet;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Set;
023    import org.kuali.rice.krad.datadictionary.DataObjectEntry;
024    import org.springframework.context.ApplicationContext;
025    import org.springframework.context.support.ClassPathXmlApplicationContext;
026    
027    public class DictionaryTesterHelper {
028    
029        private String outputDir;
030        private Set<String> configFiles;
031        private ApplicationContext ac = null;
032    
033        public DictionaryTesterHelper(String outputDir,
034                Set<String> configFiles) {
035            this.outputDir = outputDir;
036            this.configFiles = configFiles;
037        }
038    
039        public void loadApplicationContext() {
040            System.out.println ("DictionaryTesterHelper: begin loading application context");
041            List<String> configLocations = new ArrayList(configFiles);
042    //        System.out.println ("DictionaryTesterHelper: adding " + supportFiles.size() + " support files");
043    //        configLocations.add("classpath:" + dictFileName);
044            String[] configLocs = configLocations.toArray(new String[0]);
045            ac = new ClassPathXmlApplicationContext(configLocs);
046            System.out.println ("DictionaryTesterHelper: end loading application context");        
047        }
048    
049        public Map<String, DataObjectEntry> getDataObjectEntryBeans () {
050          return (Map<String, DataObjectEntry>) ac.getBeansOfType(DataObjectEntry.class);  
051        }
052        
053        public List<String> doTest() {
054            List<String> outputFileNames = new ArrayList();
055            if (ac == null) {
056                loadApplicationContext ();
057            }
058            Map<String, DataObjectEntry> beansOfType = this.getDataObjectEntryBeans();
059                    
060            for (String beanId : beansOfType.keySet()) {
061                DataObjectEntry doe = beansOfType.get(beanId);
062                if ("org.kuali.rice.krad.bo.AttributeReferenceDummy".equals(doe.getFullClassName())) {
063                    continue;
064                }
065                System.out.println("Processing data object entry: " + doe.getDataObjectClass().getName());
066                DictionaryValidator validator = new DictionaryValidator(doe, new HashSet());
067                List<String> errors = validator.validate();
068                if (errors != null) {
069                    if (!errors.isEmpty()) {
070                        throw new IllegalArgumentException("Errors validating bean "
071                                + beanId + "\n" + this.formatAsString(errors));
072                    }
073                }
074                String outputFileName = beanId + ".html";
075                outputFileNames.add(outputFileName);
076                String fullOutputFileName = this.outputDir + "/" + outputFileName;
077                DictionaryFormatter formatter = new DictionaryFormatter(doe, beansOfType, beanId, fullOutputFileName);
078                formatter.formatForHtml();
079            }
080            return outputFileNames;
081        }
082    
083        private String formatAsString(List<String> errors) {
084            int i = 0;
085            StringBuilder builder = new StringBuilder();
086            for (String error : errors) {
087                i++;
088                builder.append(i).append(". ").append(error).append("\n");
089            }
090            return builder.toString();
091        }
092    }