From c020b0d17c7e71f73f0a62d4c2ae9921a310a4bb Mon Sep 17 00:00:00 2001 From: yichen88 Date: Mon, 25 Jan 2021 16:01:17 +0100 Subject: [PATCH] Set spring configs into app data. Signed-off-by: yichen88 --- .../PostgresAppFileSystemAppProvider.java | 26 +++++++++++- .../afs/postgres/PostgresAppStorage.java | 16 ++++---- .../jpa/TimeSeriesStringDataEntity.java | 40 ------------------- .../TimeSeriesStringDataEntityRepository.java | 32 --------------- .../src/main/resources/persistence.properties | 2 +- .../afs/postgres/PostgresAppStorageTest.java | 11 ++--- .../com/powsybl/afs/postgres/TestConfig.java | 15 ++++++- 7 files changed, 54 insertions(+), 88 deletions(-) delete mode 100644 afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntity.java delete mode 100644 afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntityRepository.java diff --git a/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppFileSystemAppProvider.java b/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppFileSystemAppProvider.java index 1c06ca24..ca9ac108 100644 --- a/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppFileSystemAppProvider.java +++ b/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppFileSystemAppProvider.java @@ -10,9 +10,12 @@ import com.powsybl.afs.AppFileSystem; import com.powsybl.afs.AppFileSystemProvider; import com.powsybl.afs.AppFileSystemProviderContext; +import com.powsybl.afs.storage.EventsBus; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.Collections; import java.util.List; +import java.util.Objects; /** * @author Yichen TANG @@ -20,9 +23,28 @@ @AutoService(AppFileSystemProvider.class) public class PostgresAppFileSystemAppProvider implements AppFileSystemProvider { + private final List configs; + + public PostgresAppFileSystemAppProvider() { + this(PostgresAppFileSystemConfig.load()); + } + + public PostgresAppFileSystemAppProvider(List configs) { + this.configs = Objects.requireNonNull(configs); + } + @Override public List getFileSystems(AppFileSystemProviderContext context) { - return Collections.emptyList(); -// return Collections.singletonList(new PostgresAppFileSystem("test", true, new PostgresAppStorage())); + final PostgresAppFileSystemConfig postgresAppFileSystemConfig = configs.get(0); + AnnotationConfigApplicationContext springContext + = new AnnotationConfigApplicationContext(); + springContext.getEnvironment().setActiveProfiles("production"); + springContext.scan("com.powsybl.afs.postgres"); + springContext.register(PostgresAppStorage.class); + springContext.getBeanFactory().registerSingleton(EventsBus.class.toString(), context.getEventsBus()); + springContext.getBeanFactory().registerSingleton(String.class.toString(), postgresAppFileSystemConfig.getDriveName()); + springContext.refresh(); + final PostgresAppStorage bean = springContext.getBean(PostgresAppStorage.class); + return Collections.singletonList(new PostgresAppFileSystem(postgresAppFileSystemConfig.getDriveName(), true, bean)); } } diff --git a/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppStorage.java b/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppStorage.java index d2ef4900..5fe10ffa 100644 --- a/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppStorage.java +++ b/afs-postgres/src/main/java/com/powsybl/afs/postgres/PostgresAppStorage.java @@ -30,17 +30,21 @@ public class PostgresAppStorage extends AbstractAppStorage { private final NodeDataRepository nodeDataRepository; private final TimeSeriesService tsService; + private final String fileSystemName; + private static final int ROOT_NODE_VERSION = 0; @Autowired - public PostgresAppStorage(NodeService nodeService, + public PostgresAppStorage(String fileSystemName, + NodeService nodeService, NodeDataRepository nodeDataRepository, - TimeSeriesService tsService) { - // TODO here - this.eventsBus = new InMemoryEventsBus(); + TimeSeriesService tsService, + EventsBus eventsBus) { + this.fileSystemName = Objects.requireNonNull(fileSystemName); this.nodeService = Objects.requireNonNull(nodeService); this.nodeDataRepository = Objects.requireNonNull(nodeDataRepository); this.tsService = Objects.requireNonNull(tsService); + this.eventsBus = Objects.requireNonNull(eventsBus); bindListener(); } @@ -63,8 +67,7 @@ private void bindListener() { @Override public String getFileSystemName() { - // TODO here - return "postgres-test"; + return fileSystemName; } @Override @@ -100,7 +103,6 @@ public boolean isRemote() { @Override public NodeInfo createRootNodeIfNotExists(String name, String nodePseudoClass) { final NodeInfo info = nodeService.createRootNodeIfNotExists(name, nodePseudoClass); -// pushEvent(new NodeCreated("test", null), "APPSTORAGE_NODE_TOPIC"); return info; } diff --git a/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntity.java b/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntity.java deleted file mode 100644 index 704f4b08..00000000 --- a/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntity.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Copyright (c) 2021, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package com.powsybl.afs.postgres.jpa; - -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.experimental.Accessors; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author Yichen TANG - */ -@Entity -@Accessors(chain = true) -@Data() -@NoArgsConstructor -@Table(name = "ts_string_data") -public class TimeSeriesStringDataEntity { - @Id - @GeneratedValue - long id; - - String nodeId; - - int version; - - String name; - - int point; - - String value; -} diff --git a/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntityRepository.java b/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntityRepository.java deleted file mode 100644 index 96754e00..00000000 --- a/afs-postgres/src/main/java/com/powsybl/afs/postgres/jpa/TimeSeriesStringDataEntityRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) 2021, RTE (http://www.rte-france.com) - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package com.powsybl.afs.postgres.jpa; - -import org.springframework.data.jpa.repository.Modifying; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; -import java.util.Set; - -@Repository -public interface TimeSeriesStringDataEntityRepository extends CrudRepository { - - @Query("SELECT DISTINCT t.version FROM TimeSeriesStringDataEntity t WHERE t.nodeId = ?1") - List findDistinctVersionsByNodeId(String nodeId); - - @Query("SELECT DISTINCT t.version FROM TimeSeriesStringDataEntity t WHERE t.nodeId = ?1 and t.name = ?2") - Set findDistinctVersionsByNodeIdAndName(String nodeId, String tsName); - - List findAllByNodeIdAndNameAndVersionOrderByPoint(String nodeId, String name, int version); - - @Transactional - @Modifying - void deleteAllByNodeId(String nodeId); -} diff --git a/afs-postgres/src/main/resources/persistence.properties b/afs-postgres/src/main/resources/persistence.properties index 530896fe..207de2e8 100644 --- a/afs-postgres/src/main/resources/persistence.properties +++ b/afs-postgres/src/main/resources/persistence.properties @@ -1 +1 @@ -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=update diff --git a/afs-postgres/src/test/java/com/powsybl/afs/postgres/PostgresAppStorageTest.java b/afs-postgres/src/test/java/com/powsybl/afs/postgres/PostgresAppStorageTest.java index fdc55f48..041e5067 100644 --- a/afs-postgres/src/test/java/com/powsybl/afs/postgres/PostgresAppStorageTest.java +++ b/afs-postgres/src/test/java/com/powsybl/afs/postgres/PostgresAppStorageTest.java @@ -8,10 +8,7 @@ import com.google.common.collect.Sets; import com.powsybl.afs.postgres.jpa.NodeDataRepository; -import com.powsybl.afs.storage.AbstractAppStorageTest; -import com.powsybl.afs.storage.AppStorage; -import com.powsybl.afs.storage.NodeGenericMetadata; -import com.powsybl.afs.storage.NodeInfo; +import com.powsybl.afs.storage.*; import com.powsybl.afs.storage.events.TimeSeriesCreated; import com.powsybl.afs.storage.events.TimeSeriesDataUpdated; import com.powsybl.timeseries.*; @@ -44,10 +41,14 @@ public class PostgresAppStorageTest extends AbstractAppStorageTest { private TimeSeriesService tsService; @Autowired private NodeDataRepository nodeDataRepository; + @Autowired + private EventsBus eventsBus; + @Autowired + private String fileSystemName; @Override protected AppStorage createStorage() { - return new PostgresAppStorage(nodeService, nodeDataRepository, tsService); + return new PostgresAppStorage(fileSystemName, nodeService, nodeDataRepository, tsService, eventsBus); } @Override diff --git a/afs-postgres/src/test/java/com/powsybl/afs/postgres/TestConfig.java b/afs-postgres/src/test/java/com/powsybl/afs/postgres/TestConfig.java index e18d0c7e..a012f06d 100644 --- a/afs-postgres/src/test/java/com/powsybl/afs/postgres/TestConfig.java +++ b/afs-postgres/src/test/java/com/powsybl/afs/postgres/TestConfig.java @@ -6,6 +6,9 @@ */ package com.powsybl.afs.postgres; +import com.powsybl.afs.storage.EventsBus; +import com.powsybl.afs.storage.InMemoryEventsBus; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; @@ -15,6 +18,16 @@ */ @Configuration @PropertySource("classpath:application.properties") -@Profile({ "test" }) +@Profile({"test"}) public class TestConfig { + + @Bean + public EventsBus imMemory() { + return new InMemoryEventsBus(); + } + + @Bean(name = "fileSystemName") + public String getFileSystemName() { + return "postgres-afs"; + } }