Skip to content

Commit 7b19a1a

Browse files
authored
remplace LocalDateTime with Instant (#141)
replace LocalDateTime with Instant Signed-off-by: TOURI ANIS <[email protected]>
1 parent f2317f5 commit 7b19a1a

File tree

13 files changed

+100
-91
lines changed

13 files changed

+100
-91
lines changed

src/main/java/org/gridsuite/directory/server/DirectoryService.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import org.springframework.stereotype.Service;
2525
import org.springframework.transaction.annotation.Transactional;
2626

27-
import java.time.LocalDateTime;
28-
import java.time.ZoneOffset;
29-
import java.time.ZonedDateTime;
27+
import java.time.Instant;
3028
import java.time.temporal.ChronoUnit;
3129
import java.util.*;
3230
import java.util.function.Consumer;
@@ -178,7 +176,7 @@ private void assertDirectoryExist(UUID dirUuid) {
178176
/* methods */
179177
private DirectoryElementEntity insertElement(ElementAttributes elementAttributes, UUID parentDirectoryUuid) {
180178
//We need to limit the precision to avoid database precision storage limit issue (postgres has a precision of 6 digits while h2 can go to 9)
181-
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.MICROS);
179+
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
182180
return repositoryService.saveElement(
183181
new DirectoryElementEntity(elementAttributes.getElementUuid() == null ? UUID.randomUUID() : elementAttributes.getElementUuid(),
184182
parentDirectoryUuid,
@@ -217,7 +215,7 @@ public ElementAttributes createRootDirectory(RootDirectoryAttributes rootDirecto
217215

218216
public void createElementInDirectoryPath(String directoryPath, ElementAttributes elementAttributes, String userId) {
219217
String[] directoryPathSplit = directoryPath.split("/");
220-
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.MICROS);
218+
Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
221219
UUID currentDirectoryUuid;
222220
UUID parentDirectoryUuid = null;
223221

@@ -323,7 +321,7 @@ public void updateElement(UUID elementUuid, ElementAttributes newElementAttribut
323321
}
324322

325323
@Transactional
326-
public void updateElementLastModifiedAttributes(UUID elementUuid, LocalDateTime lastModificationDate, String lastModifiedBy) {
324+
public void updateElementLastModifiedAttributes(UUID elementUuid, Instant lastModificationDate, String lastModifiedBy) {
327325
DirectoryElementEntity elementToUpdate = getDirectoryElementEntity(elementUuid);
328326
elementToUpdate.updateModificationAttributes(lastModifiedBy, lastModificationDate);
329327

src/main/java/org/gridsuite/directory/server/dto/ElementAttributes.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import lombok.experimental.SuperBuilder;
1212
import org.gridsuite.directory.server.repository.DirectoryElementEntity;
1313

14-
import java.time.ZoneOffset;
15-
import java.time.ZonedDateTime;
14+
import java.time.Instant;
1615
import java.util.UUID;
1716

1817
import static org.gridsuite.directory.server.DirectoryService.DIRECTORY;
@@ -41,9 +40,9 @@ public class ElementAttributes {
4140

4241
private String description;
4342

44-
private ZonedDateTime creationDate;
43+
private Instant creationDate;
4544

46-
private ZonedDateTime lastModificationDate;
45+
private Instant lastModificationDate;
4746

4847
private String lastModifiedBy;
4948

@@ -56,7 +55,7 @@ public static ElementAttributes toElementAttributes(@NonNull DirectoryElementEnt
5655
}
5756

5857
public static ElementAttributes toElementAttributes(@NonNull DirectoryElementEntity entity, long subDirectoriesCount) {
59-
return toElementAttributes(entity.getId(), entity.getName(), entity.getType(), entity.getOwner(), subDirectoriesCount, entity.getDescription(), ZonedDateTime.ofInstant(entity.getCreationDate().toInstant(ZoneOffset.UTC), ZoneOffset.UTC), ZonedDateTime.ofInstant(entity.getLastModificationDate().toInstant(ZoneOffset.UTC), ZoneOffset.UTC), entity.getLastModifiedBy());
58+
return toElementAttributes(entity.getId(), entity.getName(), entity.getType(), entity.getOwner(), subDirectoriesCount, entity.getDescription(), entity.getCreationDate(), entity.getLastModificationDate(), entity.getLastModifiedBy());
6059
}
6160

6261
public static ElementAttributes toElementAttributes(@NonNull RootDirectoryAttributes rootDirectoryAttributes) {
@@ -73,13 +72,13 @@ public static ElementAttributes toElementAttributes(UUID elementUuid, @NonNull S
7372
}
7473

7574
public static ElementAttributes toElementAttributes(UUID elementUuid, @NonNull String elementName, @NonNull String elementType,
76-
@NonNull String userId, String elementDescription, ZonedDateTime creationDate, ZonedDateTime lastModificationDate, String lastModifiedBy) {
75+
@NonNull String userId, String elementDescription, Instant creationDate, Instant lastModificationDate, String lastModifiedBy) {
7776
return toElementAttributes(elementUuid, elementName, elementType, userId, 0L, elementDescription, creationDate, lastModificationDate, lastModifiedBy);
7877
}
7978

8079
public static ElementAttributes toElementAttributes(UUID elementUuid, @NonNull String elementName, @NonNull String elementType,
8180
@NonNull String userId,
82-
long subdirectoriesCount, String elementDescription, ZonedDateTime creationDate, ZonedDateTime lastModificationDate, String lastModifiedBy) {
81+
long subdirectoriesCount, String elementDescription, Instant creationDate, Instant lastModificationDate, String lastModifiedBy) {
8382
return ElementAttributes.builder().elementUuid(elementUuid).elementName(elementName)
8483
.type(elementType).owner(userId).creationDate(creationDate)
8584
.subdirectoriesCount(subdirectoriesCount).description(elementDescription)

src/main/java/org/gridsuite/directory/server/dto/RootDirectoryAttributes.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import lombok.Getter;
1111
import lombok.NoArgsConstructor;
1212

13-
import java.time.ZonedDateTime;
13+
import java.time.Instant;
1414

1515
/**
1616
* @author Nicolas Noir <nicolas.noir at rte-france.com>
@@ -24,9 +24,9 @@ public class RootDirectoryAttributes {
2424

2525
private String description;
2626

27-
private ZonedDateTime creationDate;
27+
private Instant creationDate;
2828

29-
private ZonedDateTime lastModificationDate;
29+
private Instant lastModificationDate;
3030

3131
private String lastModifiedBy;
3232
}

src/main/java/org/gridsuite/directory/server/dto/elasticsearch/DirectoryElementInfos.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.springframework.data.annotation.TypeAlias;
1515
import org.springframework.data.elasticsearch.annotations.*;
1616

17-
import java.time.LocalDateTime;
17+
import java.time.Instant;
1818
import java.util.List;
1919
import java.util.UUID;
2020

@@ -53,8 +53,8 @@ public class DirectoryElementInfos {
5353
@Field(type = FieldType.Long)
5454
private long subdirectoriesCount;
5555

56-
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
57-
LocalDateTime lastModificationDate;
56+
@Field(type = FieldType.Date, format = DateFormat.date_time)
57+
Instant lastModificationDate;
5858

5959
@Transient
6060
private List<String> pathName;

src/main/java/org/gridsuite/directory/server/repository/DirectoryElementEntity.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import jakarta.persistence.*;
1414
import org.gridsuite.directory.server.dto.elasticsearch.DirectoryElementInfos;
1515

16-
import java.time.LocalDateTime;
17-
import java.time.ZoneOffset;
16+
import java.time.Instant;
1817
import java.util.Objects;
1918
import java.util.UUID;
2019

@@ -51,10 +50,10 @@ public class DirectoryElementEntity {
5150
private String description;
5251

5352
@Column(name = "creationDate")
54-
private LocalDateTime creationDate;
53+
private Instant creationDate;
5554

5655
@Column(name = "lastModificationDate")
57-
private LocalDateTime lastModificationDate;
56+
private Instant lastModificationDate;
5857

5958
@Column(name = "lastModifiedBy")
6059
private String lastModifiedBy;
@@ -63,7 +62,7 @@ public class DirectoryElementEntity {
6362
private boolean stashed;
6463

6564
@Column(name = "stash_date")
66-
private LocalDateTime stashDate;
65+
private Instant stashDate;
6766

6867
public DirectoryElementEntity update(@NonNull ElementAttributes newElementAttributes) {
6968
boolean isElementNameUpdated = StringUtils.isNotBlank(newElementAttributes.getElementName());
@@ -76,13 +75,13 @@ public DirectoryElementEntity update(@NonNull ElementAttributes newElementAttrib
7675
this.description = newElementAttributes.getDescription();
7776
}
7877
if (isDescriptionUpdated || isElementNameUpdated) {
79-
updateModificationAttributes(lastModifiedBy, LocalDateTime.now(ZoneOffset.UTC));
78+
updateModificationAttributes(lastModifiedBy, Instant.now());
8079
}
8180
return this;
8281
}
8382

8483
public void updateModificationAttributes(@NonNull String lastModifiedBy,
85-
@NonNull LocalDateTime lastModificationDate) {
84+
@NonNull Instant lastModificationDate) {
8685
this.setLastModificationDate(lastModificationDate);
8786
this.setLastModifiedBy(lastModifiedBy);
8887
}

src/main/java/org/gridsuite/directory/server/services/ConsumerService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com>
1212
*/
1313

14-
import java.time.LocalDateTime;
14+
import java.time.Instant;
1515
import java.util.UUID;
1616
import java.util.function.Consumer;
1717

@@ -43,7 +43,7 @@ public Consumer<Message<String>> consumeElementUpdate() {
4343
String modificationDateStr = message.getHeaders().get(HEADER_MODIFICATION_DATE, String.class);
4444

4545
UUID elementUpdatedUuid = UUID.fromString(elementUpdatedUuidStr);
46-
LocalDateTime modificationDate = LocalDateTime.parse(modificationDateStr);
46+
Instant modificationDate = Instant.parse(modificationDateStr);
4747

4848
directoryService.updateElementLastModifiedAttributes(elementUpdatedUuid, modificationDate, modifiedBy);
4949
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
2+
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
3+
<changeSet author="tourian1 (generated)" id="1716805517481-1">
4+
<modifyDataType tableName="element" columnName="creation_date" newDataType="timestamptz"/>
5+
</changeSet>
6+
<changeSet author="tourian1 (generated)" id="1716805517481-2">
7+
<modifyDataType tableName="element" columnName="last_modification_date" newDataType="timestamptz"/>
8+
</changeSet>
9+
<changeSet author="tourian1 (generated)" id="1716805517481-3">
10+
<modifyDataType tableName="element" columnName="stash_date" newDataType="timestamptz"/>
11+
</changeSet>
12+
</databaseChangeLog>

src/main/resources/db/changelog/db.changelog-master.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ databaseChangeLog:
3838
- include:
3939
file: changesets/changelog_20240424T151355Z.xml
4040
relativeToChangelogFile: true
41+
42+
- include:
43+
file: changesets/changelog_20240605T120541Z.xml
44+
relativeToChangelogFile: true

src/test/java/org/gridsuite/directory/server/DirectoryElementInfosServiceTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import org.springframework.boot.test.context.SpringBootTest;
2121
import org.springframework.test.context.junit.jupiter.SpringExtension;
2222

23-
import java.time.LocalDateTime;
23+
import java.time.Instant;
24+
import java.time.temporal.ChronoUnit;
2425
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Set;
@@ -56,10 +57,10 @@ public void setup() {
5657

5758
@Test
5859
void testAddDeleteElementInfos() {
59-
var studyInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aStudy").type("STUDY").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
60-
var filterInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aFilter").type("FILTER").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
61-
var directoryInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aDirectory").type("DIRECTORY").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
62-
var contingencyListInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aContingencyList").type("CONTINGENCY_LIST").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
60+
var studyInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aStudy").type("STUDY").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
61+
var filterInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aFilter").type("FILTER").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
62+
var directoryInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aDirectory").type("DIRECTORY").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
63+
var contingencyListInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aContingencyList").type("CONTINGENCY_LIST").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
6364

6465
// Add
6566
List<DirectoryElementInfos> infos = List.of(studyInfos, filterInfos, directoryInfos, contingencyListInfos);
@@ -80,11 +81,11 @@ void testAddDeleteElementInfos() {
8081

8182
@Test
8283
void searchElementInfos() {
83-
var directoryInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aDirectory").type(DIRECTORY).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
84-
var studyInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aStudy").type(STUDY).owner("admin1").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
85-
var caseInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aCase").type(CASE).owner("admin1").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
86-
var filterInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aFilter").type(FILTER).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
87-
var contingencyListInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aContingencyList").type(CONTINGENCY_LIST).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
84+
var directoryInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aDirectory").type(DIRECTORY).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
85+
var studyInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aStudy").type(STUDY).owner("admin1").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
86+
var caseInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aCase").type(CASE).owner("admin1").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
87+
var filterInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aFilter").type(FILTER).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
88+
var contingencyListInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("aContingencyList").type(CONTINGENCY_LIST).owner("admin").parentId(UUID.randomUUID()).subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
8889

8990
List<DirectoryElementInfos> infos = List.of(directoryInfos, filterInfos, studyInfos, caseInfos, contingencyListInfos);
9091
repositoryService.saveElementsInfos(infos);
@@ -104,7 +105,7 @@ void searchElementInfos() {
104105
void searchSpecialChars() {
105106
var studyInfos = DirectoryElementInfos.builder().id(UUID.randomUUID()).name("s+Ss+ss'sp&pn(n n)ne{e e}et<t t>te|eh-ht.th/hl\\lk[k k]k")
106107
.type(STUDY).owner("admin1").parentId(UUID.randomUUID())
107-
.subdirectoriesCount(0L).lastModificationDate(LocalDateTime.now().withNano(0)).build();
108+
.subdirectoriesCount(0L).lastModificationDate(Instant.now().truncatedTo(ChronoUnit.SECONDS)).build();
108109
repositoryService.saveElementsInfos(List.of(studyInfos));
109110

110111
testNameFullAscii("s+S");

0 commit comments

Comments
 (0)