view src/main/java/de/mpiwg/gazetteer/bo/LGBranch.java @ 48:13555aff1f88

new: multiple full text searching. topics and tasks improvement.
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Thu, 21 Jan 2016 11:56:30 +0100
parents 3b3e2963c8f7
children
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 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;
import de.mpiwg.gazetteer.utils.DataProvider;


@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 DBSection section;
	
	@Transient
	private boolean transientDataLoaded = false;
	
	@Transient
	private boolean published = false;
	
	public boolean isEmpty() {
		if (this.section == null) {
			return true;
		} else {
			return false;
		}
	}
	
	@Transient
	private boolean sectionDeprecated = false;
	
	public boolean isDeprecated() {
		if (this.sectionDeprecated) {
			return true;
		} else {
			return false;
		}
	}
	
	public void loadTransientData(){
		try {
			this.section = DBService.getSectionWithContent(sectionId);
			if (this.section == null) {
				// the section has been deleted by user when doing TOC editing.
				
				this.sectionDeprecated = true;
				// get deprecated section from sections_revisions table
				this.section = DBService.getDeprecatedSectionWithContent(sectionId);
				if (this.section == null) {
					System.out.println("section " + sectionId + " doesn't exist.");
					return;
				} else {
					System.out.println("find deprecated section " + sectionId + " in sections_revisions table.");
				}
				
			} 
			this.book = section.getBook();	
			this.transientDataLoaded = true;
			this.published = atLeastOneFilePublished();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean atLeastOneFilePublished(){
		
		List<LGFile> files = DataProvider.getInstance().getAllFiles(this.id);
		for(LGFile file : files){
			if(file.getDvId() != null){
				return true;
			}
		}
		return false;
	}
	
	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 DBSection getSection() {
		return section;
	}

	public void setSection(DBSection section) {
		this.section = section;
	}

	public DBBook getBook() {
		return book;
	}

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

	public boolean isPublished() {
		return published;
	}

	public void setPublished(boolean published) {
		this.published = published;
	}	
	
	
}