io.curly.advisor.command.ReviewHystrixCommands.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.advisor.command.ReviewHystrixCommands.java

Source

/*
 *        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.advisor.command;

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

import javax.inject.Inject;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.ObservableResult;
import io.curly.advisor.model.Review;
import io.curly.advisor.model.ReviewEntity;
import io.curly.advisor.repository.ReviewRepository;
import io.curly.commons.github.User;
import io.curly.commons.stereotype.Command;
import lombok.extern.slf4j.Slf4j;
import rx.Observable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.retry.annotation.Retryable;

/**
 * @author Joao Pedro Evangelista
 */
@Slf4j
@Command
@Retryable
public class ReviewHystrixCommands {

    private final ReviewRepository repository;

    @Inject
    public ReviewHystrixCommands(ReviewRepository repository) {
        this.repository = repository;
    }

    @HystrixCommand(fallbackMethod = "defaultFindAllByArtifact")
    public Observable<Page<Review>> findAllByArtifact(final Pageable pageable, final String artifact) {
        return new ObservableResult<Page<Review>>() {
            @Override
            public Page<Review> invoke() {
                return repository.findAllByArtifact(artifact, pageable);
            }
        };
    }

    @HystrixCommand
    public Observable<Optional<Review>> findByOwner(final User user, final String id) {
        return new ObservableResult<Optional<Review>>() {
            @Override
            public Optional<Review> invoke() {
                return Optional.ofNullable(repository.findByIdAndOwner(id, user.getId()));
            }
        };
    }

    @HystrixCommand
    public void save(ReviewEntity reviewEntity, User user) {
        if (reviewEntity.getId() != null) {
            final Review fromDb = repository.findByIdAndOwner(reviewEntity.getId(), user.getId());
            if (fromDb != null && Objects.equals(fromDb.getOwner(), user.getId())) {
                Review review = reviewEntity.toNewReview(user);
                repository.save(review);
            }
        } else {
            repository.save(reviewEntity.toNewReview(user));
        }
    }

    @HystrixCommand(fallbackMethod = "defaultFindAllByOwner")
    public Observable<Page<Review>> findAllByOwner(final User user, final Pageable pageable) {
        return new ObservableResult<Page<Review>>() {
            @Override
            public Page<Review> invoke() {
                return repository.findAllByOwner(user.getId(), pageable);
            }
        };
    }

    private Page<Review> defaultFindAllByArtifact(Pageable pageable, String artifact) {
        log.warn("Requested reviews for artifact {} on page {} but fell back", artifact, pageable.getPageNumber());
        return new PageImpl<>(Collections.<Review>emptyList(), pageable, 0);
    }

    private Page<Review> defaultFindAllByOwner(User user, Pageable pageable) {
        log.warn("User {} requested all its reviews but we fell back, current page is {}", user,
                pageable.getPageNumber());
        return new PageImpl<>(Collections.<Review>emptyList(), pageable, 0);
    }
}