001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 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.server.configuration; 021 022 import com.thoughtworks.xstream.XStream; 023 import com.thoughtworks.xstream.converters.Converter; 024 import com.thoughtworks.xstream.converters.MarshallingContext; 025 import com.thoughtworks.xstream.converters.UnmarshallingContext; 026 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 027 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 028 import org.sonar.api.database.DatabaseSession; 029 import org.sonar.api.measures.Metric; 030 import org.sonar.jpa.dao.MeasuresDao; 031 032 import java.util.Collection; 033 034 public class MetricsBackup implements Backupable { 035 036 private MeasuresDao measuresDao; 037 038 public MetricsBackup(DatabaseSession session) { 039 measuresDao = new MeasuresDao(session); 040 } 041 042 protected MetricsBackup() { 043 } 044 045 public void exportXml(SonarConfig sonarConfig) { 046 Collection<Metric> metrics = measuresDao.getUserDefinedMetrics(); 047 exportXml(sonarConfig, metrics); 048 } 049 050 protected void exportXml(SonarConfig sonarConfig, Collection<Metric> metrics) { 051 sonarConfig.setMetrics(metrics); 052 } 053 054 public void importXml(SonarConfig sonarConfig) { 055 disabledUserDefinedMetrics(); 056 registerMetrics(sonarConfig); 057 } 058 059 protected void disabledUserDefinedMetrics() { 060 Collection<Metric> dbMetrics = measuresDao.getUserDefinedMetrics(); 061 measuresDao.disabledMetrics(dbMetrics); 062 } 063 064 protected void registerMetrics(SonarConfig sonarConfig) { 065 Collection<Metric> newMetrics = sonarConfig.getMetrics(); 066 measuresDao.registerMetrics(newMetrics); 067 } 068 069 public void configure(XStream xStream) { 070 xStream.alias("metric", Metric.class); 071 xStream.omitField(Metric.class, "id"); 072 xStream.omitField(Metric.class, "userManaged"); 073 xStream.omitField(Metric.class, "comparable"); 074 xStream.omitField(Metric.class, "enabled"); 075 final Converter builtIn = xStream.getConverterLookup().lookupConverterForType(Metric.class); 076 xStream.registerConverter(new Converter() { 077 public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { 078 builtIn.marshal(source, writer, context); 079 } 080 081 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { 082 Metric unmarshalled = (Metric) builtIn.unmarshal(reader, context); 083 unmarshalled.setId(null); // See http://jira.codehaus.org/browse/SONAR-1819 084 unmarshalled.setUserManaged(true); 085 unmarshalled.setEnabled(true); 086 return unmarshalled; 087 } 088 089 public boolean canConvert(Class type) { 090 return Metric.class.equals(type); 091 } 092 }); 093 } 094 095 }