Search in sources :

Example 1 with OptionUpdatedEvent

use of run.halo.app.event.options.OptionUpdatedEvent in project halo by ruibaby.

the class BackupServiceImpl method importData.

@Override
public void importData(MultipartFile file) throws IOException {
    String jsonContent = FileUtils.readString(file.getInputStream());
    ObjectMapper mapper = JsonUtils.createDefaultJsonMapper();
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
    };
    HashMap<String, Object> data = mapper.readValue(jsonContent, typeRef);
    String version = (String) Objects.requireNonNullElse(data.get("version"), "");
    if (!VersionUtil.hasSameMajorAndMinorVersion(HaloConst.HALO_VERSION, version)) {
        throw new BadRequestException("导入数据的主次版本号与当前系统版本号不匹配,不支持导入!");
    }
    List<Attachment> attachments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("attachments")), Attachment[].class));
    attachmentService.createInBatch(attachments);
    List<Category> categories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("categories")), Category[].class));
    categoryService.createInBatch(categories);
    List<Tag> tags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("tags")), Tag[].class));
    tagService.createInBatch(tags);
    List<CommentBlackList> commentBlackList = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("comment_black_list")), CommentBlackList[].class));
    commentBlackListService.createInBatch(commentBlackList);
    List<Journal> journals = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journals")), Journal[].class));
    journalService.createInBatch(journals);
    List<JournalComment> journalComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journal_comments")), JournalComment[].class));
    journalCommentService.createInBatch(journalComments);
    List<Link> links = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("links")), Link[].class));
    linkService.createInBatch(links);
    List<Log> logs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("logs")), Log[].class));
    logService.createInBatch(logs);
    List<Menu> menus = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("menus")), Menu[].class));
    menuService.createInBatch(menus);
    List<Option> options = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("options")), Option[].class));
    optionService.createInBatch(options);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
    List<Photo> photos = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("photos")), Photo[].class));
    photoService.createInBatch(photos);
    List<Post> posts = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("posts")), Post[].class));
    postService.createInBatch(posts);
    List<Content> contents = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("contents")), Content[].class));
    contentService.createInBatch(contents);
    List<ContentPatchLog> contentPatchLogs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("content_patch_logs")), ContentPatchLog[].class));
    contentPatchLogService.createInBatch(contentPatchLogs);
    List<PostCategory> postCategories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_categories")), PostCategory[].class));
    postCategoryService.createInBatch(postCategories);
    List<PostComment> postComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_comments")), PostComment[].class));
    postCommentService.createInBatch(postComments);
    List<PostMeta> postMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_metas")), PostMeta[].class));
    postMetaService.createInBatch(postMetas);
    List<PostTag> postTags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_tags")), PostTag[].class));
    postTagService.createInBatch(postTags);
    List<Sheet> sheets = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheets")), Sheet[].class));
    sheetService.createInBatch(sheets);
    List<SheetComment> sheetComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_comments")), SheetComment[].class));
    sheetCommentService.createInBatch(sheetComments);
    List<SheetMeta> sheetMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_metas")), SheetMeta[].class));
    sheetMetaService.createInBatch(sheetMetas);
    List<ThemeSetting> themeSettings = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("theme_settings")), ThemeSetting[].class));
    themeSettingService.createInBatch(themeSettings);
    eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
    List<User> users = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("user")), User[].class));
    if (users.size() > 0) {
        userService.create(users.get(0));
    }
}
Also used : SheetMeta(run.halo.app.model.entity.SheetMeta) HashMap(java.util.HashMap) JournalComment(run.halo.app.model.entity.JournalComment) Attachment(run.halo.app.model.entity.Attachment) Journal(run.halo.app.model.entity.Journal) Photo(run.halo.app.model.entity.Photo) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) PostComment(run.halo.app.model.entity.PostComment) PostMeta(run.halo.app.model.entity.PostMeta) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) Log(run.halo.app.model.entity.Log) Content(run.halo.app.model.entity.Content) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) Option(run.halo.app.model.entity.Option) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Link(run.halo.app.model.entity.Link) SheetComment(run.halo.app.model.entity.SheetComment) Category(run.halo.app.model.entity.Category) PostCategory(run.halo.app.model.entity.PostCategory) User(run.halo.app.model.entity.User) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) PostCategory(run.halo.app.model.entity.PostCategory) ThemeSetting(run.halo.app.model.entity.ThemeSetting) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Menu(run.halo.app.model.entity.Menu) PostTag(run.halo.app.model.entity.PostTag) Post(run.halo.app.model.entity.Post) BadRequestException(run.halo.app.exception.BadRequestException) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) Sheet(run.halo.app.model.entity.Sheet)

Example 2 with OptionUpdatedEvent

use of run.halo.app.event.options.OptionUpdatedEvent in project halo by ruibaby.

the class OptionServiceImpl method removePermanently.

@Override
public Option removePermanently(Integer id) {
    Option deletedOption = removeById(id);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
    return deletedOption;
}
Also used : OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) Option(run.halo.app.model.entity.Option)

