view src/main/java/de/mpiwg/monographs/servlet/MonographUtils.java @ 0:fcb8807fbd84

Fist commit!
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 10 Mar 2015 15:15:30 +0100
parents
children
line wrap: on
line source

package de.mpiwg.monographs.servlet;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.swordapp.server.SwordServerException;

import edu.harvard.iq.dvn.core.admin.UserGroup;
import edu.harvard.iq.dvn.core.admin.VDCUser;
import edu.harvard.iq.dvn.core.study.FileMetadata;
import edu.harvard.iq.dvn.core.study.Study;
import edu.harvard.iq.dvn.core.study.StudyVersion;
import edu.harvard.iq.dvn.core.vdc.VDCGroup;

public class MonographUtils {
	
	public static JSONObject jsonStudy(Study study){
		JSONObject json = new JSONObject();
		
		if(study != null){
			try {
				json.put("authority", study.getAuthority());
				json.put("id", study.getId());
				json.put("globalId", study.getGlobalId());
				json.put("harvestIdentifier", study.getHarvestIdentifier());
				json.put("persistentURL", study.getPersistentURL());
				json.put("protocol", study.getProtocol());
				json.put("version", study.getVersion());
				json.put("studyId", study.getStudyId());
				json.put("numberOfDownloads", study.getNumberOfDownloads());
				
				JSONArray array0 = new JSONArray();
				for(VDCUser user :  study.getAllowedUsers()){
					array0.put(jsonVDCUser(user));
				}
				json.put("allowedUsers", array0);
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return json;
	}

	public static JSONObject jsonVDCUser(VDCUser user){
		JSONObject json = new JSONObject();
		
		try {
			
			json.put("email", user.getEmail());
			json.put("firstName", user.getFirstName());
			json.put("id", user.getId());
			json.put("version", user.getVersion());
			json.put("userName", user.getUserName());
			json.put("position", user.getPosition());
			json.put("lastName", user.getLastName());
			json.put("institution", user.getInstitution());
			
			JSONArray array0 = new JSONArray();
			for(UserGroup group : user.getUserGroups()){
				array0.put(jsonGroup(group));
			}
			json.put("userGroups", array0);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return json;
	}
	
	public static JSONObject jsonGroup(UserGroup group){
		JSONObject json = new JSONObject();
		
		if(group != null){
			try {
				
				json.put("friendlyName", group.getFriendlyName());
				json.put("id", group.getId());
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return json;
	}
	
	
	public static JSONObject jsonVDCGroup(VDCGroup group){
		JSONObject json = new JSONObject();
		
		if(group != null){
			try {
			
				json.put("Id", group.getId());
				json.put("name", group.getName());
				json.put("parent", group.getParent());
				json.put("description", group.getDescription());
				json.put("version", group.getVersion());
			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return json;
	}
	
	
	public static JSONObject jsonStudyVersion(StudyVersion study) throws SwordServerException{
		
		JSONObject jsonStudy = new JSONObject();
		
		try {
			jsonStudy.put("id", study.getId());
			jsonStudy.put("fileMetadataSize", study.getFileMetadatas().size());
			jsonStudy.put("fileCategoriesSize", study.getFileCategories().size());
			jsonStudy.put("version", study.getVersion());
			jsonStudy.put("archiveNote", study.getArchiveNote());
			jsonStudy.put("numberOfFiles", study.getNumberOfFiles());
			jsonStudy.put("versionNumber", study.getVersionNumber());
			jsonStudy.put("versionState", study.getVersionState());
			jsonStudy.put("versionNote", study.getVersionNote());
			jsonStudy.put("deaccessionLink", study.getDeaccessionLink());
			//jsonStudy.put("deaccessionLinkAsGlobalId", study.getDeaccessionLinkAsGlobalId());
			
			
			JSONObject jsonMetadata = new JSONObject();
			jsonMetadata.put("accessToSources", study.getMetadata().getAccessToSources());
			jsonMetadata.put("authorsStr", study.getMetadata().getAuthorsStr());
			jsonMetadata.put("contact", study.getMetadata().getContact());
			jsonMetadata.put("dataSources", study.getMetadata().getDataSources());
			jsonMetadata.put("dataCollector", study.getMetadata().getDataCollector());
			jsonMetadata.put("id", study.getMetadata().getId());
			
			
			JSONArray fileMetadatas = new JSONArray();
			for(FileMetadata fileMetadata :  study.getFileMetadatas()){
				JSONObject tmp = new JSONObject();
				tmp.put("id", fileMetadata.getId());
				tmp.put("label", fileMetadata.getLabel());
				tmp.put("version", fileMetadata.getVersion());
				tmp.put("studyVersion", fileMetadata.getStudyVersion());
				
				fileMetadatas.put(tmp);
			}
			jsonStudy.put("fileMetadatas", fileMetadatas);
			
			JSONArray fileCategories = new JSONArray();
			for(String fileCategory : study.getFileCategories()){
				fileCategories.put(fileCategory);
			}
			jsonStudy.put("fileCategories", fileCategories);	
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonStudy;
	}
	
}