Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.kuali.student.common.test.spring;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
 
 public class LoadDataBean implements ApplicationContextAware{
 	private static final Log LOG = LogFactory.getLog(LoadDataBean.class);
 
 
 
 	private boolean loaded = false;
 	private String daoAnnotations;
 
 
 	public void loadData(){
 		if ( == null ||  || .trim().isEmpty()) {
 			return;
 		}
 
 		// Load all the beans
 		String[] classes = .split(",");
 		for (String line : classes) {
 			try {
 				String[] split = line.split("\\|");
 
 				// Invoke the data loader for this dao
                 invokeDataLoader(split[0]);
 
                 // Load data bean file for this dao
                 if (split.length > 1&& !split[1].isEmpty()) {
 				    String testDataFile = split[1];
 
 							testDataFile);
 					for (Object bean : (List<?>) ac.getBean("persistList")) {
 						if(!.contains(bean)){
 							.persist(bean);
 						}
 					}
 				}
                 // Load sql file for this dao
                 if (split.length > 2&& !split[2].isEmpty()) {
 
 					String testDataFile = split[2];
 				    File sqlFile;
 				    if(testDataFile.startsWith("classpath:")){
 				 	   sqlFile = new ClassPathResource(testDataFile.substring("classpath:".length())).getFile();
 				    }else{
 				    	sqlFile = new File(testDataFile);
 				    }
 					   = new BufferedReader(new FileReader(sqlFile));
 					try {
     					String ln = "";
     					int lnNr = 0;
 
     					try {
                             while((ln=in.readLine())!=null){
                                 lnNr++;
                             	if(!ln.startsWith("/")&&!ln.startsWith("--")&&StringUtils.isNotBlank(ln)){
                             		ln=ln.replaceFirst("[;/]\\s*$","");
                             		.createNativeQuery(ln).executeUpdate();
                             	}
                             }
                        } catch (PersistenceException e) {
                            .error("Failed statement at line " + lnNr + ": " + ln);
                            throw e;
                        }
finally {
					    in.close();
					}
				}
catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		 = true;
	}
	protected void invokeDataLoader(String dao){
	    try {
            //Check if there is a loader class for this dao
	        Class<?> daoType = Class.forName(dao).getInterfaces()[0];
	        Class<?> clazz = Class.forName(daoType.getName() + "Loader");
            DaoLoader daoLoader = (DaoLoader)clazz.newInstance();
            //Get spring bean for the dao
            Map<?,?> daoBeans = .getBeansOfType(daoType);
            //Invoke the loader for this doa bean (there shouldn't be more than one)
            if (daoBeans.size() == 1){
                daoLoader.setDao(daoBeans.values().iterator().next());
                daoLoader.run();
            }
	    } catch (ClassNotFoundException cnfe) {
	    	.info(cnfe);
	    } catch (Exception e) {
            .error(e);
        }
	}

Returns:
the daoAnnotations
	}

Parameters:
daoAnnotations the daoAnnotations to set
	public void setDaoAnnotations(String daoAnnotations) {
		this. = daoAnnotations;
	}
	public void setApplicationContext(ApplicationContext applicationContext){
	    this. = applicationContext;
	}
New to GrepCode? Check out our FAQ X