Complete option default value and type feature

pull/146/head
johnniang 2019-04-25 02:21:46 +08:00
parent f893294076
commit 4819a9824f
17 changed files with 307 additions and 95 deletions

View File

@ -18,7 +18,6 @@ public class SheetVisitEventListener extends AbstractVisitEventListener {
@Async
@EventListener
public void onSheetVisitEvent(SheetVisitEvent event) throws InterruptedException {
handleVisitEvent(event);
}

View File

@ -1,6 +1,7 @@
package run.halo.app.model.params;
import lombok.Data;
import run.halo.app.model.enums.AttachmentType;
/**
* Attachment query params.
@ -18,5 +19,5 @@ public class AttachmentQuery {
private String mediaType;
private String attachmentType;
private AttachmentType attachmentType;
}

View File

@ -2,6 +2,7 @@ package run.halo.app.model.properties;
/**
* AliYun properties.
*
* @author MyFaith
* @date 2019-04-04 00:00:56
*/
@ -10,27 +11,31 @@ public enum AliYunProperties implements PropertyEnum {
/**
* Aliyun oss endpoint.
*/
OSS_ENDPOINT("oss_aliyun_endpoint", String.class),
OSS_ENDPOINT("oss_aliyun_endpoint", String.class, ""),
/**
* Aliyun oss bucket name.
*/
OSS_BUCKET_NAME("oss_aliyun_bucket_name", String.class),
OSS_BUCKET_NAME("oss_aliyun_bucket_name", String.class, ""),
/**
* Aliyun oss access key.
*/
OSS_ACCESS_KEY("oss_aliyun_access_key", String.class),
OSS_ACCESS_KEY("oss_aliyun_access_key", String.class, ""),
/**
* Aliyun oss access secret.
*/
OSS_ACCESS_SECRET("oss_aliyun_access_secret", String.class);
OSS_ACCESS_SECRET("oss_aliyun_access_secret", String.class, "");
private String value;
private Class<?> type;
private final String value;
AliYunProperties(String value, Class<?> type) {
private final Class<?> type;
private final String defaultValue;
AliYunProperties(String value, Class<?> type, String defaultValue) {
this.defaultValue = defaultValue;
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -44,6 +49,11 @@ public enum AliYunProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -1,6 +1,5 @@
package run.halo.app.model.properties;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.enums.AttachmentType;
/**
@ -11,15 +10,18 @@ import run.halo.app.model.enums.AttachmentType;
*/
public enum AttachmentProperties implements PropertyEnum {
ATTACHMENT_TYPE("attachment_type", AttachmentType.class);
ATTACHMENT_TYPE("attachment_type", AttachmentType.class, AttachmentType.LOCAL.name());
private final String value;
private final Class<?> type;
AttachmentProperties(String value, Class<?> type) {
private final String defaultValue;
AttachmentProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
@ -27,7 +29,15 @@ public enum AttachmentProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;
}}
}
}

View File

@ -9,38 +9,41 @@ public enum BlogProperties implements PropertyEnum {
/**
* Blog locale.
*/
BLOG_LOCALE("blog_locale", String.class),
BLOG_LOCALE("blog_locale", String.class, ""),
/**
* Blog title.
*/
BLOG_TITLE("blog_title", String.class),
BLOG_TITLE("blog_title", String.class, ""),
/**
* Blog logo.
*/
BLOG_LOGO("blog_logo", String.class),
BLOG_LOGO("blog_logo", String.class, ""),
/**
* Blog url.
*/
BLOG_URL("blog_url", String.class),
BLOG_URL("blog_url", String.class, ""),
/**
* Blog favicon.
*/
BLOG_FAVICON("blog_favicon", String.class),
BLOG_FAVICON("blog_favicon", String.class, ""),
/**
* Blog footer info.
*/
BLOG_FOOTER_INFO("blog_footer_info", String.class);
BLOG_FOOTER_INFO("blog_footer_info", String.class, "");
private String value;
private final String value;
private Class<?> type;
private final Class<?> type;
BlogProperties(String value, Class<?> type) {
private final String defaultValue;
BlogProperties(String value, Class<?> type, String defaultValue) {
this.defaultValue = defaultValue;
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -64,4 +67,9 @@ public enum BlogProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
}

View File

@ -8,31 +8,34 @@ package run.halo.app.model.properties;
*/
public enum CommentProperties implements PropertyEnum {
GAVATAR_DEFAULT("comment_gavatar_default", String.class),
GAVATAR_DEFAULT("comment_gavatar_default", String.class, ""),
NEW_NEED_CHECK("comment_new_need_check", Boolean.class),
NEW_NEED_CHECK("comment_new_need_check", Boolean.class, ""),
NEW_NOTICE("comment_new_notice", Boolean.class),
NEW_NOTICE("comment_new_notice", Boolean.class, ""),
PASS_NOTICE("comment_pass_notice", Boolean.class),
PASS_NOTICE("comment_pass_notice", Boolean.class, ""),
REPLY_NOTICE("comment_reply_notice", Boolean.class),
REPLY_NOTICE("comment_reply_notice", Boolean.class, ""),
API_ENABLED("comment_api_enabled", Boolean.class),
API_ENABLED("comment_api_enabled", Boolean.class, ""),
PAGE_SIZE("comment_page_size", Integer.class),
PAGE_SIZE("comment_page_size", Integer.class, ""),
CONTENT_PLACEHOLDER("comment_content_placeholder", String.class),
CONTENT_PLACEHOLDER("comment_content_placeholder", String.class, ""),
CUSTOM_STYLE("comment_custom_style", String.class);
CUSTOM_STYLE("comment_custom_style", String.class, "");
private final String value;
private final Class<?> type;
CommentProperties(String value, Class<?> type) {
private final String defaultValue;
CommentProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
@ -40,6 +43,11 @@ public enum CommentProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -8,25 +8,28 @@ package run.halo.app.model.properties;
*/
public enum EmailProperties implements PropertyEnum {
HOST("email_host", String.class),
HOST("email_host", String.class, ""),
PROTOCOL("email_protocol", String.class),
PROTOCOL("email_protocol", String.class, ""),
SSL_PORT("email_ssl_port", Integer.class),
SSL_PORT("email_ssl_port", Integer.class, ""),
USERNAME("email_username", String.class),
USERNAME("email_username", String.class, ""),
PASSWORD("email_password", String.class),
PASSWORD("email_password", String.class, ""),
FROM_NAME("email_from_name", String.class),
FROM_NAME("email_from_name", String.class, ""),
ENABLED("email_enabled", Boolean.class);
ENABLED("email_enabled", Boolean.class, "");
private final String value;
private final Class<?> type;
EmailProperties(String value, Class<?> type) {
private final String defaultValue;
EmailProperties(String value, Class<?> type, String defaultValue) {
this.defaultValue = defaultValue;
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -40,7 +43,13 @@ public enum EmailProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;
}}
}
}

View File

@ -8,20 +8,22 @@ package run.halo.app.model.properties;
*/
public enum OtherProperties implements PropertyEnum {
API_ENABLED("api_enabled", Boolean.class),
API_ENABLED("api_enabled", Boolean.class, ""),
API_TOKEN("api_token", String.class),
API_TOKEN("api_token", String.class, ""),
STATISTICS_CODE("statistics_code", String.class),
;
STATISTICS_CODE("statistics_code", String.class, "");
private final String value;
private final Class<?> type;
OtherProperties(String value, Class<?> type) {
private final String defaultValue;
OtherProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
@ -29,6 +31,11 @@ public enum OtherProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -1,33 +1,41 @@
package run.halo.app.model.properties;
/**
* Post properties.
*
* @author johnniang
* @date 4/1/19
*/
public enum PostProperties implements PropertyEnum {
SUMMARY_LENGTH("post_summary_length", Integer.class),
SUMMARY_LENGTH("post_summary_length", Integer.class, ""),
RSS_PAGE_SIZE("rss_page_size", Integer.class),
RSS_PAGE_SIZE("rss_page_size", Integer.class, "20"),
INDEX_PAGE_SIZE("post_index_page_size", Integer.class),
;
INDEX_PAGE_SIZE("post_index_page_size", Integer.class, "10");
private final String value;
private final Class<?> type;
PostProperties(String value, Class<?> type) {
private final String defaultValue;
PostProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
public Class<?> getType() {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -8,20 +8,23 @@ package run.halo.app.model.properties;
*/
public enum PrimaryProperties implements PropertyEnum {
IS_INSTALLED("is_installed", Boolean.class),
IS_INSTALLED("is_installed", Boolean.class, ""),
THEME("theme", String.class),
THEME("theme", String.class, ""),
BIRTHDAY("birthday", Long.class),
BIRTHDAY("birthday", Long.class, ""),
;
private final String value;
private final Class<?> type;
PrimaryProperties(String value, Class<?> type) {
private final String defaultValue;
PrimaryProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
@ -29,7 +32,13 @@ public enum PrimaryProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;
}}
}
}

View File

@ -1,10 +1,17 @@
package run.halo.app.model.properties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import run.halo.app.model.enums.ValueEnum;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Property enum.
*
@ -16,18 +23,15 @@ public interface PropertyEnum extends ValueEnum<String> {
/**
* Converts to value with corresponding type
*
* @param value string value must not be null
* @param value string value must not be blank
* @param type property value type must not be null
* @param <T> property value type
* @return property value
*/
@SuppressWarnings("unchecked")
static <T> T convertTo(@NonNull String value, @NonNull Class<T> type) {
Assert.hasText(value, "Property value must not be blank");
if (!isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
Assert.hasText(value, "Value must not be null");
Assert.notNull(type, "Type must not be null");
if (type.isAssignableFrom(String.class)) {
return (T) value;
@ -65,6 +69,36 @@ public interface PropertyEnum extends ValueEnum<String> {
throw new UnsupportedOperationException("Unsupported convention for blog property type:" + type.getName() + " provided");
}
/**
* Converts to value with corresponding type
*
* @param value value
* @param propertyEnum property enum must not be null
* @return property value
*/
@SuppressWarnings("unchecked")
static Object convertTo(@Nullable String value, @NonNull PropertyEnum propertyEnum) {
Assert.notNull(propertyEnum, "Property enum must not be null");
if (StringUtils.isBlank(value)) {
// Set default value
value = propertyEnum.defaultValue();
}
try {
if (propertyEnum.getType().isAssignableFrom(Enum.class)) {
Class<Enum> type = (Class<Enum>) propertyEnum.getType();
Enum result = convertToEnum(value, type);
return result != null ? result : value;
}
return convertTo(value, propertyEnum.getType());
} catch (Exception e) {
// Return value
return value;
}
}
/**
* Converts to enum.
*
@ -107,10 +141,45 @@ public interface PropertyEnum extends ValueEnum<String> {
);
}
static Map<String, PropertyEnum> getValuePropertyEnumMap() {
// Get all properties
List<Class<? extends PropertyEnum>> propertyEnumClasses = new LinkedList<>();
propertyEnumClasses.add(AliYunProperties.class);
propertyEnumClasses.add(AttachmentProperties.class);
propertyEnumClasses.add(BlogProperties.class);
propertyEnumClasses.add(CommentProperties.class);
propertyEnumClasses.add(EmailProperties.class);
propertyEnumClasses.add(OtherProperties.class);
propertyEnumClasses.add(PostProperties.class);
propertyEnumClasses.add(PrimaryProperties.class);
propertyEnumClasses.add(QnYunProperties.class);
propertyEnumClasses.add(SeoProperties.class);
propertyEnumClasses.add(UpYunProperties.class);
Map<String, PropertyEnum> result = new HashMap<>();
propertyEnumClasses.forEach(propertyEnumClass -> {
PropertyEnum[] propertyEnums = propertyEnumClass.getEnumConstants();
for (PropertyEnum propertyEnum : propertyEnums) {
result.put(propertyEnum.getValue(), propertyEnum);
}
});
return result;
}
/**
* Get property type.
*
* @return property type
*/
Class<?> getType();
/**
* Default value.
*
* @return default value
*/
String defaultValue();
}

View File

@ -8,23 +8,26 @@ package run.halo.app.model.properties;
*/
public enum QnYunProperties implements PropertyEnum {
ZONE("oss_qiniu_zone", String.class),
ZONE("oss_qiniu_zone", String.class, ""),
ACCESS_KEY("oss_qiniu_access_key", String.class),
ACCESS_KEY("oss_qiniu_access_key", String.class, ""),
SECRET_KEY("oss_qiniu_secret_key", String.class),
SECRET_KEY("oss_qiniu_secret_key", String.class, ""),
DOMAIN("oss_qiniu_domain", String.class),
DOMAIN("oss_qiniu_domain", String.class, ""),
BUCKET("oss_qiniu_bucket", String.class),
BUCKET("oss_qiniu_bucket", String.class, ""),
SMALL_URL("oss_qiniu_small_url", String.class);
SMALL_URL("oss_qiniu_small_url", String.class, "");
private final String value;
private final Class<?> type;
QnYunProperties(String value, Class<?> type) {
private final String defaultValue;
QnYunProperties(String value, Class<?> type, String defaultValue) {
this.defaultValue = defaultValue;
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -43,4 +46,9 @@ public enum QnYunProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
}

View File

@ -8,27 +8,30 @@ package run.halo.app.model.properties;
*/
public enum SeoProperties implements PropertyEnum {
KEYWORDS("seo_keywords", String.class),
KEYWORDS("seo_keywords", String.class, ""),
DESCRIPTION("seo_description", String.class),
DESCRIPTION("seo_description", String.class, ""),
BAIDU_TOKEN("seo_baidu_token", String.class),
BAIDU_TOKEN("seo_baidu_token", String.class, ""),
VERIFICATION_BAIDU("seo_verification_baidu", String.class),
VERIFICATION_BAIDU("seo_verification_baidu", String.class, ""),
VERIFICATION_GOOGLE("seo_verification_google", String.class),
VERIFICATION_GOOGLE("seo_verification_google", String.class, ""),
VERIFICATION_BING("seo_verification_bing", String.class),
VERIFICATION_BING("seo_verification_bing", String.class, ""),
VERIFICATION_QIHU("seo_verification_qihu", String.class);
VERIFICATION_QIHU("seo_verification_qihu", String.class, "");
private final String value;
private final Class<?> type;
SeoProperties(String value, Class<?> type) {
private final String defaultValue;
SeoProperties(String value, Class<?> type, String defaultValue) {
this.value = value;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
@ -36,6 +39,11 @@ public enum SeoProperties implements PropertyEnum {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -8,23 +8,26 @@ package run.halo.app.model.properties;
*/
public enum UpYunProperties implements PropertyEnum {
OSS_SOURCE("oss_upyun_source", String.class),
OSS_SOURCE("oss_upyun_source", String.class, ""),
OSS_PASSWORD("oss_upyun_password", String.class),
OSS_PASSWORD("oss_upyun_password", String.class, ""),
OSS_BUCKET("oss_upyun_bucket", String.class),
OSS_BUCKET("oss_upyun_bucket", String.class, ""),
OSS_DOMAIN("oss_upyun_domain", String.class),
OSS_DOMAIN("oss_upyun_domain", String.class, ""),
OSS_OPERATOR("oss_upyun_operator", String.class),
OSS_OPERATOR("oss_upyun_operator", String.class, ""),
OSS_SMALL_URL("oss_upyun_small_url", String.class);
OSS_SMALL_URL("oss_upyun_small_url", String.class, "");
private String value;
private Class<?> type;
UpYunProperties(String value, Class<?> type) {
private final String defaultValue;
UpYunProperties(String value, Class<?> type, String defaultValue) {
this.defaultValue = defaultValue;
if (!PropertyEnum.isSupportedType(type)) {
throw new IllegalArgumentException("Unsupported blog property type: " + type);
}
@ -33,12 +36,16 @@ public enum UpYunProperties implements PropertyEnum {
this.type = type;
}
@Override
public Class<?> getType() {
return type;
}
@Override
public String defaultValue() {
return defaultValue;
}
@Override
public String getValue() {
return value;

View File

@ -15,7 +15,6 @@ import run.halo.app.model.entity.Attachment;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.params.AttachmentQuery;
import run.halo.app.model.properties.AttachmentProperties;
import run.halo.app.model.properties.PropertyEnum;
import run.halo.app.model.support.UploadResult;
import run.halo.app.repository.AttachmentRepository;
import run.halo.app.service.AttachmentService;
@ -74,8 +73,8 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
predicates.add(criteriaBuilder.equal(root.get("mediaType"), attachmentQuery.getMediaType()));
}
if(attachmentQuery.getAttachmentType() != null){
predicates.add(criteriaBuilder.equal(root.get("type"), PropertyEnum.convertToEnum(attachmentQuery.getAttachmentType(),AttachmentType.class)));
if (attachmentQuery.getAttachmentType() != null) {
predicates.add(criteriaBuilder.equal(root.get("type"), attachmentQuery.getAttachmentType()));
}
if (attachmentQuery.getKeyword() != null) {

View File

@ -22,10 +22,7 @@ import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.utils.HaloUtils;
import run.halo.app.utils.ServiceUtils;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -44,6 +41,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
private final StringCacheStore cacheStore;
private final Map<String, PropertyEnum> propertyEnumMap;
public OptionServiceImpl(OptionRepository optionRepository,
ApplicationContext applicationContext,
StringCacheStore cacheStore) {
@ -51,6 +50,8 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
this.optionRepository = optionRepository;
this.applicationContext = applicationContext;
this.cacheStore = cacheStore;
propertyEnumMap = Collections.unmodifiableMap(PropertyEnum.getValuePropertyEnumMap());
}
@Override
@ -125,7 +126,37 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public Map<String, Object> listOptions() {
return ServiceUtils.convertToMap(listAll(), Option::getKey, Option::getValue);
List<Option> options = listAll();
Set<String> keys = ServiceUtils.fetchProperty(options, Option::getKey);
Map<String, Object> result = ServiceUtils.convertToMap(options, Option::getKey, option -> {
String key = option.getKey();
PropertyEnum propertyEnum = propertyEnumMap.get(key);
if (propertyEnum == null) {
return option.getValue();
}
return PropertyEnum.convertTo(option.getValue(), propertyEnum);
});
// Add default property
propertyEnumMap.keySet()
.stream()
.filter(key -> !keys.contains(key))
.forEach(key -> {
PropertyEnum propertyEnum = propertyEnumMap.get(key);
if (StringUtils.isBlank(propertyEnum.defaultValue())) {
return;
}
result.put(key, PropertyEnum.convertTo(propertyEnum.defaultValue(), propertyEnum));
});
return result;
}
@Override

View File

@ -0,0 +1,21 @@
package run.halo.app.model.properties;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.util.Map;
/**
* @author johnniang
* @date 19-4-25
*/
@Slf4j
public class PropertyEnumTest {
@Test
public void getValuePropertyMapTest() {
Map<String, PropertyEnum> result = PropertyEnum.getValuePropertyEnumMap();
log.debug(result.toString());
}
}