01: package com.salmonllc.localizer;
02:
03: //** Copyright Statement ***************************************************
04: //The Salmon Open Framework for Internet Applications (SOFIA)
05: // Copyright (C) 1999 - 2002, Salmon LLC
06: //
07: // This program is free software; you can redistribute it and/or
08: // modify it under the terms of the GNU General Public License version 2
09: // as published by the Free Software Foundation;
10: //
11: // This program is distributed in the hope that it will be useful,
12: // but WITHOUT ANY WARRANTY; without even the implied warranty of
13: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: // GNU General Public License for more details.
15: //
16: // You should have received a copy of the GNU General Public License
17: // along with this program; if not, write to the Free Software
18: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: //
20: // For more information please visit http://www.salmonllc.com
21: //** End Copyright Statement ***************************************************
22:
23: import java.sql.ResultSet;
24: import java.sql.Statement;
25:
26: import com.salmonllc.properties.Props;
27: import com.salmonllc.sql.DBConnection;
28: import com.salmonllc.util.MessageLog;
29:
30: /**
31: * The DB Localizer is a concrete implementation of the Localizer class. This will load the localization keys from a database table
32: **/
33:
34: public class DBLocalizer extends Localizer {
35:
36: /**
37: * Constructs a new Localizer
38: */
39:
40: public DBLocalizer() {
41: super (false);
42: }
43:
44: /**
45: * Constructs a new Localizer
46: * @param translateEscapes true to translate escape sequences to their propery unicode representation
47: */
48: public DBLocalizer(boolean translateEscapes) {
49: super (false, translateEscapes);
50: }
51:
52: /**
53: * Loads the data from a database table. It uses the following properties from the application or system.property file<br>
54: * LocaleTable=The table where the localization data comes from<br>
55: * LocaleLangColumn=The column in the table that specifies the language<br>
56: * LocaleKeyColumn=The column in the table that specifies the resource key<br>
57: * LocaleResourceColumn=The column in the table that specifies the resource key<br>
58: * LocaleAppColumn=The column in the table that specifies which application a key is for (optional: If left out it won't use app name as part of the selection)<br>
59: * LocaleDBProfile=The database profile to use to load the data (optional: if left out it will use the default prrofile for the application<br>
60: * The method returns true if it succeeds and false if not.
61: */
62: protected boolean loadData(String appName, String language) {
63:
64: DBConnection conn = null;
65: try {
66: Props p = Props.getProps(appName, null);
67: String table = p.getProperty(Props.LOCALE_TABLE);
68: String lang = p.getProperty(Props.LOCALE_LANG_COLUMN);
69: String key = p.getProperty(Props.LOCALE_KEY_COLUMN);
70: String resource = p
71: .getProperty(Props.LOCALE_RESOURCE_COLUMN);
72: String app = p.getProperty(Props.LOCALE_APP_COLUMN);
73: String prof = p.getProperty(Props.LOCALE_DB_PROFILE);
74: String sql = "SELECT " + key + "," + resource + " FROM "
75: + table + " WHERE " + lang + "='" + language + "'";
76: if (app != null)
77: sql += " AND " + app + " ='" + app + "'";
78: sql += " ORDER BY " + key + " ASC";
79:
80: conn = DBConnection.getConnection(appName, prof);
81: Statement st = conn.createStatement();
82: ResultSet r = st.executeQuery(sql);
83: while (r.next())
84: addValue(r.getString(1), r.getString(2));
85: r.close();
86: st.close();
87: } catch (Exception e) {
88: MessageLog.writeErrorMessage("loadData", e, this );
89: } finally {
90: if (conn != null)
91: conn.freeConnection();
92: }
93: return (getSize() > 0);
94: }
95: }
|