Refactor option service

pull/146/head
johnniang 2019-04-29 02:59:17 +08:00
parent aabc665ba0
commit 26c0192517
12 changed files with 92 additions and 83 deletions

View File

@ -79,7 +79,7 @@ public class InstallController {
public BaseResponse<String> installBlog(@RequestBody @Valid InstallParam installParam) {
// TODO Install blog.
// Check is installed
boolean isInstalled = Boolean.parseBoolean(optionService.getByProperty(PrimaryProperties.IS_INSTALLED).orElse(Boolean.FALSE.toString()));
boolean isInstalled = optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);
if (isInstalled) {
// TODO i18n
@ -172,7 +172,7 @@ public class InstallController {
properties.put(CommentProperties.NEW_NOTICE, Boolean.FALSE.toString());
properties.put(CommentProperties.PASS_NOTICE, Boolean.FALSE.toString());
properties.put(CommentProperties.REPLY_NOTICE, Boolean.FALSE.toString());
properties.put(AttachmentProperties.ATTACHMENT_TYPE, AttachmentType.LOCAL.getValue().toString());
properties.put(AttachmentProperties.ATTACHMENT_TYPE, AttachmentType.LOCAL.name());
// Create properties
optionService.saveProperties(properties);

View File

@ -1,14 +1,12 @@
package run.halo.app.controller.content.api;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import run.halo.app.model.dto.OptionDTO;
import run.halo.app.model.support.BaseResponse;
import run.halo.app.service.OptionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@ -36,14 +34,18 @@ public class OptionController {
}
@GetMapping("map_view")
@ApiOperation("Lists all options with map view")
public Map<String, Object> listAllWithMapView() {
return optionService.listOptions();
@ApiOperation("Lists options with map view")
public Map<String, Object> listAllWithMapView(@RequestParam(value = "key", required = false) List<String> keys) {
if (CollectionUtils.isEmpty(keys)) {
return optionService.listOptions();
}
return optionService.listOptions(keys);
}
@GetMapping("keys/{key}")
@ApiOperation("Gets option value by option key")
public BaseResponse<String> getBy(@PathVariable("key") String key) {
return BaseResponse.ok(HttpStatus.OK.getReasonPhrase(), optionService.getByKey(key).orElse(""));
public BaseResponse<Object> getBy(@PathVariable("key") String key) {
return BaseResponse.ok(HttpStatus.OK.getReasonPhrase(), optionService.getByKey(key).orElse(null));
}
}

View File

@ -67,7 +67,7 @@ public class CommentEventListener {
Map<String, Object> data = new HashMap<>();
StrBuilder url = new StrBuilder(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL))
StrBuilder url = new StrBuilder(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL).toString())
.append("/archives/")
.append(post.getUrl());
data.put("url", url.toString());

View File

@ -41,10 +41,10 @@ public class AliYunFileHandler implements FileHandler {
Assert.notNull(file, "Multipart file must not be null");
// Get config
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT);
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY);
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET);
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME);
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
// Init OSS client
@ -100,10 +100,10 @@ public class AliYunFileHandler implements FileHandler {
Assert.notNull(key, "File key must not be blank");
// Get config
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT);
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY);
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET);
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME);
String ossEndPoint = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ENDPOINT).toString();
String ossAccessKey = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_KEY).toString();
String ossAccessSecret = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_ACCESS_SECRET).toString();
String ossBucketName = optionService.getByPropertyOfNonNull(AliYunProperties.OSS_BUCKET_NAME).toString();
String ossSource = StringUtils.join("https://", ossBucketName, "." + ossEndPoint);
// Init OSS client

View File

@ -1,13 +1,5 @@
package run.halo.app.handler.file;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.properties.QnYunProperties;
import run.halo.app.model.support.QiNiuPutSet;
import run.halo.app.model.support.UploadResult;
import run.halo.app.service.OptionService;
import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.JsonUtils;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
@ -23,6 +15,14 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.properties.QnYunProperties;
import run.halo.app.model.support.QiNiuPutSet;
import run.halo.app.model.support.UploadResult;
import run.halo.app.service.OptionService;
import run.halo.app.utils.FilenameUtils;
import run.halo.app.utils.JsonUtils;
import java.io.IOException;
import java.nio.file.Path;
@ -53,11 +53,11 @@ public class QnYunFileHandler implements FileHandler {
// Get all config
Zone zone = optionService.getQnYunZone();
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.ACCESS_KEY);
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.SECRET_KEY);
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.BUCKET);
String domain = optionService.getByPropertyOfNonNull(QnYunProperties.DOMAIN);
String smallUrl = optionService.getByPropertyOfNullable(QnYunProperties.SMALL_URL);
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.SECRET_KEY).toString();
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.BUCKET).toString();
String domain = optionService.getByPropertyOfNonNull(QnYunProperties.DOMAIN).toString();
String smallUrl = optionService.getByPropertyOrDefault(QnYunProperties.SMALL_URL, String.class, "");
// TODO Consider to cache the configuration
// Create configuration
@ -132,9 +132,9 @@ public class QnYunFileHandler implements FileHandler {
// Get all config
Zone zone = optionService.getQnYunZone();
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.ACCESS_KEY);
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.SECRET_KEY);
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.BUCKET);
String accessKey = optionService.getByPropertyOfNonNull(QnYunProperties.ACCESS_KEY).toString();
String secretKey = optionService.getByPropertyOfNonNull(QnYunProperties.SECRET_KEY).toString();
String bucket = optionService.getByPropertyOfNonNull(QnYunProperties.BUCKET).toString();
// TODO Consider to cache the configuration
// Create configuration

