package org.richfaces.realworld.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;

@Name("flushStrategy")
@Scope(ScopeType.APPLICATION)
@Startup
public class FlushStrategy {

	public FlushStrategy(){
		getStrategy();
	}
	private static String strategy;
	
	public String getStrategy() {
		if(strategy == null){
			try { InputStream in = this.getClass().getClassLoader()  
				               .getResourceAsStream("realworld.properties");  
			if(in != null){
				Properties p = new Properties();
				p.load(in);
				strategy = p.getProperty("flushStrategy");
			}else{
				strategy = "database";
			}
			} catch (FileNotFoundException e) {
				strategy = "database";
			} catch (IOException e) {
				strategy = "database";
			}
		}
		return strategy;
	}
	
	
	//TODO nick - make this a static method
	public boolean isDatabaseStoreStrategy(){
		return getStrategy().equals("database");
	}
}
