ambroafb.general.GeneralConfig.java Source code

Java tutorial

Introduction

Here is the source code for ambroafb.general.GeneralConfig.java

Source

/*
 * To setLanguage this license header, choose License Headers in Project Properties.
 * To setLanguage this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ambroafb.general;

//import ambro.AConnectionToDB;
import ambroafb.AmbroAFB;
import authclient.db.DBClient;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.apache.commons.lang3.StringEscapeUtils;

/**
 * ?? ??? ? ?? 
 *
 * @author tabramishvili
 */
public class GeneralConfig {

    private static final String PREFS_LANGUAGE = "saved_language";

    public static final String classForName = "com.mysql.jdbc.Driver";

    private static GeneralConfig config;
    public static final Preferences prefs = Preferences.userNodeForPackage(AmbroAFB.class);

    /**
     *
     * @return ? Instance-
     */
    public static GeneralConfig getInstance() {
        if (config == null) {
            String lang = prefs.get(PREFS_LANGUAGE, null);
            if (lang == null) {
                config = new GeneralConfig();
            } else {
                config = new GeneralConfig(lang);
            }
        }
        return config;
    }

    /*??  ? ?? ? Utils, ? - ?  ???? ? ? ?. UtilsGC(GeneralConfiguration)-? ?*/
    public ResourceBundle bundle;
    public Locale locale;
    private KFZClient client;
    private DBClient dbClient;
    private String db_username, db_password;

    private HashMap<String, Object> attributes;
    private String language;
    private static final HashMap<String, String> languageIdToName;

    static {
        languageIdToName = new HashMap<>();
        languageIdToName.put("en", "English");
        languageIdToName.put("ka", "?");
    }

    private GeneralConfig() {
        this(Locale.getDefault().getLanguage());
    }

    private GeneralConfig(String lang) {
        this(new Locale(lang));
        this.language = lang;
    }

    private GeneralConfig(Locale locale) {
        this.locale = locale;
        bundle = loadBundle(locale);
        attributes = new HashMap<>();
    }

    private ResourceBundle loadBundle(Locale locale) {
        return ResourceBundle.getBundle(Names.BUNDLE_TITLES_NAME, locale);
    }

    public DBClient getDBClient(String username, String password) {
        db_username = username;
        db_password = password;
        dbClient = new DBClient(db_username, db_password,
                authclient.Utils.getDefaultConfig(Names.DB_SERVICE_URL_ON_SERVER, Names.APP_NAME)
                        .withAltServerAddress(Names.DB_SERVICE_ALTERNATIVE_URL_ON_SERVER));
        //        dbClient = new DBClient(db_username, db_password, authclient.Utils.getDefaultConfig(Names.DB_SERVICE_URL_FOR_TEST, "AmbroAFB"));
        dbClient.withLang(GeneralConfig.getInstance().locale.getLanguage());
        return dbClient;
    }

    public DBClient getDBClient() {
        return dbClient;
    }

    public void logoutServerClient() {
        if (client != null) {
            client.logout();
        }
    }

    public String getUserName() {
        return db_username;
    }

    public String getPassword() {
        return db_password;
    }

    /**
     * ? ?? ? Locale ?
     *
     * @return
     */
    public Locale getCurrentLocal() {
        return locale;
    }

    /**
     * ? ? ResourceBundle ?
     *
     * @return
     */
    public ResourceBundle getBundle() {
        return bundle;
    }

    /**
     * ? ??  ??  ?  
     *
     * @param key
     * @return
     */
    public String getTitleFor(String key) {
        if (bundle.containsKey(key)) {
            return StringEscapeUtils.unescapeJava(bundle.getString(key));
        }
        return key;
    }

    /**
     * ? ??  ??  ??  
     *
     * @param key - , ? ?? ?? ?
     * @param language - ?, ?? ? ?? 
     * ??  ? ? ? ??? ?? ?. ?:
     * English, ? ...
     * @return
     */
    public String getTitleForLanguage(String key, String language) {
        String id = mapLanguageToId(language);
        ResourceBundle b = loadBundle(new Locale(id));
        return StringEscapeUtils.unescapeJava(b.getString(key));
    }

    /**
     *  ? ? ?? stage- ? save-restrart ?
     * ??
     */
    public void dumpIntoPrefs() {
        prefs.put(PREFS_LANGUAGE, language);
        try {
            prefs.sync();
        } catch (BackingStoreException ex) {
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * ?? ?? ?? ??? ? ? ??, 
     * ? ?? ?? ??
     *
     * @param language
     */
    public void setLanguage(String language) {
        this.language = mapLanguageToId(language);
    }

    /**
     * ? ? ? ??? ?? ?. ?: English,
     * ? ...  ?  Locale- ? ?, ???
     *
     * @return
     */
    public String getLanguage() {
        return mapIdToLanguage(language);
    }

    /**
     * ?? key-  ?? ?? ??, ? ??
     * ?  ??? ? ? ? attributes-
     * ?? ??  ??? ? ?? !!!!!
     *
     * @param key
     * @param value
     */
    public void setAttribute(String key, Object value) {
        attributes.put(key, value);
    }

    /**
     * ? ?? key- ?? ??
     *
     * @param key
     * @return
     */
    public Object getAttribute(String key) {
        return attributes.get(key);
    }

    /**
     *  ?? key- ?? ??
     *
     * @param key
     * @return
     */
    public Object removeAttribute(String key) {
        return attributes.remove(key);
    }

    public boolean hasAttribute(String key) {
        return attributes.containsKey(key);
    }

    /**
     *  : ?? Locale- ? ? , ? 
     *
     * @param language
     * @return
     */
    private static String mapLanguageToId(String language) {
        for (String key : languageIdToName.keySet()) {
            if (languageIdToName.get(key).equals(language)) {
                return key;
            }
        }
        return null;
    }

    private static String mapIdToLanguage(String lang) {
        return languageIdToName.get(lang);
    }

}