001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.personalization;
021:
022: import com.salmonllc.properties.Props;
023: import com.salmonllc.sql.DBConnection;
024: import com.salmonllc.util.MessageLog;
025:
026: import java.sql.*;
027: import java.util.Vector;
028:
029: /**
030: * This class loads and saves skins to a RDBMS table. It uses the following keys from the property file to determine how to load the skin
031: * SKIN_TABLE="SkinTable"
032: * SKIN_DB_PROFILE="SkinDBProfile"
033: * SKIN_NAME_COLUMN="SkinNameColumn"
034: * SKIN_KEY_COLUMN="SkinKeyColumn"
035: * SKIN_VALUE_COLUMN="SkinValueColumn"
036: */
037:
038: public class DBSkinManager extends SkinManager {
039:
040: String _appName = null;
041: String _table = null;
042: String _profile = null;
043: String _nameCol = null;
044: String _keyCol = null;
045: String _valueCol = null;
046:
047: void init(String appName) {
048: Props p = Props.getProps(appName, null);
049: _appName = appName;
050: _table = p.getProperty(Props.SKIN_TABLE);
051: _profile = p.getProperty(Props.SKIN_DB_PROFILE);
052: _nameCol = p.getProperty(Props.SKIN_NAME_COLUMN);
053: _keyCol = p.getProperty(Props.SKIN_KEY_COLUMN);
054: _valueCol = p.getProperty(Props.SKIN_VALUE_COLUMN);
055: }
056:
057: /*Loads the data for a skin with the specified name and append its attributes to the current skin*/
058: public void load(String name, Skin skin) {
059: DBConnection conn = null;
060: try {
061: conn = DBConnection.getConnection(_appName, _profile);
062: Statement st = conn.createStatement();
063: String sql = "SELECT " + _keyCol + "," + _valueCol
064: + " FROM " + _table + " WHERE " + _nameCol + "= '"
065: + name + "'";
066: ResultSet r = st.executeQuery(sql);
067: while (r.next()) {
068: String att = r.getString(1);
069: String val = r.getString(2);
070: boolean classAtt = false;
071: boolean instAtt = false;
072: if (att.startsWith("class."))
073: classAtt = true;
074: else if (att.startsWith("instance."))
075: instAtt = true;
076:
077: if (classAtt || instAtt) {
078: int ndx = att.lastIndexOf(".");
079: int ndx2 = att.indexOf(".");
080: if (ndx == ndx2)
081: continue;
082: String attName = att.substring(ndx + 1);
083: String attClass = att.substring(ndx2 + 1, ndx);
084: if (classAtt)
085: skin.setClassAttribute(attClass, attName, val);
086: else
087: skin.setInstanceAttribute(attClass, attName,
088: val);
089: } else {
090: skin.setProperty(att, val);
091: }
092: }
093: r.close();
094: st.close();
095: } catch (Exception e) {
096: MessageLog.writeErrorMessage("load()", e, this );
097: } finally {
098: if (conn != null)
099: conn.freeConnection();
100: }
101:
102: }
103:
104: /*Saves a skin under the specified name, if it exists it is overwritten*/
105: public void save(String name, Skin skin) {
106: DBConnection conn = null;
107: try {
108: conn = DBConnection.getConnection(_appName, _profile);
109: conn.beginTransaction();
110: Statement st = conn.createStatement();
111: st.executeQuery("DELETE FROM " + _table + " WHERE "
112: + _nameCol + "= '" + name + "'");
113: PreparedStatement st2 = conn
114: .prepareStatement("INSERT INTO " + _table + "("
115: + _nameCol + "," + _keyCol + ","
116: + _valueCol + ") VALUES ('" + name
117: + "',?,?)");
118: Attribute att[] = skin.getAllAttributes();
119: for (int i = 0; i < att.length; i++) {
120: st2.setString(1, att[i].getAttribute());
121: st2.setString(2, att[i].getValue());
122: st2.executeUpdate();
123: }
124: conn.commit();
125: } catch (Exception e) {
126: conn.rollback();
127: MessageLog.writeErrorMessage("save()", e, this );
128: } finally {
129: if (conn != null)
130: conn.freeConnection();
131: }
132: }
133:
134: /*Gets an array of available skin names*/
135: public String[] getSkinNames() {
136: Vector v = new Vector();
137: DBConnection conn = null;
138: try {
139: conn = DBConnection.getConnection(_appName, _profile);
140: Statement st = conn.createStatement();
141: String sql = "SELECT distinct " + _nameCol + " FROM "
142: + _table + " ORDER BY " + _nameCol;
143: ResultSet r = st.executeQuery(sql);
144: while (r.next()) {
145: v.add(r.getString(1));
146: }
147: r.close();
148: st.close();
149: } catch (Exception e) {
150: MessageLog.writeErrorMessage("load()", e, this );
151: } finally {
152: if (conn != null)
153: conn.freeConnection();
154: }
155: String ret[] = new String[v.size()];
156: v.copyInto(ret);
157: return ret;
158: }
159:
160: }
|