Skip to content
Snippets Groups Projects
Commit 200651b3 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Safekeeping in branch

parent c327f243
Branches
Tags
No related merge requests found
...@@ -780,7 +780,7 @@ public class ObservationBean { ...@@ -780,7 +780,7 @@ public class ObservationBean {
* @param httpServletRequest * @param httpServletRequest
* @return * @return
*/ */
public ObservationDataSchema getObservationDataSchema(Integer organizationId, Integer organismId, HttpServletRequest httpServletRequest) public ObservationDataSchema getObservationDataSchema(Integer organizationId, Integer organismId)
{ {
try try
{ {
...@@ -798,6 +798,43 @@ public class ObservationBean { ...@@ -798,6 +798,43 @@ public class ObservationBean {
} }
} }
/**
* If there exist title translations for this schema, it
* @param schema
* @param httpServletRequest
* @return
*/
public ObservationDataSchema getLocalizedObservationDataSchema(ObservationDataSchema ods, HttpServletRequest httpServletRequest) throws IOException
{
ResourceBundle bundle = SessionLocaleUtil.getI18nBundle(httpServletRequest);
// We iterate the schema, replacing default field labels with
// translated ones
// First: Convert to Jackson JsonNode tree
ObjectMapper m = new ObjectMapper();
JsonNode rootNode = m.readTree(ods.getDataSchema());
Iterator<Entry<String, JsonNode>> nodeIterator = rootNode.fields();
String fieldKeyPrefix = "observationDataField_";
// Loop through each field
while (nodeIterator.hasNext()) {
Map.Entry<String, JsonNode> schemaPropertyField = (Map.Entry<String, JsonNode>) nodeIterator.next();
// Get the property field key (e.g. "counting2")
String fieldKey = schemaPropertyField.getKey();
// Find a translation.
if(bundle.containsKey(fieldKeyPrefix + fieldKey))
{
// If found, replace with translation
// Get the property field (e.g. {"title":"Counting 2"} )
JsonNode schemaProperty = schemaPropertyField.getValue();
((ObjectNode)schemaProperty).put("title", bundle.getString(fieldKeyPrefix + fieldKey));
((ObjectNode)rootNode).replace(fieldKey, schemaProperty);
}
}
ods.setDataSchema(m.writeValueAsString(rootNode));
return ods;
}
/** /**
* *
* @param organizationId * @param organizationId
......
...@@ -366,7 +366,7 @@ public class ObservationService { ...@@ -366,7 +366,7 @@ public class ObservationService {
} }
// Which organization does this observation belong to? // Which organization does this observation belong to?
VipsLogicUser observer = SessionControllerGetter.getUserBean().getVipsLogicUser(o.getUserId()); VipsLogicUser observer = SessionControllerGetter.getUserBean().getVipsLogicUser(o.getUserId());
o.setObservationDataSchema(SessionControllerGetter.getObservationBean().getObservationDataSchema(observer.getOrganizationId().getOrganizationId(), o.getOrganismId(), httpServletRequest)); o.setObservationDataSchema(SessionControllerGetter.getObservationBean().getObservationDataSchema(observer.getOrganizationId().getOrganizationId(), o.getOrganismId()));
VipsLogicUser user = (VipsLogicUser) httpServletRequest.getSession().getAttribute("user"); VipsLogicUser user = (VipsLogicUser) httpServletRequest.getSession().getAttribute("user");
if(user == null && userUUID != null) if(user == null && userUUID != null)
......
...@@ -39,6 +39,7 @@ import javax.ws.rs.Produces; ...@@ -39,6 +39,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import no.nibio.vips.logic.i18n.SessionLocaleUtil; import no.nibio.vips.logic.i18n.SessionLocaleUtil;
import no.nibio.vips.logic.util.SessionControllerGetter;
/** /**
* @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a>
...@@ -65,15 +66,12 @@ public class ObservationDataService { ...@@ -65,15 +66,12 @@ public class ObservationDataService {
@Path("schema/{organizationId}/{organismId}") @Path("schema/{organizationId}/{organismId}")
@Produces("application/json;charset=UTF-8") @Produces("application/json;charset=UTF-8")
public Response getSchema(@PathParam("organizationId") Integer organizationId,@PathParam("organismId") Integer organismId){ public Response getSchema(@PathParam("organizationId") Integer organizationId,@PathParam("organismId") Integer organismId){
// Try to find schema for given organism/organization
ObservationDataSchema ods = null;
try try
{ {
ods = em.createNamedQuery("ObservationDataSchema.findByPK", ObservationDataSchema.class) // Try to find schema for given organism/organization
.setParameter("organizationId", organizationId) ObservationDataSchema ods = SessionControllerGetter.getObservationBean().getObservationDataSchema(organizationId, organismId);
.setParameter("organismId", organismId) /*
.getSingleResult();
ResourceBundle bundle = SessionLocaleUtil.getI18nBundle(httpServletRequest); ResourceBundle bundle = SessionLocaleUtil.getI18nBundle(httpServletRequest);
// We iterate the schema, replacing default field labels with // We iterate the schema, replacing default field labels with
...@@ -100,11 +98,16 @@ public class ObservationDataService { ...@@ -100,11 +98,16 @@ public class ObservationDataService {
} }
} }
return Response.ok().entity(rootNode).build(); return Response.ok().entity(rootNode).build();
*/
ods = SessionControllerGetter.getObservationBean().getLocalizedObservationDataSchema(ods, httpServletRequest);
return Response.ok().entity(ods.getDataSchema()).build();
}catch(IOException | NoResultException ex){} }
catch(IOException ex)
// If not found, return standard nominator/denominator (unit) form {
return Response.ok().entity(ods != null ? ods.getDataSchema() : this.getStandardSchema()).build(); return Response.serverError().entity(ex).build();
}
} }
@GET @GET
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment