view src/main/java/de/mpiwg/web/jsp/BranchPage.java @ 2:57d19e93f1c3

adding mapping classes
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Fri, 08 May 2015 16:36:25 +0200
parents 3e62083dbcbf
children 996f8f1b69db
line wrap: on
line source

package de.mpiwg.web.jsp;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;

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;

public class BranchPage extends AbstractJSPPage{

	private static Logger logger = Logger.getLogger(CreateFilePage.class);
	
	public static String bean = "branchBean";
	public static String page = "pages/branchPage.jsp";
	
	private LGBranch branch;
	private LGFile lastFile;
	private Long branchId;
	private List<LGFile> allFiles;
	private String text;
	private List<VDCUser> suggestionUserList;
	private List<VDCUser> contributors;
	
	private Long fileId;
	
	private Long userId;

	private String studyGlobalId;
	
	
	public void loadParameters(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.userId = getLongParameter("userId");
		this.fileId = getLongParameter("fileId");
		this.studyGlobalId = getParameter("studyGlobalId");
	}
	
	public void publishFile(){
		System.out.println("publishFile: " + this.fileId + " in " + studyGlobalId);
		if(fileId != null && StringUtils.isNotEmpty(studyGlobalId)){
			
			try {
				JSONObject resp = DataverseUtils.publishFile(fileId, studyGlobalId, getSessionBean().getUser().getUserName(), getSessionBean().getUser().getPassword());
				if(resp.has("fileMetadata")){
					
					Long dvId = resp.getJSONObject("fileMetadata").getLong("id");
					String fileNameInDv = resp.getJSONObject("fileMetadata").getString("label");
					
					LGFile  file = DataProvider.getInstance().getFile(getFileId());
					//LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId());
					file.setDvId(dvId);
					DataProvider.getInstance().updateFile(file);
					
					addMsg("The File has been published.");
					addMsg("The id of the file in Dataverse is: " + dvId + ".");
					addMsg("The name of the file in Dataverse is: " + fileNameInDv + ".");
					
				}else{
					if(resp.has("status") && StringUtils.equals(resp.getString("status"), "error")){
						addMsg("Dataverse reported an error: " + resp.getString("message"));
					}else{
						addMsg("Internal Error: problems generating the table for the file " + fileId);
					}
				}
				
			} catch (Exception e) {
				internalError(e);
			}
		}
	}
	
	public void loadBranch(String branchId0){
		try {
			
			this.branchId = Long.parseLong(branchId0);
			
			LGBranch branch = DataProvider.getInstance().getBranch(branchId);
			if(branch != null){
				this.loadBranch(branch);	
			}else{
				addMsg("Branch [id=" + branchId0 + "] no found.");
			}
			
			
		} catch (Exception e) {
			internalError(e);
		}
	}
	
	public void addContributor(){
		if(userId != null){
			
			VDCUser user = DataverseUtils.getUser(userId);
			System.out.println("Adding user: " + user.getUserName());
			
			this.branch.addContributor(userId);
			this.saveBranch0();
		}
	}
	
	public void deleteFile(){
		if(this.fileId != null){
			
			try {
				LGFile file = DataProvider.getInstance().getFile(fileId); 
				DataProvider.getInstance().deleteFile(file);
				LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId());
				this.loadBranch(branch);
				
			} catch (Exception e) {
				internalError(e);
			}
		}
	}
	
	public void removeContributor(){
		if(userId != null){
			
			VDCUser user = DataverseUtils.getUser(userId);
			System.out.println("Removing user: " + user.getUserName());
			
			this.branch.removeContributor(userId);
			this.saveBranch0();
		}
	}
	
	private void saveBranch0(){
		try {
			DataProvider.getInstance().updateBranch(branch);
			this.loadBranch(branch);
			addMsg("The branch has been updated!");
		} catch (Exception e) {
			internalError(e);
		}
	}
	
	public void loadBranch(LGBranch branch){
		logger.info("Loading Branch: " + branch.toString());
		this.reset();
		if(branch != null && branch.isPersistent()){
			
			try {
				this.branch = (LGBranch)branch.clone();
				this.lastFile = DataProvider.getInstance().getFile(branch.getCurrentLastFileId());
				this.allFiles = DataProvider.getInstance().getAllFiles(branch.getId());
				this.text = FileManager.getFileAsText(this.lastFile);
				this.contributors = new ArrayList<VDCUser>();
				for(Long userId : this.branch.getContributorsList()){
					VDCUser user = DataverseUtils.getUser(userId);
					if(user != null){
						this.contributors.add(user);
					}
					
				}
				
				this.loadSuggestionUserList();
			} catch (Exception e) {
				internalError(e);
			}
			
			logger.info("allFiles.size=" + allFiles.size());
		}
	}
	
	private void loadSuggestionUserList() throws Exception{
		this.suggestionUserList = new ArrayList<VDCUser>();
		for(VDCUser user : DataverseUtils.getAllUsers()){
			if(!branch.hasContributor(user.getId())){
				this.suggestionUserList.add(user);
			}
		}
	}
	
	public void reset(){
		this.branch = null;
		this.lastFile = null;
		this.allFiles = null;
	}
	
	public LGBranch getBranch() {
		return branch;
	}
	public void setBranch(LGBranch branch) {
		this.branch = branch;
	}
	public LGFile getLastFile() {
		return lastFile;
	}
	public void setLastFile(LGFile lastFile) {
		this.lastFile = lastFile;
	}
	public Long getBranchId() {
		return branchId;
	}
	public void setBranchId(Long branchId) {
		this.branchId = branchId;
	}
	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> getContributors() {
		return contributors;
	}

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

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

	public Long getUserId() {
		return userId;
	}

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

	public Long getFileId() {
		return fileId;
	}

	public void setFileId(Long fileId) {
		this.fileId = fileId;
	}
	public String getStudyGlobalId() {
		return studyGlobalId;
	}

	public void setStudyGlobalId(String studyGlobalId) {
		this.studyGlobalId = studyGlobalId;
	}
}