001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.core.review;
021
022import com.google.common.base.Preconditions;
023import com.google.common.base.Predicate;
024import com.google.common.base.Predicates;
025import com.google.common.cache.Cache;
026import com.google.common.cache.CacheBuilder;
027import com.google.common.cache.CacheLoader;
028import com.google.common.collect.Collections2;
029import org.apache.ibatis.session.SqlSession;
030import org.sonar.api.BatchComponent;
031import org.sonar.api.ServerComponent;
032import org.sonar.core.persistence.MyBatis;
033
034import javax.annotation.Nullable;
035
036import java.util.Collection;
037
038public class ReviewDao implements BatchComponent, ServerComponent {
039  private final MyBatis mybatis;
040  private final Cache<Long, Collection<ReviewDto>> cacheByResource;
041
042  public ReviewDao(MyBatis mybatis) {
043    this.mybatis = mybatis;
044    this.cacheByResource = CacheBuilder.newBuilder()
045        .weakValues()
046        .build(new CacheLoader<Long, Collection<ReviewDto>>() {
047          @Override
048          public Collection<ReviewDto> load(Long resourceId) {
049            return doSelectOpenByResourceId(resourceId);
050          }
051        });
052  }
053
054  /**
055   * @since 3.1
056   */
057  public ReviewDto findById(long reviewId) {
058    SqlSession session = mybatis.openSession();
059    try {
060      ReviewMapper mapper = session.getMapper(ReviewMapper.class);
061      return mapper.findById(reviewId);
062    } finally {
063      MyBatis.closeQuietly(session);
064    }
065  }
066
067  public Collection<ReviewDto> selectOpenByResourceId(long resourceId, @Nullable Predicate<ReviewDto>... predicates) {
068    Collection<ReviewDto> reviews = cacheByResource.getUnchecked(resourceId);
069    if (!reviews.isEmpty() && predicates != null) {
070      reviews = Collections2.filter(reviews, Predicates.and(predicates));
071    }
072    return reviews;
073  }
074
075  public Collection<ReviewDto> selectOnDeletedResources(long rootProjectId, long rootSnapshotId) {
076    SqlSession session = mybatis.openSession();
077    try {
078      ReviewMapper mapper = session.getMapper(ReviewMapper.class);
079      return mapper.selectOnDeletedResources(rootProjectId, rootSnapshotId);
080    } finally {
081      MyBatis.closeQuietly(session);
082    }
083  }
084
085  private Collection<ReviewDto> doSelectOpenByResourceId(long resourceId) {
086    SqlSession session = mybatis.openSession();
087    try {
088      ReviewMapper mapper = session.getMapper(ReviewMapper.class);
089      return mapper.selectByResourceId(resourceId);
090    } finally {
091      MyBatis.closeQuietly(session);
092    }
093  }
094
095  public ReviewDao update(Collection<ReviewDto> reviews) {
096    Preconditions.checkNotNull(reviews);
097
098    SqlSession session = mybatis.openBatchSession();
099    try {
100      ReviewMapper mapper = session.getMapper(ReviewMapper.class);
101      for (ReviewDto review : reviews) {
102        mapper.update(review);
103      }
104      session.commit();
105      return this;
106
107    } finally {
108      MyBatis.closeQuietly(session);
109    }
110  }
111}