package hirondelle.fish.main.codes;
import java.util.*;
import java.util.logging.*;
import javax.servlet.ServletContext;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.SqlId;
import hirondelle.web4j.database.Db;
import hirondelle.web4j.util.Util;
import hirondelle.web4j.model.Code;
import hirondelle.web4j.security.SafeText;
public final class CodeTableUtil {
public static final SqlId FETCH_RESTO_CODES = new SqlId("FETCH_RESTO_CODES");
public static final SqlId FETCH_LOCALE_CODES = new SqlId("TRANSLATION", "FETCH_LOCALE_CODES");
public static final SqlId FETCH_DISPOSITION_CODES = new SqlId("FETCH_DISPOSITION_CODES");
public static void init(ServletContext aContext) throws DAOException {
fLogger.fine("Fetching code tables.");
fContext = aContext;
fAllCodeTables = new LinkedHashMap<CodeTable, Map<Id, Code>>();
for(CodeTable codeTable: CodeTable.values()){
fetchAndRememberCodeTable(codeTable);
placeInAppScope(codeTable);
}
}
static Code populate(Id aCodeId, CodeTable aCodeTable){
Code result = null;
Map<Id, Code> codeTable = fAllCodeTables.get(aCodeTable);
if ( codeTable == null ){
throw new RuntimeException("Cannot populate item. Unknown code table " + Util.quote(aCodeTable));
}
if ( aCodeId != null ){
result = codeTable.get(aCodeId);
if ( result == null ){
throw new RuntimeException("Cannot find item in code table " + Util.quote(aCodeTable) + " using Id: " + Util.quote(aCodeId));
}
}
return result;
}
private static Map<CodeTable, Map<Id, Code>> fAllCodeTables;
private static ServletContext fContext;
private static final Logger fLogger = Util.getLogger(CodeTableUtil.class);
private static void fetchAndRememberCodeTable(CodeTable aCodeTable) throws DAOException {
Map<Id, Code> codeTable = null;
if ( CodeTable.DISPOSITIONS == aCodeTable ){
codeTable = getDispositions();
}
else if( CodeTable.SUPPORTED_LOCALES == aCodeTable){
codeTable = getLocales();
}
else if( CodeTable.RESTAURANTS == aCodeTable ){
codeTable = getRestoCodes();
}
else if ( CodeTable.RATINGS == aCodeTable ){
codeTable = getRatings();
}
else {
throw new AssertionError("CodeTable not known to CodeTableUtil : " + aCodeTable);
}
fLogger.fine("Code Table " + Util.quote(aCodeTable) + ": " + Util.logOnePerLine(codeTable));
fAllCodeTables.put(aCodeTable, codeTable);
}
private static Map<Id, Code> getDispositions() throws DAOException {
Map<Id, Code> result = new LinkedHashMap<Id, Code>();
List<Code> codes = Db.list(Code.class, FETCH_DISPOSITION_CODES);
result = Util.asMap(codes, Id.class, "getId");
return result;
}
private static Map<Id, Code> getLocales() throws DAOException {
Map<Id, Code> result = new LinkedHashMap<Id, Code>();
List<Code> codes = Db.list(Code.class, FETCH_LOCALE_CODES);
result = Util.asMap(codes, Id.class, "getId");
return result;
}
private static Map<Id, Code> getRestoCodes() throws DAOException {
Map<Id, Code> result = new LinkedHashMap<Id, Code>();
List<Code> codes = Db.list(Code.class, FETCH_RESTO_CODES);
result = Util.asMap(codes, Id.class, "getId");
return result;
}
private static Map<Id, Code> getRatings() throws DAOException {
Map<Id, Code> result = new LinkedHashMap<Id, Code>();
for(int idx=0; idx <= 10; ++idx ){
Id id = new Id(new Integer(idx).toString());
Code code = null;
try {
code = new Code(id, SafeText.from(id.toString()));
}
catch (ModelCtorException ex){
throw new DAOException("Cannot build Code in expected manner.",ex);
}
result.put(id, code);
}
return result;
}
private static void placeInAppScope(CodeTable aCodeTable){
Map<Id, Code> codeTable = fAllCodeTables.get(aCodeTable);
Collection<Code> codeValues = codeTable.values();
fContext.setAttribute(aCodeTable.getTableName().toString(), codeValues);
}
}