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     */
020    package org.sonar.core.review;
021    
022    import com.google.common.base.Preconditions;
023    import com.google.common.base.Predicate;
024    import com.google.common.base.Predicates;
025    import com.google.common.cache.Cache;
026    import com.google.common.cache.CacheBuilder;
027    import com.google.common.cache.CacheLoader;
028    import com.google.common.collect.Collections2;
029    import org.apache.ibatis.session.SqlSession;
030    import org.sonar.api.BatchComponent;
031    import org.sonar.api.ServerComponent;
032    import org.sonar.core.persistence.MyBatis;
033    
034    import javax.annotation.Nullable;
035    import java.util.Collection;
036    
037    public class ReviewDao implements BatchComponent, ServerComponent {
038      private final MyBatis mybatis;
039      private final Cache<Long, Collection<ReviewDto>> cacheByResource;
040    
041      public ReviewDao(MyBatis mybatis) {
042        this.mybatis = mybatis;
043        this.cacheByResource = CacheBuilder.newBuilder()
044            .weakValues()
045            .build(new CacheLoader<Long, Collection<ReviewDto>>() {
046              @Override
047              public Collection<ReviewDto> load(Long resourceId) {
048                return doSelectOpenByResourceId(resourceId);
049              }
050            });
051      }
052    
053      public Collection<ReviewDto> selectOpenByResourceId(long resourceId, @Nullable Predicate<ReviewDto>... predicates) {
054        Collection<ReviewDto> reviews = cacheByResource.getUnchecked(resourceId);
055        if (!reviews.isEmpty() && predicates != null) {
056          reviews = Collections2.filter(reviews, Predicates.and(predicates));
057        }
058        return reviews;
059      }
060    
061      public Collection<ReviewDto> selectOnDeletedResources(long rootProjectId, long rootSnapshotId) {
062        SqlSession session = mybatis.openSession();
063        try {
064          ReviewMapper mapper = session.getMapper(ReviewMapper.class);
065          return mapper.selectOnDeletedResources(rootProjectId, rootSnapshotId);
066        } finally {
067          MyBatis.closeQuietly(session);
068        }
069      }
070    
071      private Collection<ReviewDto> doSelectOpenByResourceId(long resourceId) {
072        SqlSession session = mybatis.openSession();
073        try {
074          ReviewMapper mapper = session.getMapper(ReviewMapper.class);
075          return mapper.selectByResourceId(resourceId);
076        } finally {
077          MyBatis.closeQuietly(session);
078        }
079      }
080    
081    
082      public ReviewDao update(Collection<ReviewDto> reviews) {
083        Preconditions.checkNotNull(reviews);
084    
085        SqlSession session = mybatis.openBatchSession();
086        try {
087          ReviewMapper mapper = session.getMapper(ReviewMapper.class);
088          for (ReviewDto review : reviews) {
089            mapper.update(review);
090          }
091          session.commit();
092          return this;
093    
094        } finally {
095          MyBatis.closeQuietly(session);
096        }
097      }
098    }