view src/main/java/org/mpi/openmind/repository/bo/Node.java @ 71:aeb29e362a67

New ArabicNormalizer. NormalizerUtils.normalize() now does both translit and arabic normalization. 108: arabic normalization is not applied Task-Url: https://it-dev.mpiwg-berlin.mpg.de/tracs/ismi/ticket/108
author casties
date Thu, 02 Feb 2017 17:58:52 +0100
parents 3e4b05a6cb47
children d4b456623d43
line wrap: on
line source

package org.mpi.openmind.repository.bo;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.codec.binary.Base64;
import org.mpi.openmind.repository.utils.NormalizerUtils;
import org.mpi.openmind.repository.utils.RomanizationLoC;

/**
 *
 * @author jurzua
 */
@Entity
@Table(name="node")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="node_type", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("NODE")
public abstract class Node implements Serializable, Cloneable {
    private static final long serialVersionUID = 1L;

    public static String TYPE_TBOX = "TBox";
    public static String TYPE_ABOX = "ABox";
    public static String TYPE_VARIABLE = "Variable";
    public static String TYPE_FORMULA = "Formula";
    public static String TYPE_LINK = "Link";

    public static String SYS_STATUS_CURRENT_VERSION = "CURRENT_VERSION";
    public static String SYS_STATUS_PREVIOUS_VERSION = "PREVIOUS_VERSION";
    

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="row_id")
    private Long rowId;

    
    /** modification time as DateTime for frontend access */
    public String getTimeStamp(){
    	return DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM, DateFormat.SHORT).format(new Date(this.modificationTime));
    }
    

    /** timestamp for cache modification times */
    @Transient
    private long accessTime;

    public long getAccessTime() {
		return accessTime;
	}

	public void setAccessTime(long accessTime) {
		this.accessTime = accessTime;
	}

	@Transient
    private List<View> views = new ArrayList<View>();

    public void addView(View view){
    	if(view != null){
    		this.views.add(view);
    	}
    }
    
    public List<View> getViews() {
        return views;
    }

    public void setViews(List<View> views) {
        this.views = views;
    }

    @Column(name="long_value")
    private Long longValue;
    
    @Column(name="binary_value")
    private byte[] binaryValue;
    
    @Column(name="object_class")
    private String objectClass;
    
    @Column(name="user")
    private String user;
    
  //@Type(type = "org.hibernate.type.NumericBooleanType")
    @Column(name="public")
    private Boolean isPublic = Boolean.FALSE;

    public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getObjectClass() {
		return objectClass;
	}

	public void setObjectClass(String objectClass) {
		this.objectClass = objectClass;
	}

	public Long getLongValue() {
		return longValue;
	}

    
	public void setLongValue(Long longValue) {
		this.longValue = longValue;
	}

	public byte[] getBinaryValue() {
		return binaryValue;
	}

	public void setBinaryValue(byte[] binaryValue) {
		this.binaryValue = binaryValue;
	}

	@Column(name="modification_time")
    private Long modificationTime;

    @Column(name="id")
    private Long id;

    //lenght 16777215= Medium text
    @Column(name="own_value", columnDefinition="mediumtext")
    private String ownValue;

    //, length=16777215
    @Column(name="normalized_own_value", columnDefinition="mediumtext")
    private String normalizedOwnValue;
    
    //, length=16777215
    @Column(name="normalized_arabic_own_value", columnDefinition="mediumtext")
    private String normalizedArabicOwnValue;

    @Column(name="version")
    private Long version;

    @Column(name="status")
    private String status;

    @Column(name="system_status")
    private String systemStatus;

    @Column(name="type")
    private String type;

    @Column(name="content_type")
    private String contentType;
    
    /**
     * Returns true if the entity was made persistent.
     * It means the fields 'id' and 'rowId' are not null. 
     * @return
     */
    public boolean isPersistent(){
    	if(this.getId() == null || this.getRowId() == null)
    		return false;
    	else
    		return true;
    }

    public void increaseVersion(){
    	if(this.version == null){
    		this.version = new Long(1);
    	}else{
    		this.version++;
    	}    	
    }
    
    public String getOwnValue() {
        return ownValue;
    }

    public void setOwnValue(String ownValue) {
        this.ownValue = ownValue;
        this.normalizedOwnValue = NormalizerUtils.normalize(ownValue);
        this.normalizedArabicOwnValue = NormalizerUtils.normalizeArabic(ownValue);
    }
    
    public void autoNormalize(){
    	this.normalizedOwnValue = NormalizerUtils.normalize(ownValue);
        this.normalizedArabicOwnValue = NormalizerUtils.normalizeArabic(ownValue);
    }

    public String getRomanizationLoC(){
    	return RomanizationLoC.convert(this.ownValue);
    }
    
    public String getNormalizedOwnValue() {
        return normalizedOwnValue;
    }

    public void setNormalizedOwnValue(String normalizedOwnValue) {
        this.normalizedOwnValue = normalizedOwnValue;
    }
    
    public String getNormalizedArabicOwnValue() {
		return normalizedArabicOwnValue;
	}

	public void setNormalizedArabicOwnValue(String normalizedArabicOwnValue) {
		this.normalizedArabicOwnValue = normalizedArabicOwnValue;
	}

	public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public Long getRowId() {
        return rowId;
    }

    public void setRowId(Long rowId) {
        this.rowId = rowId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getModificationTime() {
        return modificationTime;
    }

    public void setModificationTime(Long modificationTime) {
        this.modificationTime = modificationTime;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getSystemStatus() {
        return systemStatus;
    }

    /**
     * Set the system_status of this Node.
     * 
     * @param systemStatus
     */
    public void setSystemStatus(String systemStatus) {
        this.systemStatus = systemStatus;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    /*
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (rowId != null ? rowId.hashCode() : 0);
        return hash;
    }*/
	@Override
    public int hashCode() {
		if(this.rowId == null){
			return super.hashCode();
		}else{
			return rowId.hashCode();
		}
	}

    /**
     * Clones this object and every object contained by it.
     * This method was implemented because the cache. The cache must return a cloned version of a object, 
     * otherwise when the object is modified, it becomes dirty and the cache could not recognize the change of the state.
     */
    @Override
    public Object clone() {
        try {
        	Node clone = (Node)super.clone();
        	clone.setViews(new ArrayList<View>());
        	for(View view : this.views){
        		clone.addView((View)view.clone());
        	}
            return clone;
        }
        catch (CloneNotSupportedException e) {
            throw new InternalError(e.toString());
        }
    }
    
    public Boolean getIsPublic() {
		return isPublic;
	}

	public void setIsPublic(Boolean isPublic) {
		this.isPublic = isPublic;
	}

	public String getPrivacity(){
		if(isPublic != null)
			return (isPublic) ? "public" : "private";
		return "private";
	}
	
	@Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Node)) {
            return false;
        }
        Node other = (Node) object;
        
        if(this.rowId != null && other.rowId != null && this.rowId.longValue() == other.rowId.longValue())
        	return true;
        
        /*
        if ((this.rowId == null && other.rowId != null) || (this.rowId != null && !this.rowId.equals(other.rowId))) {
            return false;
        }*/
        return false;
    }

    @Override
    public String toString() {
    	String rowIdString = (this.getRowId() == null) ? "" : "rowId=" + this.getRowId() + ", ";
    	String idString = (this.getId() == null) ? "" : "id=" + getId() + ", ";
    	
        return "Node[" + rowIdString + idString + "name=" + this.getObjectClass() + ", ownValue=" + this.getOwnValue() + ", 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 = "NODE["
            + "id=\"" + this.getId() + "\", "
            + "row-id=\"" + this.getRowId() + "\", "
            + "object-class=\"" + this.getObjectClass() + "\", "
            + "mtime=\"" + this.getModificationTime() + "\", "
            + "version=\"" + this.getVersion() + "\", "
            + "user=\"" + this.getUser() + "\", "
            + "public=\"" + this.getIsPublic() + "\", "
            + "system-status=\"" + this.getSystemStatus() + "\", "
            + "status=\"" + this.getStatus() + "\", "
            + "type=\"" + this.getType() + "\", "
            + "content-type=\"" + this.getContentType() + "\", "
            + "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!!!";
    }

    public String getNodeType() {
        return "NODE";
    }
}