Commit 215966a1 authored by Matija Obreza's avatar Matija Obreza
Browse files

Reindex entities on application startup (off by default)

- elasticsearch.startup.reindex=true
parent c5100199
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -21,28 +21,39 @@ import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.genesys.blocks.oauth.model.OAuthRole;
import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
import org.genesys.blocks.oauth.util.AppBlocksInitializer;
import org.genesys.blocks.security.NotUniqueUserException;
import org.genesys.blocks.security.UserException;
import org.genesys.blocks.security.model.BasicUser;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.DescriptorList;
import org.genesys.catalog.model.user.User;
import org.genesys.catalog.model.user.UserRole;
import org.genesys.catalog.model.vocab.ControlledVocabulary;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.persistence.user.UserRepository;
import org.genesys.catalog.server.service.ElasticsearchService;
import org.genesys.catalog.service.UserService;
import org.genesys.common.model.Crop;
import org.genesys.common.model.Partner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import com.google.common.collect.Sets;
/**
* Run things at startup and after application context is initialized
*/
@Component
public class StartupInitializer implements InitializingBean {
private static final Logger LOG = LoggerFactory.getLogger(StartupInitializer.class);
public class ApplicationStartup implements InitializingBean, ApplicationListener<ContextRefreshedEvent> {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationStartup.class);
private static final String DEFAULT_ADMIN_EMAIL = "admin@example.com";
......@@ -66,13 +77,16 @@ public class StartupInitializer implements InitializingBean {
@Autowired
private OAuthClientRepository oauthClientRepository;
// TODO Renove in 2.0
@Bean
private AppBlocksInitializer appBlocksInitializer() {
return new AppBlocksInitializer();
}
@Autowired
private ElasticsearchService elasticsearch;
@Value("${elasticsearch.startup.reindex}")
private boolean reindexElastic;
/**
* Things to run immediately
*/
@Override
public void afterPropertiesSet() throws Exception {
if (StringUtils.isBlank(defaultAdminEmail)) {
......@@ -85,6 +99,16 @@ public class StartupInitializer implements InitializingBean {
startup();
}
/**
* Things to run after startup
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (reindexElastic) {
reindexElasticsearch();
}
}
@Transactional
void startup() {
try {
......@@ -127,4 +151,15 @@ public class StartupInitializer implements InitializingBean {
LOG.info("Startup initializer done.");
}
private void reindexElasticsearch() {
elasticsearch.reindex(Crop.class);
elasticsearch.reindex(Partner.class);
elasticsearch.reindex(VocabularyTerm.class);
elasticsearch.reindex(ControlledVocabulary.class);
elasticsearch.reindex(Descriptor.class);
elasticsearch.reindex(DescriptorList.class);
elasticsearch.reindex(Dataset.class);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment