Skip to content
Snippets Groups Projects
Commit 72dc4fd1 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-grpc-datetime' into 'f-grpc-f-acm'

F grpc datetime

See merge request !52
parents 4610f4f3 227044b7
No related branches found
No related tags found
3 merge requests!58REL: prepare release 0.7.2,!52F grpc datetime,!45F grpc f acm
Pipeline #18548 passed
...@@ -19,6 +19,9 @@ This is an important security update. ...@@ -19,6 +19,9 @@ This is an important security update.
### Fixed ### Fixed
* Wrong serialization of date time values in the GRPC-API (resulting in
org.caosdb.server.datatime@12347abcd or similar).
### Security ### Security
......
...@@ -30,6 +30,11 @@ import org.antlr.v4.runtime.CommonTokenStream; ...@@ -30,6 +30,11 @@ import org.antlr.v4.runtime.CommonTokenStream;
import org.caosdb.server.query.CQLParsingErrorListener; import org.caosdb.server.query.CQLParsingErrorListener;
import org.caosdb.server.query.CQLParsingErrorListener.ParsingError; import org.caosdb.server.query.CQLParsingErrorListener.ParsingError;
/**
* Factory which parses string into CaosDB's DATETIME values.
*
* @author tf
*/
public class DateTimeFactory2 implements DateTimeFactoryInterface { public class DateTimeFactory2 implements DateTimeFactoryInterface {
private Long millis = null; private Long millis = null;
...@@ -51,6 +56,14 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface { ...@@ -51,6 +56,14 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface {
private Integer nanosecond = null; private Integer nanosecond = null;
private TimeZone timeZone = TimeZone.getDefault(); private TimeZone timeZone = TimeZone.getDefault();
public DateTimeFactory2(TimeZone timeZone) {
this.timeZone = timeZone;
}
public DateTimeFactory2() {
this(TimeZone.getDefault());
}
@Override @Override
public void setDate(final String string) { public void setDate(final String string) {
this.date = Integer.valueOf(string); this.date = Integer.valueOf(string);
...@@ -220,6 +233,16 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface { ...@@ -220,6 +233,16 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface {
} }
public static DateTimeInterface valueOf(final String str) { public static DateTimeInterface valueOf(final String str) {
return valueOf(str, TimeZone.getDefault());
}
public static DateTimeInterface valueOf(final String str, TimeZone timeZone) {
final DateTimeFactory2 dtf = new DateTimeFactory2(timeZone);
return dtf.parse(str);
}
public DateTimeInterface parse(String str) {
final DateTimeLexer lexer = new DateTimeLexer(CharStreams.fromString(str)); final DateTimeLexer lexer = new DateTimeLexer(CharStreams.fromString(str));
final CommonTokenStream tokens = new CommonTokenStream(lexer); final CommonTokenStream tokens = new CommonTokenStream(lexer);
...@@ -230,9 +253,7 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface { ...@@ -230,9 +253,7 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface {
final CQLParsingErrorListener el = new CQLParsingErrorListener(DateTimeLexer.UNKNOWN_CHAR); final CQLParsingErrorListener el = new CQLParsingErrorListener(DateTimeLexer.UNKNOWN_CHAR);
parser.addErrorListener(el); parser.addErrorListener(el);
final DateTimeFactory2 dtf = new DateTimeFactory2(); parser.datetime(this);
parser.datetime(dtf);
if (el.hasErrors()) { if (el.hasErrors()) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
...@@ -243,7 +264,7 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface { ...@@ -243,7 +264,7 @@ public class DateTimeFactory2 implements DateTimeFactoryInterface {
"Parsing DateTime finished with errors. \n" + sb.toString()); "Parsing DateTime finished with errors. \n" + sb.toString());
} }
return dtf.getDateTime(); return getDateTime();
} }
@Override @Override
......
...@@ -24,6 +24,7 @@ package org.caosdb.server.grpc; ...@@ -24,6 +24,7 @@ package org.caosdb.server.grpc;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import org.caosdb.api.entity.v1.AtomicDataType; import org.caosdb.api.entity.v1.AtomicDataType;
import org.caosdb.api.entity.v1.CollectionValues; import org.caosdb.api.entity.v1.CollectionValues;
import org.caosdb.api.entity.v1.DataType; import org.caosdb.api.entity.v1.DataType;
...@@ -72,6 +73,12 @@ import org.caosdb.server.permissions.EntityPermission; ...@@ -72,6 +73,12 @@ import org.caosdb.server.permissions.EntityPermission;
public class CaosDBToGrpcConverters { public class CaosDBToGrpcConverters {
private TimeZone timeZone;
public CaosDBToGrpcConverters(TimeZone timeZone) {
this.timeZone = timeZone;
}
/** Get the unit as string. */ /** Get the unit as string. */
public String getStringUnit(final EntityInterface entity) { public String getStringUnit(final EntityInterface entity) {
final Iterator<Property> iterator = entity.getProperties().iterator(); final Iterator<Property> iterator = entity.getProperties().iterator();
...@@ -236,7 +243,7 @@ public class CaosDBToGrpcConverters { ...@@ -236,7 +243,7 @@ public class CaosDBToGrpcConverters {
return builder; return builder;
} }
private ScalarValue.Builder convertScalarValue(final Value value) { protected ScalarValue.Builder convertScalarValue(final Value value) {
if (value instanceof BooleanValue) { if (value instanceof BooleanValue) {
return convertBooleanValue((BooleanValue) value); return convertBooleanValue((BooleanValue) value);
...@@ -260,20 +267,20 @@ public class CaosDBToGrpcConverters { ...@@ -260,20 +267,20 @@ public class CaosDBToGrpcConverters {
} else if (wrappedValue instanceof Integer) { } else if (wrappedValue instanceof Integer) {
return ScalarValue.newBuilder().setIntegerValue((Integer) wrappedValue); return ScalarValue.newBuilder().setIntegerValue((Integer) wrappedValue);
} else { } else {
return convertStringValue(value); return convertStringValue(value.toString());
} }
} }
private org.caosdb.api.entity.v1.ScalarValue.Builder convertStringValue(final Value value) { private org.caosdb.api.entity.v1.ScalarValue.Builder convertStringValue(final String value) {
if (value.toString().isEmpty()) { if (value.isEmpty()) {
return ScalarValue.newBuilder().setSpecialValue(SpecialValue.SPECIAL_VALUE_EMPTY_STRING); return ScalarValue.newBuilder().setSpecialValue(SpecialValue.SPECIAL_VALUE_EMPTY_STRING);
} }
return ScalarValue.newBuilder().setStringValue(value.toString()); return ScalarValue.newBuilder().setStringValue(value);
} }
private org.caosdb.api.entity.v1.ScalarValue.Builder convertDateTimeInterface( private org.caosdb.api.entity.v1.ScalarValue.Builder convertDateTimeInterface(
final DateTimeInterface value) { final DateTimeInterface value) {
return convertStringValue(value); return convertStringValue(value.toDateTimeString(timeZone));
} }
private org.caosdb.api.entity.v1.ScalarValue.Builder convertBooleanValue( private org.caosdb.api.entity.v1.ScalarValue.Builder convertBooleanValue(
...@@ -282,7 +289,7 @@ public class CaosDBToGrpcConverters { ...@@ -282,7 +289,7 @@ public class CaosDBToGrpcConverters {
} }
private ScalarValue.Builder convertReferenceValue(final ReferenceValue value) { private ScalarValue.Builder convertReferenceValue(final ReferenceValue value) {
return convertStringValue(value); return convertStringValue(value.toString());
} }
private org.caosdb.api.entity.v1.Value.Builder convertCollectionValue( private org.caosdb.api.entity.v1.Value.Builder convertCollectionValue(
......
...@@ -23,6 +23,7 @@ package org.caosdb.server.grpc; ...@@ -23,6 +23,7 @@ package org.caosdb.server.grpc;
import io.grpc.stub.StreamObserver; import io.grpc.stub.StreamObserver;
import java.util.HashMap; import java.util.HashMap;
import java.util.TimeZone;
import java.util.UUID; import java.util.UUID;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.caosdb.api.entity.v1.DeleteRequest; import org.caosdb.api.entity.v1.DeleteRequest;
...@@ -63,7 +64,9 @@ import org.caosdb.server.utils.ServerMessages; ...@@ -63,7 +64,9 @@ import org.caosdb.server.utils.ServerMessages;
public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBase { public class EntityTransactionServiceImpl extends EntityTransactionServiceImplBase {
private final CaosDBToGrpcConverters caosdbToGrpc = new CaosDBToGrpcConverters(); // TODO(tf) let the clients define the time zone of the date time values which are being returned.
private final CaosDBToGrpcConverters caosdbToGrpc =
new CaosDBToGrpcConverters(TimeZone.getDefault());
private final GrpcToCaosDBConverters grpcToCaosdb = new GrpcToCaosDBConverters(); private final GrpcToCaosDBConverters grpcToCaosdb = new GrpcToCaosDBConverters();
private final FileTransmissionServiceImpl fileTransmissionService; private final FileTransmissionServiceImpl fileTransmissionService;
......
package org.caosdb.server.grpc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.TimeZone;
import org.caosdb.datetime.DateTimeFactory2;
import org.caosdb.server.datatype.Value;
import org.junit.Test;
public class CaosDBToGrpcConvertersTest {
@Test
public void testConvertScalarValue_Datetime() {
TimeZone timeZone = TimeZone.getTimeZone("UTC");
DateTimeFactory2 factory = new DateTimeFactory2(timeZone);
CaosDBToGrpcConverters converters = new CaosDBToGrpcConverters(timeZone);
Value value = null;
assertNull(converters.convertScalarValue(value));
value = factory.parse("2022");
assertEquals(converters.convertScalarValue(value).toString(), "string_value: \"2022\"\n");
value = factory.parse("2022-12");
assertEquals(converters.convertScalarValue(value).toString(), "string_value: \"2022-12\"\n");
value = factory.parse("2022-12-24");
assertEquals(converters.convertScalarValue(value).toString(), "string_value: \"2022-12-24\"\n");
value = factory.parse("2022-12-24T18:15:00");
assertEquals(
converters.convertScalarValue(value).toString(),
"string_value: \"2022-12-24T18:15:00+0000\"\n");
value = factory.parse("2022-12-24T18:15:00.999999");
assertEquals(
converters.convertScalarValue(value).toString(),
"string_value: \"2022-12-24T18:15:00.999999+0000\"\n");
value = factory.parse("2022-12-24T18:15:00.999999UTC");
assertEquals(
converters.convertScalarValue(value).toString(),
"string_value: \"2022-12-24T18:15:00.999999+0000\"\n");
value = factory.parse("2022-12-24T18:15:00.999999+0200");
assertEquals(
converters.convertScalarValue(value).toString(),
"string_value: \"2022-12-24T16:15:00.999999+0000\"\n");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment