From 292a7bef59c393924936a256c127b4d34f7d424c Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Tue, 12 Nov 2024 22:53:41 +0530 Subject: [PATCH 1/9] Add traits endpoint --- .../traits/controller/TraitsController.java | 44 +++++++++++++++++ .../java/com/strandls/traits/pojo/Traits.java | 21 ++++++++ .../com/strandls/traits/pojo/TraitsValue.java | 20 ++++++++ .../services/Impl/TraitsServicesImpl.java | 49 +++++++++++++++++++ .../traits/services/TraitsServices.java | 7 +++ 5 files changed, 141 insertions(+) diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index 7e50106..6e70b9c 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -4,10 +4,12 @@ package com.strandls.traits.controller; import java.util.List; +import java.util.Map; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -26,6 +28,7 @@ import com.strandls.traits.pojo.Facts; import com.strandls.traits.pojo.FactsCreateData; import com.strandls.traits.pojo.FactsUpdateData; +import com.strandls.traits.pojo.Traits; import com.strandls.traits.pojo.TraitsValue; import com.strandls.traits.pojo.TraitsValuePair; import com.strandls.traits.services.TraitsServices; @@ -72,6 +75,28 @@ public Response getAllTraits() { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); } } + + @POST + @Path(ApiConstants.TRAIT + ApiConstants.CREATE) + @Produces(MediaType.APPLICATION_JSON) + + @ApiOperation(value = "Fetch all the Traits", notes = "Returns all the IBP traits", response = TraitsValuePair.class, responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 400, message = "unable to fetch all the traits", response = String.class) }) + + public Response createTrait(@QueryParam("dataType") String dataType, + @QueryParam("description") String description, + @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, + @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory, + @QueryParam("values") String values) { + try { + //List result = services.getAllObservationTraits(); + String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values); + return Response.status(Status.OK).entity(result).build(); + } catch (Exception e) { + return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } @GET @Path("/{objectType}/{objectId}") @@ -156,6 +181,25 @@ public Response getTraitList(@PathParam("speciesGroupId") String speciesGroupId) } } + + @GET + @Path(ApiConstants.TRAIT + "/{traitId}") + @Consumes(MediaType.TEXT_PLAIN) + @Produces(MediaType.APPLICATION_JSON) + + @ApiOperation(value = "Find facts by taxonId", notes = "Returns list of facts for a particular TaxonId", response = Facts.class, responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 400, message = "traits not found for TaxonId", response = String.class) }) + + public Response getTraitBytraitId(@PathParam("traitId") String trtId) { + try { + Long traitId = Long.parseLong(trtId); + Map result = services.fetchByTraitId(traitId); + return Response.status(Status.OK).entity(result).build(); + } catch (Exception e) { + return Response.status(Status.BAD_REQUEST).build(); + } + } @GET @Path(ApiConstants.TAXON + "/{taxonId}") diff --git a/src/main/java/com/strandls/traits/pojo/Traits.java b/src/main/java/com/strandls/traits/pojo/Traits.java index cfd656e..f325d07 100644 --- a/src/main/java/com/strandls/traits/pojo/Traits.java +++ b/src/main/java/com/strandls/traits/pojo/Traits.java @@ -4,6 +4,7 @@ package com.strandls.traits.pojo; import java.io.Serializable; +import java.time.LocalDateTime; import javax.persistence.Column; import javax.persistence.Entity; @@ -24,6 +25,8 @@ public class Traits implements Serializable { */ private static final long serialVersionUID = -7750012729432714454L; private Long id; + private LocalDateTime createdOn; + private LocalDateTime lastRevised; private String dataType; private String description; private Long fieldId; @@ -46,6 +49,24 @@ public Long getId() { public void setId(Long id) { this.id = id; } + + @Column(name="created_on") + public LocalDateTime getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(LocalDateTime createdOn) { + this.createdOn = createdOn; + } + + @Column(name="last_revised") + public LocalDateTime getLastRevised() { + return lastRevised; + } + + public void setLastRevised(LocalDateTime lastRevised) { + this.lastRevised = lastRevised; + } @Column(name = "data_types") public String getDataType() { diff --git a/src/main/java/com/strandls/traits/pojo/TraitsValue.java b/src/main/java/com/strandls/traits/pojo/TraitsValue.java index 6a438eb..281e054 100644 --- a/src/main/java/com/strandls/traits/pojo/TraitsValue.java +++ b/src/main/java/com/strandls/traits/pojo/TraitsValue.java @@ -28,6 +28,8 @@ public class TraitsValue implements Serializable { private String value; private String icon; private Long traitInstanceId; + private String description; + private String source; private Boolean isDeleted; @Id @@ -49,6 +51,24 @@ public String getValue() { public void setValue(String value) { this.value = value; } + + @Column(name = "description") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Column(name = "source") + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } @Column(name = "icon") public String getIcon() { diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index 07067e3..6508ca3 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -4,9 +4,12 @@ package com.strandls.traits.services.Impl; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.Comparator; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -257,6 +260,42 @@ public int compare(Traits o1, Traits o2) { return traitValuePair; } + + @Override + public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, + String units, Boolean showInObservation, Boolean isParticipatory, String values) { + Traits traits = new Traits(); + traits.setId(null); + traits.setCreatedOn(LocalDateTime.now()); + traits.setDataType(dataType); + traits.setDescription(description); + traits.setFieldId(fieldId); + traits.setName(name); + traits.setTraitTypes(traitTypes); + traits.setLastRevised(LocalDateTime.now()); + traits.setUnits(units); + traits.setIsNotObservationTraits(!showInObservation); + traits.setShowInObservation(showInObservation); + traits.setIsParticipatory(isParticipatory); + traits.setIsDeleted(false); + traits.setSource("IBP"); + traitsDao.save(traits); + String[] array = values.split("\\|"); + List list = Arrays.asList(array); + for (String value : list) { + String[] parts = value.split(":", 2); + TraitsValue traitValue = new TraitsValue(); + traitValue.setTraitInstanceId(traits.getId()); + traitValue.setId(null); + traitValue.setDescription(parts[0]); + traitValue.setSource("IBP"); + traitValue.setIcon(null); + traitValue.setIsDeleted(false); + traitValue.setValue(parts[1]); + traitsValueDao.save(traitValue); + } + return traits.getId().toString(); + } @Override public List createFacts(HttpServletRequest request, String objectType, Long objectId, @@ -367,6 +406,16 @@ else if (objectType.equalsIgnoreCase(OBJECTTYPE.SPECIES.getValue())) objectId, "species", result.getId(), activityType, mailData); } } + + @Override + public Map fetchByTraitId(Long traitId) { + Traits traitDetails = traitsDao.findById(traitId); + List traitValuesList = traitsValueDao.findTraitsValue(traitId); + Map details = new HashMap(); + details.put("traits", traitDetails); + details.put("values", traitValuesList); + return details; + } @Override public List fetchByTaxonId(Long taxonId) { diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index 90aeae1..bd99bb7 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -4,6 +4,7 @@ package com.strandls.traits.services; import java.util.List; +import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -11,6 +12,7 @@ import com.strandls.traits.pojo.Facts; import com.strandls.traits.pojo.FactsCreateData; import com.strandls.traits.pojo.FactsUpdateData; +import com.strandls.traits.pojo.Traits; import com.strandls.traits.pojo.TraitsValue; import com.strandls.traits.pojo.TraitsValuePair; @@ -27,9 +29,14 @@ public interface TraitsServices { public List getAllObservationTraits(); public List getObservationTraitList(Long speciesId); + + public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, + String units, Boolean showInObservation, Boolean isParticipatory, String values); public List createFacts(HttpServletRequest request, String objectType, Long objectId, FactsCreateData factsCreateData); + + public Map fetchByTraitId(Long traitId); public List fetchByTaxonId(Long taxonId); From d9a60cdaefb5f307c0e378188baf17553c66ae72 Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Fri, 15 Nov 2024 11:17:47 +0530 Subject: [PATCH 2/9] Added update endpoint --- .../traits/controller/TraitsController.java | 25 +++++++++++-- .../services/Impl/TraitsServicesImpl.java | 36 +++++++++++++++++-- .../traits/services/TraitsServices.java | 11 +++--- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index 6e70b9c..501c910 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -88,10 +88,29 @@ public Response createTrait(@QueryParam("dataType") String dataType, @QueryParam("description") String description, @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory, - @QueryParam("values") String values) { + @QueryParam("values") String values, @QueryParam("taxonIds") String taxonIds) { try { - //List result = services.getAllObservationTraits(); - String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values); + String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values,taxonIds); + return Response.status(Status.OK).entity(result).build(); + } catch (Exception e) { + return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @POST + @Path(ApiConstants.TRAIT + ApiConstants.UPDATE) + @Produces(MediaType.APPLICATION_JSON) + + @ApiOperation(value = "Fetch all the Traits", notes = "Returns all the IBP traits", response = TraitsValuePair.class, responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 400, message = "unable to fetch all the traits", response = String.class) }) + + public Response updateTrait( + @QueryParam("description") String description, @QueryParam("id") Long id, + @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, + @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory) { + try { + String result = services.updateTraits(description, id, name, traitTypes, showInObservation, isParticipatory); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index 6508ca3..00ce092 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -261,9 +261,23 @@ public int compare(Traits o1, Traits o2) { } + @Override + public String updateTraits (String description,Long id,String name,String traitTypes,Boolean showInObservation,Boolean isParticipatory) { + Traits trait = traitsDao.findById(id); + trait.setDescription(description); + trait.setName(name); + trait.setTraitTypes(traitTypes); + trait.setShowInObservation(showInObservation); + trait.setIsNotObservationTraits(!showInObservation); + trait.setIsParticipatory(isParticipatory); + trait.setLastRevised(LocalDateTime.now()); + traitsDao.update(trait); + return trait.getId().toString(); + } + @Override public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values) { + String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds) { Traits traits = new Traits(); traits.setId(null); traits.setCreatedOn(LocalDateTime.now()); @@ -280,20 +294,36 @@ public String createTraits (String dataType, String description, Long fieldId, S traits.setIsDeleted(false); traits.setSource("IBP"); traitsDao.save(traits); + if(values != null && !values.isEmpty()) { String[] array = values.split("\\|"); List list = Arrays.asList(array); for (String value : list) { - String[] parts = value.split(":", 2); + String[] parts = value.split(":"); TraitsValue traitValue = new TraitsValue(); traitValue.setTraitInstanceId(traits.getId()); traitValue.setId(null); traitValue.setDescription(parts[0]); traitValue.setSource("IBP"); - traitValue.setIcon(null); + if(parts.length==3) { + traitValue.setIcon(parts[2]); + } else { + traitValue.setIcon(null); + } traitValue.setIsDeleted(false); traitValue.setValue(parts[1]); traitsValueDao.save(traitValue); } + } + if(taxonIds != null && !taxonIds.isEmpty()) { + String[] array = taxonIds.split("\\|"); + List list = Arrays.asList(array); + for (String value : list) { + TraitTaxonomyDefinition taxonId = new TraitTaxonomyDefinition(); + taxonId.setTraitTaxonId(traits.getId()); + taxonId.setTaxonomyDefifintionId(Long.parseLong(value)); + traitTaxonomyDef.save(taxonId); + } + } return traits.getId().toString(); } diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index bd99bb7..5c9bf71 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -7,6 +7,7 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.QueryParam; import com.strandls.traits.pojo.FactValuePair; import com.strandls.traits.pojo.Facts; @@ -29,13 +30,15 @@ public interface TraitsServices { public List getAllObservationTraits(); public List getObservationTraitList(Long speciesId); - - public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values); + + public String createTraits(String dataType, String description, Long fieldId, String name, String traitTypes, + String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds); + + public String updateTraits (String description,Long id,String name,String traitTypes,Boolean showInObservation,Boolean isParticipatory); public List createFacts(HttpServletRequest request, String objectType, Long objectId, FactsCreateData factsCreateData); - + public Map fetchByTraitId(Long traitId); public List fetchByTaxonId(Long taxonId); From 82a8276598908e81636845d3c64dcf410b741ecd Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Fri, 15 Nov 2024 11:39:00 +0530 Subject: [PATCH 3/9] Added trait icon functionality --- .../strandls/traits/controller/TraitsController.java | 4 ++-- src/main/java/com/strandls/traits/pojo/Traits.java | 10 ++++++++++ .../traits/services/Impl/TraitsServicesImpl.java | 3 ++- .../com/strandls/traits/services/TraitsServices.java | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index 501c910..24dca69 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -88,9 +88,9 @@ public Response createTrait(@QueryParam("dataType") String dataType, @QueryParam("description") String description, @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory, - @QueryParam("values") String values, @QueryParam("taxonIds") String taxonIds) { + @QueryParam("values") String values, @QueryParam("taxonIds") String taxonIds, @QueryParam("icon") String icon) { try { - String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values,taxonIds); + String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values,taxonIds,icon); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); diff --git a/src/main/java/com/strandls/traits/pojo/Traits.java b/src/main/java/com/strandls/traits/pojo/Traits.java index f325d07..59e3f7b 100644 --- a/src/main/java/com/strandls/traits/pojo/Traits.java +++ b/src/main/java/com/strandls/traits/pojo/Traits.java @@ -38,6 +38,7 @@ public class Traits implements Serializable { private Boolean isParticipatory; private Boolean isDeleted; private String source; + private String icon; @Id @GeneratedValue @@ -148,6 +149,15 @@ public String getSource() { public void setSource(String source) { this.source = source; } + + @Column(name = "icon") + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } @Column(name = "units") public String getUnits() { diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index 00ce092..f0ab7c9 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -277,7 +277,7 @@ public String updateTraits (String description,Long id,String name,String traitT @Override public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds) { + String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds, String icon) { Traits traits = new Traits(); traits.setId(null); traits.setCreatedOn(LocalDateTime.now()); @@ -293,6 +293,7 @@ public String createTraits (String dataType, String description, Long fieldId, S traits.setIsParticipatory(isParticipatory); traits.setIsDeleted(false); traits.setSource("IBP"); + traits.setIcon(icon); traitsDao.save(traits); if(values != null && !values.isEmpty()) { String[] array = values.split("\\|"); diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index 5c9bf71..97cba45 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -32,7 +32,7 @@ public interface TraitsServices { public List getObservationTraitList(Long speciesId); public String createTraits(String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds); + String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds, String icon); public String updateTraits (String description,Long id,String name,String traitTypes,Boolean showInObservation,Boolean isParticipatory); From 4ccc80cc8a52eb6d382557d014750d463c313636 Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Tue, 3 Dec 2024 11:40:48 +0530 Subject: [PATCH 4/9] Added trait update endpoint --- .../traits/controller/TraitsController.java | 31 +++--- .../com/strandls/traits/pojo/TraitsValue.java | 12 +- .../services/Impl/TraitsServicesImpl.java | 103 ++++++++++++++---- .../traits/services/TraitsServices.java | 8 +- 4 files changed, 115 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index 24dca69..d08f495 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -75,7 +75,7 @@ public Response getAllTraits() { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); } } - + @POST @Path(ApiConstants.TRAIT + ApiConstants.CREATE) @Produces(MediaType.APPLICATION_JSON) @@ -84,19 +84,22 @@ public Response getAllTraits() { @ApiResponses(value = { @ApiResponse(code = 400, message = "unable to fetch all the traits", response = String.class) }) - public Response createTrait(@QueryParam("dataType") String dataType, - @QueryParam("description") String description, + public Response createTrait(@QueryParam("dataType") String dataType, @QueryParam("description") String description, @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, - @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory, - @QueryParam("values") String values, @QueryParam("taxonIds") String taxonIds, @QueryParam("icon") String icon) { + @QueryParam("units") String units, @QueryParam("speciesField") String speciesField, + @QueryParam("source") String source, @QueryParam("showInObservation") Boolean showInObservation, + @QueryParam("isParticipatory") Boolean isParticipatory, @QueryParam("values") String values, + @QueryParam("taxonIds") String taxonIds, @QueryParam("icon") String icon, @QueryParam("min") String min, + @QueryParam("max") String max) { try { - String result = services.createTraits(dataType, description, Long.parseLong("39"), name, traitTypes, null, showInObservation, isParticipatory,values,taxonIds,icon); + String result = services.createTraits(dataType, description, Long.parseLong(speciesField), source, name, + traitTypes, units, showInObservation, isParticipatory, values, taxonIds, icon, min, max); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); } } - + @POST @Path(ApiConstants.TRAIT + ApiConstants.UPDATE) @Produces(MediaType.APPLICATION_JSON) @@ -105,12 +108,14 @@ public Response createTrait(@QueryParam("dataType") String dataType, @ApiResponses(value = { @ApiResponse(code = 400, message = "unable to fetch all the traits", response = String.class) }) - public Response updateTrait( - @QueryParam("description") String description, @QueryParam("id") Long id, + public Response updateTrait(@QueryParam("description") String description, @QueryParam("id") Long id, @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, - @QueryParam("showInObservation") Boolean showInObservation,@QueryParam("isParticipatory") Boolean isParticipatory) { + @QueryParam("showInObservation") Boolean showInObservation, + @QueryParam("isParticipatory") Boolean isParticipatory, @QueryParam("source") String source, + @QueryParam("traitValues") String traitValues) { try { - String result = services.updateTraits(description, id, name, traitTypes, showInObservation, isParticipatory); + String result = services.updateTraits(description, id, name, traitTypes, showInObservation, isParticipatory, + source, traitValues); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); @@ -200,7 +205,7 @@ public Response getTraitList(@PathParam("speciesGroupId") String speciesGroupId) } } - + @GET @Path(ApiConstants.TRAIT + "/{traitId}") @Consumes(MediaType.TEXT_PLAIN) @@ -279,7 +284,7 @@ public Response getTraitsValue(@PathParam("traitId") String traitId) { @Produces(MediaType.APPLICATION_JSON) @ValidateUser - + @ApiOperation(value = "Updates the Traits with Values", notes = "Returns the list of allTraitValue Pair", response = FactValuePair.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 400, message = "Unable to edit the Traits", response = String.class) }) diff --git a/src/main/java/com/strandls/traits/pojo/TraitsValue.java b/src/main/java/com/strandls/traits/pojo/TraitsValue.java index 281e054..2993360 100644 --- a/src/main/java/com/strandls/traits/pojo/TraitsValue.java +++ b/src/main/java/com/strandls/traits/pojo/TraitsValue.java @@ -31,6 +31,7 @@ public class TraitsValue implements Serializable { private String description; private String source; private Boolean isDeleted; + private Long displayOrder; @Id @GeneratedValue @@ -51,7 +52,16 @@ public String getValue() { public void setValue(String value) { this.value = value; } - + + @Column(name = "display_order", columnDefinition = "BIGINT") + public Long getDisplayOrder() { + return displayOrder; + } + + public void setDisplayOrder(Long displayOrder) { + this.displayOrder = displayOrder; + } + @Column(name = "description") public String getDescription() { return description; diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index f0ab7c9..ca0bc78 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -260,9 +260,10 @@ public int compare(Traits o1, Traits o2) { return traitValuePair; } - + @Override - public String updateTraits (String description,Long id,String name,String traitTypes,Boolean showInObservation,Boolean isParticipatory) { + public String updateTraits(String description, Long id, String name, String traitTypes, Boolean showInObservation, + Boolean isParticipatory, String source, String traitValues) { Traits trait = traitsDao.findById(id); trait.setDescription(description); trait.setName(name); @@ -270,14 +271,47 @@ public String updateTraits (String description,Long id,String name,String traitT trait.setShowInObservation(showInObservation); trait.setIsNotObservationTraits(!showInObservation); trait.setIsParticipatory(isParticipatory); + trait.setSource(source); trait.setLastRevised(LocalDateTime.now()); traitsDao.update(trait); + if (traitValues != null && !traitValues.isEmpty()) { + String[] array = traitValues.split("\\|"); + List list = Arrays.asList(array); + Long index = (long) 1; + for (String value : list) { + String[] parts = value.split(":"); + if (parts[0] != null && !parts[0].isEmpty()) { + TraitsValue traitValue = traitsValueDao.findById(Long.parseLong(parts[0])); + traitValue.setValue(parts[2]); + traitValue.setDisplayOrder(index); + traitValue.setDescription(parts[1]); + traitsValueDao.update(traitValue); + } else { + TraitsValue traitValue = new TraitsValue(); + traitValue.setTraitInstanceId(id); + traitValue.setId(null); + traitValue.setDescription(parts[1]); + traitValue.setSource(source); + traitValue.setDisplayOrder(index); + if (parts.length == 4) { + traitValue.setIcon(parts[3]); + } else { + traitValue.setIcon(null); + } + traitValue.setIsDeleted(false); + traitValue.setValue(parts[2]); + traitsValueDao.save(traitValue); + } + index = index + 1; + } + } return trait.getId().toString(); } - + @Override - public String createTraits (String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds, String icon) { + public String createTraits(String dataType, String description, Long fieldId, String source, String name, + String traitTypes, String units, Boolean showInObservation, Boolean isParticipatory, String values, + String taxonIds, String icon, String min, String max) { Traits traits = new Traits(); traits.setId(null); traits.setCreatedOn(LocalDateTime.now()); @@ -292,30 +326,55 @@ public String createTraits (String dataType, String description, Long fieldId, S traits.setShowInObservation(showInObservation); traits.setIsParticipatory(isParticipatory); traits.setIsDeleted(false); - traits.setSource("IBP"); + traits.setSource(source); traits.setIcon(icon); traitsDao.save(traits); - if(values != null && !values.isEmpty()) { - String[] array = values.split("\\|"); - List list = Arrays.asList(array); - for (String value : list) { - String[] parts = value.split(":"); + if (values != null && !values.isEmpty()) { + String[] array = values.split("\\|"); + List list = Arrays.asList(array); + Long index = (long) 1; + for (String value : list) { + String[] parts = value.split(":"); + TraitsValue traitValue = new TraitsValue(); + traitValue.setTraitInstanceId(traits.getId()); + traitValue.setId(null); + traitValue.setDescription(parts[0]); + traitValue.setSource(source); + if (parts.length == 3) { + traitValue.setIcon(parts[2]); + } else { + traitValue.setIcon(null); + } + traitValue.setIsDeleted(false); + traitValue.setValue(parts[1]); + traitValue.setDisplayOrder(index); + traitsValueDao.save(traitValue); + index = index + 1; + } + } + if (min != null && !min.isEmpty()) { TraitsValue traitValue = new TraitsValue(); traitValue.setTraitInstanceId(traits.getId()); traitValue.setId(null); - traitValue.setDescription(parts[0]); - traitValue.setSource("IBP"); - if(parts.length==3) { - traitValue.setIcon(parts[2]); - } else { - traitValue.setIcon(null); - } + traitValue.setDescription("Min Value"); + traitValue.setSource(source); + traitValue.setIcon(null); traitValue.setIsDeleted(false); - traitValue.setValue(parts[1]); + traitValue.setValue(min); traitsValueDao.save(traitValue); } + if (max != null && !max.isEmpty()) { + TraitsValue traitValue = new TraitsValue(); + traitValue.setTraitInstanceId(traits.getId()); + traitValue.setId(null); + traitValue.setDescription("Max Value"); + traitValue.setSource(source); + traitValue.setIcon(null); + traitValue.setIsDeleted(false); + traitValue.setValue(max); + traitsValueDao.save(traitValue); } - if(taxonIds != null && !taxonIds.isEmpty()) { + if (taxonIds != null && !taxonIds.isEmpty()) { String[] array = taxonIds.split("\\|"); List list = Arrays.asList(array); for (String value : list) { @@ -437,12 +496,12 @@ else if (objectType.equalsIgnoreCase(OBJECTTYPE.SPECIES.getValue())) objectId, "species", result.getId(), activityType, mailData); } } - + @Override public Map fetchByTraitId(Long traitId) { Traits traitDetails = traitsDao.findById(traitId); List traitValuesList = traitsValueDao.findTraitsValue(traitId); - Map details = new HashMap(); + Map details = new HashMap(); details.put("traits", traitDetails); details.put("values", traitValuesList); return details; diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index 97cba45..6998cba 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -31,10 +31,12 @@ public interface TraitsServices { public List getObservationTraitList(Long speciesId); - public String createTraits(String dataType, String description, Long fieldId, String name, String traitTypes, - String units, Boolean showInObservation, Boolean isParticipatory, String values, String taxonIds, String icon); + public String createTraits(String dataType, String description, Long fieldId, String source, String name, + String traitTypes, String units, Boolean showInObservation, Boolean isParticipatory, String values, + String taxonIds, String icon, String min, String max); - public String updateTraits (String description,Long id,String name,String traitTypes,Boolean showInObservation,Boolean isParticipatory); + public String updateTraits(String description, Long id, String name, String traitTypes, Boolean showInObservation, + Boolean isParticipatory, String source, String traitValues); public List createFacts(HttpServletRequest request, String objectType, Long objectId, FactsCreateData factsCreateData); From 3510078438df28eb44c0c81dd2ef5261350bb40b Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Fri, 3 Jan 2025 15:04:12 +0530 Subject: [PATCH 5/9] Changed pojo type from LocalDateTime to Date and added new endpoints --- pom.xml | 108 +++++---- .../traits/controller/TraitsController.java | 27 ++- .../java/com/strandls/traits/pojo/Traits.java | 27 +-- .../services/Impl/TraitsServicesImpl.java | 221 ++++++++++++++++-- .../traits/services/TraitsServices.java | 9 +- 5 files changed, 313 insertions(+), 79 deletions(-) diff --git a/pom.xml b/pom.xml index b4f55ed..bd72a69 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,8 @@ - + - + 4.0.0 com.strandls.traits @@ -73,7 +75,8 @@ 1.7.13 - + org.apache.logging.log4j log4j-core @@ -85,7 +88,7 @@ slf4j-log4j12 1.7.13 - + javax.ws.rs @@ -93,14 +96,16 @@ 2.1.1 - + org.glassfish.jersey.core jersey-server 2.31 - + org.glassfish.jersey.containers jersey-container-servlet-core @@ -127,7 +132,8 @@ 4.2.3 - + com.google.inject.extensions guice-servlet @@ -142,40 +148,42 @@ provided - + org.glassfish.jersey.media jersey-media-json-jackson 2.31 - - + + - com.fasterxml.jackson.core - jackson-core - 2.11.0 + com.fasterxml.jackson.core + jackson-core + 2.11.0 - + - com.sun.xml.bind - jaxb-core - 2.3.0.1 + com.sun.xml.bind + jaxb-core + 2.3.0.1 - com.sun.xml.bind - jaxb-impl - 2.3.1 + com.sun.xml.bind + jaxb-impl + 2.3.1 - + - io.swagger - swagger-annotations - 1.6.1 + io.swagger + swagger-annotations + 1.6.1 - - + + io.swagger @@ -189,7 +197,7 @@ hibernate-core 5.4.24.Final - + org.hibernate @@ -212,21 +220,27 @@ - com.strandls.biodiv - authentication-utility - 1.0.0 - - + com.strandls.biodiv + authentication-utility + 1.0.0 + + - com.strandls.biodiv - taxonomy-client - 1.0.0 + com.strandls.biodiv + taxonomy-client + 1.0.0 - + + + com.strandls.biodiv + esmodule-client + 1.0.0 + + - com.strandls.biodiv - activity-client - 1.0.0 + com.strandls.biodiv + activity-client + 1.0.0 @@ -262,7 +276,8 @@ ${swagger-ui.version} - ${project.build.directory}/swagger-ui + + ${project.build.directory}/swagger-ui @@ -274,7 +289,8 @@ - ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version} + + ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version} **/*.* @@ -287,7 +303,8 @@ - + com.google.code.maven-replacer-plugin replacer ${replacer.version} @@ -300,7 +317,8 @@ - ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html + + ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html https://petstore.swagger.io/v2/swagger.json @@ -360,7 +378,9 @@ java org.openapitools.codegen.OpenAPIGenerator - generate -i ${project.basedir}/target/swagger.json -g java -o target/sdk --skip-validate-spec -c swagger-config.json + generate -i ${project.basedir}/target/swagger.json -g + java -o target/sdk --skip-validate-spec -c + swagger-config.json true true diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index d08f495..54ccfaa 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -112,10 +112,10 @@ public Response updateTrait(@QueryParam("description") String description, @Quer @QueryParam("name") String name, @QueryParam("traitTypes") String traitTypes, @QueryParam("showInObservation") Boolean showInObservation, @QueryParam("isParticipatory") Boolean isParticipatory, @QueryParam("source") String source, - @QueryParam("traitValues") String traitValues) { + @ApiParam(name = "traitValues") Map>> traitValues) { try { String result = services.updateTraits(description, id, name, traitTypes, showInObservation, isParticipatory, - source, traitValues); + source, traitValues.get("traitValues")); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); @@ -278,6 +278,29 @@ public Response getTraitsValue(@PathParam("traitId") String traitId) { } } + @PUT + @Path(ApiConstants.UPDATE + "/{objectType}/{objectId}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + + // @ValidateUser + + @ApiOperation(value = "Updates the Traits with Values", notes = "Returns the list of allTraitValue Pair", response = FactValuePair.class, responseContainer = "List") + @ApiResponses(value = { @ApiResponse(code = 400, message = "Unable to edit the Traits", response = String.class) }) + + public Response addNewTraits(@Context HttpServletRequest request, @PathParam("objectType") String objectType, + @PathParam("objectId") String objectId, @ApiParam(name = "factsAddData") Map factsAddData) { + try { + Long objId = Long.parseLong(objectId); + String result = services.addNewTraits(request, objectType, objId, factsAddData); + // List result = services.updateTraits(request, objectType, + // objId, trait, factsUpdateData); + return Response.status(Status.OK).entity(result).build(); + } catch (Exception e) { + return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + @PUT @Path(ApiConstants.UPDATE + "/{objectType}/{objectId}/{traitId}") @Consumes(MediaType.APPLICATION_JSON) diff --git a/src/main/java/com/strandls/traits/pojo/Traits.java b/src/main/java/com/strandls/traits/pojo/Traits.java index 59e3f7b..21259ca 100644 --- a/src/main/java/com/strandls/traits/pojo/Traits.java +++ b/src/main/java/com/strandls/traits/pojo/Traits.java @@ -5,6 +5,7 @@ import java.io.Serializable; import java.time.LocalDateTime; +import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; @@ -25,8 +26,8 @@ public class Traits implements Serializable { */ private static final long serialVersionUID = -7750012729432714454L; private Long id; - private LocalDateTime createdOn; - private LocalDateTime lastRevised; + private Date createdOn; + private Date lastRevised; private String dataType; private String description; private Long fieldId; @@ -50,22 +51,22 @@ public Long getId() { public void setId(Long id) { this.id = id; } - - @Column(name="created_on") - public LocalDateTime getCreatedOn() { + + @Column(name = "created_on") + public Date getCreatedOn() { return createdOn; } - - public void setCreatedOn(LocalDateTime createdOn) { + + public void setCreatedOn(Date createdOn) { this.createdOn = createdOn; } - - @Column(name="last_revised") - public LocalDateTime getLastRevised() { + + @Column(name = "last_revised") + public Date getLastRevised() { return lastRevised; } - - public void setLastRevised(LocalDateTime lastRevised) { + + public void setLastRevised(Date lastRevised) { this.lastRevised = lastRevised; } @@ -149,7 +150,7 @@ public String getSource() { public void setSource(String source) { this.source = source; } - + @Column(name = "icon") public String getIcon() { return icon; diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index ca0bc78..1ffe1da 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -3,6 +3,7 @@ */ package com.strandls.traits.services.Impl; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.ArrayList; @@ -11,6 +12,7 @@ import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -26,10 +28,16 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import com.strandls.activity.pojo.MailData; import com.strandls.authentication_utility.util.AuthUtil; import com.strandls.taxonomy.controllers.SpeciesServicesApi; import com.strandls.taxonomy.controllers.TaxonomyTreeServicesApi; +import com.strandls.esmodule.ApiException; +import com.strandls.esmodule.controllers.EsServicesApi; import com.strandls.taxonomy.pojo.BreadCrumb; import com.strandls.traits.dao.FactsDAO; import com.strandls.traits.dao.TraitTaxonomyDefinitionDao; @@ -76,6 +84,9 @@ public class TraitsServicesImpl implements TraitsServices { @Inject private SpeciesServicesApi speciesService; + @Inject + private EsServicesApi esService; + @Inject private TaxonomyTreeServicesApi taxonomyTreeService; @@ -263,7 +274,7 @@ public int compare(Traits o1, Traits o2) { @Override public String updateTraits(String description, Long id, String name, String traitTypes, Boolean showInObservation, - Boolean isParticipatory, String source, String traitValues) { + Boolean isParticipatory, String source, List> traitValues) { Traits trait = traitsDao.findById(id); trait.setDescription(description); trait.setName(name); @@ -272,35 +283,35 @@ public String updateTraits(String description, Long id, String name, String trai trait.setIsNotObservationTraits(!showInObservation); trait.setIsParticipatory(isParticipatory); trait.setSource(source); - trait.setLastRevised(LocalDateTime.now()); + trait.setLastRevised(new Date()); traitsDao.update(trait); if (traitValues != null && !traitValues.isEmpty()) { - String[] array = traitValues.split("\\|"); - List list = Arrays.asList(array); Long index = (long) 1; - for (String value : list) { - String[] parts = value.split(":"); - if (parts[0] != null && !parts[0].isEmpty()) { - TraitsValue traitValue = traitsValueDao.findById(Long.parseLong(parts[0])); - traitValue.setValue(parts[2]); + for (Map value : traitValues) { + if (value.get("id") != null) { + TraitsValue traitValue = traitsValueDao.findById(Long.valueOf(value.get("id").toString())); + traitValue.setValue(value.get("value").toString()); traitValue.setDisplayOrder(index); - traitValue.setDescription(parts[1]); + traitValue.setSource(source); + traitValue.setDescription(value.get("description").toString()); + if (value.get("icon") != null) { + traitValue.setIcon(value.get("icon").toString()); + } traitsValueDao.update(traitValue); } else { TraitsValue traitValue = new TraitsValue(); traitValue.setTraitInstanceId(id); traitValue.setId(null); - traitValue.setDescription(parts[1]); + traitValue.setDescription(value.get("description").toString()); traitValue.setSource(source); traitValue.setDisplayOrder(index); - if (parts.length == 4) { - traitValue.setIcon(parts[3]); - } else { - traitValue.setIcon(null); + if (value.get("icon") != null) { + traitValue.setIcon(value.get("icon").toString()); } traitValue.setIsDeleted(false); - traitValue.setValue(parts[2]); + traitValue.setValue(value.get("value").toString()); traitsValueDao.save(traitValue); + } index = index + 1; } @@ -314,13 +325,13 @@ public String createTraits(String dataType, String description, Long fieldId, St String taxonIds, String icon, String min, String max) { Traits traits = new Traits(); traits.setId(null); - traits.setCreatedOn(LocalDateTime.now()); + traits.setCreatedOn(new Date()); traits.setDataType(dataType); traits.setDescription(description); traits.setFieldId(fieldId); traits.setName(name); traits.setTraitTypes(traitTypes); - traits.setLastRevised(LocalDateTime.now()); + traits.setLastRevised(new Date()); traits.setUnits(units); traits.setIsNotObservationTraits(!showInObservation); traits.setShowInObservation(showInObservation); @@ -329,7 +340,7 @@ public String createTraits(String dataType, String description, Long fieldId, St traits.setSource(source); traits.setIcon(icon); traitsDao.save(traits); - if (values != null && !values.isEmpty()) { + if (values != null && !values.isEmpty() && dataType == "STRING") { String[] array = values.split("\\|"); List list = Arrays.asList(array); Long index = (long) 1; @@ -517,6 +528,178 @@ public List fetchTraitsValue(Long traitId) { return traitsValueDao.findTraitsValue(traitId); } + @Override + public String addNewTraits(HttpServletRequest request, String objectType, Long objectId, + Map factsAddData) { + try { + Object SpeciesEs = esService.fetch("extended_species", "_doc", objectId.toString()).getDocument(); + ObjectMapper objectMapper = new ObjectMapper(); + if (SpeciesEs instanceof String) { + // Parse the JSON string + String speciesEsJson = (String) SpeciesEs; + JsonNode rootNode = objectMapper.readTree(speciesEsJson); + List> factsEs = objectMapper.convertValue(rootNode.get("facts"), List.class); + if (factsEs == null) { + factsEs = new ArrayList<>(); + } + for (Entry fact : factsAddData.entrySet()) { + Traits trait = traitsDao.findById(Long.parseLong(fact.getKey())); + if (trait.getDataType().equals("STRING")) { + List traitsValueList = fact.getValue(); + if (trait.getTraitTypes().equals(TRAITTYPE.SINGLECATEGORICAL.getValue())) { + if (traitsValueList != null && !traitsValueList.isEmpty()) { + Integer value = traitsValueList.get(0); + traitsValueList.clear(); + traitsValueList.add(value); + } + } + String attribution = trait.getSource(); + +// traits with preDefined list + List valueList = traitsValueDao.findTraitsValue(trait.getId()); + List validValueId = new ArrayList(); + for (TraitsValue tv : valueList) { + validValueId.add(tv.getId()); + } + + String activityType = TRAITMSG.ADDEDFACT.getValue(); + if (traitsValueList != null && !traitsValueList.isEmpty()) { + for (Integer newValue : traitsValueList) { + if (validValueId.contains((long) newValue)) { + Facts new_fact = new Facts(null, attribution, (long) 51, false, 822L, objectId, + null, trait.getId(), (long) newValue, null, objectType, null, null, null); + + String value = traitsValueDao.findById(new_fact.getTraitValueId()).getValue(); + String description = trait.getName() + ":" + value; + + saveUpdateFacts(request, objectType, objectId, new_fact, description, activityType, + null); + + Map EsAddFact = new LinkedHashMap<>(); + EsAddFact.put("nameId", trait.getId()); + EsAddFact.put("valueId", newValue); + EsAddFact.put("fromDate", null); + EsAddFact.put("name", trait.getName()); + EsAddFact.put("type", trait.getTraitTypes()); + EsAddFact.put("color", null); + EsAddFact.put("range", null); + EsAddFact.put("toDate", null); + EsAddFact.put("isParticipatory", trait.getIsParticipatory()); + EsAddFact.put("value", null); + factsEs.add(EsAddFact); + } + } + } + } else if (trait.getDataType().equalsIgnoreCase(DATATYPE.NUMERIC.getValue())) { + List traitsValueList = fact.getValue(); + String[] values = traitsValueList.get(0).split(":"); + if (values.length == 1) { + Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, + objectId, null, trait.getId(), null, values[0].trim(), objectType, null, null, + null); + String description = trait.getName() + ":" + traitsValueList.get(0).toString(); + + saveUpdateFacts(request, objectType, objectId, facts, description, + TRAITMSG.ADDEDFACT.getValue(), null); + Map EsAddFact = new LinkedHashMap<>(); + Map range = new LinkedHashMap<>(); + range.put("nameId", trait.getId()); + range.put("min", values[0].trim()); + range.put("max", null); + EsAddFact.put("nameId", trait.getId()); + EsAddFact.put("valueId", null); + EsAddFact.put("fromDate", null); + EsAddFact.put("name", trait.getName()); + EsAddFact.put("type", trait.getTraitTypes()); + EsAddFact.put("color", null); + EsAddFact.put("range", range); + EsAddFact.put("toDate", null); + EsAddFact.put("isParticipatory", trait.getIsParticipatory()); + EsAddFact.put("value", values[0].trim()); + factsEs.add(EsAddFact); + } else { + Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, + objectId, null, trait.getId(), null, values[0].trim(), objectType, values[1].trim(), + null, null); + String description = trait.getName() + ":" + traitsValueList.get(0).toString(); + + saveUpdateFacts(request, objectType, objectId, facts, description, + TRAITMSG.ADDEDFACT.getValue(), null); + Map EsAddFact = new LinkedHashMap<>(); + Map range = new LinkedHashMap<>(); + range.put("nameId", trait.getId()); + range.put("min", values[0].trim()); + range.put("max", values[1].trim()); + EsAddFact.put("nameId", trait.getId()); + EsAddFact.put("valueId", null); + EsAddFact.put("fromDate", null); + EsAddFact.put("name", trait.getName()); + EsAddFact.put("type", trait.getTraitTypes()); + EsAddFact.put("color", null); + EsAddFact.put("range", range); + EsAddFact.put("toDate", null); + EsAddFact.put("isParticipatory", trait.getIsParticipatory()); + EsAddFact.put("value", values[0].trim()); + factsEs.add(EsAddFact); + } + } else if (trait.getDataType().equalsIgnoreCase(DATATYPE.DATE.getValue())) { + List traitsValueList = fact.getValue(); + String pattern = "yyyy-MM-dd"; + SimpleDateFormat sdf = new SimpleDateFormat(pattern); + Date fromDate = sdf.parse(traitsValueList.get(0)); + Date toDate = null; + if (traitsValueList.size() == 2) { + toDate = sdf.parse(traitsValueList.get(1)); + } + Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, objectId, + null, trait.getId(), null, null, objectType, null, fromDate, toDate); + + String description = trait.getName() + ":" + traitsValueList.toString(); + + saveUpdateFacts(request, objectType, objectId, facts, description, + TRAITMSG.ADDEDFACT.getValue(), null); + + SimpleDateFormat sdfEs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + + Map EsAddFact = new LinkedHashMap<>(); + EsAddFact.put("nameId", trait.getId()); + EsAddFact.put("valueId", null); + EsAddFact.put("fromDate", sdfEs.format(fromDate)); + EsAddFact.put("name", trait.getName()); + EsAddFact.put("type", trait.getTraitTypes()); + EsAddFact.put("color", null); + EsAddFact.put("range", null); + EsAddFact.put("toDate", sdfEs.format(toDate)); + EsAddFact.put("isParticipatory", trait.getIsParticipatory()); + EsAddFact.put("value", null); + factsEs.add(EsAddFact); + + } + } + Map fields = new HashMap<>(); + fields.put("facts", factsEs); + String updateContent = objectMapper.writeValueAsString(fields); + esService.updateEsField("extended_species", objectId.toString(), updateContent); + return factsEs.toString(); + } + + return SpeciesEs.toString(); + } catch (ApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return e.toString(); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return e.toString(); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return e.toString(); + } + + } + @Override public List updateTraits(HttpServletRequest request, String objectType, Long objectId, Long traitId, FactsUpdateData factsUpdateData) { diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index 6998cba..05c104d 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -7,7 +7,9 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; import com.strandls.traits.pojo.FactValuePair; import com.strandls.traits.pojo.Facts; @@ -17,6 +19,8 @@ import com.strandls.traits.pojo.TraitsValue; import com.strandls.traits.pojo.TraitsValuePair; +import io.swagger.annotations.ApiParam; + /** * @author Abhishek Rudra * @@ -36,7 +40,7 @@ public String createTraits(String dataType, String description, Long fieldId, St String taxonIds, String icon, String min, String max); public String updateTraits(String description, Long id, String name, String traitTypes, Boolean showInObservation, - Boolean isParticipatory, String source, String traitValues); + Boolean isParticipatory, String source, List> list); public List createFacts(HttpServletRequest request, String objectType, Long objectId, FactsCreateData factsCreateData); @@ -49,6 +53,9 @@ public List createFacts(HttpServletRequest request, String object public List fetchTraitsValue(Long traitId); + public String addNewTraits(HttpServletRequest request, String objectType, Long objectId, + Map factsAddData); + public List updateTraits(HttpServletRequest request, String objectType, Long objectId, Long traitId, FactsUpdateData factsUpdateData); From 55ad1595445a64bec2626c5d97f6dd259c17139d Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Fri, 3 Jan 2025 16:08:06 +0530 Subject: [PATCH 6/9] Removed formatting issues --- pom.xml | 104 ++++++++++++++++++++++++-------------------------------- 1 file changed, 45 insertions(+), 59 deletions(-) diff --git a/pom.xml b/pom.xml index bd72a69..fda7eb0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,8 +1,6 @@ - + - + 4.0.0 com.strandls.traits @@ -75,8 +73,7 @@ 1.7.13 - + org.apache.logging.log4j log4j-core @@ -88,7 +85,7 @@ slf4j-log4j12 1.7.13 - + javax.ws.rs @@ -96,16 +93,14 @@ 2.1.1 - + org.glassfish.jersey.core jersey-server 2.31 - + org.glassfish.jersey.containers jersey-container-servlet-core @@ -132,8 +127,7 @@ 4.2.3 - + com.google.inject.extensions guice-servlet @@ -148,42 +142,40 @@ provided - + org.glassfish.jersey.media jersey-media-json-jackson 2.31 - - + + - com.fasterxml.jackson.core - jackson-core - 2.11.0 + com.fasterxml.jackson.core + jackson-core + 2.11.0 - + - com.sun.xml.bind - jaxb-core - 2.3.0.1 + com.sun.xml.bind + jaxb-core + 2.3.0.1 - com.sun.xml.bind - jaxb-impl - 2.3.1 + com.sun.xml.bind + jaxb-impl + 2.3.1 - + - io.swagger - swagger-annotations - 1.6.1 + io.swagger + swagger-annotations + 1.6.1 - - + + io.swagger @@ -197,7 +189,7 @@ hibernate-core 5.4.24.Final - + org.hibernate @@ -220,27 +212,27 @@ - com.strandls.biodiv - authentication-utility - 1.0.0 - - + com.strandls.biodiv + authentication-utility + 1.0.0 + + - com.strandls.biodiv - taxonomy-client - 1.0.0 + com.strandls.biodiv + taxonomy-client + 1.0.0 - + com.strandls.biodiv esmodule-client 1.0.0 - + - com.strandls.biodiv - activity-client - 1.0.0 + com.strandls.biodiv + activity-client + 1.0.0 @@ -276,8 +268,7 @@ ${swagger-ui.version} - - ${project.build.directory}/swagger-ui + ${project.build.directory}/swagger-ui @@ -289,8 +280,7 @@ - - ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version} + ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version} **/*.* @@ -303,8 +293,7 @@ - + com.google.code.maven-replacer-plugin replacer ${replacer.version} @@ -317,8 +306,7 @@ - - ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html + ${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html https://petstore.swagger.io/v2/swagger.json @@ -378,9 +366,7 @@ java org.openapitools.codegen.OpenAPIGenerator - generate -i ${project.basedir}/target/swagger.json -g - java -o target/sdk --skip-validate-spec -c - swagger-config.json + generate -i ${project.basedir}/target/swagger.json -g java -o target/sdk --skip-validate-spec -c swagger-config.json true true From b763e52cc5f04c12494657fc829a811ccf1519f1 Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Mon, 6 Jan 2025 03:14:44 +0530 Subject: [PATCH 7/9] Other details added to facts --- .../traits/controller/TraitsController.java | 11 +-- .../services/Impl/TraitsServicesImpl.java | 82 +++++++++++++++---- .../traits/services/TraitsServices.java | 2 +- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/strandls/traits/controller/TraitsController.java b/src/main/java/com/strandls/traits/controller/TraitsController.java index 54ccfaa..d16fb51 100644 --- a/src/main/java/com/strandls/traits/controller/TraitsController.java +++ b/src/main/java/com/strandls/traits/controller/TraitsController.java @@ -283,18 +283,15 @@ public Response getTraitsValue(@PathParam("traitId") String traitId) { @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - // @ValidateUser - - @ApiOperation(value = "Updates the Traits with Values", notes = "Returns the list of allTraitValue Pair", response = FactValuePair.class, responseContainer = "List") + @ApiOperation(value = "Adds new Traits", notes = "Returns the list of allTraitValue Pair", response = FactValuePair.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 400, message = "Unable to edit the Traits", response = String.class) }) public Response addNewTraits(@Context HttpServletRequest request, @PathParam("objectType") String objectType, - @PathParam("objectId") String objectId, @ApiParam(name = "factsAddData") Map factsAddData) { + @PathParam("objectId") String objectId, @ApiParam(name = "factsAddData") Map factsAddData, + @QueryParam("userId") String userId, @QueryParam("taxonId") String taxonId) { try { Long objId = Long.parseLong(objectId); - String result = services.addNewTraits(request, objectType, objId, factsAddData); - // List result = services.updateTraits(request, objectType, - // objId, trait, factsUpdateData); + String result = services.addNewTraits(request, objectType, objId, factsAddData, userId, taxonId); return Response.status(Status.OK).entity(result).build(); } catch (Exception e) { return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build(); diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index 1ffe1da..c41eb84 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -3,6 +3,7 @@ */ package com.strandls.traits.services.Impl; +import java.awt.Color; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; @@ -530,7 +531,7 @@ public List fetchTraitsValue(Long traitId) { @Override public String addNewTraits(HttpServletRequest request, String objectType, Long objectId, - Map factsAddData) { + Map factsAddData, String userId, String taxonId) { try { Object SpeciesEs = esService.fetch("extended_species", "_doc", objectId.toString()).getDocument(); ObjectMapper objectMapper = new ObjectMapper(); @@ -545,10 +546,10 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o for (Entry fact : factsAddData.entrySet()) { Traits trait = traitsDao.findById(Long.parseLong(fact.getKey())); if (trait.getDataType().equals("STRING")) { - List traitsValueList = fact.getValue(); + List traitsValueList = fact.getValue(); if (trait.getTraitTypes().equals(TRAITTYPE.SINGLECATEGORICAL.getValue())) { if (traitsValueList != null && !traitsValueList.isEmpty()) { - Integer value = traitsValueList.get(0); + Object value = traitsValueList.get(0); traitsValueList.clear(); traitsValueList.add(value); } @@ -564,10 +565,11 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o String activityType = TRAITMSG.ADDEDFACT.getValue(); if (traitsValueList != null && !traitsValueList.isEmpty()) { - for (Integer newValue : traitsValueList) { - if (validValueId.contains((long) newValue)) { - Facts new_fact = new Facts(null, attribution, (long) 51, false, 822L, objectId, - null, trait.getId(), (long) newValue, null, objectType, null, null, null); + for (Object newValue : traitsValueList) { + if (validValueId.contains(Long.valueOf(newValue.toString()))) { + Facts new_fact = new Facts(null, attribution, Long.parseLong(userId), false, 822L, + objectId, Long.parseLong(taxonId), trait.getId(), + Long.valueOf(newValue.toString()), null, objectType, null, null, null); String value = traitsValueDao.findById(new_fact.getTraitValueId()).getValue(); String description = trait.getName() + ":" + value; @@ -590,13 +592,14 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o } } } - } else if (trait.getDataType().equalsIgnoreCase(DATATYPE.NUMERIC.getValue())) { + } else if (trait.getDataType().equalsIgnoreCase(DATATYPE.NUMERIC.getValue()) + && fact.getValue() != null) { List traitsValueList = fact.getValue(); String[] values = traitsValueList.get(0).split(":"); if (values.length == 1) { - Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, - objectId, null, trait.getId(), null, values[0].trim(), objectType, null, null, - null); + Facts facts = new Facts(null, trait.getSource(), Long.parseLong(userId), false, + defaultLicenseId, objectId, Long.parseLong(taxonId), trait.getId(), null, + values[0].trim(), objectType, null, null, null); String description = trait.getName() + ":" + traitsValueList.get(0).toString(); saveUpdateFacts(request, objectType, objectId, facts, description, @@ -618,9 +621,9 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o EsAddFact.put("value", values[0].trim()); factsEs.add(EsAddFact); } else { - Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, - objectId, null, trait.getId(), null, values[0].trim(), objectType, values[1].trim(), - null, null); + Facts facts = new Facts(null, trait.getSource(), Long.parseLong(userId), false, + defaultLicenseId, objectId, Long.parseLong(taxonId), trait.getId(), null, + values[0].trim(), objectType, values[1].trim(), null, null); String description = trait.getName() + ":" + traitsValueList.get(0).toString(); saveUpdateFacts(request, objectType, objectId, facts, description, @@ -651,8 +654,9 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o if (traitsValueList.size() == 2) { toDate = sdf.parse(traitsValueList.get(1)); } - Facts facts = new Facts(null, trait.getSource(), (long) 51, false, defaultLicenseId, objectId, - null, trait.getId(), null, null, objectType, null, fromDate, toDate); + Facts facts = new Facts(null, trait.getSource(), Long.parseLong(userId), false, + defaultLicenseId, objectId, Long.parseLong(taxonId), trait.getId(), null, null, + objectType, null, fromDate, toDate); String description = trait.getName() + ":" + traitsValueList.toString(); @@ -669,11 +673,55 @@ public String addNewTraits(HttpServletRequest request, String objectType, Long o EsAddFact.put("type", trait.getTraitTypes()); EsAddFact.put("color", null); EsAddFact.put("range", null); - EsAddFact.put("toDate", sdfEs.format(toDate)); + if (traitsValueList.size() == 2) { + EsAddFact.put("toDate", sdfEs.format(toDate)); + } else { + EsAddFact.put("toDate", null); + } EsAddFact.put("isParticipatory", trait.getIsParticipatory()); EsAddFact.put("value", null); factsEs.add(EsAddFact); + } else { + List traitsValueList = fact.getValue(); + String activityType = TRAITMSG.ADDEDFACT.getValue(); + if (traitsValueList != null && !traitsValueList.isEmpty()) { + for (Object newValue : traitsValueList) { + if (newValue instanceof Map) { + String color = ((Map) newValue).get("value").toString(); + Facts new_fact = new Facts(null, trait.getSource(), Long.parseLong(userId), false, + defaultLicenseId, objectId, Long.parseLong(taxonId), trait.getId(), null, + color, objectType, null, null, null); + String description = trait.getName() + ":" + color; + + saveUpdateFacts(request, objectType, objectId, new_fact, description, activityType, + null); + + String[] parts = color.replace("rgb(", "").replace(")", "").split(","); + + float[] hsv = Color.RGBtoHSB(Integer.parseInt(parts[0].trim()), + Integer.parseInt(parts[1].trim()), Integer.parseInt(parts[2].trim()), null); + + Map EsAddFact = new LinkedHashMap<>(); + Map range = new LinkedHashMap<>(); + range.put("s", hsv[1] * 100); + range.put("h", hsv[0] * 360); + range.put("v", hsv[2] * 100); + range.put("nameId", trait.getId()); + EsAddFact.put("nameId", trait.getId()); + EsAddFact.put("valueId", null); + EsAddFact.put("fromDate", null); + EsAddFact.put("name", trait.getName()); + EsAddFact.put("type", trait.getTraitTypes()); + EsAddFact.put("color", range); + EsAddFact.put("range", null); + EsAddFact.put("toDate", null); + EsAddFact.put("isParticipatory", trait.getIsParticipatory()); + EsAddFact.put("value", color); + factsEs.add(EsAddFact); + } + } + } } } Map fields = new HashMap<>(); diff --git a/src/main/java/com/strandls/traits/services/TraitsServices.java b/src/main/java/com/strandls/traits/services/TraitsServices.java index 05c104d..0012052 100644 --- a/src/main/java/com/strandls/traits/services/TraitsServices.java +++ b/src/main/java/com/strandls/traits/services/TraitsServices.java @@ -54,7 +54,7 @@ public List createFacts(HttpServletRequest request, String object public List fetchTraitsValue(Long traitId); public String addNewTraits(HttpServletRequest request, String objectType, Long objectId, - Map factsAddData); + Map factsAddData, String userId, String taxonId); public List updateTraits(HttpServletRequest request, String objectType, Long objectId, Long traitId, FactsUpdateData factsUpdateData); From 1b0097b5ccb062cf540ebe796d6326b2fbd72f6c Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Mon, 6 Jan 2025 13:18:11 +0530 Subject: [PATCH 8/9] Resolved string issue --- .../com/strandls/traits/services/Impl/TraitsServicesImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index c41eb84..2b8756a 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -341,7 +341,7 @@ public String createTraits(String dataType, String description, Long fieldId, St traits.setSource(source); traits.setIcon(icon); traitsDao.save(traits); - if (values != null && !values.isEmpty() && dataType == "STRING") { + if (values != null && !values.isEmpty()) { String[] array = values.split("\\|"); List list = Arrays.asList(array); Long index = (long) 1; From 2b3f91582c8582893b1a5407d407be6a70705d0a Mon Sep 17 00:00:00 2001 From: "mekala.ravi" Date: Mon, 6 Jan 2025 21:24:44 +0530 Subject: [PATCH 9/9] Added feedback changes --- .../strandls/traits/services/Impl/TraitsServicesImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java index 2b8756a..52f7e48 100644 --- a/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java +++ b/src/main/java/com/strandls/traits/services/Impl/TraitsServicesImpl.java @@ -341,7 +341,7 @@ public String createTraits(String dataType, String description, Long fieldId, St traits.setSource(source); traits.setIcon(icon); traitsDao.save(traits); - if (values != null && !values.isEmpty()) { + if (values != null && !values.isEmpty() && dataType.equals("STRING")) { String[] array = values.split("\\|"); List list = Arrays.asList(array); Long index = (long) 1; @@ -364,7 +364,7 @@ public String createTraits(String dataType, String description, Long fieldId, St index = index + 1; } } - if (min != null && !min.isEmpty()) { + if (min != null && !min.isEmpty() && dataType.equals("NUMERIC")) { TraitsValue traitValue = new TraitsValue(); traitValue.setTraitInstanceId(traits.getId()); traitValue.setId(null); @@ -375,7 +375,7 @@ public String createTraits(String dataType, String description, Long fieldId, St traitValue.setValue(min); traitsValueDao.save(traitValue); } - if (max != null && !max.isEmpty()) { + if (max != null && !max.isEmpty() && dataType.equals("NUMERIC")) { TraitsValue traitValue = new TraitsValue(); traitValue.setTraitInstanceId(traits.getId()); traitValue.setId(null);