ScheduledDataUpdater.java
/*
* Copyright 2026 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.genesys.server.service.worker;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.apache.commons.text.StringEscapeUtils;
import org.genesys.server.service.ArticleService;
import org.genesys.server.service.ContentService;
import org.genesys.server.service.CountryService;
import org.genesys.server.service.EMailService;
import org.genesys.server.service.ArticleTranslationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ScheduledDataUpdater {
public static final String ERROR_MESSAGE = "update <span style=\"color: red\">failed</span>: ";
public static final String SUCCESS_MESSAGE = "update <span style=\"color: green\">successful</span>";
@Autowired
private InstituteUpdater instituteUpdater;
@Autowired
private CountryService countryService;
@Autowired
private CountryAlternateNamesUpdater alternateNamesUpdater;
@Autowired
private ITPGRFAStatusUpdater itpgrfaUpdater;
@Autowired
private ArticleService articleService;
@Autowired
private ContentService contentService;
@Autowired
private EMailService emailService;
@Value("${base.url}")
private String apiUrl;
// Scheduled initialDelay: 1 day; fixedDelay: 14 days
// HZ lockAtLeastFor 1 hour; lockAtMostFor 2 hours
@Scheduled(initialDelayString = "P1D", fixedDelayString = "P14D")
@SchedulerLock(name = "org.genesys.server.service.worker.ScheduledDataUpdater", lockAtLeastFor = "PT1H", lockAtMostFor = "PT2H")
// @Scheduled(fixedDelayString = "PT30M") // For tests
public void updateExternalCountryData() {
String faoWiewsUpdateError = null;
String countryIsoCodesUpdateError = null;
String countryNamesUpdateError = null;
String itpgrfaStatusUpdateError = null;
// FAO WIEWS
log.info("Updating FAO WIEWS");
try {
instituteUpdater.updateFaoInstitutes();
} catch (final Throwable e) {
log.error(e.getMessage(), e);
faoWiewsUpdateError = StringEscapeUtils.escapeHtml4(e.getMessage());
}
log.info("Updating FAO WIEWS: done");
// Country ISO codes
log.info("Updating country ISO codes");
try {
countryService.updateCountryData();
} catch (final Throwable e) {
log.error(e.getMessage(), e);
countryIsoCodesUpdateError = StringEscapeUtils.escapeHtml4(e.getMessage());
}
log.info("Updating country ISO codes: done");
// Country names (alternate GEO names)
log.info("Updating alternate GEO names");
try {
alternateNamesUpdater.updateAlternateNames();
} catch (final Throwable e) {
log.error(e.getMessage(), e);
countryNamesUpdateError = StringEscapeUtils.escapeHtml4(e.getMessage());
}
log.info("Updating alternate GEO names: done");
// ITPGRFA country status
log.info("Updating country ITPGRFA status");
try {
itpgrfaUpdater.downloadAndUpdate();
} catch (final Throwable e) {
log.error(e.getMessage(), e);
itpgrfaStatusUpdateError = StringEscapeUtils.escapeHtml4(e.getMessage());
}
log.info("Updating country ITPGRFA status: done");
ArticleTranslationService.TranslatedArticle article = articleService.getGlobalArticle(ContentService.SMTP_AUTOMATIC_RESOURCE_UPDATE_NOTIFICATION, Locale.ENGLISH);
if (article != null) {
Map<String, Object> root = new HashMap<>();
root.put("serverUrl", apiUrl);
root.put("faoWiews", faoWiewsUpdateError == null ? SUCCESS_MESSAGE : ERROR_MESSAGE + faoWiewsUpdateError);
root.put("countryIsoCodes", countryIsoCodesUpdateError == null ? SUCCESS_MESSAGE : ERROR_MESSAGE + countryIsoCodesUpdateError);
root.put("countryNames", countryNamesUpdateError == null ? SUCCESS_MESSAGE : ERROR_MESSAGE + countryNamesUpdateError);
root.put("ITPGRFACountryStatus", itpgrfaStatusUpdateError == null ? SUCCESS_MESSAGE : ERROR_MESSAGE + itpgrfaStatusUpdateError);
String body = article.getEntity().getBody();
String title = article.getEntity().getTitle();
String mailSubject = faoWiewsUpdateError == null && countryIsoCodesUpdateError == null && countryNamesUpdateError == null && itpgrfaStatusUpdateError == null
? title : "Warning: " + title;
String mailBody = contentService.processTemplate(body, root);
emailService.sendMail(mailSubject, mailBody, (String) null, "helpdesk@genesys-pgr.org");
} else {
log.warn("Global article not found: {}", ContentService.SMTP_AUTOMATIC_RESOURCE_UPDATE_NOTIFICATION);
}
}
}