view src/main/java/de/mpiwg/web/BranchEditor.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.web;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.apache.commons.lang.SerializationUtils;
import org.apache.log4j.Logger;

import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.bo.LGFile;
import de.mpiwg.gazetteer.dataverse.DataverseUtils;
import de.mpiwg.gazetteer.dataverse.bo.VDCUser;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.gazetteer.utils.FileManager;
import de.mpiwg.gazetteer.utils.SelectableObject;

public class BranchEditor extends AbstractBean{


	private static Logger logger = Logger.getLogger(BranchEditor.class);
	
	private LGBranch currentBranch;
	private LGFile currentLastFile;
	private List<LGFile> allFiles;
	private String text;
	private List<VDCUser> suggestionUserList;
	private List<SelectableObject<VDCUser>> contributors;
	
	public void loadBranch(Long branchId){
		LGBranch branch = DataProvider.getInstance().getBranch(branchId);
		this.loadBranch(branch);
	}
	
	public void loadBranch(LGBranch branch){
		logger.info("Loading Branch: " + branch.toString());
		this.reset();
		if(branch != null && branch.isPersistent()){
			
			try {
				this.currentBranch = (LGBranch)branch.clone();
				this.currentLastFile = DataProvider.getInstance().getFile(branch.getCurrentLastFileId());
				this.allFiles = DataProvider.getInstance().getAllFiles(branch.getId());
				this.text = FileManager.getFile(this.currentLastFile);
				this.contributors = new ArrayList<SelectableObject<VDCUser>>();
				for(Long userId : this.currentBranch.getContributorsList()){
					VDCUser user = DataverseUtils.getUser(userId);
					if(user != null){
						this.contributors.add(new SelectableObject<VDCUser>(user));
					}
					
				}
				//for(this.currentBranch.getContributorsList())
			} catch (Exception e) {
				internalError(e);
			}
			
			logger.info("allFiles.size=" + allFiles.size());
		}
	}
	
	public void reset(){
		this.currentBranch = null;
		this.currentLastFile = null;
		this.allFiles = null;
	}
	
	public void listenerShowContributorsDialog(ActionEvent event){
		this.suggestionUserList = new ArrayList<VDCUser>();
		try {
			for(VDCUser user : DataverseUtils.getAllUsers()){
				if(!currentBranch.hasContributor(user.getId())){
					this.suggestionUserList.add(user);
				}
			}
		} catch (Exception e) {
			internalError(e);
		}
		
		
	}
	
	public void listenerCloseContributorsDialog(ActionEvent event){
		this.suggestionUserList = null;
	}
	
	public void listenerUpdateBranch(ActionEvent event){
		this.saveBranch0();
	}
	
	public void listenerAddContributor(ActionEvent event){
		VDCUser user = (VDCUser)getRequestBean("contributor");
		if(user != null){
			this.suggestionUserList = null;
			this.currentBranch.addContributor(user.getId());
		}
		
		this.saveBranch0();
	}
	
	public void listenerRemoveContributor(ActionEvent event){
		List<VDCUser> toDelete = new ArrayList<VDCUser>();
		for(SelectableObject<VDCUser> so : new ArrayList<SelectableObject<VDCUser>>(this.contributors)){
			if(so.isSelected()){
				toDelete.add(so.getObj());
				this.contributors.remove(so);
			}
		}
		
		for(VDCUser user : toDelete){
			this.currentBranch.removeContributor(user.getId());
		}
		
		this.saveBranch0();
		
	}
	
	private void saveBranch0(){
		try {
			DataProvider.getInstance().updateBranch(currentBranch);
			this.loadBranch(currentBranch);
			addMsg("The branch has been updated!");
		} catch (Exception e) {
			internalError(e);
		}
	}
	
	public void listenerSaveText(ActionEvent event){
		try {
			LGFile newFile = DataProvider.getInstance().saveFile(currentBranch.getId(), this.text, getSessionBean().getUser().getId(), this.currentLastFile.getId());
			LGBranch branch = DataProvider.getInstance().getBranch(newFile.getBranchId());
			this.loadBranch(branch);
			addMsg("New File create " + newFile.getId());
		} catch (Exception e) {
			internalError(e);
		}
	}
	
	public LGBranch getCurrentBranch() {
		return currentBranch;
	}

	public void setCurrentBranch(LGBranch currentBranch) {
		this.currentBranch = currentBranch;
	}

	public LGFile getCurrentLastFile() {
		return currentLastFile;
	}

	public void setCurrentLastFile(LGFile currentLastFile) {
		this.currentLastFile = currentLastFile;
	}

	public List<LGFile> getAllFiles() {
		return allFiles;
	}

	public void setAllFiles(List<LGFile> allFiles) {
		this.allFiles = allFiles;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public List<VDCUser> getSuggestionUserList() {
		return suggestionUserList;
	}

	public void setSuggestionUserList(List<VDCUser> suggestionUserList) {
		this.suggestionUserList = suggestionUserList;
	}

	public List<SelectableObject<VDCUser>> getContributors() {
		return contributors;
	}

	public void setContributors(List<SelectableObject<VDCUser>> contributors) {
		this.contributors = contributors;
	}	
}