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_DIVISION_CONFIG;
037: import com.flexive.core.configuration.GenericConfigurationImpl;
038: import com.flexive.core.storage.ContentStorage;
039: import com.flexive.core.storage.StorageManager;
040: import com.flexive.shared.FxContext;
041: import com.flexive.shared.FxSharedUtils;
042: import com.flexive.shared.exceptions.FxApplicationException;
043: import com.flexive.shared.exceptions.FxDbException;
044: import com.flexive.shared.exceptions.FxNoAccessException;
045: import com.flexive.shared.interfaces.DivisionConfigurationEngine;
046: import com.flexive.shared.interfaces.DivisionConfigurationEngineLocal;
047: import com.flexive.shared.structure.TypeStorageMode;
048: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050:
051: import javax.ejb.*;
052: import java.sql.Connection;
053: import java.sql.PreparedStatement;
054: import java.sql.SQLException;
055:
056: /**
057: * Division configuration implementation.
058: *
059: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
060: */
061:
062: @TransactionManagement(TransactionManagementType.CONTAINER)
063: @Stateless(name="DivisionConfigurationEngine")
064: public class DivisionConfigurationEngineBean extends
065: GenericConfigurationImpl implements
066: DivisionConfigurationEngine, DivisionConfigurationEngineLocal {
067: private static final transient Log LOG = LogFactory
068: .getLog(DivisionConfigurationEngineBean.class);
069: /**
070: * Division config cache root.
071: */
072: private static final String CACHE_ROOT = "/divisionConfig";
073:
074: /**
075: * {@inheritDoc}
076: */
077: @Override
078: protected Connection getConnection() throws SQLException {
079: return Database.getDbConnection();
080: }
081:
082: /**
083: * {@inheritDoc}
084: */
085: @Override
086: protected PreparedStatement getInsertStatement(Connection conn,
087: String path, String key, String value) throws SQLException,
088: FxNoAccessException {
089: if (!FxContext.get().getTicket().isGlobalSupervisor()) {
090: throw new FxNoAccessException(
091: "ex.configuration.update.perm.division");
092: }
093: String sql = "INSERT INTO " + TBL_DIVISION_CONFIG
094: + " (cpath, ckey, cvalue) VALUES (?, ?, ?)";
095: PreparedStatement stmt = conn.prepareStatement(sql);
096: stmt.setString(1, path);
097: stmt.setString(2, key);
098: stmt.setString(3, value);
099: return stmt;
100: }
101:
102: /**
103: * {@inheritDoc}
104: */
105: @Override
106: protected PreparedStatement getSelectStatement(Connection conn,
107: String path, String key) throws SQLException {
108: String sql = "SELECT cvalue FROM " + TBL_DIVISION_CONFIG
109: + " WHERE cpath=? AND ckey=?";
110: PreparedStatement stmt = conn.prepareStatement(sql);
111: stmt.setString(1, path);
112: stmt.setString(2, key);
113: return stmt;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: @Override
120: protected PreparedStatement getSelectStatement(Connection conn,
121: String path) throws SQLException {
122: String sql = "SELECT ckey, cvalue FROM " + TBL_DIVISION_CONFIG
123: + " WHERE cpath=?";
124: PreparedStatement stmt = conn.prepareStatement(sql);
125: stmt.setString(1, path);
126: return stmt;
127: }
128:
129: /**
130: * {@inheritDoc}
131: */
132: @Override
133: protected PreparedStatement getUpdateStatement(Connection conn,
134: String path, String key, String value) throws SQLException,
135: FxNoAccessException {
136: if (!FxContext.get().getTicket().isGlobalSupervisor()) {
137: throw new FxNoAccessException(
138: "ex.configuration.update.perm.division");
139: }
140: String sql = "UPDATE " + TBL_DIVISION_CONFIG
141: + " SET cvalue=? WHERE cpath=? AND ckey=?";
142: PreparedStatement stmt = conn.prepareStatement(sql);
143: stmt.setString(1, value);
144: stmt.setString(2, path);
145: stmt.setString(3, key);
146: return stmt;
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: @Override
153: protected PreparedStatement getDeleteStatement(Connection conn,
154: String path, String key) throws SQLException,
155: FxNoAccessException {
156: if (!FxContext.get().getTicket().isGlobalSupervisor()) {
157: throw new FxNoAccessException(
158: "ex.configuration.delete.perm.division");
159: }
160: String sql = "DELETE FROM " + TBL_DIVISION_CONFIG
161: + " WHERE cpath=? "
162: + (key != null ? " AND ckey=?" : "");
163: PreparedStatement stmt = conn.prepareStatement(sql);
164: stmt.setString(1, path);
165: if (key != null) {
166: stmt.setString(2, key);
167: }
168: return stmt;
169: }
170:
171: /**
172: * {@inheritDoc}
173: */
174: @Override
175: protected String getCachePath(String path) {
176: return CACHE_ROOT + path;
177: }
178:
179: /**
180: * {@inheritDoc}
181: */
182: @TransactionAttribute(TransactionAttributeType.REQUIRED)
183: public void installBinary(long binaryId, String resourceName)
184: throws FxApplicationException {
185: Connection con = null;
186: try {
187: ContentStorage storage = StorageManager
188: .getContentStorage(TypeStorageMode.Hierarchical);
189: String binaryName = resourceName;
190: String subdir = "";
191: if (binaryName.indexOf('/') > 0) {
192: binaryName = binaryName.substring(binaryName
193: .lastIndexOf('/') + 1);
194: subdir = resourceName.substring(0, resourceName
195: .lastIndexOf('/') + 1);
196: }
197: ClassLoader cl = Thread.currentThread()
198: .getContextClassLoader();
199: con = getConnection();
200: long length = 0;
201: String[] files = FxSharedUtils.loadFromInputStream(
202: cl.getResourceAsStream("fxresources/binaries/"
203: + subdir + "resourceindex.flexive"), -1)
204: .replaceAll("\r", "").split("\n");
205: for (String file : files) {
206: if (file.startsWith(binaryName + "|")) {
207: length = Long.parseLong(file.split("\\|")[1]);
208: break;
209: }
210: }
211: //TODO: check if length is still 0 or exception
212: storage.storeBinary(con, binaryId, 1, 1, binaryName,
213: length,
214: cl.getResourceAsStream("fxresources/binaries/"
215: + resourceName));
216: } catch (SQLException e) {
217: throw new FxDbException(LOG, e, "ex.db.sqlError", e
218: .getMessage());
219: } finally {
220: try {
221: if (con != null)
222: con.close();
223: } catch (SQLException e) {
224: //ignore
225: }
226: }
227: }
228: }
|