Mercurial > hg > LGServices
view src/main/java/de/mpiwg/web/jsp/BranchPage.java @ 66:f106f2487ac1
fix connection not close
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 03 Aug 2016 16:19:31 +0200 |
parents | 824b808a7481 |
children |
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(BranchPage.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 file.setDvId(dvId); String datasetTitle = resp.getJSONObject("fileMetadata").getString("datasetTitle"); file.setDatasetTitle(datasetTitle); // get fileId in LGDataverse, so that we can associate the two files // TODO keep track on them. If one in LGDataverse get deleted, notify the one in LGServices Long fileIdInDv = resp.getJSONObject("fileMetadata").getLong("fileId"); System.out.println("fileId in LGDataverse: " + fileIdInDv); file.setFileIdInDv(fileIdInDv); // get datasetGlobalId and save it to file database table, so that we will have the url to the dataset String datasetGlobalId = resp.getString("datasetGlobalId"); file.setDatasetPersistentId(datasetGlobalId); // the datasetGlobalId here is persistentId // update to File table in mysql database 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 forceReloadBranch() { // reload DataProvider DataProvider.resetInstance(); this.loadBranch(this.branch); } public void loadBranch(String branchId0){ System.out.println("loading branch: " + 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()); // TODO the currentLastFileId of branch is not updated from the saving? 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(); } }