NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//$Id$
package com.adventnet.la.i18n;

import com.adventnet.authentication.Credential;
import com.adventnet.authentication.util.AuthUtil;
import com.adventnet.ds.query.Column;
import com.adventnet.ds.query.Criteria;
import com.adventnet.ds.query.QueryConstants;
import com.adventnet.persistence.DataAccess;
import com.adventnet.persistence.DataAccessException;
import com.adventnet.persistence.DataObject;
import com.adventnet.persistence.Row;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class ResourceBundleMgr
{
private static ResourceBundleMgr instance = null;
private HashMap<Locale,ResourceBundle> serverRsMap = null;
private HashMap<Locale,String> clientRsMap = null;
private Locale serverLocale = null;
private static final Logger LOGGER = Logger.getLogger(ResourceBundleMgr.class.getName());

private ResourceBundleMgr()
{
serverRsMap = new HashMap<Locale,ResourceBundle>();
clientRsMap = new HashMap<Locale,String>();
serverLocale = initSeverlocale();
}

public static synchronized ResourceBundleMgr getInstance()
{
if(instance == null)
{
instance = new ResourceBundleMgr();
}

return instance;
}

public Locale getLocale()
{
Locale locale = LocaleThreadLocal.get();
if(locale == null)
{
if(serverLocale != null)
{
locale = serverLocale;
}
else
{
LOGGER.info("WARNING : No Locale Found");
locale = new Locale("--","--");
}
}
return locale;
}

public synchronized Locale getLocale(HttpServletRequest httprequest)
{
HttpSession session = httprequest.getSession();
Locale locale = (Locale)session.getAttribute("USER_LOCALE"); //No I18N

if(locale == null ||(session.getAttribute("IS_REFRESH_NEEDED") != null && session.getAttribute("IS_REFRESH_NEEDED").equals("true")))
{
Long localeID = -1L;
LOGGER.fine("Locale Details Not Found in Session Cache");
if(AuthUtil.getUserCredential() == null)
{
session.setAttribute("IS_REFRESH_NEEDED","true");
}
else {
session.setAttribute("IS_REFRESH_NEEDED","false");
locale = getUserLocale(AuthUtil.getUserCredential());
}

if(locale == null) // user have't selected any option getting locale from admin cache
{
LOGGER.fine("User have't selected any option getting locale from Admin cache");
locale = getAdminlocaleCache();
}

if(locale == null || locale.getLanguage().equals("--")) // User Selected browser default , or Admin cache has null value
{
LOGGER.fine("User selected Browserdefault locale or AdminlocaleCache has null value");
locale = resolveLocaleFromRequest(httprequest);
}
else
{
localeID = getLocaleID(locale);
}

LOGGER.fine("LocaleID retrived from DataBase : "+localeID);
session.setAttribute("USER_LOCALE",locale); //No I18N
session.setAttribute("USER_LOCALE_ID", localeID.toString()); //No I18N
}

LOGGER.fine("Going to return Locale : "+locale+" From selected User or From given Request ");

return locale;
}

public Locale resolveLocaleFromRequest(HttpServletRequest httprequest)
{
Locale locale = getDefaultLocale(httprequest);
if(locale == null)
{
LOGGER.fine("Unable to Find Locale in Request, Going to return Server Locale");
locale = serverLocale;
}
return locale;
}

public Locale getServerLocale()
{
return serverLocale;
}

public synchronized void addServerBundle(ResourceBundle bundle,Locale locale)
{
serverRsMap.put(locale, bundle);
}

public synchronized ResourceBundle getServerBundle(Locale locale)
{
return serverRsMap.get(locale);
}

public synchronized void addClientMessage(String clientMessage,Locale locale)
{
clientRsMap.put(locale, clientMessage);
}

public synchronized String getClientMessage(Locale locale)
{
return clientRsMap.get(locale);
}

public synchronized boolean isClientKeypresent(Locale locale)
{
return clientRsMap.containsKey(locale);
}
public synchronized boolean isServerKeypresent(Locale locale)
{
return serverRsMap.containsKey(locale);
}

public void setServerLocale(Locale locale)
{
if(locale!= null)
{
serverLocale = locale;
}
}

public Locale getDefaultLocale(HttpServletRequest httprequest)
{
Locale locale = httprequest.getLocale();
locale = resolveWithSupportedLocale(locale);
return locale;
}

public void updateAdminLocaleCache(Locale locale)
{
LOGGER.info("UpdateServerLocale called for : "+locale);
try
{
updateServerlocale(locale.getLanguage(),locale.getCountry());
}
catch (DataAccessException e)
{
e.printStackTrace();
}
}
private Long getLocaleID(Locale locale)
{
return getLocaleID(locale.getLanguage(),locale.getCountry());
}
private Long getLocaleID(String language, String country)
{
Long localeID = -1L;
Criteria crit = new Criteria(Column.getColumn("EventlogLocaleDetails" , "LOCALE_LANGUAGE"),language,QueryConstants.EQUAL); //No I18N
crit = crit.and(new Criteria(Column.getColumn("EventlogLocaleDetails" , "LOCALE_COUNTRY"),country,QueryConstants.EQUAL)); //No I18N
try
{
DataObject localeDO = DataAccess.get("EventlogLocaleDetails",crit); //No I18N
if(!localeDO.isEmpty())
{
localeID = (Long)localeDO.getFirstValue("EventlogLocaleDetails", "LOCALE_ID"); //No I18N
}
}
catch (DataAccessException e)
{
e.printStackTrace();
}

LOGGER.info("Language : "+language+" Country : " +country+" LocaleID : "+localeID);
return localeID;
}

private Locale initSeverlocale()
{
Locale locale = getSystemLocale();
// getUserLocale(ela_admin_id);
//LOGGER.info("AdminLocale From DataBase : "+locale);
//if(locale == null || locale.getLanguage().equals("--"))
// {
// locale = getSystemLocale();
//}
LOGGER.info("EVENTLOG SERVER LOCALE INITIALIZED AS : "+locale);
return locale;
}

public Locale getSystemLocale()
{
Locale locale = getAdminlocaleCache();
LOGGER.info("AdminlocaleCache From DataBase : "+locale);
if(locale == null)
{
LOGGER.info("AdminLocaleCache not found , System locale selected");
locale = resolveWithSupportedLocale(Locale.getDefault());
if(locale == null)
{
LOGGER.info("Non of Locales found, Build locale selected");
locale = getBuildLocale();
}
}
LOGGER.info("System Locale returned : "+locale);
return locale;
}

public Locale getUserLocale(Credential credential)
{
if(credential != null)
{
return getUserLocale(credential.getUserId());
}
else
{
LOGGER.info("credential has null value");
return null;
}
}

public Long getUserLocaleID(Long userid)
{
Long result = null;
ArrayList<String> tableList = new ArrayList<String>();
tableList.add("EventlogLocaleDetails");
tableList.add("EventlogUsersPersonal");
try
{

DataObject localeDO = DataAccess.get(tableList,new Criteria(Column.getColumn("EventlogUsersPersonal" , "LOGIN_ID"),userid,QueryConstants.EQUAL));
if(!localeDO.isEmpty() && localeDO.containsTable("EventlogLocaleDetails"))
{
Row row = localeDO.getFirstRow("EventlogLocaleDetails"); //No I18N
result = (Long) row.get("LOCALE_ID"); //No I18N
}

}
catch (DataAccessException exp)
{
exp.printStackTrace();
}

LOGGER.info("getUserLocaleID userid : "+userid+" and result "+result);

return result;
}

public Locale getUserLocale(Long userID)
{
Locale result = null;
ArrayList<String> tableList = new ArrayList<String>();
tableList.add("EventlogLocaleDetails");
tableList.add("EventlogUsersPersonal");
try
{

DataObject localeDO = DataAccess.get(tableList,new Criteria(Column.getColumn("EventlogUsersPersonal" , "LOGIN_ID"),userID,QueryConstants.EQUAL));
if(!localeDO.isEmpty() && localeDO.containsTable("EventlogLocaleDetails"))
{
Row row = localeDO.getFirstRow("EventlogLocaleDetails"); //No I18N
result = new Locale(row.get("LOCALE_LANGUAGE").toString(),row.get("LOCALE_COUNTRY").toString()); //No I18N
}

}
catch (DataAccessException exp)
{
exp.printStackTrace();
}

LOGGER.info("getUserLocale loginID : "+userID+" and result "+result);

return result;
}

public void updateUserlocale(Long userID,Long localeID) throws DataAccessException
{
LOGGER.info("updateUserlocale userID : "+userID+" and localeID "+localeID);

try
{
Criteria crit = new Criteria(Column.getColumn("EventlogUsersPersonal" , "LOGIN_ID"),userID,QueryConstants.EQUAL);
DataObject localeDO = DataAccess.get("EventlogUsersPersonal",crit); //No I18N
if(localeDO.isEmpty())
{
Row row = new Row("EventlogUsersPersonal");
row.set("LOGIN_ID", userID);
row.set("LOCALE_ID", localeID);
localeDO.addRow(row);
}
else
{
Row row = localeDO.getFirstRow("EventlogUsersPersonal");
row.set("LOCALE_ID", localeID);
localeDO.updateRow(row);
}

DataAccess.update(localeDO);
}
catch(Exception exp)
{
exp.printStackTrace();
}

}

public Locale resolveWithSupportedLocale(Locale locale)
{
LOGGER.info("Resolve With given Locale Initiated For : "+locale);
String lang = locale.getLanguage();
String country = locale.getCountry();
if(lang == null && country == null)
{
return null;
}
Locale result = null;
Criteria langBasedCrit = new Criteria(Column.getColumn("EventlogLocaleDetails" , "LOCALE_LANGUAGE"), lang ,QueryConstants.EQUAL); //No I18N
Criteria countryBasedCrit = new Criteria(Column.getColumn("EventlogLocaleDetails" , "LOCALE_COUNTRY"), country ,QueryConstants.EQUAL); //No I18N
try
{
DataObject localeDO = DataAccess.get("EventlogLocaleDetails",langBasedCrit.or(countryBasedCrit)); //No I18N
if(localeDO.isEmpty())
{
return null;
}
else if(localeDO.size("EventlogLocaleDetails") > 1)
{

Row langBasedrow = localeDO.getRow("EventlogLocaleDetails",langBasedCrit);
Row countryBasedrow = localeDO.getRow("EventlogLocaleDetails",countryBasedCrit);
if(langBasedrow != null)
{
result = new Locale((String) langBasedrow.get("LOCALE_LANGUAGE"), (String)langBasedrow.get("LOCALE_COUNTRY"));
}
else
{
result = new Locale((String) countryBasedrow.get("LOCALE_LANGUAGE"), (String)countryBasedrow.get("LOCALE_COUNTRY"));
}
}
else
{
Row row = localeDO.getFirstRow("EventlogLocaleDetails");
result = new Locale((String) row.get("LOCALE_LANGUAGE"), (String)row.get("LOCALE_COUNTRY"));
}

}
catch(Exception exp)
{
exp.printStackTrace();
}

LOGGER.info("Locale resolved from selected Locale , Previous Locale : "+""+" Converted Locale : "+result);

return result;
}

public Locale getBuildLocale()
{
return new Locale("en","US");//No I18N
}

private void updateServerlocale(String lang, String cntry) throws DataAccessException
{
String newLocale = lang+"_"+cntry;
Criteria crd = new Criteria(Column.getColumn("SystemConfigurations" , "CONF_NAME"),"APP_LOCALE",QueryConstants.EQUAL); //No I18N
DataObject localeDO = DataAccess.get("SystemConfigurations",crd); //No I18N
if(!localeDO.isEmpty())
{
Row row = localeDO.getFirstRow("SystemConfigurations"); //No I18N
String locale = row.get("CONF_VALUE").toString(); //No I18N
if(!newLocale.equals(locale))
{
row.set("CONF_VALUE",newLocale); //No I18N
localeDO.updateRow(row);
DataAccess.update(localeDO);
}
}
else
{
Row row = new Row("SystemConfigurations"); //No I18N
row.set("CONF_NAME","APP_LOCALE"); //No I18N
row.set("CONF_VALUE",newLocale); //No I18N
localeDO.addRow(row);
DataAccess.update(localeDO);
}
}

private Locale getAdminlocaleCache()
{
Locale result = null;
Criteria crd = new Criteria(Column.getColumn("SystemConfigurations" , "CONF_NAME"),"APP_LOCALE",QueryConstants.EQUAL); //No I18N
DataObject localeDO = null;
try
{
localeDO = DataAccess.get("SystemConfigurations",crd); //No I18N
if(!localeDO.isEmpty())
{
Row row = localeDO.getFirstRow("SystemConfigurations"); //No I18N
String locale = row.get("CONF_VALUE").toString(); //No I18N
result = new Locale(locale.split("_",2)[0],locale.split("_",2)[1]);
}

}
catch (DataAccessException e)
{
e.printStackTrace();
}

LOGGER.info("AdminlocaleCache returning : "+result);
return result;
}


}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.