view src/main/java/org/mpi/openmind/repository/bo/Attribute.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.Entity;
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.json.JSONArray;

import cl.maps.triple.TripleKey;
import cl.maps.utils.AttKey;

/**
 * ALTER TABLE `openmind`.`node` ADD COLUMN `possible_value` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci AFTER `user`;
 * @author jurzua
 */
@Entity
@DiscriminatorValue("ATTRIBUTE")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Attribute extends Node implements Serializable, Comparable<Attribute> {
	
    @Column(name="source_id")
    private Long sourceId;
    
    @Column(name="source_modif")
    private Long sourceModif;
    
    @Column(name="source_obj_class")
    private String sourceObjectClass;
    
    /**
     * Only definitions can have attributes
     * which indicate possible values for the instances if the them.
     */
    @Column(name="possible_value", columnDefinition="mediumtext")
    private String possibleValues;

    
    @Transient
    private List<String> possibleValuesList = null;
    
    public List<String> getPossibleValuesList(){
    	if(this.possibleValuesList == null){
    		this.possibleValuesList = new ArrayList<String>();
    		if(StringUtils.isNotBlank(this.possibleValues)){
    			try{
        			JSONArray array = new JSONArray(this.possibleValues);
        			for(int i=0; i < array.length(); i++){
        				this.possibleValuesList.add(array.getString(i));
        			}
        		}catch (Exception e) {e.printStackTrace();}	
    		}
    	}
    	return this.possibleValuesList;
    }
    
	public void setPossibleValuesList(List<String> possibleValuesList) {
		this.possibleValuesList = possibleValuesList;
	}

	public Attribute(){}
	
	public Attribute(Attribute defAtt){
		this.setObjectClass(defAtt.getOwnValue());
		this.setContentType(defAtt.getContentType());
	}


    public String getName(){
        return getObjectClass();
    }

    public String getValue(){
        return getOwnValue();
    }
    
    public String getHtmlValue(){
    	String value = getOwnValue();
    	if(StringUtils.isNotEmpty(value)){
    		StringBuilder sb = new StringBuilder();
        	int lineLong = 80;
        	
        	
        	int count = 0;
        	while(!((count * lineLong) > value.length())){
        		int beginIndex = count * lineLong;
            	int endIndex = beginIndex + ((value.length() >= (lineLong * (count+1))) ? lineLong : value.length() % lineLong);
        		sb.append(value.substring(beginIndex, endIndex) + "<br/>");
        		count++;
        	}
        	return sb.toString();	
    	}
    	return null;
    }

    public void setName(String name){
        setObjectClass(name);
    }

    public void setValue(String value){
        setOwnValue(value);
    }

    public Attribute(String label, String contentType, String value) {
        this.setObjectClass(label);
        this.setContentType(contentType);
        this.setOwnValue(value);
    }
    
    public Long getSourceId() {
		return sourceId;
	}


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


	public Long getSourceModif() {
		return sourceModif;
	}


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


	public String getSourceObjectClass() {
		return sourceObjectClass;
	}


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

	public String getPossibleValues() {
		if(this.possibleValuesList != null && this.possibleValuesList.size() > 0){
			JSONArray array = new JSONArray();
			for(String possibleValue : this.possibleValuesList){
				array.put(possibleValue);
			}
			possibleValues = array.toString();
		}
		return possibleValues;
	}
	
	public String getPossibleValuesShort(){
		String s = this.getPossibleValues();
		if(StringUtils.isNotBlank(s)){
			if(s.length() > 20){
				s = s.substring(0, 20);
				s += "...";
			}	
		}
		return s;
	}

	public void setPossibleValues(String possibleValues) {
		this.possibleValues = possibleValues;
	}

	public boolean equalsContent(Attribute other){
		if(other == null)
			return false;
		
		if(StringUtils.equals(getOwnValue(), other.getOwnValue()) && 
				StringUtils.equals(getObjectClass(), other.getObjectClass())){
			return true;
		}
		return false;
	}
	
	public void loadEntValue(org.mpi.openmind.repository.bo.Entity ent){
		this.sourceId = ent.getId();
		this.sourceModif = ent.getModificationTime();
		this.sourceObjectClass = ent.getObjectClass();
	}
	
	public TripleKey<AttKey, Long, Long> getKey(){
		return new TripleKey<AttKey, Long, Long>(new AttKey(this.getSourceObjectClass(), this.getName()), this.getSourceId(), this.getId());
	}

	@Override
	public int compareTo(Attribute e) {
		if(e == null)
			return 1;
		if(StringUtils.isNotEmpty(this.getOwnValue())){
			if(StringUtils.isNotEmpty(e.getOwnValue())){
				return this.getOwnValue().compareTo(e.getOwnValue());	
			}else{
				return 1;
			}
		}	
		return 0;
	}
	
    @Override
    public String toString() {
        String rowIdString = (this.getRowId() == null) ? "" : "rowId=" + this.getRowId() + ", ";
        String idString = (this.getId() == null) ? "" : "id=" + getId() + ", ";

        return "Attribute[" + rowIdString + idString + "source=" + this.getSourceId() + ", 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 = "ATTRIBUTE["
            + "id=\"" + this.getId() + "\", "
            + "row-id=\"" + this.getRowId() + "\", "
            + "name=\"" + this.getObjectClass() + "\", "
            + "content-type=\"" + this.getContentType() + "\", "
            + "source-id=\"" + this.getSourceId() + "\", "
            + "source-mtime=\"" + this.getSourceModif() + "\", "
            + "source-oc=\"" + this.getSourceObjectClass() + "\", "
            + "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!!!";
    }
}