view src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java @ 30:ab2fae5c5156

new: check our Chinese Local Gazetteers dataverse for login
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 11 Sep 2015 15:54:01 +0200
parents cbc2c83022c7
children 2e1662afc81c
line wrap: on
line source

package de.mpiwg.gazetteer.dataverse;


import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.mpiwg.gazetteer.bo.LGBranch;
import de.mpiwg.gazetteer.bo.LGFile;
import de.mpiwg.gazetteer.dataverse.bo.Datasets;
import de.mpiwg.gazetteer.dataverse.bo.Dataverse;
import de.mpiwg.gazetteer.dataverse.bo.Study;
import de.mpiwg.gazetteer.dataverse.bo.VDCUser;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.gazetteer.utils.HTTPUtils;
import de.mpiwg.gazetteer.utils.HTTPUtils.HttpStringResponse;
import de.mpiwg.gazetteer.utils.PropertiesUtils;

public class DataverseUtils {
	
	
	
	private static List<VDCUser> userList;
	
	static{
		userList = new ArrayList<VDCUser>();
		userList.add(new VDCUser(new Long(10), "jurzua", "1234"));
		userList.add(new VDCUser(new Long(11), "john", "1234"));
		userList.add(new VDCUser(new Long(12), "zoe", "1234"));
		userList.add(new VDCUser(new Long(13), "paul", "1234"));
		
	}
		

	
	public static JSONObject publishFile(Long fileId, String datasetId, String userName, String password) throws Exception{
			
			LGFile lgFile = DataProvider.getInstance().getFile(fileId);
			LGBranch branch = DataProvider.getInstance().getBranch(lgFile.getBranchId());
			String link = PropertiesUtils.getPropValue("dvn_server") + "/saveResource";
			
			// get datasetGlobalId by datasetId
			String apiToken = PropertiesUtils.getPropValue("dvn_apitoken");
			String dvnServerPath = PropertiesUtils.getPropValue("dvn_server");
		
			String query2 = dvnServerPath + "/api/datasets/"+ datasetId + "?key=" + apiToken;
			HttpStringResponse response1 = null;
			String datasetGlobalId = ""; 
			try {
				response1 = HTTPUtils.getStringResponse(query2);
				JSONObject jsonObject = new JSONObject(response1.content);
				if(StringUtils.equals(jsonObject.getString("status"), "OK")){
					JSONObject data = jsonObject.getJSONObject("data");
					String dsProtocol = data.getString("protocol");
					String dsAuthority = data.getString("authority");
					String dsIdentifier = data.getString("identifier");
					// concatenate info to datasetGlobalId
					datasetGlobalId = dsProtocol + ":"+ dsAuthority + "/" + dsIdentifier;
				} 
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
	
			MultipartUtility multipart = new MultipartUtility(link, "UTF-8");
            multipart.addFormField("datasetGlobalId", datasetGlobalId);
            multipart.addFormField("password", password);
            multipart.addFormField("user", userName);
            
            //String table = HTTPUtils.getTableOfFile(lgFile.getId());
            String table = HTTPUtils.getTabDelimitedOfFile(lgFile.getId());
            
            System.out.println("Table string lenght" + table.length());
            
            //multipart.addFilePart0(branch.getLabel() + ".html", table);
            
            // put table and name into a jsonObject then pass it
            JSONObject fileObj = new JSONObject();
            
            // TODO publish to LGDataverse using tab-delimiter data 
            //fileObj.put(branch.getLabel() + ".html", table);
            fileObj.put(branch.getLabel() + ".csv", table);
            
            String fileStr = fileObj.toString();
            multipart.addFormField("file", fileStr);
            
            // end
            
            
            String response = multipart.finish();
			
            System.out.println(response);
			
            return new JSONObject(response);
	}
	
	public static JSONObject publishFile_old(Long fileId, String studyGlobalId, String userName, String password) throws Exception{
		
		LGFile lgFile = DataProvider.getInstance().getFile(fileId);
		LGBranch branch = DataProvider.getInstance().getBranch(lgFile.getBranchId());
		String link = PropertiesUtils.getPropValue("dvn_server") + "/saveResource";
		
		
		MultipartUtility multipart = new MultipartUtility(link, "UTF-8");
        
        multipart.addFormField("studyId", studyGlobalId);
        multipart.addFormField("password", password);
        multipart.addFormField("user", userName);
        //multipart.addFormField("fileLabel", branch.getLabel() + ".html");
  

        String table = HTTPUtils.getTableOfFile(lgFile.getId());
        System.out.println("Table string lenght" + table.length());
        
        multipart.addFilePart0(branch.getLabel() + ".html", table);
        
        String response = multipart.finish();
		
        System.out.println(response);
		
        return new JSONObject(response);
	}
	
	
	public static List<Datasets> getDatasetsInAllDataverse(String userName, String password){
		// get the list of all dataverse
		List<Datasets> datasetList = new ArrayList<Datasets>();
		
		try {
			JSONArray dataverseArray;
			dataverseArray = getAllDataverseAlias();
			for (int i = 0; i < dataverseArray.length(); i++) {
				String aAlias = dataverseArray.getString(i);
				if (!StringUtils.equals("root", aAlias)) {					
					// it is not "root" dataverse
					List<Datasets> aDatasetList = getDatasets(userName, password, aAlias);

					// combine datasetList into a List<Datasets> 
					datasetList.addAll(aDatasetList);
			
				}
			}
			
		} catch (KeyManagementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
		return datasetList;
				
	}
	
	
	private static List<Datasets> getDatasets(String userName, String password, String dvId) throws IOException, JSONException, KeyManagementException, NoSuchAlgorithmException{
		
		String apiToken = PropertiesUtils.getPropValue("dvn_apitoken");
		String dvnServerPath = PropertiesUtils.getPropValue("dvn_server");
		
		/* 
		// list all the DvObjects under dataverse id. GET http://$SERVER/api/dataverses/$id/contents
		String dvId = PropertiesUtils.getPropValue("dv_id");	// get Local gazetteers dataverse id from config file
		// TODO the dvId above probably need to revise. Each user may have h/her own dataverse.
		// In this case, need to find out which dvIds belong to the user.
		
		// in the position of 'dvId', it can be used as the alias also as recommended by dataverse
		*/
		String query1 = dvnServerPath + "/api/dataverses/"+ dvId + "/contents?key=" + apiToken;
		
		
		List<Datasets> datasetList = new ArrayList<Datasets>();
		
		HttpStringResponse response = null;
		try {
			response = HTTPUtils.getStringResponse(query1);
			JSONObject json = new JSONObject(response.content);

			System.out.println("get contents in dataverse " + dvId + ": " + json);
			
			if(StringUtils.equals(json.getString("status"), "OK")){
				// go through all datasets in this dataverse
				JSONArray jsonData = json.getJSONArray("data");
				for (int i = 0; i < jsonData.length(); i ++) {
					JSONObject aDataset = jsonData.getJSONObject(i);
					String datasetId = aDataset.getString("id");
					
					System.out.println("checking datasetId= " + datasetId);
					
					// check if the user has roles to access to the dataset. 
					// needed to be contributor or above, or in defined groups
					if (canPublish(userName, password, dvId, datasetId)) {
						System.out.println("User @" + userName + " can upload files to datasetId= " + datasetId);
						
						JSONObject datasetJson = getDatasetInfo(datasetId);
						
						datasetJson.put("dvIdInDataverse", dvId);
							
						Datasets datasets = new Datasets(datasetJson);
						datasetList.add(datasets);
						
					}
					
				}
			}
			
		} catch (Exception e) {
			if(response != null){
				System.err.println(response.content);
			}
			e.printStackTrace();
		}
		return datasetList;
	}
	
	
	private static JSONArray getAllDataverseAlias() throws IOException, KeyManagementException, NoSuchAlgorithmException, JSONException {
		
		String dvnServerPath = PropertiesUtils.getPropValue("dvn_server");
		String query = dvnServerPath + "/getAllDataverseAlias";		// get all dataverse alias from LGDataverse
		
		HttpStringResponse response = null;
		response = HTTPUtils.getStringResponse(query);
		JSONObject data = new JSONObject(response.content);
		JSONArray allDataverseAlias = data.getJSONArray("dataverseAlias");
			
		return allDataverseAlias;
		
	}
	
	private static JSONObject getDatasetInfo(String datasetId) {
		JSONObject datasetJson = new JSONObject();	// datasetJson is the JSONObejct we collecting info about a dataset
		// show the dataset whose id is given: GET http://$SERVER/api/datasets/$id?key=$apiKey
	
		try {
			
			String dvnServerPath = PropertiesUtils.getPropValue("dvn_server");
			String apiToken = PropertiesUtils.getPropValue("dvn_apitoken");
			
			String query2 = dvnServerPath+ "/api/datasets/"+ datasetId + "?key=" + apiToken;
			HttpStringResponse response1 = null;
			
			
			response1 = HTTPUtils.getStringResponse(query2);
			JSONObject jsonObject = new JSONObject(response1.content);
			if(StringUtils.equals(jsonObject.getString("status"), "OK")){			
				// collect information for the dataset
				datasetJson.put("status", "OK");
			
				JSONObject data = jsonObject.getJSONObject("data");
				datasetJson.put("id", datasetId);
			
				datasetJson.put("authority", data.getString("authority"));
				datasetJson.put("persistentURL", data.getString("persistentUrl"));
				datasetJson.put("protocol", data.getString("protocol"));
				datasetJson.put("identifier", data.getString("identifier"));
	
				JSONObject latestVersion = data.getJSONObject("latestVersion");
				datasetJson.put("createTime", latestVersion.getString("createTime"));
			
				String versionState = latestVersion.getString("versionState");
				datasetJson.put("versionState",versionState);
			
				if (StringUtils.equals(versionState, "RELEASED")) {
					datasetJson.put("version", latestVersion.getString("versionNumber")+"."+latestVersion.getString("versionMinorNumber"));
				} else {
					datasetJson.put("version","");
				}
		
				JSONArray citationFields = latestVersion.getJSONObject("metadataBlocks").getJSONObject("citation")
					.getJSONArray("fields");
				for (int j = 0; j < citationFields.length(); j++) {
					JSONObject a = citationFields.getJSONObject(j);
					if( StringUtils.equals(a.getString("typeName"),"title")) {
						String title = a.getString("value");
						//System.out.println("title: " + title);
						datasetJson.put("title", title);
					}
				}

			} else {
				datasetJson.put("status", "ERROR");
			
			}
		
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		
		return datasetJson;
		
		
	}

	private static boolean canPublish(String userName, String password, String dvId, String datasetId) {
		
		String queryRoleAssignments;
		try {
			queryRoleAssignments = PropertiesUtils.getPropValue("dvn_server") + "/getRoleAssignments?user=" + userName + "&password=" + password 
						+ "&datasetId=" + datasetId;

			HttpStringResponse responseRoleAssignments = null;
			
			responseRoleAssignments = HTTPUtils.getStringResponse(queryRoleAssignments);
			JSONObject raJson = new JSONObject(responseRoleAssignments.content);
	
			if(StringUtils.equals(raJson.getString("state"), "ok") && raJson.has("roleAssignments")){
				JSONArray roleAssignments = raJson.getJSONArray("roleAssignments");
				
				for( int j = 0, size = roleAssignments.length(); j < size; j++ ) {
					
					JSONObject aRoleAssignment = roleAssignments.getJSONObject(j);
					String aIdentifier = aRoleAssignment.getString("identifier");
					String aRoleName = aRoleAssignment.getString("roleName");
					
				
					boolean checkRoles = false;
					// check user has the right to upload file:
					// 1. check user identifier 
					if (StringUtils.equals(aIdentifier, "@"+userName) ) {
						checkRoles = true;
					
					// 2. check user in the group which has the right to upload file	
					} else if (StringUtils.startsWith(aIdentifier, "&")) {
						
						String delims = "[&/-]+";
						String[] tokens = aIdentifier.split(delims);
						String group = tokens[tokens.length-1]; 	// parse group name from it. It is the last one.
							
						List<String> userGroups = new ArrayList<String>();	
						userGroups.add(group);
						// userGroups is designed to be List. But as here it is used as only one element, this could be modified in the future.
						if (isUserInGroups(userName, userGroups) ) {
							checkRoles = true;
						}
						
					}
				
					if (checkRoles) {
						if (StringUtils.equals(aRoleName, "Contributor") || StringUtils.equals(aRoleName, "Curator") || StringUtils.equals(aRoleName, "Admin")) {	
							return true;
						}
					}
						
				}
			
			}

		
		} catch (IOException e) {
			e.printStackTrace();
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}

	// getStudy() has been replaced by getDatasets() when we migrate to dataverse 4.0
	public static List<Study> getStudy(String userName, String password) throws IOException{
	
		String query = PropertiesUtils.getPropValue("dvn_server") + "/getStudies?user=" + userName + "&password=" + password;
		List<Study> list = new ArrayList<Study>();
		
		HttpStringResponse response1 = null;
		try {
			response1 = HTTPUtils.getStringResponse(query);
			JSONObject json = new JSONObject(response1.content);
			if(json.has("studies")){
				
				JSONArray userArray = json.getJSONArray("studies");
				
				for(int i=0; i<userArray.length(); i++){
					JSONObject studyJson = userArray.getJSONObject(i);
					Study study = new Study(studyJson);
					list.add(study);
				}	
			}
		} catch (Exception e) {
			if(response1 != null){
				System.err.println(response1.content);
			}
			e.printStackTrace();
		}
		
		return list;
	}
	
	public static VDCUser login0(String userName, String password){
		for(VDCUser user : userList){
			if(StringUtils.equals(userName, user.getUserName()) && StringUtils.equals(password, user.getPassword())){
				return user;
			}
		}
		return null;
	}
	
	public static List<VDCUser> getAllUsers0(){
		return userList;
	}
	
	public static VDCUser getUser(Long id){
		try {
			for(VDCUser user : getAllUsers()){
				if(user.getId().equals(id)){
					return user;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getUsername(Long id){

		try {
			for(VDCUser user : getAllUsers()){
				if(user.getId().equals(id)){
					return user.getUserName();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "user-no-found ("+ id +")";
	}
	
	
	public static List<VDCUser> getAllUsers() throws Exception{
		List<VDCUser> list = new ArrayList<VDCUser>();
		
		String query = PropertiesUtils.getPropValue("dvn_server") + "/getAllUsers";
		/*
		System.out.println("************************************************");
		System.out.println("URL: " + query);
		System.out.println("************************************************");
		*/
		HttpStringResponse response = null;
		try {
			response = HTTPUtils.getStringResponse(query);
			JSONObject json = new JSONObject(response.content);
			if(StringUtils.equals(json.getString("state"), "ok")){
				
				JSONArray userArray = json.getJSONArray("users");
				
				for(int i=0; i<userArray.length(); i++){
					JSONObject userJson = userArray.getJSONObject(i);
					VDCUser user = new VDCUser(userJson);
					list.add(user);
				}
					
			}
			
			
		} catch (Exception e) {
			if(response != null){
				System.err.println(response.content);
			}
			e.printStackTrace();
		}
		
		
		return list;	
	}
	
	public static VDCUser login(String userName, String password) throws Exception{
		
		System.out.println("userName=" + userName + ", password=" + password);
		
		String query = PropertiesUtils.getPropValue("dvn_server") + "/getUser?user=" + userName + "&password=" + password;
		
		System.out.println("************************************************");
		System.out.println("URL: " + query);
		System.out.println("************************************************");
		
		HttpStringResponse response = null;
		try {
			response = HTTPUtils.getStringResponse(query);
			JSONObject json = new JSONObject(response.content);
			if(StringUtils.equals(json.getString("state"), "ok")){
				VDCUser user = new VDCUser(json.getJSONObject("user"));
				return user;	
			}
			
			
		} catch (Exception e) {
			if(response != null){
				System.err.println(response.content);
			}
			e.printStackTrace();
		}
		
		return null;
	}

	public static boolean isUserInGroups(String userName, List<String> userGroups) throws IOException {
		/* Get group members for each group in userGroup list, then check if user in any of them
		 * get group members in a group by dataverse Api: GET http://$server/api/dataverses/$id/groups?key=$apiToken
		 * 								$id could be also an alias (string) in the input query above 
		 */
		
		String apiToken = PropertiesUtils.getPropValue("dvn_apitoken");
		
		for (String aGroup: userGroups) {
			String query = PropertiesUtils.getPropValue("dvn_server") + "/api/dataverses/2/groups/"+ aGroup +"?key=" + apiToken;
			//System.out.println("URL: " + query);
		
			HttpStringResponse response = null;
			try {
				response = HTTPUtils.getStringResponse(query);
				JSONObject json = new JSONObject(response.content);
			
				if(StringUtils.equals(json.getString("status"), "OK")){
					JSONObject jsonData = json.getJSONObject("data");
					JSONArray jsonContainedRoleAssignees = jsonData.getJSONArray("containedRoleAssignees");
					for (int i = 0; i < jsonContainedRoleAssignees.length(); i ++) {
						String aName = jsonContainedRoleAssignees.getString(i);
						if (aName.compareTo("@" + userName) == 0) {
							System.out.println("user "+ aName +" in group: " + aGroup);
							return true;	
						}
					}
			
				}
			} catch (Exception e) {
				if(response != null){
					System.err.println(response.content);
				}
				e.printStackTrace();
			}
		
		}
		
		return false;
	}

	public static boolean isUserInLG(String userName, String password) throws IOException {
		String localgazetteersDvId = PropertiesUtils.getPropValue("localgazetteers_dvId");
		
		
		String query = PropertiesUtils.getPropValue("dvn_server") + "/getRoleAssignments?user=" + userName + "&password=" + password + "&datasetId=" + localgazetteersDvId;
			
		HttpStringResponse response = null;
		try {
			response = HTTPUtils.getStringResponse(query);
			JSONObject json = new JSONObject(response.content);
			
			if(StringUtils.equals(json.getString("state"), "ok")){
				
				JSONArray jsonRoleAssignees = json.getJSONArray("roleAssignments");
				for (int i = 0; i < jsonRoleAssignees.length(); i ++) {
					JSONObject aUser = jsonRoleAssignees.getJSONObject(i);
					String aName = aUser.getString("identifier");
					String aRoleName = aUser.getString("roleName");
					
					if (aName.compareTo("@" + userName) == 0 && 
							( aRoleName.compareTo("Admin")==0 || aRoleName.compareTo("Curator")==0 || aRoleName.compareTo("Contributor")==0 )) {
						
						System.out.println("user "+ aName +" has role");
						return true;	
					}
				}
			
			}
		} catch (Exception e) {
			if(response != null){
				System.err.println(response.content);
			}
			e.printStackTrace();
		}
		
		
		
		return false;
	}



}