package com.foc.business.workflow.implementation;

import java.sql.Date;

import com.foc.admin.FocUser;
import com.foc.business.workflow.WFSite;
import com.foc.business.workflow.WFTitle;
import com.foc.business.workflow.map.WFStage;
import com.foc.desc.FocConstructor;
import com.foc.desc.FocDesc;
import com.foc.desc.FocObject;
import com.foc.desc.field.FField;
import com.foc.log.FocLogEvent;
import com.foc.property.FObject;
import com.foc.util.Utils;

public class WFLog extends FocObject implements FocLogEvent {
	
	public WFLog(FocConstructor constr){
		super(constr);
		newFocProperties();
	}
	
	@Override
	public void dispose() {
		super.dispose();
	}

	public FocObject getObjectLogged() {
		FObject prop = (FObject) getFocProperty(WFLogDesc.FLD_MASTER);
		return (FocObject) prop.getObject();
	}
	
	public void setObjectLogged(FocObject focObject) {
		FObject prop = (FObject) getFocProperty(WFLogDesc.FLD_MASTER);
		prop.setObject_WithoutListeners(focObject);
	}
	
	public void setLogSubjectReference(long ref) {
		FObject prop = (FObject) getFocProperty(WFLogDesc.FLD_MASTER);
		if(prop != null) prop.setLocalReferenceInt(ref);
	}
	
	public void setEventType(int type){
		setPropertyMultiChoice(WFLogDesc.FLD_EVENT_TYPE, type);
	}

	public int getEventType(){
		return getPropertyMultiChoice(WFLogDesc.FLD_EVENT_TYPE);
	}

	public int getEventStatus(){
		return getPropertyMultiChoice(WFLogDesc.FLD_EVENT_STATUS);
	}

	public String getEventTypeTitle(){
		return getPropertyMultiChoiceTitle(WFLogDesc.FLD_EVENT_TYPE);
	}

	public void setUser(FocUser user){
		setPropertyObject(WFLogDesc.FLD_USER, user);
	}

	public FocUser getUser(){
		return (FocUser) getPropertyObject(WFLogDesc.FLD_USER);
	}

	public void setDateTime(Date date){
		setPropertyDate(WFLogDesc.FLD_DATE_TIME, date);
	}

	public Date getDateTime(){
		return getPropertyDate(WFLogDesc.FLD_DATE_TIME);
	}

	public void setDescription(String str){
		setPropertyString(WFLogDesc.FLD_DESCERIPTION, str);
	}

	public String getDescription(){
		return getPropertyString(WFLogDesc.FLD_DESCERIPTION);
	}

	public void setTitle(WFTitle title){
		setPropertyObject(WFLogDesc.FLD_TITLE, title);
	}

	public WFTitle getTitle(){
		return (WFTitle) getPropertyObject(WFLogDesc.FLD_TITLE);
	}

	public void setOnBehalfOf(boolean onBehalfOf){
		setPropertyMultiChoice(WFLogDesc.FLD_ON_BEHALF_OF, onBehalfOf ? 1 : 0);
	}

	public boolean isOnBehalfOf(){
		int onBehalf = getPropertyMultiChoice(WFLogDesc.FLD_ON_BEHALF_OF);
		return onBehalf == 1 ? true : false;
	}

	public void setComment(String comment){
		setPropertyString(WFLogDesc.FLD_COMMENT, comment);
	}

	public String getComment(){
		return getPropertyString(WFLogDesc.FLD_COMMENT);
	}
	
	public void setPreviousStage(WFStage stage){
		setPropertyObject(WFLogDesc.FLD_PREVIOUS_STAGE, stage);
	}

	public WFStage getPreviousStage(){
		return (WFStage) getPropertyObject(WFLogDesc.FLD_PREVIOUS_STAGE);
	}

	public void setTargetStage(WFStage stage){
		setPropertyObject(WFLogDesc.FLD_TARGET_STAGE, stage);
	}

	public WFStage getTargetStage(){
		return (WFStage) getPropertyObject(WFLogDesc.FLD_TARGET_STAGE);
	}
	
	public boolean getEventUndone(){
		return getPropertyBoolean(WFLogDesc.FLD_EVENT_UNDONE);
	}
	
	public void setEventUndone(boolean undone){
		setPropertyBoolean(WFLogDesc.FLD_EVENT_UNDONE, undone);
	}
	
	public String getSignedTransactionXML(){
    return getPropertyString(WFLogDesc.FLD_SIGNED_TRANSACTION_XML);
  }
  
  public void setSignedTransactionXML(String xmlContent){
    setPropertyString(WFLogDesc.FLD_SIGNED_TRANSACTION_XML, xmlContent);
  }

	@Override
	public String logEvent_GetEntityName() {
		FField  masterField = getThisFocDesc().getFieldByID(WFLogDesc.FLD_MASTER);
		FocDesc tableBeingLogged = masterField != null ? masterField.getFocDesc() : null;
		String storageName = tableBeingLogged != null ? tableBeingLogged.getStorageName() : "";
		return storageName;
	}

	@Override
	public long logEvent_GetEntityReference() {
		FObject prop = (FObject) getFocProperty(WFLogDesc.FLD_MASTER);
		long ref = prop != null ? prop.getLocalReferenceInt() : 0;
		return ref;
	}

	@Override
	public String logEvent_GetEntityCompanyName() {
		FocObject master = getObjectLogged();
		String compName = master != null && master.getCompany() != null ? master.getCompany().getName() : null;   
		return compName;
	}

	@Override
	public String logEvent_GetEntitySiteName() {
		FocObject master = getObjectLogged();
		WFSite site = master != null ? master.workflow_GetSite() : null;
		String siteName = site != null ? site.getName() : null;   
		return siteName;
	}

	@Override
	public String logEvent_GetEntityCode() {
		FocObject master = getObjectLogged();
		String code = master != null && master.code_getCode() != null ? master.code_getCode() : null;   
		return code;
	}

	@Override
	public Date logEvent_GetEntityDate() {
		FocObject master = getObjectLogged();
		return master != null ? master.getDate() : null;   
	}

	@Override
	public int logEvent_GetEventType() {
		return getEventType();
	}

	@Override
	public String logEvent_GetUsername() {
		String userName = "";
		
		FocUser user = getUser();
		if(user != null) {
			userName = user.getFullName();
			if(Utils.isStringEmpty(userName)) {
				userName = user.getName();
			}
		}
		return userName;
	}

	@Override
	public java.util.Date logEvent_GetDateTime() {
		return getDateTime();
	}

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

	@Override
	public long logEvent_GetLogEventReference() {
		return getReferenceInt();
	}

	@Override
	public int logEvent_GetStatus() {
		return getEventStatus();
	}
}

@@ -5656,20 +5656,28 @@
 %09return 
-null
+getComment()
 ;%0D%0A%09%7D%0D%0A%0D

Send the Log comment to log Listener