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.properties;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.ibatis.session.SqlSession;
024    import org.sonar.api.BatchComponent;
025    import org.sonar.api.ServerComponent;
026    import org.sonar.core.persistence.MyBatis;
027    
028    import java.util.List;
029    
030    public class PropertiesDao implements BatchComponent, ServerComponent {
031    
032      private MyBatis mybatis;
033    
034      public PropertiesDao(MyBatis mybatis) {
035        this.mybatis = mybatis;
036      }
037    
038      /**
039       * Returns the logins of users who have flagged as favourite the resource identified by the given id.
040       *
041       * @param resourceId the resource id
042       * @return the list of logins (maybe be empty - obviously)
043       */
044      public List<String> findUserIdsForFavouriteResource(Long resourceId) {
045        SqlSession session = mybatis.openSession();
046        PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
047        try {
048          return mapper.findUserIdsForFavouriteResource(resourceId);
049        } finally {
050          MyBatis.closeQuietly(session);
051        }
052      }
053    
054      public List<PropertyDto> selectGlobalProperties() {
055        SqlSession session = mybatis.openSession();
056        PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
057        try {
058          return mapper.selectGlobalProperties();
059        } finally {
060          MyBatis.closeQuietly(session);
061        }
062      }
063    
064      public List<PropertyDto> selectProjectProperties(String resourceKey) {
065        SqlSession session = mybatis.openSession();
066        PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
067        try {
068          return mapper.selectProjectProperties(resourceKey);
069        } finally {
070          MyBatis.closeQuietly(session);
071        }
072      }
073    
074      public void setProperty(PropertyDto property) {
075        SqlSession session = mybatis.openSession();
076        PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
077        try {
078          PropertyDto persistedProperty = mapper.selectByKey(property);
079          if (persistedProperty != null && !StringUtils.equals(persistedProperty.getValue(), property.getValue())) {
080            persistedProperty.setValue(property.getValue());
081            mapper.update(persistedProperty);
082            session.commit();
083    
084          } else if (persistedProperty == null) {
085            mapper.insert(property);
086            session.commit();
087          }
088        } finally {
089          MyBatis.closeQuietly(session);
090        }
091      }
092    }