Commit 211adc66 authored by Maxym Borodenko's avatar Maxym Borodenko

Scheduled repeat of email notifications to genebanks

parent 8958ad42
......@@ -16,6 +16,7 @@
package org.genesys2.server.service;
import java.util.List;
import java.util.Set;
import java.util.UUID;
......@@ -171,6 +172,13 @@ public interface RequestService {
Page<MaterialSubRequest> list(FaoInstitute institute, MaterialSubRequestFilter filter, Pageable pageRequest);
/**
* Allow admin to find and load not confirmed sub-requests.
*
* @return list of sub-requests
*/
List<MaterialSubRequest> listNotConfirmedSubRequests();
MaterialRequest get(String uuid);
MaterialSubRequest get(FaoInstitute institute, String uuid);
......
......@@ -19,6 +19,8 @@ package org.genesys2.server.service.impl;
import java.io.IOException;
import java.util.*;
import com.google.common.collect.Lists;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.exception.NotFoundElement;
......@@ -500,16 +502,6 @@ public class RequestServiceImpl implements RequestService {
return subRequestRepository.findAll(predicate, pageable);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Page<MaterialSubRequest> listSubRequests(MaterialSubRequestFilter filter, Pageable pageable) {
final BooleanBuilder predicate = new BooleanBuilder();
if (filter != null) {
predicate.and(filter.buildPredicate());
}
return subRequestRepository.findAll(predicate, pageable);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest get(String uuid) {
......@@ -522,12 +514,37 @@ public class RequestServiceImpl implements RequestService {
return subRequestRepository.findByInstCodeAndUuid(institute.getCode(), uuid);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Page<MaterialSubRequest> listSubRequests(MaterialSubRequestFilter filter, Pageable pageable) {
final BooleanBuilder predicate = new BooleanBuilder();
if (filter != null) {
predicate.and(filter.buildPredicate());
}
return subRequestRepository.findAll(predicate, pageable);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialSubRequest getSubrequest(String uuid) {
return subRequestRepository.findByUuid(uuid);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public List<MaterialSubRequest> listNotConfirmedSubRequests() {
Calendar calFrom = Calendar.getInstance();
calFrom.add(Calendar.MONTH, -1);
Calendar calTo = Calendar.getInstance();
calTo.add(Calendar.DAY_OF_MONTH, -5);
QMaterialSubRequest msr = QMaterialSubRequest.materialSubRequest;
Predicate predicate = msr.state.eq(MaterialSubRequest.NOTCONFIRMED).and(msr.lastReminderDate.between(calFrom.getTime(), calTo.getTime()));
return Lists.newArrayList(subRequestRepository.findAll(predicate));
}
@Override
@PostAuthorize("returnObject==null || hasRole('ADMINISTRATOR')")
public MaterialSubRequest getSubrequest(UUID uuid, Integer version) {
......
/*
* Copyright 2019 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.service.worker;
import java.util.List;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.genesys2.server.component.security.AsAdminInvoker;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.service.RequestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Scheduled repeat of email notifications to genebanks.
*
* Task runs on 1 cluster node every 4 hours.
*
* @author Maxym Borodenko
*/
@Component
public class ScheduledSubRequestNotification {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(ScheduledSubRequestNotification.class);
/** The request service. */
@Autowired
private RequestService requestService;
/** Execute code as admin */
@Autowired
private AsAdminInvoker asAdminInvoker;
@Scheduled(cron = "0 0 */4 ? * *") // every four hours
@SchedulerLock(name = "org.genesys2.server.service.worker.ScheduledSubRequestNotification")
public void runRelayingRequests() throws Exception {
asAdminInvoker.invoke(() -> {
LOG.warn("Started scheduled relay of requests.");
final List<MaterialSubRequest> subRequestList = requestService.listNotConfirmedSubRequests();
for (final MaterialSubRequest msr : subRequestList) {
requestService.relayRequest(msr);
}
LOG.warn("Sent email notification(s) to {} genebank(s).", subRequestList.size());
return true;
});
}
}
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