package ch.elexis.data;

import org.apache.commons.lang.StringUtils;

import ch.elexis.core.data.events.ElexisEventDispatcher;
import ch.rgw.tools.JdbcLink;
import ch.rgw.tools.JdbcLink.Stm;
import ch.rgw.tools.net.NetTool;

/**
 * Trace actions done in the system by a user.
 */
public class Trace {
	
	public static final String TABLENAME = "traces";
	
	public static void addTraceEntry(String username, String workstation, String action){
		if (StringUtils.isEmpty(username)) {
			User user = (User) ElexisEventDispatcher.getSelected(User.class);
			if (user != null) {
				username = user.getId();
			}
		}
		String _username =
			(StringUtils.isEmpty(username)) ? "unknown" : StringUtils.abbreviate(username, 30);
		
		if (StringUtils.isEmpty(workstation)) {
			workstation = NetTool.hostname;
		}
		String _workstation = (StringUtils.isEmpty(workstation)) ? "unknown"
				: StringUtils.abbreviate(workstation, 40);
		String _action = (StringUtils.isEmpty(action)) ? "" : action;
		
		JdbcLink connection = PersistentObject.getConnection();
		Stm statement = connection.getStatement();
		try {
			statement.exec("INSERT INTO " + TABLENAME + " VALUES("
				+ Long.toString(System.currentTimeMillis()) + ", "
				+ connection.wrapFlavored(_workstation) + ", " + connection.wrapFlavored(_username)
				+ ", " + connection.wrapFlavored(_action) + ")");
		} finally {
			connection.releaseStatement(statement);
		}
	}
	
	public static void addTraceEntry(String action){
		addTraceEntry(null, null, action);
	}
	
}

@@ -18,16 +18,82 @@
 .data;%0A%0A
+import java.sql.PreparedStatement;%0Aimport java.sql.SQLException;%0A%0A
 import o
@@ -127,16 +127,48 @@
 ngUtils;
+%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -252,42 +252,8 @@
 nk;%0A
-import ch.rgw.tools.JdbcLink.Stm;%0A
 impo
@@ -1109,11 +1109,110 @@
 ;%0A%09%09
-Stm
+%0A%09%09String insertStatement = %22INSERT INTO %22 + TABLENAME + %22 VALUES(?, ?, ?, ?)%22;%0A%09%09%0A%09%09PreparedStatement
  sta
@@ -1234,16 +1234,24 @@
 tion.get
+Prepared
 Statemen
@@ -1252,16 +1252,31 @@
 atement(
+insertStatement
 );%0A%09%09try
@@ -1295,185 +1295,132 @@
 ent.
-exec(%22INSERT INTO %22 + TABLENAME + %22 VALUES(%22%0A%09%09%09%09+ Long.toString(System.currentTimeMillis()) + %22, %22%0A%09%09%09%09+ connection.wrapFlavored(_workstation) + %22, %22 + connection.wrapFlavored(
+setString(1, Long.toString(System.currentTimeMillis()));%0A%09%09%09statement.setString(2, _workstation);%0A%09%09%09statement.setString(3, 
 _use
@@ -1429,60 +1429,171 @@
 ame)
+;
 %0A%09%09%09
-%09+ %22, %22 + connection.wrapFlavored(_action) + %22)%22
+statement.setString(4, _action);%0A%09%09%09statement.execute();%0A%09%09%7D catch (SQLException e) %7B%0A%09%09%09LoggerFactory.getLogger(Trace.class).error(%22Catched this - FIX IT%22, e
 );%0A%09
@@ -1626,16 +1626,24 @@
 .release
+Prepared
 Statemen

Fix trace by using prepared statement