comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:fcb8807fbd84
1 package de.mpiwg.monographs.servlet;
2
3 import org.codehaus.jettison.json.JSONArray;
4 import org.codehaus.jettison.json.JSONObject;
5 import org.swordapp.server.SwordServerException;
6
7 import edu.harvard.iq.dvn.core.admin.UserGroup;
8 import edu.harvard.iq.dvn.core.admin.VDCUser;
9 import edu.harvard.iq.dvn.core.study.FileMetadata;
10 import edu.harvard.iq.dvn.core.study.Study;
11 import edu.harvard.iq.dvn.core.study.StudyVersion;
12 import edu.harvard.iq.dvn.core.vdc.VDCGroup;
13
14 public class MonographUtils {
15
16 public static JSONObject jsonStudy(Study study){
17 JSONObject json = new JSONObject();
18
19 if(study != null){
20 try {
21 json.put("authority", study.getAuthority());
22 json.put("id", study.getId());
23 json.put("globalId", study.getGlobalId());
24 json.put("harvestIdentifier", study.getHarvestIdentifier());
25 json.put("persistentURL", study.getPersistentURL());
26 json.put("protocol", study.getProtocol());
27 json.put("version", study.getVersion());
28 json.put("studyId", study.getStudyId());
29 json.put("numberOfDownloads", study.getNumberOfDownloads());
30
31 JSONArray array0 = new JSONArray();
32 for(VDCUser user : study.getAllowedUsers()){
33 array0.put(jsonVDCUser(user));
34 }
35 json.put("allowedUsers", array0);
36
37
38 } catch (Exception e) {
39 e.printStackTrace();
40 }
41 }
42
43 return json;
44 }
45
46 public static JSONObject jsonVDCUser(VDCUser user){
47 JSONObject json = new JSONObject();
48
49 try {
50
51 json.put("email", user.getEmail());
52 json.put("firstName", user.getFirstName());
53 json.put("id", user.getId());
54 json.put("version", user.getVersion());
55 json.put("userName", user.getUserName());
56 json.put("position", user.getPosition());
57 json.put("lastName", user.getLastName());
58 json.put("institution", user.getInstitution());
59
60 JSONArray array0 = new JSONArray();
61 for(UserGroup group : user.getUserGroups()){
62 array0.put(jsonGroup(group));
63 }
64 json.put("userGroups", array0);
65
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69
70
71 return json;
72 }
73
74 public static JSONObject jsonGroup(UserGroup group){
75 JSONObject json = new JSONObject();
76
77 if(group != null){
78 try {
79
80 json.put("friendlyName", group.getFriendlyName());
81 json.put("id", group.getId());
82
83 } catch (Exception e) {
84 e.printStackTrace();
85 }
86 }
87
88 return json;
89 }
90
91
92 public static JSONObject jsonVDCGroup(VDCGroup group){
93 JSONObject json = new JSONObject();
94
95 if(group != null){
96 try {
97
98 json.put("Id", group.getId());
99 json.put("name", group.getName());
100 json.put("parent", group.getParent());
101 json.put("description", group.getDescription());
102 json.put("version", group.getVersion());
103
104 } catch (Exception e) {
105 e.printStackTrace();
106 }
107 }
108
109 return json;
110 }
111
112
113 public static JSONObject jsonStudyVersion(StudyVersion study) throws SwordServerException{
114
115 JSONObject jsonStudy = new JSONObject();
116
117 try {
118 jsonStudy.put("id", study.getId());
119 jsonStudy.put("fileMetadataSize", study.getFileMetadatas().size());
120 jsonStudy.put("fileCategoriesSize", study.getFileCategories().size());
121 jsonStudy.put("version", study.getVersion());
122 jsonStudy.put("archiveNote", study.getArchiveNote());
123 jsonStudy.put("numberOfFiles", study.getNumberOfFiles());
124 jsonStudy.put("versionNumber", study.getVersionNumber());
125 jsonStudy.put("versionState", study.getVersionState());
126 jsonStudy.put("versionNote", study.getVersionNote());
127 jsonStudy.put("deaccessionLink", study.getDeaccessionLink());
128 //jsonStudy.put("deaccessionLinkAsGlobalId", study.getDeaccessionLinkAsGlobalId());
129
130
131 JSONObject jsonMetadata = new JSONObject();
132 jsonMetadata.put("accessToSources", study.getMetadata().getAccessToSources());
133 jsonMetadata.put("authorsStr", study.getMetadata().getAuthorsStr());
134 jsonMetadata.put("contact", study.getMetadata().getContact());
135 jsonMetadata.put("dataSources", study.getMetadata().getDataSources());
136 jsonMetadata.put("dataCollector", study.getMetadata().getDataCollector());
137 jsonMetadata.put("id", study.getMetadata().getId());
138
139
140 JSONArray fileMetadatas = new JSONArray();
141 for(FileMetadata fileMetadata : study.getFileMetadatas()){
142 JSONObject tmp = new JSONObject();
143 tmp.put("id", fileMetadata.getId());
144 tmp.put("label", fileMetadata.getLabel());
145 tmp.put("version", fileMetadata.getVersion());
146 tmp.put("studyVersion", fileMetadata.getStudyVersion());
147
148 fileMetadatas.put(tmp);
149 }
150 jsonStudy.put("fileMetadatas", fileMetadatas);
151
152 JSONArray fileCategories = new JSONArray();
153 for(String fileCategory : study.getFileCategories()){
154 fileCategories.put(fileCategory);
155 }
156 jsonStudy.put("fileCategories", fileCategories);
157 } catch (Exception e) {
158 e.printStackTrace();
159 }
160 return jsonStudy;
161 }
162
163 }