View File

@ -1,11 +1,5 @@
package run.halo.app.handler.file;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.properties.UpYunProperties;
import run.halo.app.model.support.UploadResult;
import run.halo.app.service.OptionService;
import run.halo.app.utils.FilenameUtils;
import com.UpYun;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -14,6 +8,12 @@ import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.properties.UpYunProperties;
import run.halo.app.model.support.UploadResult;
import run.halo.app.service.OptionService;
import run.halo.app.utils.FilenameUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@ -39,13 +39,13 @@ public class UpYunFileHandler implements FileHandler {
public UploadResult upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE);
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD);
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET);
String ossDomain = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_DOMAIN);
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR);
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE).toString();
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
String ossDomain = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_DOMAIN).toString();
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
// small url can be null
String ossSmallUrl = optionService.getByPropertyOfNullable(UpYunProperties.OSS_SMALL_URL);
String ossSmallUrl = optionService.getByPropertyOrDefault(UpYunProperties.OSS_SMALL_URL, String.class, "");
// Create up yun
UpYun upYun = new UpYun(ossBucket, ossOperator, ossPassword);
@ -101,10 +101,10 @@ public class UpYunFileHandler implements FileHandler {
Assert.notNull(key, "File key must not be blank");
// Get config
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE);
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD);
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET);
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR);
String ossSource = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_SOURCE).toString();
String ossPassword = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_PASSWORD).toString();
String ossBucket = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_BUCKET).toString();
String ossOperator = optionService.getByPropertyOfNonNull(UpYunProperties.OSS_OPERATOR).toString();
// Create up yun
UpYun upYun = new UpYun(ossBucket, ossOperator, ossPassword);

View File

@ -1,6 +1,8 @@
package run.halo.app.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.Option;
@ -11,10 +13,12 @@ import run.halo.app.model.entity.Option;
* @date 3/20/19
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OptionDTO implements OutputConverter<OptionDTO, Option> {
private String key;
private String value;
private Object value;
}

View File

