Commit d5d8126c authored by Maxym Borodenko's avatar Maxym Borodenko

ApplicationStartup actions to /admin actions

parent bd6cb473
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.api.v1;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import io.swagger.annotations.Api;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.ElasticsearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Maxym Borodenko
*/
@RestController("adminApi1")
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(AdminController.API_BASE)
@Api(tags = { "admin" })
public class AdminController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/admin";
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(AdminController.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private FaoInstituteRepository instituteRepository;
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private ImageGalleryService imageGalleryService;
@Autowired
private ElasticsearchService elasticsearch;
@Autowired
private CustomAclService aclService;
@Autowired
private DatasetService datasetService;
@PostMapping(value = "/ensure-inst-folders")
public Boolean ensureInstituteFolders() throws Exception {
LOG.info("Ensure institute folders");
List<RepositoryFolder> wiewsFolders = repositoryService.getFolders(Paths.get("/wiews"), RepositoryFolder.DEFAULT_SORT);
for (RepositoryFolder instFolder: wiewsFolders) {
LOG.warn("Ensuring inheritance for {}", instFolder.getFolderPath());
FaoInstitute institute = instituteRepository.findByCode(instFolder.getName());
if (institute != null) {
repositoryService.ensureFolder(instFolder.getFolderPath(), institute);
} else {
LOG.error("No institute " + instFolder.getName() + " for path " + instFolder.getPath());
}
}
return true;
}
@PostMapping(value = "/ensure-dataset-folders")
public Boolean ensureDatasetFolders() throws Exception {
LOG.info("Ensure dataset folders");
for (Dataset dataset : datasetRepository.findAll()) {
final Path datasetPath = datasetService.getDatasetRepositoryFolder(dataset);
LOG.warn("Ensuring inheritance for {}", datasetPath);
repositoryService.ensureFolder(datasetPath, dataset.getOwner());
}
return true;
}
/**
* We want all thumbnails to exist
*/
@PostMapping(value = "/ensure-thumbnails")
public Boolean ensureThumbnails() {
LOG.info("Ensure thumbnails");
int page = 0;
Page<ImageGallery> galleries = null;
do {
PageRequest p = new PageRequest(page++, 10);
galleries = imageGalleryService.listImageGalleries(p);
galleries.forEach(gallery -> {
LOG.warn("Ensuring all thumbnails for {}", gallery.getPath());
imageGalleryService.ensureThumbnails(gallery);
});
} while (galleries != null && galleries.hasNext());
return true;
}
@PostMapping(value = "/reindex-es")
public Boolean reindexElasticsearch() {
LOG.info("Reindex Elasticsearch");
elasticsearch.reindexAll();
return true;
}
@PostMapping(value = "/cleanup-acl")
public Boolean cleanupAcl() {
LOG.info("Cleanup ACL");
aclService.cleanupAcl();
return true;
}
}
/**
* Copyright 2014 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.
......@@ -12,11 +12,13 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.mvc.admin;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
......@@ -29,7 +31,14 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.lang3.ObjectUtils;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionGeo;
......@@ -39,10 +48,12 @@ import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.PDCIRepository;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.CountryNamesUpdater;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.service.GeoService;
......@@ -59,6 +70,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
......@@ -140,6 +152,24 @@ public class AdminController {
@Autowired
private PDCIRepository pdciRepository;
@Autowired
private RepositoryService repositoryService;
@Autowired
private FaoInstituteRepository instituteRepository;
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private ImageGalleryService imageGalleryService;
@Autowired
private ElasticsearchService elasticsearch;
@Autowired
private CustomAclService aclService;
@RequestMapping("/")
public String root(Model model) {
return "/admin/index";
......@@ -407,4 +437,65 @@ public class AdminController {
genesysService.removeDOIs();
return "redirect:/admin/";
}
@PostMapping(value = "/ensureInstituteFolders")
public String ensureInstituteFolders() throws Exception {
LOG.info("Ensure institute folders");
List<RepositoryFolder> wiewsFolders = repositoryService.getFolders(Paths.get("/wiews"), RepositoryFolder.DEFAULT_SORT);
for (RepositoryFolder instFolder: wiewsFolders) {
LOG.warn("Ensuring inheritance for {}", instFolder.getFolderPath());
FaoInstitute institute = instituteRepository.findByCode(instFolder.getName());
if (institute != null) {
repositoryService.ensureFolder(instFolder.getFolderPath(), institute);
} else {
LOG.error("No institute " + instFolder.getName() + " for path " + instFolder.getPath());
}
}
return "redirect:/admin/";
}
@PostMapping(value = "/ensureDatasetFolders")
public String ensureDatasetFolders() throws Exception {
LOG.info("Ensure dataset folders");
for (Dataset dataset : datasetRepository.findAll()) {
final Path datasetPath = datasetService.getDatasetRepositoryFolder(dataset);
LOG.warn("Ensuring inheritance for {}", datasetPath);
repositoryService.ensureFolder(datasetPath, dataset.getOwner());
}
return "redirect:/admin/";
}
/**
* We want all thumbnails to exist
*/
@PostMapping(value = "/ensureThumbnails")
public String ensureThumbnails() {
LOG.info("Ensure thumbnails");
int page = 0;
Page<ImageGallery> galleries = null;
do {
PageRequest p = new PageRequest(page++, 10);
galleries = imageGalleryService.listImageGalleries(p);
galleries.forEach(gallery -> {
LOG.warn("Ensuring all thumbnails for {}", gallery.getPath());
imageGalleryService.ensureThumbnails(gallery);
});
} while (galleries != null && galleries.hasNext());
return "redirect:/admin/";
}
@PostMapping(value = "/reindex-es")
public String reindexElasticsearch() {
LOG.info("Reindex Elasticsearch");
elasticsearch.reindexAll();
return "redirect:/admin/";
}
@PostMapping(value = "/cleanup-acl")
public String cleanupAcl() {
LOG.info("Cleanup ACL");
aclService.cleanupAcl();
return "redirect:/admin/";
}
}
......@@ -15,10 +15,6 @@
*/
package org.genesys2.spring.config;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.transaction.Transactional;
import org.apache.commons.lang3.StringUtils;
......@@ -28,33 +24,17 @@ import org.genesys.blocks.oauth.persistence.OAuthClientRepository;
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.CustomAclService;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.UserRepository;
import org.genesys2.server.security.AsAdminInvoker;
import org.genesys2.server.service.ElasticsearchService;
import org.genesys2.server.service.UserService;
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.ApplicationListener;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
......@@ -65,7 +45,7 @@ import com.google.common.collect.Sets;
*/
@Component
@Import({ ApplicationUpgrades.class })
public class ApplicationStartup implements InitializingBean, ApplicationListener<ContextRefreshedEvent> {
public class ApplicationStartup implements InitializingBean {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationStartup.class);
private static final String DEFAULT_ADMIN_EMAIL = "admin@example.com";
......@@ -81,10 +61,6 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
@Value("${default.oauthclient.clientSecret}")
private String defaultOAuthClientSecret;
@Value("${file.repository.image.thumbnailSizes}")
private int thumbnailSizes[];
@Autowired
private UserRepository userRepository;
......@@ -95,12 +71,6 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
@Autowired
private OAuthClientRepository oauthClientRepository;
@Autowired
private CustomAclService aclService;
@Autowired
protected AsAdminInvoker asAdminInvoker;
/**
* Things to run immediately
*/
......@@ -114,20 +84,6 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
}
startup();
ensureThumbnails();
}
/**
* Things to run after startup
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
aclService.cleanupAcl();
if (reindexElastic) {
reindexElasticsearch();
}
}
/**
......@@ -141,18 +97,6 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
LOG.error("Default admin account could not be created", e);
}
ensure1OAuthClient();
try {
ensureDatasetFolders();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
try {
ensureInsituteFolders();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
@Autowired
......@@ -200,79 +144,4 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
LOG.info("Startup initializer done.");
}
@Autowired
private ElasticsearchService elasticsearch;
@Value("${elasticsearch.startup.reindex}")
private boolean reindexElastic;
private void reindexElasticsearch() {
elasticsearch.reindexAll();
}
@Autowired
private ImageGalleryService imageGalleryService;
/**
* We want all thumbnails to exist
*/
private void ensureThumbnails() throws Exception {
asAdminInvoker.invoke(() -> {
int page = 0;
Page<ImageGallery> galleries = null;
do {
PageRequest p = new PageRequest(page++, 10);
galleries = imageGalleryService.listImageGalleries(p);
galleries.forEach(gallery -> {
LOG.warn("Ensuring all thumbnails for {}", gallery.getPath());
imageGalleryService.ensureThumbnails(gallery);
});
} while (galleries != null && galleries.hasNext());
return true;
});
}
@Autowired
private DatasetService datasetService;
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private RepositoryService repositoryService;
private void ensureDatasetFolders() throws Exception {
asAdminInvoker.invoke(() -> {
for (Dataset dataset : datasetRepository.findAll()) {
final Path datasetPath = datasetService.getDatasetRepositoryFolder(dataset);
LOG.warn("Ensuring inheritance for {}", datasetPath);
repositoryService.ensureFolder(datasetPath, dataset.getOwner());
}
return true;
});
}
@Autowired
private FaoInstituteRepository instituteRepository;
private void ensureInsituteFolders() throws Exception {
asAdminInvoker.invoke(() -> {
List<RepositoryFolder> wiewsFolders = repositoryService.getFolders(Paths.get("/wiews"), RepositoryFolder.DEFAULT_SORT);
for (RepositoryFolder instFolder: wiewsFolders) {
LOG.warn("Ensuring inheritance for {}", instFolder.getFolderPath());
FaoInstitute institute = instituteRepository.findByCode(instFolder.getName());
if (institute != null) {
repositoryService.ensureFolder(instFolder.getFolderPath(), institute);
} else {
LOG.error("No institute " + instFolder.getName() + " for path " + instFolder.getPath());
}
}
return true;
});
}
}
......@@ -158,5 +158,36 @@
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<h3>Repository</h3>
<form method="post" action="<c:url value="/admin/ensureInstituteFolders" />">
<input type="submit" class="btn btn-default" class="btn btn-default" value="Ensure institute folders" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<form method="post" action="<c:url value="/admin/ensureDatasetFolders" />">
<input type="submit" class="btn btn-default" class="btn btn-default" value="Ensure dataset folders" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<form method="post" action="<c:url value="/admin/ensureThumbnails" />">
<input type="submit" class="btn btn-default" class="btn btn-default" value="Ensure thumbnails" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<h3>Elasticsearch</h3>
<form method="post" action="<c:url value="/admin/reindex-es" />">
<input type="submit" class="btn btn-default" class="btn btn-default" value="Reindex Elasticsearch" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<h3>ACL</h3>
<form method="post" action="<c:url value="/admin/cleanup-acl" />">
<input type="submit" class="btn btn-default" class="btn btn-default" value="Cleanup ACL" />
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
Markdown is supported
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