Complete option updated event

pull/146/head
johnniang 2019-04-29 00:55:10 +08:00
parent 63d072776e
commit 98a6fc9e75
3 changed files with 55 additions and 6 deletions

View File

@ -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));

View File

@ -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);
}
}

View File

@ -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<Option, Integer> impl
private final Map<String, PropertyEnum> 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<Option, Integer> impl
// TODO Optimize the queries
options.forEach(this::save);
publishEvent();
}
@Override
@ -106,6 +114,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
// TODO Optimize the query
optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue()));
publishEvent();
}
@Override
@ -113,6 +123,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
Assert.notNull(property, "Property must not be null");
save(property.getValue(), value);
publishEvent();
}
@Override
@ -122,6 +134,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
}
properties.forEach((property, value) -> save(property.getValue(), value));
publishEvent();
}
@Override
@ -164,11 +178,11 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public Map<String, Object> listByKeys(String params) {
Assert.notNull(params, "Keys must not be null");
Map<String,Object> options = listOptions();
Map<String,Object> result = new HashMap<>();
Map<String, Object> options = listOptions();
Map<String, Object> 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<Option, Integer> impl
return blogUrl;
}
private void publishEvent() {
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
}
}