package hirondelle.fish.translate.translation;
import java.util.*;
import hirondelle.fish.translate.basetext.BaseText;
import hirondelle.fish.translate.locale.SupportedLocale;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.database.DuplicateException;
import hirondelle.web4j.database.SqlId;
import hirondelle.web4j.database.Db;
import hirondelle.web4j.ui.translate.Translation;
import hirondelle.web4j.model.Id;
import hirondelle.web4j.config.ConnectionSrc;
import static hirondelle.fish.translate.translation.TranslationEdit.ADD;
import static hirondelle.fish.translate.translation.TranslationEdit.CHANGE;
import static hirondelle.fish.translate.translation.TranslationEdit.DELETE;
import static hirondelle.fish.translate.translation.TranslationEdit.FETCH;
import static hirondelle.fish.translate.translation.TranslationEdit.LIST;
public final class TranslationDAO {
public static final SqlId FETCH_FROM_CODER_KEYS = getSqlId("FETCH_TRANSLATIONS_FROM_CODER_KEYS");
public static final SqlId FETCH_FROM_NATURAL_LANG_1 = getSqlId("FETCH_TRANSLATIONS_FROM_NATURAL_LANGUAGE_KEYS_ONE");
public static final SqlId FETCH_FROM_NATURAL_LANG_2 = getSqlId("FETCH_TRANSLATIONS_FROM_NATURAL_LANGUAGE_KEYS_TWO");
public Map<String, Map<String, String>> getTranslations() throws DAOException {
return Translation.asNestedMap(list());
}
List<Translation> list() throws DAOException {
List<Translation> transForCoderKeys = Db.list(Translation.class, FETCH_FROM_CODER_KEYS);
List<Translation> transForLangOne = Db.list(Translation.class, FETCH_FROM_NATURAL_LANG_1);
List<Translation> transForLangTwo = Db.list(Translation.class, FETCH_FROM_NATURAL_LANG_2);
Set<Translation> noDupes = new LinkedHashSet<Translation>();
noDupes.addAll(transForCoderKeys);
noDupes.addAll(transForLangOne);
noDupes.addAll(transForLangTwo);
List<Translation> result = new ArrayList<Translation>(noDupes);
Collections.sort(result);
return result;
}
List<Translation> listFor(Id aBaseTextId) throws DAOException {
return Db.list(Translation.class, LIST, aBaseTextId);
}
void add(Translation aTranslation) throws DAOException, DuplicateException {
Object[] params = {aTranslation.getBaseTextId(), aTranslation.getLocaleId(), aTranslation.getTranslation()};
Db.edit(ADD, params);
}
Translation fetch(Id aBaseTextId, Id aLocaleId) throws DAOException {
return Db.fetch(Translation.class, FETCH, aBaseTextId, aLocaleId);
}
void change(Translation aTranslation) throws DAOException {
Object[] params = {aTranslation.getTranslation(), aTranslation.getBaseTextId(), aTranslation.getLocaleId()};
Db.edit(CHANGE, params);
}
void delete(Id aBaseTextId, Id aLocaleId) throws DAOException {
Db.edit(DELETE, aBaseTextId, aLocaleId);
}
private static SqlId getSqlId(String aSqlId){
return new SqlId(ConnectionSrc.TRANSLATION, aSqlId);
}
}