view src/main/java/de/mpiwg/gazetteer/bo/LGTopic.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 9dbbbfd474f4
children 95bf4ac726e6
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="Topic")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class LGTopic extends DBEntry implements Cloneable, Serializable {
	private static final long serialVersionUID = 7805209462045170778L;

	@Column(name="userId")
	private Long userId;
	
	@Column(name="contributors")
	private String contributors = "[]";
	
	@Column(name="nameEn")
	private String nameEn;
	
	@Column(name="nameCh")
	private String nameCh;
	
	@Column(name="namePinyin")
	private String namePinyin;
	
	@Column(name="description")
	private String description;

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

	
	@Transient
	private List<Long> contributorsList;
	

	@Transient
	private boolean transientDataLoaded = false;
	
	@Transient
	private Integer numOfSections = 0;
	
	
	public boolean isEmpty() {
		if (nameEn.equals("") || nameCh.equals("") || namePinyin.equals("") ) {
			return true;
		} else {
			return false;
		}
	}

	
	
	
	public boolean isTransientDataLoaded() {
		return transientDataLoaded;
	}



	public Object clone() throws CloneNotSupportedException {
		 LGTopic clone = (LGTopic)super.clone();
		 clone.setContributorsList(new ArrayList<Long>(clone.getContributorsList()));
		 
		 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 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 getNameEn() {
		return nameEn;
	}


	public void setNameEn(String nameEn) {
		this.nameEn = nameEn;
	}


	public String getNameCh() {
		return nameCh;
	}


	public void setNameCh(String nameCh) {
		this.nameCh = nameCh;
	}


	public String getNamePinyin() {
		return namePinyin;
	}


	public void setNamePinyin(String namePinyin) {
		this.namePinyin = namePinyin;
	}


	public String getDescription() {
		return description;
	}



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



	public String getKeywords() {
		return keywords;
	}


	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}


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


	public void setTransientDataLoaded(boolean transientDataLoaded) {
		this.transientDataLoaded = transientDataLoaded;
	}
	

	
	public Integer getNumOfSections() {
		if (numOfSections == 0){
			// TODO get number of sections in this topic from db table TopicSectionRelation with topicId=this.topicId
			Long topicId = this.getId();
			
			this.setNumOfSections(DataProvider.getInstance().getNumberOfSectionInTopic(topicId));
			
		}
		
		return numOfSections;
	}

	public void setNumOfSections(Integer numOfSections) {
		this.numOfSections = numOfSections;
	}




	public DuplexKey<Long, Long> getKey(){
		return new DuplexKey<Long, Long>(this.userId, this.id);
	}

	public String info() {
		return nameEn + "(" + nameCh + ", " + namePinyin + ")";
	}

	
	
	
	@Override
	public String toString(){
		return "LGTopic[nameEn=" + nameEn + ", nameCh=" + nameCh + ", namePinyin=" + namePinyin + "]";
	}

	

	
}