@@ -29,96 +29,99 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
2929
3030 myShepherd .setAction ("api.UserHome" );
3131 myShepherd .beginDBTransaction ();
32+ try {
33+ JSONObject home = new JSONObject ();
34+ User currentUser = myShepherd .getUser (request );
35+ if (currentUser == null ) {
36+ response .setStatus (401 );
37+ response .setHeader ("Content-Type" , "application/json" );
38+ response .getWriter ().write ("{\" success\" : false}" );
39+ return ;
40+ }
41+ home .put ("user" , currentUser .infoJSONObject (context , true ));
42+
43+ // TODO: Replace with OpenSearch
44+
45+ JSONArray encountersArr = new JSONArray ();
46+ int count = 0 ;
47+ for (Encounter enc : myShepherd .getEncountersForSubmitter (currentUser )) {
48+ JSONObject ej = new JSONObject ();
49+ ej .put ("id" , enc .getId ());
50+ ej .put ("date" , enc .getDate ());
51+ ej .put ("numberAnnotations" , Util .collectionSize (enc .getAnnotations ()));
52+ ej .put ("taxonomy" , enc .getTaxonomyString ());
53+ encountersArr .put (ej );
54+ count ++;
55+ if (count > 2 ) break ;
56+ }
57+ home .put ("latestEncounters" , encountersArr );
58+
59+ JSONObject itaskJson = null ;
60+ List <ImportTask > itasks = myShepherd .getImportTasksForUser (currentUser );
61+ if (itasks .size () > 0 ) {
62+ itaskJson = new JSONObject ();
63+ itaskJson .put ("id" , itasks .get (0 ).getId ());
64+ itaskJson .put ("dateTimeCreated" , itasks .get (0 ).getCreated ());
65+ itaskJson .put ("numberEncounters" , Util .collectionSize (itasks .get (0 ).getEncounters ()));
66+ itaskJson .put ("numberMediaAssets" , Util .collectionSize (itasks .get (0 ).getMediaAssets ()));
67+ }
68+ home .put ("latestBulkImportTask" , Util .jsonNull (itaskJson ));
69+
70+ JSONObject latestIndivJson = null ;
71+ for (Encounter enc : myShepherd .getEncountersForSubmitter (currentUser , "modified DESC" )) {
72+ if (enc .getIndividual () != null ) {
73+ latestIndivJson = new JSONObject ();
74+ latestIndivJson .put ("id" , enc .getIndividual ().getId ());
75+ latestIndivJson .put ("dateTime" , enc .getModified ());
76+ break ;
77+ }
78+ }
79+ home .put ("latestIndividual" , Util .jsonNull (latestIndivJson ));
80+
81+ // match result: if within 2 weeks, match result page; if older, the encounter page
82+ JSONObject matchJson = null ;
83+ List <Task > tasks = myShepherd .getIdentificationTasksForUser (currentUser );
84+ if (!Util .collectionIsEmptyOrNull (tasks )) {
85+ matchJson = new JSONObject ();
86+ matchJson .put ("id" , tasks .get (0 ).getId ());
87+ matchJson .put ("dateTimeCreated" , new DateTime (tasks .get (0 ).getCreatedLong ()));
88+ matchJson .put ("encounterId" , JSONObject .NULL );
89+ List <Annotation > anns = tasks .get (0 ).getObjectAnnotations ();
90+ if (!Util .collectionIsEmptyOrNull (anns ))
91+ for (Annotation ann : anns ) {
92+ Encounter enc = ann .findEncounter (myShepherd );
93+ if (enc != null ) {
94+ matchJson .put ("encounterId" , enc .getId ());
95+ break ;
96+ }
97+ }
98+ }
99+ home .put ("latestMatchTask" , Util .jsonNull (matchJson ));
100+
101+ JSONArray projArr = new JSONArray ();
102+ count = 0 ;
103+ for (Project proj : currentUser .getProjects (myShepherd )) {
104+ JSONObject pj = new JSONObject ();
105+ pj .put ("id" , proj .getId ());
106+ pj .put ("name" , proj .getResearchProjectName ());
107+ pj .put ("percentComplete" , proj .getPercentWithIncrementalIds ());
108+ pj .put ("numberEncounters" , proj .getEncounters ().size ());
109+ projArr .put (pj );
110+ count ++;
111+ if (count > 2 ) break ;
112+ }
113+ home .put ("projects" , projArr );
114+
115+ response .setStatus (200 );
116+ response .setCharacterEncoding ("UTF-8" );
117+ response .setHeader ("Content-Type" , "application/json" );
118+ response .getWriter ().write (home .toString ());
32119
33- JSONObject home = new JSONObject ();
34- User currentUser = myShepherd .getUser (request );
35- if (currentUser == null ) {
36- response .setStatus (401 );
37- response .setHeader ("Content-Type" , "application/json" );
38- response .getWriter ().write ("{\" success\" : false}" );
39- myShepherd .rollbackDBTransaction ();
40- myShepherd .closeDBTransaction ();
41- return ;
42- }
43- home .put ("user" , currentUser .infoJSONObject (context , true ));
44-
45- // TODO: Replace with OpenSearch
46-
47- JSONArray encountersArr = new JSONArray ();
48- int count = 0 ;
49- for (Encounter enc : myShepherd .getEncountersForSubmitter (currentUser )) {
50- JSONObject ej = new JSONObject ();
51- ej .put ("id" , enc .getId ());
52- ej .put ("date" , enc .getDate ());
53- ej .put ("numberAnnotations" , Util .collectionSize (enc .getAnnotations ()));
54- ej .put ("taxonomy" , enc .getTaxonomyString ());
55- encountersArr .put (ej );
56- count ++;
57- if (count > 2 ) break ;
58- }
59- home .put ("latestEncounters" , encountersArr );
60-
61- JSONObject itaskJson = null ;
62- List <ImportTask > itasks = myShepherd .getImportTasksForUser (currentUser );
63- if (itasks .size () > 0 ) {
64- itaskJson = new JSONObject ();
65- itaskJson .put ("id" , itasks .get (0 ).getId ());
66- itaskJson .put ("dateTimeCreated" , itasks .get (0 ).getCreated ());
67- itaskJson .put ("numberEncounters" , Util .collectionSize (itasks .get (0 ).getEncounters ()));
68- itaskJson .put ("numberMediaAssets" , Util .collectionSize (itasks .get (0 ).getMediaAssets ()));
69- }
70- home .put ("latestBulkImportTask" , Util .jsonNull (itaskJson ));
71-
72- JSONObject latestIndivJson = null ;
73- for (Encounter enc : myShepherd .getEncountersForSubmitter (currentUser , "modified DESC" )) {
74- if (enc .getIndividual () != null ) {
75- latestIndivJson = new JSONObject ();
76- latestIndivJson .put ("id" , enc .getIndividual ().getId ());
77- latestIndivJson .put ("dateTime" , enc .getModified ());
78- break ;
79- }
80- }
81- home .put ("latestIndividual" , Util .jsonNull (latestIndivJson ));
82-
83- // match result: if within 2 weeks, match result page; if older, the encounter page
84- JSONObject matchJson = null ;
85- List <Task > tasks = myShepherd .getIdentificationTasksForUser (currentUser );
86- if (!Util .collectionIsEmptyOrNull (tasks )) {
87- matchJson = new JSONObject ();
88- matchJson .put ("id" , tasks .get (0 ).getId ());
89- matchJson .put ("dateTimeCreated" , new DateTime (tasks .get (0 ).getCreatedLong ()));
90- matchJson .put ("encounterId" , JSONObject .NULL );
91- List <Annotation > anns = tasks .get (0 ).getObjectAnnotations ();
92- if (!Util .collectionIsEmptyOrNull (anns ))
93- for (Annotation ann : anns ) {
94- Encounter enc = ann .findEncounter (myShepherd );
95- if (enc != null ) {
96- matchJson .put ("encounterId" , enc .getId ());
97- break ;
98- }
99- }
100- }
101- home .put ("latestMatchTask" , Util .jsonNull (matchJson ));
102-
103- JSONArray projArr = new JSONArray ();
104- count = 0 ;
105- for (Project proj : currentUser .getProjects (myShepherd )) {
106- JSONObject pj = new JSONObject ();
107- pj .put ("id" , proj .getId ());
108- pj .put ("name" , proj .getResearchProjectName ());
109- pj .put ("percentComplete" , proj .getPercentWithIncrementalIds ());
110- pj .put ("numberEncounters" , proj .getEncounters ().size ());
111- projArr .put (pj );
112- count ++;
113- if (count > 2 ) break ;
114- }
115- home .put ("projects" , projArr );
116-
117- response .setStatus (200 );
118- response .setCharacterEncoding ("UTF-8" );
119- response .setHeader ("Content-Type" , "application/json" );
120- response .getWriter ().write (home .toString ());
121- myShepherd .rollbackDBTransaction ();
122- myShepherd .closeDBTransaction ();
120+ }
121+ catch (Exception e ){e .printStackTrace ();}
122+ finally {
123+ myShepherd .rollbackDBTransaction ();
124+ myShepherd .closeDBTransaction ();
125+ }
123126 }
124127}
0 commit comments