-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
runtime/java/src/main/java/org/ivoa/vodml/jpa/AttributeConverters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Created on 7 Oct 2024 | ||
* Copyright 2024 Paul Harrison ([email protected]) | ||
* | ||
* 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 ([email protected]) | ||
*/ | ||
public class AttributeConverters { | ||
|
||
private static final String SPLIT_CHAR = ";"; | ||
public static class StringListConverter implements AttributeConverter<List<String>, String> { | ||
|
||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object) | ||
*/ | ||
@Override | ||
public String convertToDatabaseColumn(List<String> attribute) { | ||
return attribute != null ? String.join(SPLIT_CHAR, attribute) : ""; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) | ||
*/ | ||
@Override | ||
public List<String> convertToEntityAttribute(String dbData) { | ||
return dbData != null ? Arrays.asList(dbData.split(SPLIT_CHAR)) : new ArrayList<String>() ; | ||
} | ||
|
||
} | ||
|
||
public static abstract class NumberListConverter <T> implements AttributeConverter<List<T>, String> { | ||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object) | ||
*/ | ||
@Override | ||
public String convertToDatabaseColumn(List<T> attribute) { | ||
|
||
if(attribute!= null) | ||
{ | ||
return attribute.stream().map(T::toString).collect(Collectors.joining (SPLIT_CHAR)); | ||
} | ||
else | ||
{ | ||
return ""; | ||
} | ||
} | ||
|
||
|
||
} | ||
public static class IntListConverter extends NumberListConverter<Integer> | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) | ||
*/ | ||
@Override | ||
public List<Integer> 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<Double> | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) | ||
*/ | ||
@Override | ||
public List<Double> 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<Boolean> | ||
{ | ||
|
||
/** | ||
* {@inheritDoc} | ||
* overrides @see jakarta.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) | ||
*/ | ||
@Override | ||
public List<Boolean> convertToEntityAttribute(String dbData) { | ||
if (dbData != null) | ||
{ | ||
return Stream.of(dbData.split(SPLIT_CHAR)).map(Boolean::parseBoolean).toList(); | ||
} | ||
else return new ArrayList<>(); | ||
|
||
|
||
} | ||
|
||
} | ||
} | ||
|
||
|
125 changes: 125 additions & 0 deletions
125
tools/gradletooling/sample/src/test/java/org/ivoa/dm/notstccoords/CoordsModelDbTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Created on 5 Nov 2021 | ||
* Copyright 2021 Paul Harrison ([email protected]) | ||
* | ||
* 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 ([email protected]) | ||
* @since 5 Nov 2021 | ||
*/ | ||
class CoordsModelDbTest extends AutoDBRoundTripTest<CoordsModel,Long,AnObject> { | ||
|
||
/** 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<AnObject> 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); | ||
|
||
} | ||
} |