diff src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java @ 28:cbc2c83022c7 dev_dataverse4

new: add dataverse level when publish which means users can have/publish their own dataverses and datasets
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Wed, 09 Sep 2015 17:04:01 +0200
parents 3b3e2963c8f7
children ab2fae5c5156
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java	Tue Sep 08 16:33:31 2015 +0200
+++ b/src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java	Wed Sep 09 17:04:01 2015 +0200
@@ -37,7 +37,7 @@
 		userList.add(new VDCUser(new Long(13), "paul", "1234"));
 		
 	}
-	
+		
 
 	
 	public static JSONObject publishFile(Long fileId, String datasetId, String userName, String password) throws Exception{
@@ -127,31 +127,64 @@
         System.out.println(response);
 		
         return new JSONObject(response);
-}
+	}
+	
 	
-	public static List<Datasets> getDatasets(String userName, String password) throws IOException{
+	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");
-
-		// TODO list all dataverse:
-		// Implement an api in LGDataverse that can get the list of all dataverses in our system/instance
-		// String queryAllDataverse = dvnServerPath + "/getDataverseAliasList";
 		
-		
-		
-		
-		
-		
+		/* 
 		// 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;
@@ -159,7 +192,7 @@
 			response = HTTPUtils.getStringResponse(query1);
 			JSONObject json = new JSONObject(response.content);
 
-			System.out.println("json: " + json);
+			System.out.println("get contents in dataverse " + dvId + ": " + json);
 			
 			if(StringUtils.equals(json.getString("status"), "OK")){
 				// go through all datasets in this dataverse
@@ -177,9 +210,11 @@
 						
 						JSONObject datasetJson = getDatasetInfo(datasetId);
 						
+						datasetJson.put("dvIdInDataverse", dvId);
+							
 						Datasets datasets = new Datasets(datasetJson);
 						datasetList.add(datasets);
-					
+						
 					}
 					
 				}
@@ -195,6 +230,20 @@
 	}
 	
 	
+	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
@@ -275,7 +324,6 @@
 	
 			if(StringUtils.equals(raJson.getString("state"), "ok") && raJson.has("roleAssignments")){
 				JSONArray roleAssignments = raJson.getJSONArray("roleAssignments");
-				//System.out.println("has role assignments." + roleAssignments);
 				
 				for( int j = 0, size = roleAssignments.length(); j < size; j++ ) {
 					
@@ -283,8 +331,7 @@
 					String aIdentifier = aRoleAssignment.getString("identifier");
 					String aRoleName = aRoleAssignment.getString("roleName");
 					
-					//System.out.println("identifier: " + aIdentifier+ ", roleName: " + aRoleName);
-		
+				
 					boolean checkRoles = false;
 					// check user has the right to upload file:
 					// 1. check user identifier