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.workflow; 021 022import com.google.common.annotations.VisibleForTesting; 023import org.apache.ibatis.session.SqlSession; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026import org.sonar.api.ServerComponent; 027import org.sonar.api.config.Settings; 028import org.sonar.api.utils.KeyValueFormat; 029import org.sonar.core.persistence.MyBatis; 030import org.sonar.core.properties.PropertiesMapper; 031import org.sonar.core.properties.PropertyDto; 032import org.sonar.core.review.ReviewCommentDto; 033import org.sonar.core.review.ReviewCommentMapper; 034import org.sonar.core.review.ReviewDto; 035import org.sonar.core.review.ReviewMapper; 036import org.sonar.api.workflow.Comment; 037import org.sonar.api.workflow.internal.DefaultReview; 038import java.util.Date; 039import java.util.List; 040 041public class ReviewDatabaseStore implements ReviewStore, ServerComponent { 042 private static final Logger LOG = LoggerFactory.getLogger(ReviewDatabaseStore.class); 043 044 private MyBatis mybatis; 045 046 public ReviewDatabaseStore(MyBatis mb) { 047 this.mybatis = mb; 048 } 049 050 public void store(DefaultReview review) { 051 store(review, new Date()); 052 } 053 054 @VisibleForTesting 055 void store(DefaultReview review, Date now) { 056 if (review.getReviewId() == null) { 057 LOG.error("Review has no id. Violation id is: " + review.getViolationId()); 058 return; 059 } 060 061 SqlSession session = mybatis.openSession(); 062 ReviewMapper mapper = session.getMapper(ReviewMapper.class); 063 ReviewCommentMapper commentMapper = session.getMapper(ReviewCommentMapper.class); 064 try { 065 ReviewDto dto = mapper.findById(review.getReviewId()); 066 dto.setResolution(review.getResolution()); 067 dto.setStatus(review.getStatus()); 068 dto.setData(KeyValueFormat.format(review.getProperties())); 069 dto.setUpdatedAt(now); 070 mapper.update(dto); 071 072 for (Comment comment : review.getNewComments()) { 073 ReviewCommentDto commentDto = new ReviewCommentDto(); 074 commentDto.setReviewId(dto.getId()); 075 commentDto.setText(comment.getMarkdownText()); 076 commentDto.setCreatedAt(now); 077 commentDto.setUpdatedAt(now); 078 commentDto.setUserId(comment.getUserId()); 079 commentMapper.insert(commentDto); 080 } 081 session.commit(); 082 083 } finally { 084 MyBatis.closeQuietly(session); 085 } 086 } 087 088 public void completeProjectSettings(Long projectId, Settings settings, List<String> propertyKeys) { 089 if (propertyKeys.isEmpty()) { 090 return; 091 } 092 093 SqlSession session = mybatis.openSession(); 094 PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); 095 try { 096 List<PropertyDto> dtos = mapper.selectSetOfResourceProperties(projectId, propertyKeys); 097 for (PropertyDto dto : dtos) { 098 settings.setProperty(dto.getKey(), dto.getValue()); 099 } 100 101 } finally { 102 MyBatis.closeQuietly(session); 103 } 104 } 105}