view src/main/java/de/mpiwg/web/jsp/BranchPage.java @ 31:c8d367a4bbcd

new: user updates label in branch
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Thu, 24 Sep 2015 13:35:39 +0200
parents 3b3e2963c8f7
children 2e1662afc81c
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.DBService;
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 datasetId;

	public void loadParameters(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		this.userId = getLongParameter("userId");
		this.fileId = getLongParameter("fileId");
		this.datasetId = getParameter("datasetId");
		
	}
	
	public void publishFile(){
		System.out.println("publishFile: " + this.fileId + " in " + datasetId);
		if(fileId != null && StringUtils.isNotEmpty(datasetId)){
		
			try {
				JSONObject resp = DataverseUtils.publishFile(fileId, datasetId, getSessionBean().getUser().getUserName(), getSessionBean().getUser().getPassword());
				System.out.println("resp: "+ resp);
				
				if(resp.has("status") && StringUtils.equals(resp.getString("status"), "ok")){
					// update record for the uploaded file
					LGFile file = DataProvider.getInstance().getFile(getFileId());
					long dvId = Long.parseLong(this.getDatasetId());	// dvId means datasetId, and all dataset are in "Chinese Local Gazetteers" dataverse
					file.setDvId(dvId);
					
					String datasetTitle = resp.getJSONObject("fileMetadata").getString("datasetTitle");
					
					file.setDatasetTitle(datasetTitle);
					
					DataProvider.getInstance().updateFile(file);
					
					addMsg("The File has been uploaded to dataverse.");
					
				} else if (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 publishFile_removed(){
		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");
					
					// TODO: bug? fileNameInDv will be duplicated and cannot be distinguished since the names of file from a branch with different versions will be the same
					
					
					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 getDatasetId() {
		return datasetId;
	}

	public void setDatasetId(String datasetId) {
		this.datasetId = datasetId;
	}

	public void updateLabel() {
		String branchLabel = getParameter("branchLabel");
		//logger.info("updateLabel: " + branchLabel + ", for branch id=" + this.getBranchId());
		this.getBranch().setLabel(branchLabel);
		this.saveBranch0();
		
	}
	
}