From 98a6fc9e75fb618307a3e86b75e96014957b678a Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 29 Apr 2019 00:55:10 +0800 Subject: [PATCH] Complete option updated event --- .../FreemarkerConfigAwareListener.java | 14 ++++++++-- .../app/event/options/OptionUpdatedEvent.java | 21 +++++++++++++++ .../app/service/impl/OptionServiceImpl.java | 26 ++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) rename src/main/java/run/halo/app/event/{theme => freemarker}/FreemarkerConfigAwareListener.java (88%) create mode 100644 src/main/java/run/halo/app/event/options/OptionUpdatedEvent.java diff --git a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java similarity index 88% rename from src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java rename to src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java index f5de0ac22..a8147b814 100644 --- a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/freemarker/FreemarkerConfigAwareListener.java @@ -1,4 +1,4 @@ -package run.halo.app.event.theme; +package run.halo.app.event.freemarker; import freemarker.template.Configuration; import freemarker.template.TemplateModelException; @@ -9,6 +9,8 @@ import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import run.halo.app.event.options.OptionUpdatedEvent; +import run.halo.app.event.theme.ThemeActivatedEvent; import run.halo.app.event.user.UserUpdatedEvent; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.service.OptionService; @@ -76,11 +78,19 @@ public class FreemarkerConfigAwareListener { @Async @EventListener public void onUserUpdate(UserUpdatedEvent event) throws TemplateModelException { - log.debug("Received user update event, user id: [{}]", event.getUserId()); + log.debug("Received user updated event, user id: [{}]", event.getUserId()); loadUserConfig(); } + @Async + @EventListener + public void onOptionUpdate(OptionUpdatedEvent event) throws TemplateModelException { + log.debug("Received option updated event"); + + loadOptionsConfig(); + } + private void loadUserConfig() throws TemplateModelException { configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); diff --git a/src/main/java/run/halo/app/event/options/OptionUpdatedEvent.java b/src/main/java/run/halo/app/event/options/OptionUpdatedEvent.java new file mode 100644 index 000000000..5ec7029a8 --- /dev/null +++ b/src/main/java/run/halo/app/event/options/OptionUpdatedEvent.java @@ -0,0 +1,21 @@ +package run.halo.app.event.options; + +import org.springframework.context.ApplicationEvent; + +/** + * Option updated event. + * + * @author johnniang + * @date 19-4-29 + */ +public class OptionUpdatedEvent extends ApplicationEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public OptionUpdatedEvent(Object source) { + super(source); + } +} diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index 9cffe0dcf..7fca348a4 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -5,10 +5,12 @@ import com.qiniu.common.Zone; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import run.halo.app.cache.StringCacheStore; +import run.halo.app.event.options.OptionUpdatedEvent; import run.halo.app.exception.MissingPropertyException; import run.halo.app.model.dto.OptionDTO; import run.halo.app.model.entity.Option; @@ -43,13 +45,17 @@ public class OptionServiceImpl extends AbstractCrudService impl private final Map propertyEnumMap; + private final ApplicationEventPublisher eventPublisher; + public OptionServiceImpl(OptionRepository optionRepository, ApplicationContext applicationContext, - StringCacheStore cacheStore) { + StringCacheStore cacheStore, + ApplicationEventPublisher eventPublisher) { super(optionRepository); this.optionRepository = optionRepository; this.applicationContext = applicationContext; this.cacheStore = cacheStore; + this.eventPublisher = eventPublisher; propertyEnumMap = Collections.unmodifiableMap(PropertyEnum.getValuePropertyEnumMap()); } @@ -96,6 +102,8 @@ public class OptionServiceImpl extends AbstractCrudService impl // TODO Optimize the queries options.forEach(this::save); + + publishEvent(); } @Override @@ -106,6 +114,8 @@ public class OptionServiceImpl extends AbstractCrudService impl // TODO Optimize the query optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue())); + + publishEvent(); } @Override @@ -113,6 +123,8 @@ public class OptionServiceImpl extends AbstractCrudService impl Assert.notNull(property, "Property must not be null"); save(property.getValue(), value); + + publishEvent(); } @Override @@ -122,6 +134,8 @@ public class OptionServiceImpl extends AbstractCrudService impl } properties.forEach((property, value) -> save(property.getValue(), value)); + + publishEvent(); } @Override @@ -164,11 +178,11 @@ public class OptionServiceImpl extends AbstractCrudService impl @Override public Map listByKeys(String params) { Assert.notNull(params, "Keys must not be null"); - Map options = listOptions(); - Map result = new HashMap<>(); + Map options = listOptions(); + Map result = new HashMap<>(); String[] keysParam = params.split(","); for (String key : keysParam) { - result.put(key,options.get(key)); + result.put(key, options.get(key)); } return result; } @@ -342,4 +356,8 @@ public class OptionServiceImpl extends AbstractCrudService impl return blogUrl; } + + private void publishEvent() { + eventPublisher.publishEvent(new OptionUpdatedEvent(this)); + } }