1
1
package org .gridsuite .directory .server .services ;
2
2
3
+ import lombok .extern .slf4j .Slf4j ;
3
4
import org .gridsuite .directory .server .dto .ElementAttributes ;
5
+ import org .gridsuite .directory .server .dto .elasticsearch .DirectoryElementInfos ;
4
6
import org .gridsuite .directory .server .repository .DirectoryElementEntity ;
5
7
import org .gridsuite .directory .server .repository .DirectoryElementRepository ;
8
+ import org .springframework .data .elasticsearch .client .elc .ElasticsearchTemplate ;
9
+ import org .springframework .data .elasticsearch .core .IndexOperations ;
6
10
import org .springframework .stereotype .Service ;
7
11
8
12
import java .util .List ;
11
15
import static org .gridsuite .directory .server .dto .ElementAttributes .toElementAttributes ;
12
16
13
17
@ Service
18
+ @ Slf4j
14
19
public class SupervisionService {
15
20
private final DirectoryElementRepository directoryElementRepository ;
16
21
private final DirectoryRepositoryService repositoryService ;
22
+ private final ElasticsearchTemplate esTemplate ;
17
23
18
- public SupervisionService (DirectoryRepositoryService repositoryService , DirectoryElementRepository directoryElementRepository ) {
24
+ public SupervisionService (DirectoryRepositoryService repositoryService , DirectoryElementRepository directoryElementRepository ,
25
+ ElasticsearchTemplate esTemplate ) {
19
26
this .repositoryService = repositoryService ;
20
27
this .directoryElementRepository = directoryElementRepository ;
28
+ this .esTemplate = esTemplate ;
21
29
}
22
30
23
31
public List <ElementAttributes > getStashedElementsAttributes () {
@@ -35,4 +43,28 @@ public void deleteElementsByIds(List<UUID> uuids) {
35
43
public List <DirectoryElementEntity > getStashedElements () {
36
44
return directoryElementRepository .findAllByStashed (true );
37
45
}
46
+
47
+ public boolean recreateIndexDirectoryElementInfos () {
48
+ final IndexOperations idxDirectoryElementInfos = esTemplate .indexOps (DirectoryElementInfos .class );
49
+ final String idxDirectoryElementInfosName = idxDirectoryElementInfos .getIndexCoordinates ().getIndexName ();
50
+ log .warn ("Recreating ElasticSearch index {}" , idxDirectoryElementInfosName );
51
+ if (idxDirectoryElementInfos .exists ()) {
52
+ log .info ("Index {} found, delete it." , idxDirectoryElementInfosName );
53
+ if (idxDirectoryElementInfos .delete ()) {
54
+ log .info ("Successfully delete index {}" , idxDirectoryElementInfosName );
55
+ } else {
56
+ log .error ("A problem seems to happen when deleting index {}..." , idxDirectoryElementInfosName );
57
+ return false ;
58
+ }
59
+ }
60
+ if (idxDirectoryElementInfos .createWithMapping ()) {
61
+ log .info ("Index {} successfully recreated!" , idxDirectoryElementInfosName );
62
+ } else {
63
+ log .info ("An error happen while re-creating index {}..." , idxDirectoryElementInfosName );
64
+ return false ;
65
+ }
66
+ log .info ("Re-indexing all elements of {}" , idxDirectoryElementInfosName );
67
+ repositoryService .reindexAllElements ();
68
+ return true ;
69
+ }
38
70
}
0 commit comments