diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java index 05b1e7fb..1fddc7be 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsAPIServiceImpl.java @@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import ch.ethz.seb.sebserver.gbl.Constants; @@ -73,6 +74,23 @@ public class LmsAPIServiceImpl implements LmsAPIService { this.templateFactories = new EnumMap<>(factories); } + /** Listen to LmsSetupChangeEvent to release an affected LmsAPITemplate from cache + * + * @param event the event holding the changed LmsSetup */ + @EventListener + public void notifyLmsSetupChange(final LmsSetupChangeEvent event) { + final LmsSetup lmsSetup = event.getLmsSetup(); + if (lmsSetup == null) { + return; + } + + if (log.isDebugEnabled()) { + log.debug("LmsSetup changed. Update cache by removing eventually used references"); + } + + this.cache.remove(new CacheKey(lmsSetup.getModelId(), 0)); + } + @Override public void cleanup() { this.cache.clear(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsSetupChangeEvent.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsSetupChangeEvent.java new file mode 100644 index 00000000..e3b26315 --- /dev/null +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/LmsSetupChangeEvent.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl; + +import org.springframework.context.ApplicationEvent; + +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup; + +public class LmsSetupChangeEvent extends ApplicationEvent { + + private static final long serialVersionUID = -7239994198026689531L; + + public LmsSetupChangeEvent(final LmsSetup source) { + super(source); + } + + public LmsSetup getLmsSetup() { + return (LmsSetup) this.source; + } + +} diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java index 5ec15070..51934319 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java @@ -30,6 +30,7 @@ import ch.ethz.seb.sebserver.gbl.model.Entity; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup; import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetupTestResult; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; +import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.LmsSetupRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.servicelayer.PaginationService; import ch.ethz.seb.sebserver.webservice.servicelayer.authorization.AuthorizationService; @@ -38,6 +39,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionServic import ch.ethz.seb.sebserver.webservice.servicelayer.dao.LmsSetupDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.UserActivityLogDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.lms.LmsAPIService; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.LmsSetupChangeEvent; import ch.ethz.seb.sebserver.webservice.servicelayer.validation.BeanValidationService; @WebServiceProfile @@ -132,4 +134,10 @@ public class LmsSetupController extends ActivatableEntityController notifySaved(final LmsSetup entity) { + this.applicationEventPublisher.publishEvent(new LmsSetupChangeEvent(entity)); + return super.notifySaved(entity); + } + }