view src/main/java/org/mpi/openmind/repository/bo/Relation.java @ 33:e52f593f9e0d

new transaction logger "openmind.transactionlog" logging entity save actions and their data. more comments in code.
author casties
date Fri, 26 Aug 2016 11:42:41 +0200
parents c23ae718fdd3
children 5737ab564b94
line wrap: on
line source

package org.mpi.openmind.repository.bo;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Transient;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.utils.OMUtils;

import cl.maps.penta.PentaKey;
import cl.maps.utils.RelKey;

/**
 *
 * @author jurzua
 */
@javax.persistence.Entity
@DiscriminatorValue("RELATION")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Relation extends Node implements Serializable, Comparable<Relation> {
    
	public Relation(){
		this.setSystemStatus(SYS_STATUS_CURRENT_VERSION);
	}
	
	
	public static Relation defRelation(Relation other, WrapperService ws) throws Exception{
		
		Relation rel = new Relation();
		
		rel.setOwnValue(other.getOwnValue());
		rel.setObjectClass(other.getObjectClass());
		rel.setType(other.getType());
		
		Entity src = ws.getDefinitionById(other.getSourceId());
		Entity tar = ws.getDefinitionById(other.getTargetId());
		if(src == null || tar == null){
			throw new Exception("src and/or target not found!");
		}
		rel.setSource(src);
		rel.setTarget(tar);	
		return rel;
	}
	
	public static Relation entRelation(Relation other, WrapperService ws) throws Exception{
		
		Relation rel = new Relation();
		
		rel.setOwnValue(other.getOwnValue());
		rel.setObjectClass(other.getOwnValue());
		rel.setType(Node.TYPE_ABOX);
		
		Entity src = ws.getEntityById(other.getSourceId());
		Entity tar = ws.getEntityById(other.getTargetId());
		
		if(src == null || tar == null){
			throw new Exception("src and/or target not found!");
		}
		
		rel.setSource(src);
		rel.setTarget(tar);		
		return rel;
	}
	
	public Relation(Entity source, Entity target){
		this.setSource(source);
		this.setTarget(target);
		source.addSourceRelation(this);
		target.addTargetRelation(this);
		this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
	}
	
	public Relation(Entity srcNonLW, Entity tarNonLW, String ownValue){
		this.setSource(srcNonLW);
		this.setTarget(tarNonLW);
		srcNonLW.addSourceRelation(this);
		tarNonLW.addTargetRelation(this);
		this.setOwnValue(ownValue);
		this.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION);
	}
	
    @Column(name="source_id")
    private Long sourceId;
    
    @Column(name="source_modif")
    private Long sourceModif;
    
    @Column(name="source_obj_class")
    private String sourceObjectClass;
    
    @Column(name="target_id")
    private Long targetId;
    
    @Column(name="target_modif")
    private Long targetModif;
    
    @Column(name="target_obj_class")
    private String targetObjectClass;

	@Transient
    private Entity source;

    @Transient
    private Entity target;
    
    @Transient
    private List<Attribute> attributes = new ArrayList<Attribute>();
    
    public Attribute getAttributeByName(String name) {
        for (Attribute attribute : this.getAttributes()) {
        	if (attribute.getObjectClass().equals(name)) {
        		return attribute;
        	}
        }
        return null;
    }
    
    public void addAttribute(Attribute att){
    	if(att != null){
    		att.setSourceId(this.getId());
    		att.setSourceModif(this.getModificationTime());
    		att.setSystemStatus(this.getSystemStatus());
    		this.attributes.add(att);
    	}
    }
    
    @Override
    public void setSystemStatus(String status) {
    	super.setSystemStatus(status);
    	if(this.attributes != null){
    		for(Attribute att : this.attributes){
    			att.setSystemStatus(status);
        	}	
    	}
    	
    }
    
    public List<Attribute> getAttributes() {
        return attributes;
    }

	public void setAttributes(List<Attribute> attributes) {
		this.attributes = attributes;
	}

	public String getSourceObjectClass() {
		return sourceObjectClass;
	}

	public void setSourceObjectClass(String sourceObjectClass) {
		this.sourceObjectClass = sourceObjectClass;
	}

	public String getTargetObjectClass() {
		return targetObjectClass;
	}

	public void setTargetObjectClass(String targetObjectClass) {
		this.targetObjectClass = targetObjectClass;
	}

    public Long getSourceModif() {
		return sourceModif;
	}

	public void setSourceModif(Long sourceModif) {
		this.sourceModif = sourceModif;
	}

	public Long getTargetModif() {
		return targetModif;
	}

	public void setTargetModif(Long targetModif) {
		this.targetModif = targetModif;
	}

    public Entity getSource() {
        return source;
    }

    public void setSource(Entity src) {
        this.source = src;
        if(src != null){
        	this.setSourceId(src.getId());
        	this.setSourceModif(src.getModificationTime());
        	this.setSourceObjectClass(src.getObjectClass());
        }
    }

    public Entity getTarget() {
        return target;
    }

    public void setTarget(Entity target) {
        this.target = target;
        if(target != null){
        	this.setTargetId(target.getId());
        	this.setTargetModif(target.getModificationTime());
        	this.setTargetObjectClass(target.getObjectClass());
        }
    }


    public Long getSourceId() {
        return sourceId;
    }

    public void setSourceId(Long sourceId) {
        this.sourceId = sourceId;
    }

    public Long getTargetId() {
        return targetId;
    }

    public void setTargetId(Long targetId) {
        this.targetId = targetId;
    }

    public boolean equalsContent(Relation other){
    	if(other == null){
    		return false;
    	}
    	
    	if(StringUtils.equals(getOwnValue(), other.getOwnValue()) &&
    			OMUtils.equals(getSourceId(), other.getSourceId()) &&
    			OMUtils.equals(getTargetId(), other.getTargetId())){
    		return true;
    	}
    	
    	return false;
    }
    
    
    @Override
    public String toString() {
    	String source = ", Src[id=" + sourceId;
    	if(getSource() != null)
    		source += ", src=" + getSource().getOwnValue();
    	source +="] ";
    	String target = ", Tar[id=" + targetId;
    	if(getTarget() != null)
    		target += ", tar=" + getTarget().getOwnValue();
    	target +="] ";
        return "Relation[" +
        		"rowId=" + this.getRowId() + ", " +
        		"id=" + getId() +", " +
        		"ownValue=" + getOwnValue() + source + target + ", " +
        		"sysStatus=" + this.getSystemStatus() + "]";
    }

    /**
     * Returns a String representation with base64-encoded own-value.
     * 
     * To be used for the transaction log.
     * 
     * @return
     */
    public String toEncString() {
        try {
            String es = "RELATION["
            + "id=\"" + this.getId() + "\", "
            + "row-id=\"" + this.getRowId() + "\", "
            + "object_class=\"" + this.getObjectClass() + "\", "
            + "source-id=\"" + this.getSourceId() + "\", "
            + "source-mtime=\"" + this.getSourceModif() + "\", "
            + "source-oc=\"" + this.getSourceObjectClass() + "\", "
            + "target-id=\"" + this.getTargetId() + "\", "
            + "target-mtime=\"" + this.getTargetModif() + "\", "
            + "target-oc=\"" + this.getTargetObjectClass() + "\", "
            + "mtime=\"" + this.getModificationTime() + "\", "
            + "version=\"" + this.getVersion() + "\", "
            + "user=\"" + this.getUser() + "\", "
            + "public=\"" + this.getIsPublic() + "\", "
            + "b64-value=\"" + ((this.getOwnValue() != null) ? Base64.encodeBase64String(this.getOwnValue().getBytes("UTF-8")) : "") + "\"]";
            return es;
        } catch (UnsupportedEncodingException e) {
            // this shouldn't happen
            e.printStackTrace();
        }
        return "!!!ENCODING-ERROR!!!";
    }

    
    @Override
	public int compareTo(Relation e) {
		if(e == null)
			return 1;
		if(StringUtils.isNotEmpty(this.getOwnValue()))
			return this.getOwnValue().compareTo(e.getOwnValue());
		else return 0;
	}

	public PentaKey<RelKey, Long, Long, String, Long> getKey(){
		return new PentaKey<RelKey, Long, Long, String, Long>(
				new RelKey(this.getSourceId(), this.getTargetId(), this.getOwnValue()), 
				this.getSourceId(), 
				this.getTargetId(),
				this.getOwnValue(),
				this.getId());
	}
}