Skip to content

Commit 91468f3

Browse files
authored
indexation directory elements (#114)
Signed-off-by: Ghazwa REHILI <[email protected]>
1 parent 82bebf6 commit 91468f3

17 files changed

+724
-147
lines changed

pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
<properties>
4444
<powsybl-ws-dependencies.version>2.7.0</powsybl-ws-dependencies.version>
45+
<testcontainers.version>1.18.3</testcontainers.version>
4546
<liquibase-hibernate-package>org.gridsuite.directory.server</liquibase-hibernate-package>
4647
<to-string-verifier-version>1.4.8</to-string-verifier-version>
4748
<native-buildtools.version>0.10.0</native-buildtools.version>
@@ -172,6 +173,12 @@
172173
<version>${to-string-verifier-version}</version>
173174
<scope>test</scope>
174175
</dependency>
176+
<dependency>
177+
<groupId>org.testcontainers</groupId>
178+
<artifactId>elasticsearch</artifactId>
179+
<version>${testcontainers.version}</version>
180+
<scope>test</scope>
181+
</dependency>
175182
</dependencies>
176183
</dependencyManagement>
177184

@@ -208,6 +215,12 @@
208215
<artifactId>spring-boot-starter-data-jpa</artifactId>
209216
</dependency>
210217

218+
<!-- elasticsearch -->
219+
<dependency>
220+
<groupId>org.springframework.data</groupId>
221+
<artifactId>spring-data-elasticsearch</artifactId>
222+
</dependency>
223+
211224
<!-- Runtime dependencies -->
212225
<dependency>
213226
<groupId>com.powsybl</groupId>
@@ -260,6 +273,11 @@
260273
<artifactId>h2</artifactId>
261274
<scope>test</scope>
262275
</dependency>
276+
<dependency>
277+
<groupId>org.testcontainers</groupId>
278+
<artifactId>elasticsearch</artifactId>
279+
<scope>test</scope>
280+
</dependency>
263281
<dependency>
264282
<groupId>com.powsybl</groupId>
265283
<artifactId>powsybl-config-test</artifactId>

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.swagger.v3.oas.annotations.tags.Tag;
1414
import org.gridsuite.directory.server.dto.ElementAttributes;
1515
import org.gridsuite.directory.server.dto.RootDirectoryAttributes;
16+
import org.gridsuite.directory.server.services.DirectoryRepositoryService;
1617
import org.springframework.data.util.Pair;
1718
import org.springframework.http.HttpStatus;
1819
import org.springframework.http.MediaType;
@@ -33,8 +34,11 @@ public class DirectoryController {
3334

3435
private final DirectoryService service;
3536

36-
public DirectoryController(DirectoryService service) {
37+
private final DirectoryRepositoryService repositoryService;
38+
39+
public DirectoryController(DirectoryService service, DirectoryRepositoryService repositoryService) {
3740
this.service = service;
41+
this.repositoryService = repositoryService;
3842
}
3943

4044
@PostMapping(value = "/root-directories", consumes = MediaType.APPLICATION_JSON_VALUE)
@@ -247,7 +251,15 @@ public ResponseEntity<Void> notify(@PathVariable("elementUuid") UUID elementUuid
247251
public ResponseEntity<Void> elementExists(@PathVariable("directoryUuid") UUID directoryUuid,
248252
@PathVariable("elementName") String elementName,
249253
@PathVariable("type") String type) {
250-
HttpStatus status = service.elementExists(directoryUuid, elementName, type) ? HttpStatus.OK : HttpStatus.NO_CONTENT;
254+
HttpStatus status = repositoryService.isElementExists(directoryUuid, elementName, type) ? HttpStatus.OK : HttpStatus.NO_CONTENT;
251255
return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).build();
252256
}
257+
258+
@PostMapping(value = "/elements/reindex-all")
259+
@Operation(summary = "reindex the element")
260+
@ApiResponse(responseCode = "200", description = "Elements reindexed")
261+
public ResponseEntity<Void> reindexAllElements() {
262+
service.reindexAllElements();
263+
return ResponseEntity.ok().build();
264+
}
253265
}

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

Lines changed: 80 additions & 116 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.directory.server.dto.elasticsearch;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
import lombok.*;
11+
import lombok.experimental.SuperBuilder;
12+
import org.springframework.data.annotation.Id;
13+
import org.springframework.data.annotation.TypeAlias;
14+
import org.springframework.data.elasticsearch.annotations.*;
15+
16+
import java.time.LocalDateTime;
17+
import java.util.UUID;
18+
19+
/**
20+
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
21+
*/
22+
@SuperBuilder
23+
@NoArgsConstructor
24+
@Setter
25+
@Getter
26+
@ToString
27+
@EqualsAndHashCode
28+
@Schema(description = "Directory element infos")
29+
@Document(indexName = "#{@environment.getProperty('powsybl-ws.elasticsearch.index.prefix')}directory-elements")
30+
@Setting(settingPath = "elasticsearch_settings.json")
31+
@TypeAlias(value = "DirectoryElementInfos")
32+
public class DirectoryElementInfos {
33+
@Id
34+
private UUID id;
35+
36+
@MultiField(
37+
mainField = @Field(name = "name", type = FieldType.Text),
38+
otherFields = {
39+
@InnerField(suffix = "fullascii", type = FieldType.Keyword, normalizer = "fullascii"),
40+
@InnerField(suffix = "raw", type = FieldType.Keyword)
41+
}
42+
)
43+
private String name;
44+
45+
private UUID parentId;
46+
47+
private String type;
48+
49+
private String owner;
50+
51+
@Field(type = FieldType.Long)
52+
private long subdirectoriesCount;
53+
54+
@Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second_millis)
55+
LocalDateTime lastModificationDate;
56+
57+
@Field(type = FieldType.Boolean)
58+
private Boolean isPrivate;
59+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.directory.server.elasticsearch;
8+
9+
import org.gridsuite.directory.server.dto.elasticsearch.DirectoryElementInfos;
10+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
11+
12+
import java.util.UUID;
13+
14+
/**
15+
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
16+
*/
17+
public interface DirectoryElementInfosRepository extends ElasticsearchRepository<DirectoryElementInfos, UUID> {
18+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
This Source Code Form is subject to the terms of the Mozilla Public
4+
License, v. 2.0. If a copy of the MPL was not distributed with this
5+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.directory.server.elasticsearch;
8+
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
13+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
14+
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
15+
16+
import javax.annotation.Nonnull;
17+
import java.util.Optional;
18+
19+
/**
20+
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
21+
*/
22+
@Configuration
23+
@EnableElasticsearchRepositories
24+
public class ESConfig extends ElasticsearchConfiguration {
25+
26+
@Value("#{'${spring.data.elasticsearch.embedded:false}' ? 'localhost' : '${spring.data.elasticsearch.host}'}")
27+
private String esHost;
28+
29+
@Value("#{'${spring.data.elasticsearch.embedded:false}' ? '${spring.data.elasticsearch.embedded.port:}' : '${spring.data.elasticsearch.port}'}")
30+
private int esPort;
31+
32+
@Value("${spring.data.elasticsearch.client.timeout:60}")
33+
int timeout;
34+
35+
@Value("${spring.data.elasticsearch.username:#{null}}")
36+
private Optional<String> username;
37+
38+
@Value("${spring.data.elasticsearch.password:#{null}}")
39+
private Optional<String> password;
40+
41+
//It should work without having to specify the bean name but it doesn't. To investigate.
42+
@Bean(name = "elasticsearchClientConfiguration")
43+
@Override
44+
@SuppressWarnings("squid:S2095")
45+
@Nonnull
46+
public ClientConfiguration clientConfiguration() {
47+
var clientConfiguration = ClientConfiguration.builder()
48+
.connectedTo(esHost + ":" + esPort)
49+
.withConnectTimeout(timeout * 1000L).withSocketTimeout(timeout * 1000L);
50+
51+
if (username.isPresent() && password.isPresent()) {
52+
clientConfiguration.withBasicAuth(username.get(), password.get());
53+
}
54+
55+
return clientConfiguration.build();
56+
}
57+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.gridsuite.directory.server.dto.ElementAttributes;
1212

1313
import jakarta.persistence.*;
14+
import org.gridsuite.directory.server.dto.elasticsearch.DirectoryElementInfos;
15+
1416
import java.time.LocalDateTime;
1517
import java.util.Objects;
1618
import java.util.UUID;
@@ -102,4 +104,16 @@ public DirectoryElementEntity stashElement(boolean stashed, LocalDateTime stashD
102104
this.stashed = stashed;
103105
return this;
104106
}
107+
108+
public DirectoryElementInfos toDirectoryElementInfos() {
109+
return DirectoryElementInfos.builder()
110+
.id(getId())
111+
.name(getName())
112+
.owner(getOwner())
113+
.parentId(getParentId() == null ? getId() : getParentId())
114+
.type(getType())
115+
.isPrivate(getIsPrivate())
116+
.lastModificationDate(getLastModificationDate())
117+
.build();
118+
}
105119
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
@Repository
2424
public interface DirectoryElementRepository extends JpaRepository<DirectoryElementEntity, UUID> {
2525

26+
List<DirectoryElementEntity> findAllByStashed(boolean stashed);
27+
2628
List<DirectoryElementEntity> findAllByParentIdAndStashed(UUID parentId, boolean stashed);
2729

2830
List<DirectoryElementEntity> findAllByIdInAndStashed(List<UUID> uuids, boolean stashed);
@@ -50,10 +52,6 @@ public interface DirectoryElementRepository extends JpaRepository<DirectoryEleme
5052

5153
boolean existsByIdAndOwnerOrIsPrivateAndId(UUID id, String owner, boolean isPrivate, UUID id2);
5254

53-
default boolean canRead(UUID id, String userId) {
54-
return existsByIdAndOwnerOrIsPrivateAndId(id, userId, false, id);
55-
}
56-
5755
interface SubDirectoryCount {
5856
UUID getId();
5957

0 commit comments

Comments
 (0)