@ -101,7 +101,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return option value or null
*/
@Nullable
String getByKeyOfNullable(@NonNull String key);
Object getByKeyOfNullable(@NonNull String key);
/**
* Gets option value of non null.
@ -110,7 +110,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return option value of non null
*/
@NonNull
String getByKeyOfNonNull(@NonNull String key);
Object getByKeyOfNonNull(@NonNull String key);
/**
* Get option by key
@ -119,7 +119,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return an optional option value
*/
@NonNull
Optional<String> getByKey(@NonNull String key);
Optional<Object> getByKey(@NonNull String key);
/**
* Gets option value by blog property.
@ -128,7 +128,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return an option value
*/
@Nullable
String getByPropertyOfNullable(@NonNull PropertyEnum property);
Object getByPropertyOfNullable(@NonNull PropertyEnum property);
/**
* Gets option value by blog property.
@ -138,7 +138,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @throws MissingPropertyException throws when property value dismisses
*/
@NonNull
String getByPropertyOfNonNull(@NonNull PropertyEnum property);
Object getByPropertyOfNonNull(@NonNull PropertyEnum property);
/**
* Gets option value by blog property.
@ -147,7 +147,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @return an optional option value
*/
@NonNull
Optional<String> getByProperty(@NonNull PropertyEnum property);
Optional<Object> getByProperty(@NonNull PropertyEnum property);
/**
* Gets property value by blog property.

View File

@ -256,7 +256,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment> extend
User user = authentication.getDetail().getUser();
commentParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
commentParam.setEmail(user.getEmail());
commentParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
commentParam.setAuthorUrl(optionService.getByPropertyOrDefault(BlogProperties.BLOG_URL, String.class, null));
}
// Validate the comment param manually

View File

@ -62,7 +62,7 @@ public class MailServiceImpl implements MailService {
public void sendMail(String to, String subject, String content) {
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME).toString();
try {
OhMyEmail.subject(subject)
@ -89,7 +89,7 @@ public class MailServiceImpl implements MailService {
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME).toString();
try {
StrBuilder text = new StrBuilder();
@ -120,7 +120,7 @@ public class MailServiceImpl implements MailService {
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachFilename) {
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME).toString();
File file = new File(attachFilename);
try {
@ -149,11 +149,11 @@ public class MailServiceImpl implements MailService {
// Get default properties
Properties defaultProperties = OhMyEmail.defaultConfig(log.isDebugEnabled());
// Set smtp host
defaultProperties.setProperty("mail.smtp.host", optionService.getByPropertyOfNonNull(EmailProperties.HOST));
defaultProperties.setProperty("mail.smtp.host", optionService.getByPropertyOfNonNull(EmailProperties.HOST).toString());
// Config email
OhMyEmail.config(defaultProperties,
optionService.getByPropertyOfNonNull(EmailProperties.USERNAME),
optionService.getByPropertyOfNonNull(EmailProperties.PASSWORD));
optionService.getByPropertyOfNonNull(EmailProperties.USERNAME).toString(),
optionService.getByPropertyOfNonNull(EmailProperties.PASSWORD).toString());
// Set config loaded with true
loaded = true;

View File

@ -24,7 +24,6 @@ import run.halo.app.utils.HaloUtils;
import run.halo.app.utils.ServiceUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* OptionService implementation class
@ -209,40 +208,44 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public List<OptionDTO> listDtos() {
return listAll().stream().map(option -> new OptionDTO().<OptionDTO>convertFrom(option)).collect(Collectors.toList());
List<OptionDTO> result = new LinkedList<>();
listOptions().forEach((key, value) -> result.add(new OptionDTO(key, value)));
return result;
}
@Override
public String getByKeyOfNullable(String key) {
public Object getByKeyOfNullable(String key) {
return getByKey(key).orElse(null);
}
@Override
public String getByKeyOfNonNull(String key) {
public Object getByKeyOfNonNull(String key) {
return getByKey(key).orElseThrow(() -> new MissingPropertyException("You have to config " + key + " setting"));
}
@Override
public Optional<String> getByKey(String key) {
public Optional<Object> getByKey(String key) {
Assert.hasText(key, "Option key must not be blank");
return optionRepository.findByKey(key).map(Option::getValue);
return Optional.ofNullable(listOptions().get(key));
}
@Override
public String getByPropertyOfNullable(PropertyEnum property) {
public Object getByPropertyOfNullable(PropertyEnum property) {
return getByProperty(property).orElse(null);
}
@Override
public String getByPropertyOfNonNull(PropertyEnum property) {
public Object getByPropertyOfNonNull(PropertyEnum property) {
Assert.notNull(property, "Blog property must not be null");
return getByKeyOfNonNull(property.getValue());
}
@Override
public Optional<String> getByProperty(PropertyEnum property) {
public Optional<Object> getByProperty(PropertyEnum property) {
Assert.notNull(property, "Blog property must not be null");
return getByKey(property.getValue());
@ -257,7 +260,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public <T> Optional<T> getByProperty(PropertyEnum property, Class<T> propertyType) {
return getByProperty(property).map(propertyValue -> PropertyEnum.convertTo(propertyValue, propertyType));
return getByProperty(property).map(propertyValue -> PropertyEnum.convertTo(propertyValue.toString(), propertyType));
}
@Override
@ -267,12 +270,12 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public <T> Optional<T> getByKey(String key, Class<T> valueType) {
return getByKey(key).map(value -> PropertyEnum.convertTo(value, valueType));
return getByKey(key).map(value -> PropertyEnum.convertTo(value.toString(), valueType));
}
@Override
public <T extends Enum<T>> Optional<T> getEnumByProperty(PropertyEnum property, Class<T> valueType) {
return getByProperty(property).map(value -> PropertyEnum.convertToEnum(value, valueType));
return getByProperty(property).map(value -> PropertyEnum.convertToEnum(value.toString(), valueType));
}
@Override
@ -282,7 +285,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
@Override
public <V, E extends ValueEnum<V>> Optional<E> getValueEnumByProperty(PropertyEnum property, Class<V> valueType, Class<E> enumType) {
return getByProperty(property).map(value -> ValueEnum.valueToEnum(enumType, PropertyEnum.convertTo(value, valueType)));
return getByProperty(property).map(value -> ValueEnum.valueToEnum(enumType, PropertyEnum.convertTo(value.toString(), valueType)));
}
@Override
@ -325,7 +328,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
return getByProperty(QnYunProperties.ZONE).map(qiniuZone -> {
Zone zone;
switch (qiniuZone) {
switch (qiniuZone.toString()) {
case "z0":
zone = Zone.zone0();
break;
@ -354,7 +357,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
public Locale getLocale() {
return getByProperty(BlogProperties.BLOG_LOCALE).map(localeStr -> {
try {
return Locale.forLanguageTag(localeStr);
return Locale.forLanguageTag(localeStr.toString());
} catch (Exception e) {
return Locale.getDefault();
}
@ -366,7 +369,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
// Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port", "8080");
String blogUrl = getByPropertyOfNullable(BlogProperties.BLOG_URL);
String blogUrl = getByProperty(BlogProperties.BLOG_URL).orElse("").toString();
if (StrUtil.isNotBlank(blogUrl)) {
blogUrl = StrUtil.removeSuffix(blogUrl, "/");

View File

@ -296,7 +296,7 @@ public class ThemeServiceImpl implements ThemeService {
if (activatedThemeId == null) {
synchronized (this) {
if (activatedThemeId == null) {
activatedThemeId = optionService.getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_ID);
activatedThemeId = optionService.getByPropertyOrDefault(PrimaryProperties.THEME, String.class, DEFAULT_THEME_ID);
}
}
}