view src/main/java/de/mpiwg/gazetteer/bo/LGBranch.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 5610250d021a
line wrap: on
line source

package de.mpiwg.gazetteer.bo;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.json.JSONArray;
import org.json.JSONObject;

import cl.maps.duplex.DuplexKey;
import de.mpiwg.gazetteer.dataverse.DataverseUtils;
import de.mpiwg.gazetteer.db.DBBook;
import de.mpiwg.gazetteer.db.DBSection;
import de.mpiwg.gazetteer.utils.DBService;


@Entity
@Table(name="Branch")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class LGBranch extends DBEntry implements Cloneable, Serializable {
	private static final long serialVersionUID = 7805209462045170778L;
	

	@Column(name="sectionId")
	private Long sectionId;
	
	@Column(name="userId")
	private Long userId;
	
	@Column(name="contributors")
	private String contributors = "[]";

	@Column(name="label")
	private String label;
	
	@Column(name="description")
	private String description;
	
	@Column(name="currentLastFileId")
	private Long currentLastFileId;

	@Transient
	private List<Long> contributorsList;
	
	@Transient
	private String contributorsLabel;
	
	@Transient
	private DBBook book;
	
	@Transient
	private String sectionName;
	
	@Transient
	private boolean transientDataLoaded = false;
	
	public void loadTransientData(){
		try {
			DBSection section = DBService.getSectionWithContent(sectionId);
			DBBook book = DBService.getBook(section.getBookId());
			this.sectionName = section.getName();
			this.book = book;
			this.transientDataLoaded = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isTransientDataLoaded() {
		return transientDataLoaded;
	}



	public Object clone() throws CloneNotSupportedException {
		 LGBranch clone = (LGBranch)super.clone();
		 clone.setContributorsList(new ArrayList<Long>(clone.getContributorsList()));
		 clone.book = this.book;
		 return clone;
	}
	 
	 
	
	public boolean hasContributor(Long userId){
		for(Long id : getContributorsList()){
			if(id.equals(userId)){
				return true;
			}
		}
		return false;
	}
	
	public List<Long> getContributorsList(){
		if(contributorsList == null){
			reloadContributorsList();
		}
		return contributorsList;
	}
	
	public List<String> getContributorsNameList(){
		List<String> list = new ArrayList<String>();
		for(Long userId : getContributorsList()){
			list.add(DataverseUtils.getUsername(userId));
		}
		return list;
	}
	
	public String getUsername(){
		if(this.userId != null){
			return DataverseUtils.getUsername(this.userId);
		}
		return null;
	}
	
	private void reloadContributorsList(){
		this.contributorsList = new ArrayList<Long>();
		try {
			JSONArray array = new JSONArray(this.contributors);
			for(int i=0; i<array.length(); i++){
				this.contributorsList.add(array.getLong(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void addContributor(Long userId){
		if(!getContributorsList().contains(userId)){
			getContributorsList().add(userId);
		}
		upgradeContributors();
	}
	
	public void removeContributor(Long userId){
		if(getContributorsList().contains(userId)){
			getContributorsList().remove(userId);
			this.upgradeContributors();
		}
	}
	
	public void upgradeContributors(){
		JSONArray array = new JSONArray(getContributorsList());
		this.contributors = array.toString();
	}
	
	
	public Long getSectionId() {
		return sectionId;
	}

	public void setSectionId(Long sectionId) {
		this.sectionId = sectionId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getContributors() {
		return contributors;
	}

	public void setContributors(String contributors) {
		this.contributors = contributors;
	}
	
	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}	

	public Long getCurrentLastFileId() {
		return currentLastFileId;
	}

	public void setCurrentLastFileId(Long currentLastFileId) {
		this.currentLastFileId = currentLastFileId;
	}

	public DuplexKey<Long, Long> getKey(){
		return new DuplexKey<Long, Long>(this.userId, this.id);
	}
	
	
	@Override
	public String toString(){
		return "LGBranch[label=" + label + ", id=" + id + ", sectionId=" + sectionId + "]";
	}

	public String getContributorsLabel() {
		return contributorsLabel;
	}

	public void setContributorsLabel(String contributorsLabel) {
		this.contributorsLabel = contributorsLabel;
	}

	public void setContributorsList(List<Long> contributorsList) {
		this.contributorsList = contributorsList;
	}

	public String getSectionName() {
		return sectionName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	public DBBook getBook() {
		return book;
	}

	public void setBook(DBBook book) {
		this.book = book;
	}	
}