package hirondelle.fish.translate.locale;
import hirondelle.fish.util.ReqParam;
import hirondelle.web4j.action.ActionTemplateListAndEdit;
import hirondelle.web4j.action.ResponsePage;
import hirondelle.web4j.config.ConnectionSrc;
import hirondelle.web4j.config.Startup;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.database.SqlId;
import hirondelle.web4j.model.ModelCtorException;
import hirondelle.web4j.model.ModelFromRequest;
import hirondelle.web4j.request.RequestParameter;
import hirondelle.web4j.request.RequestParser;
import java.util.Locale;
public final class SupportedLocaleAction extends ActionTemplateListAndEdit {
public SupportedLocaleAction(RequestParser aRequestParser){
super(FORWARD, REDIRECT_TO_LISTING, aRequestParser);
}
public static final SqlId LOCALE_LIST = getTranslationSqlId("LOCALE_LIST");
public static final SqlId LOCALE_FETCH = getTranslationSqlId("LOCALE_FETCH");
public static final SqlId LOCALE_ADD = getTranslationSqlId("LOCALE_ADD");
public static final SqlId LOCALE_CHANGE = getTranslationSqlId("LOCALE_CHANGE");
public static final SqlId LOCALE_DELETE = getTranslationSqlId("LOCALE_DELETE");
public static final RequestParameter SUPPORTED_LOCALE_ID = ReqParam.ID;
public static final RequestParameter SHORT_FORM = RequestParameter.withLengthCheck("Short Form");
public static final RequestParameter DESCRIPTION = RequestParameter.withLengthCheck("Description");
protected void doList() throws DAOException {
addToRequest(ITEMS_FOR_LISTING, fSupportedLocaleDAO.list());
}
protected void validateUserInput() {
try {
ModelFromRequest builder = new ModelFromRequest(getRequestParser());
fSupportedLocale = builder.build(SupportedLocale.class, SUPPORTED_LOCALE_ID, SHORT_FORM, DESCRIPTION);
}
catch (ModelCtorException ex){
addError(ex);
}
}
protected void attemptAdd() throws DAOException {
try {
fSupportedLocaleDAO.add(fSupportedLocale);
addMessage("_1_ added successfully.", fSupportedLocale.getDescription());
refreshPickLists();
}
catch (DuplicateException ex){
addError("Locale already taken. Please use another Short Form.");
}
}
protected void attemptFetchForChange() throws DAOException {
SupportedLocale locale = fSupportedLocaleDAO.fetch(getIdParam(SUPPORTED_LOCALE_ID));
if( locale == null ){
addError("Locale no longer exists. Likely deleted by another user.");
}
else {
addToRequest(ITEM_FOR_EDIT, locale);
}
}
protected void attemptChange() throws DAOException {
try {
boolean success = fSupportedLocaleDAO.change(fSupportedLocale);
if (success){
addMessage("Locale for _1_ changed successfully.", fSupportedLocale.getDescription());
refreshPickLists();
}
else {
addError("No update occurred. Item likely deleted by another user.");
}
}
catch (DuplicateException ex){
addError("Short Form already taken. Please choose a different Short Form.");
}
}
protected void attemptDelete() throws DAOException {
try {
fSupportedLocaleDAO.delete(getIdParam(SUPPORTED_LOCALE_ID));
addMessage("Item deleted successfully.");
refreshPickLists();
}
catch(DAOException ex){
addError("Cannot delete. Item used elsewhere.");
}
}
private SupportedLocale fSupportedLocale;
private SupportedLocaleDAO fSupportedLocaleDAO = new SupportedLocaleDAO();
private static final ResponsePage FORWARD = new ResponsePage(
"Locales", "view.jsp", SupportedLocaleAction.class
);
private static final ResponsePage REDIRECT_TO_LISTING = new ResponsePage("SupportedLocaleAction.do?Operation=List");
private void refreshPickLists() throws DAOException {
Startup.lookUpCodeTablesAndPlaceIntoAppScope();
}
private static SqlId getTranslationSqlId(String aSqlStatementId){
return new SqlId(ConnectionSrc.TRANSLATION, aSqlStatementId);
}
}