Example 3 with OptionUpdatedEvent

use of run.halo.app.event.options.OptionUpdatedEvent in project halo by halo-dev.

the class ThemeRepositoryImpl method setActivatedTheme.

@Override
public void setActivatedTheme(@NonNull String themeId) {
    Assert.hasText(themeId, "Theme id must not be blank");
    final var newThemeOption = optionRepository.findByKey(THEME.getValue()).map(themeOption -> {
        // set theme id
        themeOption.setValue(themeId);
        return themeOption;
    }).orElseGet(() -> new Option(THEME.getValue(), themeId));
    optionRepository.save(newThemeOption);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
}
Also used : ThemeNotFoundException(run.halo.app.exception.ThemeNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NotFoundException(run.halo.app.exception.NotFoundException) StringUtils(org.apache.commons.lang3.StringUtils) ThemeNotSupportException(run.halo.app.exception.ThemeNotSupportException) FileUtils(run.halo.app.utils.FileUtils) ThemePropertyScanner(run.halo.app.theme.ThemePropertyScanner) Option(run.halo.app.model.entity.Option) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Version(run.halo.app.utils.Version) THEME(run.halo.app.model.properties.PrimaryProperties.THEME) Repository(org.springframework.stereotype.Repository) Path(java.nio.file.Path) AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) IOException(java.io.IOException) ApplicationListener(org.springframework.context.ApplicationListener) FileUtils.copyFolder(run.halo.app.utils.FileUtils.copyFolder) HaloProperties(run.halo.app.config.properties.HaloProperties) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Paths(java.nio.file.Paths) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) DEFAULT_THEME_ID(run.halo.app.model.support.HaloConst.DEFAULT_THEME_ID) FileUtils.deleteFolderQuietly(run.halo.app.utils.FileUtils.deleteFolderQuietly) Optional(java.util.Optional) NonNull(org.springframework.lang.NonNull) HaloConst(run.halo.app.model.support.HaloConst) ServiceException(run.halo.app.exception.ServiceException) Assert(org.springframework.util.Assert) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) Option(run.halo.app.model.entity.Option)

Example 4 with OptionUpdatedEvent

use of run.halo.app.event.options.OptionUpdatedEvent in project halo-plugin-experimental by guqing.

the class OptionServiceImpl method save.

@Override
@Transactional
public void save(Map<String, Object> optionMap) {
    if (CollectionUtils.isEmpty(optionMap)) {
        return;
    }
    Map<String, Option> optionKeyMap = ServiceUtils.convertToMap(listAll(), Option::getKey);
    List<Option> optionsToCreate = new LinkedList<>();
    List<Option> optionsToUpdate = new LinkedList<>();
    optionMap.forEach((key, value) -> {
        Option oldOption = optionKeyMap.get(key);
        if (oldOption == null || !StringUtils.equals(oldOption.getValue(), value.toString())) {
            OptionParam optionParam = new OptionParam();
            optionParam.setKey(key);
            optionParam.setValue(value.toString());
            ValidationUtils.validate(optionParam);
            if (oldOption == null) {
                // Create it
                optionsToCreate.add(optionParam.convertTo());
            } else if (!StringUtils.equals(oldOption.getValue(), value.toString())) {
                // Update it
                optionParam.update(oldOption);
                optionsToUpdate.add(oldOption);
            }
        }
    });
    // Update them
    updateInBatch(optionsToUpdate);
    // Create them
    createInBatch(optionsToCreate);
    if (!CollectionUtils.isEmpty(optionsToUpdate) || !CollectionUtils.isEmpty(optionsToCreate)) {
        // If there is something changed
        eventPublisher.publishEvent(new OptionUpdatedEvent(this));
    }
}
Also used : OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) OptionParam(run.halo.app.model.params.OptionParam) Option(run.halo.app.model.entity.Option) LinkedList(java.util.LinkedList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with OptionUpdatedEvent

use of run.halo.app.event.options.OptionUpdatedEvent in project halo-plugin-experimental by guqing.

the class OptionServiceImpl method update.

@Override
public void update(Integer optionId, OptionParam optionParam) {
    Option optionToUpdate = getById(optionId);
    optionParam.update(optionToUpdate);
    update(optionToUpdate);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
}
Also used : OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) Option(run.halo.app.model.entity.Option)

Aggregations

OptionUpdatedEvent (run.halo.app.event.options.OptionUpdatedEvent)18 Option (run.halo.app.model.entity.Option)18 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 Paths (java.nio.file.Paths)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Slf4j (lombok.extern.slf4j.Slf4j)3 StringUtils (org.apache.commons.lang3.StringUtils)3 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)3 ApplicationListener (org.springframework.context.ApplicationListener)3 NonNull (org.springframework.lang.NonNull)3 Repository (org.springframework.stereotype.Repository)3 Transactional (org.springframework.transaction.annotation.Transactional)3