001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.services.entityproperties;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.Timestamp;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.jasig.portal.EntityIdentifier;
016: import org.jasig.portal.RDBMServices;
017: import org.jasig.portal.services.EntityPropertyRegistry;
018:
019: /**
020: * A portal RDBM based entity property store implementation
021: *
022: * @author Alex Vigdor av317@columbia.edu
023: * @version $Revision: 35492 $
024: */
025: public class RDBMPropertyStore implements IEntityPropertyStore {
026:
027: private static final Log log = LogFactory
028: .getLog(RDBMPropertyStore.class);
029:
030: protected static Class propsType = null;
031: protected final static String TABLE_NAME = "UP_ENTITY_PROP";
032: protected final static String TYPE_COL = "ENTITY_TYPE_ID";
033: protected final static String KEY_COL = "ENTITY_KEY";
034: protected final static String NAME_COL = "PROPERTY_NAME";
035: protected final static String VALUE_COL = "PROPERTY_VALUE";
036: protected final static String DATE_COL = "LAST_MODIFIED";
037: protected final static String selectProperties = "SELECT "
038: + NAME_COL + ", " + VALUE_COL + " FROM " + TABLE_NAME
039: + " WHERE " + TYPE_COL + "=? AND " + KEY_COL + "=?";
040: protected final static String deleteProperty = "DELETE FROM "
041: + TABLE_NAME + " WHERE " + TYPE_COL + "=? AND " + KEY_COL
042: + "=? AND " + NAME_COL + "=?";
043: protected final static String insertProperty = "INSERT INTO "
044: + TABLE_NAME + " VALUES (?,?,?,?,?)";
045:
046: public RDBMPropertyStore() {
047: try {
048: if (propsType == null) {
049: propsType = Class
050: .forName("org.jasig.portal.services.entityproperties.EntityProperties");
051: }
052: } catch (Exception e) {
053: log
054: .error(
055: "RDBMPropertyStore.Constructor Unable to create propstype",
056: e);
057: }
058: }
059:
060: public String[] getPropertyNames(EntityIdentifier entityID) {
061: String[] propNames = null;
062: EntityProperties ep = getCachedProperties(entityID);
063: if (ep != null) {
064: propNames = ep.getPropertyNames();
065: }
066: return propNames;
067: }
068:
069: public String getProperty(EntityIdentifier entityID, String name) {
070: String propVal = null;
071: EntityProperties ep = getCachedProperties(entityID);
072: if (ep != null) {
073: propVal = ep.getProperty(name);
074: }
075: return propVal;
076: }
077:
078: public void storeProperty(EntityIdentifier entityID, String name,
079: String value) {
080: this .unStoreProperty(entityID, name);
081: Connection conn = null;
082: PreparedStatement ps = null;
083: try {
084: conn = this .getConnection();
085: ps = conn.prepareStatement(insertProperty);
086: ps.clearParameters();
087: ps.setInt(1, org.jasig.portal.EntityTypes.getEntityTypeID(
088: entityID.getType()).intValue());
089: ps.setString(2, entityID.getKey());
090: ps.setString(3, name);
091: ps.setString(4, value);
092: ps.setTimestamp(5,
093: new Timestamp(System.currentTimeMillis()));
094: ps.executeUpdate();
095: ps.close();
096: clearCache(entityID);
097: } catch (Exception e) {
098: log.error("RDBMPropertyStore.storeProperty " + ps, e);
099: } finally {
100: this .releaseConnection(conn);
101: }
102: }
103:
104: public void unStoreProperty(EntityIdentifier entityID, String name) {
105: Connection conn = null;
106: PreparedStatement ps = null;
107: try {
108: conn = this .getConnection();
109: ps = conn.prepareStatement(deleteProperty);
110: ps.clearParameters();
111: ps.setInt(1, org.jasig.portal.EntityTypes.getEntityTypeID(
112: entityID.getType()).intValue());
113: ps.setString(2, entityID.getKey());
114: ps.setString(3, name);
115: ps.executeUpdate();
116: ps.close();
117: clearCache(entityID);
118: } catch (Exception e) {
119: log.error("RDBMPropertyStore.unStoreProperty " + ps, e);
120: } finally {
121: this .releaseConnection(conn);
122: }
123: }
124:
125: protected Connection getConnection() {
126: return RDBMServices.getConnection();
127: }
128:
129: protected void releaseConnection(Connection conn) {
130: RDBMServices.releaseConnection(conn);
131: }
132:
133: protected EntityProperties getCachedProperties(
134: EntityIdentifier entityID) {
135: EntityProperties ep = EntityPropertyRegistry.instance()
136: .getCachedProperties(entityID);
137: if (ep == null) {
138: ep = new EntityProperties(entityID.getKey());
139: Connection conn = null;
140: PreparedStatement ps = null;
141: try {
142: conn = this .getConnection();
143: ps = conn.prepareStatement(selectProperties);
144: ps.clearParameters();
145: ps
146: .setInt(1, org.jasig.portal.EntityTypes
147: .getEntityTypeID(entityID.getType())
148: .intValue());
149: ps.setString(2, entityID.getKey());
150: ResultSet rs = ps.executeQuery();
151: while (rs.next()) {
152: ep.setProperty(rs.getString(NAME_COL), rs
153: .getString(VALUE_COL));
154: }
155: addToCache(ep);
156: rs.close();
157: ps.close();
158: } catch (Exception e) {
159: log.error("RDBMPropertyStore.getPropertyNames: " + ps,
160: e);
161: } finally {
162: this .releaseConnection(conn);
163: }
164: }
165: return ep;
166: }
167:
168: protected void clearCache(EntityIdentifier entityID) {
169: EntityPropertyRegistry.instance().clearCache(entityID);
170: }
171:
172: protected void addToCache(EntityProperties ep) {
173: EntityPropertyRegistry.instance().addToCache(ep);
174: }
175:
176: }
|