All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.odysz.semantic.LoggingUser Maven / Gradle / Ivy

package io.odysz.semantic;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.xml.sax.SAXException;

import io.odysz.common.Utils;
import io.odysz.semantic.util.SQLString;
import io.odysz.semantics.IUser;
import io.odysz.semantics.SemanticObject;
import io.odysz.semantics.meta.TableMeta;
import io.odysz.semantics.x.SemanticException;
import io.odysz.transact.x.TransException;

/**
 * This robot handle logs of table a_log()
 * 
 * @author [email protected]
 */
public class LoggingUser implements IUser {

	private DATranscxt logSemantic;
	private String uid;
	private SemanticObject action;
	@SuppressWarnings("unused")
	private String sessionKey;
	private String logConn;

	public static IUser dumbUser;

	/**
	 * @param logConn connection of logger is not an independent data source.
	 * @param userId
	 * @param action
	 */
	public LoggingUser(String logConn, String userId, SemanticObject action) {
		this.logConn = logConn;
		this.uid = userId;
		this.action = action;

		dumbUser = new IUser() {
			@Override public TableMeta meta(String ... connId) { return null; }
			@Override public ArrayList dbLog(ArrayList sqls) { return null; }
			@Override public String uid() { return "dummy"; }
			@Override public IUser logAct(String funcName, String funcId) { return this; }
			@Override public String sessionKey() { return null; }
			@Override public IUser sessionKey(String skey) { return null; }
			@Override public IUser notify(Object note) throws TransException { return this; }
			@Override public List notifies() { return null; }
			@Override public long touchedMs() { return 0; }
		};

		try {
			// @param logCfgPath e.g. "src/test/res/semantic-log.xml"
			// DATranscxt.loadSemantics(logConn);

			logSemantic = new DATranscxt(logConn); //, DATranscxt.meta(logConn));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override public TableMeta meta(String ... connId) { return null; }

	@Override
	public String uid() { return uid; }

	@Override
	public ArrayList dbLog(final ArrayList sqls) {
		return genLog(logConn, logSemantic, "a_logs", sqls, this,
				action.getString("funcName"),
				// 1.5.0,
				// action.getString("funcId"));
				this.sessionId() + "@" + this.deviceId());
	}

	/**
	 * Generate sqls for db logging.
	 * No exception can be thrown here, no error message for client if failed.
	 * @param logBuilder
	 * @param logTabl
	 * @param sqls
	 * @param commitUser
	 * @param funcName
	 * @param funcId
	 * @return sqls
	 */
	public static ArrayList genLog(String connLog, DATranscxt logBuilder, String logTabl,
			final ArrayList sqls, IUser commitUser, String funcName, String funcId) {
		try {
			logBuilder.insert(logTabl, dumbUser) // dummy for stop recursive logging
				.nv("oper", commitUser.uid())
				.nv("funcName", funcName)
				.nv("funcId", funcId)
				.nv("cnt", String.valueOf(sqls.size()))
				.nv("txt", txt(sqls))
				// .ins(logBuilder.basictx().clone(null)); // Note: must cloned, otherwise there are resulved values.
				.ins(logBuilder.instancontxt(connLog, null)); // Note: must cloned, otherwise there are resulved values.
		} catch (SQLException e) {
			// failed case must be a bug - commitLog()'s exception already caught.
			Utils.warn("Wrong configuration can leads to this failure. Check includes:\n" +
					"config.xml/k=log-connId value, make sure the connection is the correct for the semantics.xml.");
			e.printStackTrace();
		} catch (TransException e) {
			e.printStackTrace();
		}
		return sqls;
	}

	static String txt(ArrayList sqls) {
		return sqls == null ? null :
			sqls.stream()
				.map(e -> SQLString.formatSql(e))
				.collect(Collectors.joining(";"));
	}

	@Override
	public boolean login(Object req) throws TransException { return false; }

	@Override
	public String sessionId() { return null; }

	@Override
	public SemanticObject logout() { return null; }

	@Override
	public void writeJsonRespValue(Object writer) throws IOException { }

	@Override
	public IUser logAct(String funcName, String funcId) { return this; }

	@Override
	public IUser sessionKey(String skey) {
		this.sessionKey = skey;
		return this;
	}

	@Override
	public IUser notify(Object note) throws TransException {
		return null;
	}

	@Override
	public List notifies() { return null; }

	@Override
	public long touchedMs() { return System.currentTimeMillis(); }

	@Override
	public String sessionKey() { return null; }
}