Java tutorial
/* * Copyright 2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.curly.artifact.service; import io.curly.artifact.model.Artifact; import io.curly.commons.github.User; import io.curly.commons.logging.annotation.Loggable; import io.curly.commons.stereotype.Command; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.retry.annotation.Retryable; import org.springframework.util.Assert; import java.util.Objects; /** * @author Joo Pedro Evangelista */ @Slf4j @Command @Retryable public class DefaultArtifactService implements ArtifactService { private final ArtifactRepository repository; @Autowired public DefaultArtifactService(ArtifactRepository artifactRepository) { this.repository = artifactRepository; } @Loggable @Override @Cacheable("artifacts") public Page<Artifact> findAll(Pageable pageable) { Assert.notNull(pageable, "Page information must be not null!"); log.debug("Finding for page {}", pageable.getPageNumber()); return repository.findAll(pageable); } @Loggable @Override public Page<Artifact> findAllOwned(Pageable pageable, User user) { Assert.notNull(pageable, "Page information must be not null!"); return repository.findAllByOwner(user.getId(), pageable); } @Loggable @Override @CachePut(value = { "artifacts", "singleArtifact" }, key = "#artifact.id") public Artifact save(Artifact artifact, User user) { Assert.notNull(artifact, "Artifact must be not null1"); Assert.notNull(user); if (artifact.getId() == null) { return repository.save(artifact); } else { Artifact ownedArtifact = repository.findByIdAndOwner(artifact.getId(), user.getId()); if (ownedArtifact != null && Objects.equals(ownedArtifact.getOwner(), user.getId())) { return repository.save(artifact); } } throw new IllegalStateException("Cannot find an entity to be overridden and the id was not null "); } @Loggable @Override @Cacheable(value = "singleArtifact", key = "#id") public Artifact findOne(String id) { Assert.hasText(id, "Id must be not null or empty!"); return repository.findOne(id); } @Override @Loggable @CacheEvict(value = { "artifacts", "singleArtifact" }, key = "#id") public void delete(String id, User user) { Assert.notNull(user, "User must be not null"); Assert.hasText(id, "Id must be not empty"); log.debug("Looking for entity with id {}", id); Artifact artifact = findOne(id); if (artifact != null && (artifact.getOwner().equals(user.getId()))) { repository.delete(artifact); } else { log.error("Cannot process #delete({},{})", id, user.getId()); } } }