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;
034:
035: import com.flexive.core.Database;
036: import static com.flexive.core.DatabaseConst.TBL_HISTORY;
037: import com.flexive.shared.FxContext;
038: import com.flexive.shared.FxLanguage;
039: import com.flexive.shared.FxSharedUtils;
040: import com.flexive.shared.content.FxPK;
041: import com.flexive.shared.interfaces.HistoryTrackerEngine;
042: import com.flexive.shared.interfaces.HistoryTrackerEngineLocal;
043: import com.flexive.shared.security.UserTicket;
044: import com.flexive.shared.structure.FxType;
045: import org.apache.commons.lang.StringUtils;
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: import javax.ejb.*;
050: import java.sql.Connection;
051: import java.sql.PreparedStatement;
052:
053: /**
054: * History tracker service
055: *
056: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
057: */
058: @Stateless(name="HistoryTrackerEngine")
059: @TransactionManagement(TransactionManagementType.CONTAINER)
060: public class HistoryTrackerEngineBean implements HistoryTrackerEngine,
061: HistoryTrackerEngineLocal {
062:
063: private static transient Log LOG = LogFactory
064: .getLog(HistoryTrackerEngineBean.class);
065:
066: private static final String HISTORY_INSERT = "INSERT INTO "
067: + TBL_HISTORY
068: +
069: //1 2 3 4 5 6 7 8 9
070: "(ACCOUNT,LOGINNAME,TIMESTP,ACTION_KEY,ACTION_ARGS,EN_MESSAGE,SESSION,APPLICATION,REMOTEHOST,"
071: +
072: //10 11 12 13
073: "TYPEID,TYPENAME,PKID,PKVER)VALUES"
074: + "(?,?,?,?,?,?,?,?,?,?,?,?,?)";
075:
076: /**
077: * {@inheritDoc}
078: */
079: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
080: public void track(String key, Object... args) {
081: track(null, null, key, args);
082: }
083:
084: /**
085: * {@inheritDoc}
086: */
087: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
088: public void track(FxType type, String key, Object... args) {
089: track(type, null, key, args);
090: }
091:
092: /**
093: * {@inheritDoc}
094: */
095: @TransactionAttribute(TransactionAttributeType.SUPPORTS)
096: public void track(FxType type, FxPK pk, String key, Object... args) {
097: Connection con = null;
098: PreparedStatement ps = null;
099: try {
100: final UserTicket ticket = FxContext.get().getTicket();
101: con = Database.getDbConnection();
102: ps = con.prepareStatement(HISTORY_INSERT);
103: ps.setLong(1, ticket.getUserId());
104: ps.setString(2, ticket.getLoginName());
105:
106: ps.setLong(3, System.currentTimeMillis());
107: ps.setString(4, key);
108: ps.setString(5, StringUtils.join(args, '|'));
109: try {
110: ps
111: .setString(6, FxSharedUtils
112: .getLocalizedMessage("History",
113: FxLanguage.ENGLISH, "en", key,
114: args));
115: } catch (Exception e) {
116: ps.setString(6, key);
117: }
118: FxContext si = FxContext.get();
119: ps.setString(7, (si.getSessionId() == null ? "<unknown>"
120: : si.getSessionId()));
121: ps.setString(8,
122: (si.getApplicationId() == null ? "<unknown>" : si
123: .getApplicationId()));
124: ps.setString(9, (si.getRemoteHost() == null ? "<unknown>"
125: : si.getRemoteHost()));
126: if (type != null) {
127: ps.setLong(10, type.getId());
128: ps.setString(11, type.getName());
129: } else {
130: ps.setNull(10, java.sql.Types.NUMERIC);
131: ps.setNull(11, java.sql.Types.VARCHAR);
132: }
133: if (pk != null) {
134: ps.setLong(12, pk.getId());
135: ps.setInt(13, pk.getVersion());
136: } else {
137: ps.setNull(12, java.sql.Types.NUMERIC);
138: ps.setNull(13, java.sql.Types.NUMERIC);
139: }
140: ps.executeUpdate();
141: } catch (Exception ex) {
142: LOG.error(ex.getMessage());
143: } finally {
144: Database.closeObjects(HistoryTrackerEngineBean.class, con,
145: ps);
146: }
147: }
148: }
|