001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.ejb.beans.configuration;
034:
035: import com.flexive.core.Database;
036: import static com.flexive.core.DatabaseConst.TBL_USER_CONFIG;
037: import com.flexive.core.configuration.GenericConfigurationImpl;
038: import com.flexive.shared.FxContext;
039: import com.flexive.shared.exceptions.FxNoAccessException;
040: import com.flexive.shared.interfaces.UserConfigurationEngine;
041: import com.flexive.shared.interfaces.UserConfigurationEngineLocal;
042:
043: import javax.ejb.Stateless;
044: import javax.ejb.TransactionManagement;
045: import javax.ejb.TransactionManagementType;
046: import java.sql.Connection;
047: import java.sql.PreparedStatement;
048: import java.sql.SQLException;
049:
050: /**
051: * User configuration. Currently no security checks are included - a user
052: * may always update/delete own parameters (and never modify parameters of other users).
053: *
054: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: */
056:
057: @TransactionManagement(TransactionManagementType.CONTAINER)
058: @Stateless(name="UserConfigurationEngine")
059: public class UserConfigurationEngineBean extends
060: GenericConfigurationImpl implements UserConfigurationEngine,
061: UserConfigurationEngineLocal {
062: /**
063: * User config cache root. Be sure to set timeout for this cache
064: * region, otherwise user configuration data will never expire from the cache.
065: */
066: private static final String CACHE_ROOT = "/userConfig/";
067:
068: /** {@inheritDoc} */
069: @Override
070: protected Connection getConnection() throws SQLException {
071: return Database.getDbConnection();
072: }
073:
074: /** {@inheritDoc} */
075: @Override
076: protected PreparedStatement getInsertStatement(Connection conn,
077: String path, String key, String value) throws SQLException,
078: FxNoAccessException {
079: String sql = "INSERT INTO " + TBL_USER_CONFIG
080: + "(user_id, cpath, ckey, cvalue) VALUES (?, ?, ?, ?)";
081: PreparedStatement stmt = conn.prepareStatement(sql);
082: stmt.setLong(1, FxContext.get().getTicket().getUserId());
083: stmt.setString(2, path);
084: stmt.setString(3, key);
085: stmt.setString(4, value);
086: return stmt;
087: }
088:
089: /** {@inheritDoc} */
090: @Override
091: protected PreparedStatement getSelectStatement(Connection conn,
092: String path, String key) throws SQLException {
093: String sql = "SELECT cvalue FROM " + TBL_USER_CONFIG
094: + " WHERE user_id=? AND cpath=? AND ckey=?";
095: PreparedStatement stmt = conn.prepareStatement(sql);
096: stmt.setLong(1, FxContext.get().getTicket().getUserId());
097: stmt.setString(2, path);
098: stmt.setString(3, key);
099: return stmt;
100: }
101:
102: /** {@inheritDoc} */
103: @Override
104: protected PreparedStatement getSelectStatement(Connection conn,
105: String path) throws SQLException {
106: String sql = "SELECT ckey, cvalue FROM " + TBL_USER_CONFIG
107: + " WHERE user_id=? AND cpath=?";
108: PreparedStatement stmt = conn.prepareStatement(sql);
109: stmt.setLong(1, FxContext.get().getTicket().getUserId());
110: stmt.setString(2, path);
111: return stmt;
112: }
113:
114: /** {@inheritDoc} */
115: @Override
116: protected PreparedStatement getUpdateStatement(Connection conn,
117: String path, String key, String value) throws SQLException,
118: FxNoAccessException {
119: String sql = "UPDATE "
120: + TBL_USER_CONFIG
121: + " SET cvalue=? WHERE user_id=? AND cpath=? AND ckey=?";
122: PreparedStatement stmt = conn.prepareStatement(sql);
123: stmt.setString(1, value);
124: stmt.setLong(2, FxContext.get().getTicket().getUserId());
125: stmt.setString(3, path);
126: stmt.setString(4, key);
127: return stmt;
128: }
129:
130: /** {@inheritDoc} */
131: @Override
132: protected PreparedStatement getDeleteStatement(Connection conn,
133: String path, String key) throws SQLException,
134: FxNoAccessException {
135: String sql = "DELETE FROM " + TBL_USER_CONFIG
136: + " WHERE user_id=? AND cpath=? "
137: + (key != null ? " AND ckey=?" : "");
138: PreparedStatement stmt = conn.prepareStatement(sql);
139: stmt.setLong(1, FxContext.get().getTicket().getUserId());
140: stmt.setString(2, path);
141: if (key != null) {
142: stmt.setString(3, key);
143: }
144: return stmt;
145: }
146:
147: /** {@inheritDoc} */
148: @Override
149: protected String getCachePath(String path) {
150: return CACHE_ROOT + FxContext.get().getTicket().getUserId()
151: + path;
152: }
153: }
|