From 45910fcd4ced31044c534799a423d952b103cdae Mon Sep 17 00:00:00 2001 From: Paul Harrison Date: Mon, 7 Oct 2024 16:29:04 +0100 Subject: [PATCH] add some forgotten files --- .../ivoa/vodml/jpa/AttributeConverters.java | 128 ++++++++++++++++++ .../dm/notstccoords/CoordsModelDbTest.java | 125 +++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 runtime/java/src/main/java/org/ivoa/vodml/jpa/AttributeConverters.java create mode 100644 tools/gradletooling/sample/src/test/java/org/ivoa/dm/notstccoords/CoordsModelDbTest.java diff --git a/runtime/java/src/main/java/org/ivoa/vodml/jpa/AttributeConverters.java b/runtime/java/src/main/java/org/ivoa/vodml/jpa/AttributeConverters.java new file mode 100644 index 00000000..5d45ba05 --- /dev/null +++ b/runtime/java/src/main/java/org/ivoa/vodml/jpa/AttributeConverters.java @@ -0,0 +1,128 @@ +/* + * Created on 7 Oct 2024 + * Copyright 2024 Paul Harrison (paul.harrison@manchester.ac.uk) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License in file LICENSE + */ + +package org.ivoa.vodml.jpa; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import jakarta.persistence.AttributeConverter; + +/** + * Attribute converters to convert lists to comma separated strings. + * @author Paul Harrison (paul.harrison@manchester.ac.uk) + */ +public class AttributeConverters { + + private static final String SPLIT_CHAR = ";"; + public static class StringListConverter implements AttributeConverter, String> { + + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object) + */ + @Override + public String convertToDatabaseColumn(List attribute) { + return attribute != null ? String.join(SPLIT_CHAR, attribute) : ""; + } + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) + */ + @Override + public List convertToEntityAttribute(String dbData) { + return dbData != null ? Arrays.asList(dbData.split(SPLIT_CHAR)) : new ArrayList() ; + } + + } + + public static abstract class NumberListConverter implements AttributeConverter, String> { + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object) + */ + @Override + public String convertToDatabaseColumn(List attribute) { + + if(attribute!= null) + { + return attribute.stream().map(T::toString).collect(Collectors.joining (SPLIT_CHAR)); + } + else + { + return ""; + } + } + + + } + public static class IntListConverter extends NumberListConverter + { + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) + */ + @Override + public List convertToEntityAttribute(String dbData) { + if (dbData != null) + { + return Stream.of(dbData.split(SPLIT_CHAR)).map(Integer::parseInt).toList(); + } + else return new ArrayList<>(); + + } + + } + public static class DoubleListConverter extends NumberListConverter + { + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) + */ + @Override + public List convertToEntityAttribute(String dbData) { + if (dbData != null) + { + return Stream.of(dbData.split(SPLIT_CHAR)).map(Double::parseDouble).toList(); + } + else return new ArrayList<>(); + + } + + } + public static class BooleanListConverter extends NumberListConverter + { + + /** + * {@inheritDoc} + * overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) + */ + @Override + public List convertToEntityAttribute(String dbData) { + if (dbData != null) + { + return Stream.of(dbData.split(SPLIT_CHAR)).map(Boolean::parseBoolean).toList(); + } + else return new ArrayList<>(); + + + } + + } +} + + diff --git a/tools/gradletooling/sample/src/test/java/org/ivoa/dm/notstccoords/CoordsModelDbTest.java b/tools/gradletooling/sample/src/test/java/org/ivoa/dm/notstccoords/CoordsModelDbTest.java new file mode 100644 index 00000000..e3a0ade4 --- /dev/null +++ b/tools/gradletooling/sample/src/test/java/org/ivoa/dm/notstccoords/CoordsModelDbTest.java @@ -0,0 +1,125 @@ +/* + * Created on 5 Nov 2021 + * Copyright 2021 Paul Harrison (paul.harrison@manchester.ac.uk) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License in file LICENSE + */ + +package org.ivoa.dm.notstccoords; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; + +import org.ivoa.dm.ivoa.RealQuantity; +import org.ivoa.dm.ivoa.Unit; +import org.ivoa.vodml.testing.AutoDBRoundTripTest; + +/** + * An example test for the "not coords" model. + * note that this test runs JSON and XML serialisation test as well as validating the model instance. + * @author Paul Harrison (paul.harrison@manchester.ac.uk) + * @since 5 Nov 2021 + */ +class CoordsModelDbTest extends AutoDBRoundTripTest { + + /** logger for this class */ + private static final org.slf4j.Logger logger = + org.slf4j.LoggerFactory.getLogger(CoordsModelDbTest.class); +private AnObject a; + + @Override + public CoordsModel createModel() { + // see + // https://github.com/mcdittmar/ivoa-dm-examples/blob/master/assets/examples/coords/current/instances/astrocoordsys.jovial for jovial version of this test. + Unit deg = new Unit("deg"); + SpaceSys ICRS_SYS = + new SpaceSys() + .withFrame( + SpaceFrame.createSpaceFrame( + f -> { + f.refPosition = new StdRefLocation("TOPOCENTER"); + f.spaceRefFrame = "ICRS"; + f.planetaryEphem = "DE432"; + })); + + TimeSys TIMESYS_TT = + new TimeSys() + .withFrame( + TimeFrame.createTimeFrame( + f -> { + f.refPosition = new StdRefLocation("TOPOCENTER"); + f.timescale = "TT"; + f.refDirection = + new CustomRefLocation() + .withEpoch("J2014.25") + .withPosition( + LonLatPoint.createLonLatPoint( + p -> { + p.lon = new RealQuantity(6.752477, deg); + p.lat = new RealQuantity(-16.716116, deg); + p.dist = new RealQuantity(8.6, new Unit("ly")); + p.coordSys = ICRS_SYS; + })); + })); + GenericSys SPECSYS = + new GenericSys() + .withFrame( + GenericFrame.createGenericFrame( + f -> { + f.refPosition = new StdRefLocation("TOPOCENTER"); + f.planetaryEphem = "DE432"; + })); + + + + // note that this cannot be added directly as it is a dtype... + LonLatPoint llp = new LonLatPoint(new RealQuantity(45.0, deg), new RealQuantity(15.0, deg), new RealQuantity(1.5, new Unit("Mpc")), ICRS_SYS); + a = new AnObject(llp); + CoordsModel modelInstance = new CoordsModel(); + + modelInstance.addReference(TIMESYS_TT); + modelInstance.addReference(SPECSYS); + modelInstance.addReference(ICRS_SYS); + modelInstance.addContent(a); + + + + + return modelInstance; + } + + @Override + public void testModel(CoordsModel coordsModel) { + List ts = coordsModel.getContent(AnObject.class); + assertNotNull(ts); + assertEquals(1, ts.size()); + AnObject ts1 = ts.get(0); + SpaceSys ss = ts1.getPosition().getCoordSys(); + assertNotNull(ss); + + } + +/** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoDBRoundTripTest#entityForDb() + */ +@Override +public AnObject entityForDb() { + return a; + +} + +/** + * {@inheritDoc} + * overrides @see org.ivoa.vodml.testing.AutoDBRoundTripTest#testEntity(org.ivoa.vodml.jpa.JPAManipulationsForObjectType) + */ +@Override +public void testEntity(AnObject e) { + SpaceSys ss = e.getPosition().getCoordSys(); + assertNotNull(ss); + +} +}