view src/main/java/de/mpiwg/gazetteer/bo/DBEntry.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
line wrap: on
line source

package de.mpiwg.gazetteer.bo;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
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.Temporal;
import javax.persistence.TemporalType;

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class DBEntry {
	
	private static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy-HH:mm");
	
	public DBEntry(){}
	
	@Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name="id")
    protected Long id;
	
	@Temporal(TemporalType.TIMESTAMP)
    @Column(name="creationDate")
	private Date creationDate;
	
	@Temporal(TemporalType.TIMESTAMP)
    @Column(name="lastChangeDate")
	private Date lastChangeDate;
	
	public boolean isPersistent(){
	    	if(this.getId() == null){
	    		return false;
	    	}else{
	    		return true;
	    	}	
    }
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getLastChangeDate() {
		return lastChangeDate;
	}

	public void setLastChangeDate(Date lastChangeDate) {
		this.lastChangeDate = lastChangeDate;
	}

	public String getFomattedLastChange(){
		if(this.lastChangeDate != null)
			return DATE_FORMAT.format(this.lastChangeDate);
		return null;
	}
	
	public String getFomattedCreation(){
		if(this.creationDate != null)
			return DATE_FORMAT.format(this.creationDate);
		return null;
	}
	
	
	public String getInfo(){
		if(isPersistent()){
			StringBuilder sb = new StringBuilder("id=" + id);
			
			if(creationDate != null){
				sb.append(", creation date=" + creationDate);
			}
			
			if(lastChangeDate != null){
				sb.append(", last change=" + lastChangeDate);
			}				
			return sb.toString();	
		}
		return new String();
	}
	
	@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 DBEntry)) {
            return false;
        }
        DBEntry other = (DBEntry) object;
        
        if(this.id != null && other.id != null && this.id.longValue() == other.id.longValue())
        	return true;
        
        return false;
    }
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append(this.getClass().toString());
		sb.append(" [id=" + this.id + "] ");
		return sb.toString();
	}
}