mirror of https://github.com/halo-dev/halo
Complete option updated event
parent
63d072776e
commit
98a6fc9e75
|
@ -1,4 +1,4 @@
|
||||||
package run.halo.app.event.theme;
|
package run.halo.app.event.freemarker;
|
||||||
|
|
||||||
import freemarker.template.Configuration;
|
import freemarker.template.Configuration;
|
||||||
import freemarker.template.TemplateModelException;
|
import freemarker.template.TemplateModelException;
|
||||||
|
@ -9,6 +9,8 @@ import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Component;
|
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.event.user.UserUpdatedEvent;
|
||||||
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
|
@ -76,11 +78,19 @@ public class FreemarkerConfigAwareListener {
|
||||||
@Async
|
@Async
|
||||||
@EventListener
|
@EventListener
|
||||||
public void onUserUpdate(UserUpdatedEvent event) throws TemplateModelException {
|
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();
|
loadUserConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Async
|
||||||
|
@EventListener
|
||||||
|
public void onOptionUpdate(OptionUpdatedEvent event) throws TemplateModelException {
|
||||||
|
log.debug("Received option updated event");
|
||||||
|
|
||||||
|
loadOptionsConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void loadUserConfig() throws TemplateModelException {
|
private void loadUserConfig() throws TemplateModelException {
|
||||||
configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null));
|
configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null));
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,10 +5,12 @@ import com.qiniu.common.Zone;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import run.halo.app.cache.StringCacheStore;
|
import run.halo.app.cache.StringCacheStore;
|
||||||
|
import run.halo.app.event.options.OptionUpdatedEvent;
|
||||||
import run.halo.app.exception.MissingPropertyException;
|
import run.halo.app.exception.MissingPropertyException;
|
||||||
import run.halo.app.model.dto.OptionDTO;
|
import run.halo.app.model.dto.OptionDTO;
|
||||||
import run.halo.app.model.entity.Option;
|
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 Map<String, PropertyEnum> propertyEnumMap;
|
||||||
|
|
||||||
|
private final ApplicationEventPublisher eventPublisher;
|
||||||
|
|
||||||
public OptionServiceImpl(OptionRepository optionRepository,
|
public OptionServiceImpl(OptionRepository optionRepository,
|
||||||
ApplicationContext applicationContext,
|
ApplicationContext applicationContext,
|
||||||
StringCacheStore cacheStore) {
|
StringCacheStore cacheStore,
|
||||||
|
ApplicationEventPublisher eventPublisher) {
|
||||||
super(optionRepository);
|
super(optionRepository);
|
||||||
this.optionRepository = optionRepository;
|
this.optionRepository = optionRepository;
|
||||||
this.applicationContext = applicationContext;
|
this.applicationContext = applicationContext;
|
||||||
this.cacheStore = cacheStore;
|
this.cacheStore = cacheStore;
|
||||||
|
this.eventPublisher = eventPublisher;
|
||||||
|
|
||||||
propertyEnumMap = Collections.unmodifiableMap(PropertyEnum.getValuePropertyEnumMap());
|
propertyEnumMap = Collections.unmodifiableMap(PropertyEnum.getValuePropertyEnumMap());
|
||||||
}
|
}
|
||||||
|
@ -96,6 +102,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
|
|
||||||
// TODO Optimize the queries
|
// TODO Optimize the queries
|
||||||
options.forEach(this::save);
|
options.forEach(this::save);
|
||||||
|
|
||||||
|
publishEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -106,6 +114,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
|
|
||||||
// TODO Optimize the query
|
// TODO Optimize the query
|
||||||
optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue()));
|
optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue()));
|
||||||
|
|
||||||
|
publishEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -113,6 +123,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
Assert.notNull(property, "Property must not be null");
|
Assert.notNull(property, "Property must not be null");
|
||||||
|
|
||||||
save(property.getValue(), value);
|
save(property.getValue(), value);
|
||||||
|
|
||||||
|
publishEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -122,6 +134,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
}
|
}
|
||||||
|
|
||||||
properties.forEach((property, value) -> save(property.getValue(), value));
|
properties.forEach((property, value) -> save(property.getValue(), value));
|
||||||
|
|
||||||
|
publishEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -164,11 +178,11 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> listByKeys(String params) {
|
public Map<String, Object> listByKeys(String params) {
|
||||||
Assert.notNull(params, "Keys must not be null");
|
Assert.notNull(params, "Keys must not be null");
|
||||||
Map<String,Object> options = listOptions();
|
Map<String, Object> options = listOptions();
|
||||||
Map<String,Object> result = new HashMap<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
String[] keysParam = params.split(",");
|
String[] keysParam = params.split(",");
|
||||||
for (String key : keysParam) {
|
for (String key : keysParam) {
|
||||||
result.put(key,options.get(key));
|
result.put(key, options.get(key));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -342,4 +356,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
|
|
||||||
return blogUrl;
|
return blogUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void publishEvent() {
|
||||||
|
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue