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.jpa.dao;
021
022import org.apache.commons.collections.CollectionUtils;
023import org.apache.commons.collections.Predicate;
024import org.sonar.api.database.DatabaseSession;
025import org.sonar.api.measures.Metric;
026
027import java.util.*;
028
029public class MeasuresDao extends BaseDao {
030
031  private final Map<String, Metric> metricsByName = new HashMap<String, Metric>();
032
033  public MeasuresDao(DatabaseSession session) {
034    super(session);
035  }
036
037  public Metric getMetric(Metric metric) {
038    return getMetricsByName().get(metric.getKey());
039  }
040
041  public List<Metric> getMetrics(List<Metric> metrics) {
042    List<Metric> result = new ArrayList<Metric>();
043    for (Metric metric : metrics) {
044      result.add(getMetric(metric));
045    }
046    return result;
047  }
048
049  public Metric getMetric(String metricName) {
050    return getMetricsByName().get(metricName);
051  }
052
053  public Collection<Metric> getMetrics() {
054    return getMetricsByName().values();
055  }
056
057  public Collection<Metric> getEnabledMetrics() {
058    return CollectionUtils.select(getMetricsByName().values(), new Predicate() {
059      public boolean evaluate(Object o) {
060        return ((Metric) o).getEnabled();
061      }
062    });
063  }
064
065  public Collection<Metric> getUserDefinedMetrics() {
066    return CollectionUtils.select(getMetricsByName().values(), new Predicate() {
067      public boolean evaluate(Object o) {
068        Metric m = (Metric) o;
069        return (m.getEnabled() && m.getOrigin() != Metric.Origin.JAV);
070      }
071    });
072  }
073
074  public void disableAutomaticMetrics() {
075    getSession().createQuery("update " + Metric.class.getSimpleName() + " m set m.enabled=false where m.userManaged=false").executeUpdate();
076    getSession().commit();
077    metricsByName.clear();
078  }
079
080  public void registerMetrics(Collection<Metric> metrics) {
081    if (metrics != null) {
082      for (Metric metric : metrics) {
083        metric.setEnabled(Boolean.TRUE);
084        persistMetric(metric);
085      }
086      getSession().commit();
087    }
088  }
089
090  public void persistMetric(Metric metric) {
091    Metric dbMetric = getMetric(metric);
092    if (dbMetric != null) {
093      dbMetric.merge(metric);
094      getSession().getEntityManager().merge(dbMetric);
095
096    } else {
097      getSession().getEntityManager().persist(metric);
098    }
099
100    metricsByName.clear();
101  }
102
103  public void disabledMetrics(Collection<Metric> metrics) {
104    for (Metric metric : metrics) {
105      metric.setEnabled(Boolean.FALSE);
106      getSession().getEntityManager().persist(metric);
107      metricsByName.put(metric.getName(), metric);
108    }
109  }
110
111  private Map<String, Metric> getMetricsByName() {
112    if (metricsByName.isEmpty()) {
113      List<Metric> metrics = getSession().getResults(Metric.class);
114      for (Metric metric : metrics) {
115        metricsByName.put(metric.getKey(), metric);
116      }
117    }
118    return metricsByName;
119  }
120}