view DVN-web/src/main/java/de/mpiwg/monographs/servlet/MonographUtils.java @ 4:9b408c9b05ab

Integration with LGServices.
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 07 May 2015 14:56:46 +0200
parents 2ae72563a29d
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.Metadata;
import edu.harvard.iq.dvn.core.study.Study;
import edu.harvard.iq.dvn.core.study.StudyVersion;
import edu.harvard.iq.dvn.core.vdc.VDCGroup;
import edu.harvard.iq.dvn.core.web.study.StudyUI;
import edu.harvard.iq.dvn.core.vdc.VDC;

public class MonographUtils {
	
	public static JSONObject jsonStudyUI(StudyUI studyUI){
		JSONObject json = null;
		
		if(studyUI != null){
			try {
				json = jsonStudy(studyUI.getStudy());
				json.put("metadata", jsonMetadata(studyUI.getMetadata()));
				
				String status =
						(studyUI.getStudy().getLatestVersion().isDraft()) ? 
								"Draft" : 
								((studyUI.getStudy().getLatestVersion().isInReview()) ? 
										"In Review" :(studyUI.getStudy().getLatestVersion().isReleased()) ? 
												"Released" : "Deaccessioned");
				json.put("status", status);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return json;
	}
	
	public static JSONObject jsonMetadata(Metadata meta){
		JSONObject json = new JSONObject();
		
		try {
			
			json.put("accessToSources", meta.getAccessToSources());
			json.put("actionsToMinimizeLoss", meta.getActionsToMinimizeLoss());
			json.put("authorsStr", meta.getAuthorsStr());
			json.put("availabilityStatus", meta.getAvailabilityStatus());
			json.put("citationRequirements", meta.getCitationRequirements());
			json.put("subTitle", meta.getSubTitle());
			json.put("title", meta.getTitle());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return json;
	}
	
	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("createTime", study.getCreateTime());
				json.put("numberOfDownloads", study.getNumberOfDownloads());
				
				JSONObject owner = jsonVDC(study.getOwner());
				json.put("owner", owner);
				
				JSONObject creator = jsonVDCUser(study.getCreator());
				json.put("creator", creator);
				
				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 jsonVDC(VDC vdc){
		JSONObject json = new JSONObject();
		
		try {
			
			json.put("name", vdc.getName());
			json.put("aboutThis", vdc.getAboutThisDataverse());
			json.put("visibility", vdc.getVisibility());
			json.put("version", vdc.getVersion());
			json.put("parentSite", vdc.getParentSite());
			json.put("logo", vdc.getLogo());
			json.put("header", vdc.getHeader());
			json.put("alias", vdc.getAlias());
			
		} 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;
	}
	
}