Search in sources :

Example 1 with AlreadyExistsException

use of run.halo.app.exception.AlreadyExistsException in project halo by halo-dev.

the class LinkServiceImpl method updateBy.

@Override
@NonNull
public Link updateBy(Integer id, @NonNull LinkParam linkParam) {
    Assert.notNull(id, "Id must not be null");
    Assert.notNull(linkParam, "Link param must not be null");
    // Check the name
    boolean exist = linkRepository.existsByNameAndIdNot(linkParam.getName(), id);
    if (exist) {
        throw new AlreadyExistsException("友情链接 " + linkParam.getName() + " 已存在").setErrorData(linkParam.getName());
    }
    // Check the url
    exist = linkRepository.existsByUrlAndIdNot(linkParam.getUrl(), id);
    if (exist) {
        throw new AlreadyExistsException("友情链接 " + linkParam.getUrl() + " 已存在").setErrorData(linkParam.getUrl());
    }
    Link link = getById(id);
    linkParam.update(link);
    return update(link);
}
Also used : AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) Link(run.halo.app.model.entity.Link) NonNull(org.springframework.lang.NonNull)

Example 2 with AlreadyExistsException

use of run.halo.app.exception.AlreadyExistsException in project halo by halo-dev.

the class ThemeRepositoryImpl method attemptToAdd.

@Override
public ThemeProperty attemptToAdd(ThemeProperty newProperty) {
    // 1. check existence
    final var alreadyExist = fetchThemePropertyByThemeId(newProperty.getId()).isPresent();
    if (alreadyExist) {
        throw new AlreadyExistsException("当前安装的主题已存在");
    }
    // 2. check version compatibility
    if (!checkThemePropertyCompatibility(newProperty)) {
        throw new ThemeNotSupportException("当前主题仅支持 Halo " + newProperty.getRequire() + " 及以上的版本");
    }
    // 3. move the temp folder into templates/themes/{theme_id}
    final var sourceThemePath = Paths.get(newProperty.getThemePath());
    final var targetThemePath = getThemeRootPath().resolve(newProperty.getId());
    // 4. clear target theme folder firstly
    deleteFolderQuietly(targetThemePath);
    log.info("Copying new theme({}) from {} to {}", newProperty.getId(), sourceThemePath, targetThemePath);
    try {
        copyFolder(sourceThemePath, targetThemePath);
    } catch (IOException e) {
        // clear data
        deleteFolderQuietly(targetThemePath);
        throw new ServiceException("复制主题文件失败!", e);
    } finally {
        log.info("Clean temporary theme folder {}", sourceThemePath);
        deleteFolderQuietly(sourceThemePath);
    }
    // or else throw should never happen
    return ThemePropertyScanner.INSTANCE.fetchThemeProperty(targetThemePath).orElseThrow();
}
Also used : AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) ThemeNotSupportException(run.halo.app.exception.ThemeNotSupportException) ServiceException(run.halo.app.exception.ServiceException) IOException(java.io.IOException)

Example 3 with AlreadyExistsException

use of run.halo.app.exception.AlreadyExistsException in project halo-plugin-experimental by guqing.

the class ThemeRepositoryImpl method attemptToAdd.

@Override
public ThemeProperty attemptToAdd(ThemeProperty newProperty) {
    // 1. check existence
    final var alreadyExist = fetchThemeByThemeId(newProperty.getId()).isPresent();
    if (alreadyExist) {
        throw new AlreadyExistsException("当前安装的主题已存在");
    }
    // Not support current halo version.
    if (checkThemePropertyCompatibility(newProperty)) {
        throw new ThemeNotSupportException("当前主题仅支持 Halo " + newProperty.getRequire() + " 及以上的版本");
    }
    // 3. move the temp folder into templates/themes/{theme_id}
    final var sourceThemePath = Paths.get(newProperty.getThemePath());
    final var targetThemePath = getThemeRootPath().resolve(newProperty.getId());
    // 4. clear target theme folder firstly
    deleteFolderQuietly(targetThemePath);
    log.info("Copying new theme({}) from {} to {}", newProperty.getId(), sourceThemePath, targetThemePath);
    try {
        copyFolder(sourceThemePath, targetThemePath);
    } catch (IOException e) {
        // clear data
        deleteFolderQuietly(targetThemePath);
        throw new ServiceException("复制主题文件失败!", e);
    } finally {
        log.info("Clean temporary theme folder {}", sourceThemePath);
        deleteFolderQuietly(sourceThemePath);
    }
    // or else throw should never happen
    return ThemePropertyScanner.INSTANCE.fetchThemeProperty(targetThemePath).orElseThrow();
}
Also used : AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) ThemeNotSupportException(run.halo.app.exception.ThemeNotSupportException) ServiceException(run.halo.app.exception.ServiceException) IOException(java.io.IOException)

Example 4 with AlreadyExistsException

use of run.halo.app.exception.AlreadyExistsException in project halo-plugin-experimental by guqing.

the class TagServiceImpl method create.

@Override
@Transactional
public Tag create(Tag tag) {
    // Check if the tag is exist
    long count = tagRepository.countByNameOrSlug(tag.getName(), tag.getSlug());
    log.debug("Tag count: [{}]", count);
    if (count > 0) {
        // If the tag has exist already
        throw new AlreadyExistsException("该标签已存在").setErrorData(tag);
    }
    // Get tag name
    return super.create(tag);
}
Also used : AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with AlreadyExistsException

use of run.halo.app.exception.AlreadyExistsException in project halo by ruibaby.

the class LinkServiceImpl method createBy.

@Override
@NonNull
public Link createBy(@NonNull LinkParam linkParam) {
    Assert.notNull(linkParam, "Link param must not be null");
    // Check the name
    boolean exist = existByName(linkParam.getName());
    if (exist) {
        throw new AlreadyExistsException("友情链接 " + linkParam.getName() + " 已存在").setErrorData(linkParam.getName());
    }
    // Check the url
    exist = existByUrl(linkParam.getUrl());
    if (exist) {
        throw new AlreadyExistsException("友情链接 " + linkParam.getUrl() + " 已存在").setErrorData(linkParam.getUrl());
    }
    return create(linkParam.convertTo());
}
Also used : AlreadyExistsException(run.halo.app.exception.AlreadyExistsException) NonNull(org.springframework.lang.NonNull)

Aggregations

AlreadyExistsException (run.halo.app.exception.AlreadyExistsException)12 NonNull (org.springframework.lang.NonNull)6 IOException (java.io.IOException)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ServiceException (run.halo.app.exception.ServiceException)3 ThemeNotSupportException (run.halo.app.exception.ThemeNotSupportException)3 Link (run.halo.app.model.entity.Link)3