-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...n/src/main/java/it/smartcommunitylabdhub/core/components/solr/SolrFieldsExportRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package it.smartcommunitylabdhub.core.components.solr; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.TemplateEngine; | ||
import org.thymeleaf.context.Context; | ||
|
||
import it.smartcommunitylabdhub.core.models.indexers.SolrEntityIndexer; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@Slf4j | ||
@Profile("generate-solr") | ||
public class SolrFieldsExportRunner implements CommandLineRunner { | ||
@Autowired | ||
ApplicationContext context; | ||
|
||
@Autowired | ||
private TemplateEngine templateEngine; | ||
|
||
private List<SolrEntityIndexer<?>> services; | ||
|
||
@Autowired(required = false) | ||
public void setServices(List<SolrEntityIndexer<?>> services) { | ||
this.services = services; | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
log.info("Running solr fields export..."); | ||
int returnCode = 0; | ||
if(services != null) { | ||
try { | ||
Map<String, IndexField> fieldsMap = new HashMap<>(); | ||
services.forEach(service -> { | ||
for(IndexField field : service.fields()) { | ||
if(!fieldsMap.containsKey(field.getName())) | ||
fieldsMap.put(field.getName(), field); | ||
} | ||
}); | ||
Map<String, Object> variables = new HashMap<>(); | ||
variables.put("fields", fieldsMap.values()); | ||
final Context ctx = new Context(); | ||
ctx.setVariables(variables); | ||
String content = templateEngine.process("solr_fields.xml", ctx); | ||
String out = "solr/solr_fields.xml"; | ||
Path fp = Paths.get(out); | ||
Files.createDirectories(fp.getParent()); | ||
File file = new File(out); | ||
log.info("writing solr fields to {}...",file.getAbsolutePath()); | ||
FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8); | ||
} catch (Exception e) { | ||
log.error("Error with export: {}", e.getMessage()); | ||
returnCode = 1; | ||
} | ||
} | ||
int exitCode = returnCode == 0 | ||
? SpringApplication.exit(context, () -> 0) | ||
: SpringApplication.exit(context, () -> 1); | ||
System.exit(exitCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<root> | ||
<field th:each="item : ${fields}" th:name="${item.name}" th:type="${item.type}" th:indexed="${item.indexed}" th:multiValued="${item.multiValued}" th:stored="${item.stored}" th:uninvertible="${item.uninvertible}"/> | ||
</root> |