view src/main/java/de/mpiwg/gazetteer/dataverse/DataverseUtils.java @ 0:7682c04c63a8

First commit of the source code!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 14:50:41 +0100
parents
children
line wrap: on
line source

package de.mpiwg.gazetteer.dataverse;

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

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

import de.mpiwg.gazetteer.dataverse.bo.VDCUser;
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 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;
	}

	
	
}