*/
package org.sonar.plugins.core.purges;
+import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.time.DateUtils;
import org.sonar.api.batch.PurgeContext;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.purge.PurgeUtils;
import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.Logs;
import org.sonar.core.purge.AbstractPurge;
import java.util.Date;
*/
public class PurgeEntities extends AbstractPurge {
- private static final int MINIMUM_AGE_IN_HOURS = -12;
+ private Configuration configuration;
- public PurgeEntities(DatabaseSession session) {
+ public PurgeEntities(DatabaseSession session, Configuration conf) {
super(session);
+ this.configuration = conf;
}
public void purge(PurgeContext context) {
- final Date beforeDate = DateUtils.addHours(new Date(), MINIMUM_AGE_IN_HOURS);
+ int minimumPeriodInHours = PurgeUtils.getMinimumPeriodInHours(configuration);
+ final Date beforeDate = DateUtils.addHours(new Date(), - minimumPeriodInHours);
+ Logs.INFO.info("Deleting files data before " + beforeDate);
+
Query query = getSession().createQuery("SELECT s.id FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=false AND scope=:scope AND s.createdAt<:date");
query.setParameter("scope", Resource.SCOPE_ENTITY);
query.setParameter("date", beforeDate);
*/
package org.sonar.plugins.core.purges;
+import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.time.DateUtils;
import org.sonar.api.batch.PurgeContext;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.purge.PurgeUtils;
+import org.sonar.api.utils.Logs;
import org.sonar.core.purge.AbstractPurge;
+import javax.persistence.Query;
import java.util.Date;
import java.util.List;
-import javax.persistence.Query;
-
/**
* @since 1.11
*/
public class PurgeUnprocessed extends AbstractPurge {
- private static final int MINIMUM_AGE_IN_HOURS = -12;
+ private Configuration configuration;
- public PurgeUnprocessed(DatabaseSession session) {
+ public PurgeUnprocessed(DatabaseSession session, Configuration conf) {
super(session);
+ this.configuration = conf;
}
public void purge(PurgeContext context) {
- final Date beforeDate = DateUtils.addHours(new Date(), MINIMUM_AGE_IN_HOURS);
+ int minimumPeriodInHours = PurgeUtils.getMinimumPeriodInHours(configuration);
+ final Date beforeDate = DateUtils.addHours(new Date(), -minimumPeriodInHours);
+ Logs.INFO.info("Deleting unprocessed data before " + beforeDate);
+
Query query = getSession().createQuery("SELECT s.id FROM " + Snapshot.class.getSimpleName() + " s WHERE s.last=false AND status=:status AND s.createdAt<:date");
query.setParameter("status", Snapshot.STATUS_UNPROCESSED);
query.setParameter("date", beforeDate);
*/
package org.sonar.plugins.core.purges;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
public void purgeEntities() {
setupData("sharedFixture", "purgeEntities");
- final PurgeEntities purge = new PurgeEntities(getSession());
+ final PurgeEntities purge = new PurgeEntities(getSession(), new PropertiesConfiguration());
purge.purge(null);
checkTables("purgeEntities", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources");
*/
package org.sonar.plugins.core.purges;
+import org.apache.commons.configuration.PropertiesConfiguration;
import org.junit.Test;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
public void purgeUnprocessed() {
setupData("sharedFixture", "purgeUnprocessed");
- new PurgeUnprocessed(getSession()).purge(null);
+ new PurgeUnprocessed(getSession(), new PropertiesConfiguration()).purge(null);
checkTables("purgeUnprocessed", "snapshots", "project_measures", "measure_data", "rule_failures", "snapshot_sources");
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.purge;
+
+import org.apache.commons.configuration.Configuration;
+
+public final class PurgeUtils {
+
+ public static final int DEFAULT_MINIMUM_PERIOD_IN_HOURS = 12;
+ public static final String PROP_KEY_MINIMUM_PERIOD_IN_HOURS = "sonar.purge.minimumPeriodInHours";
+
+ private PurgeUtils() {
+ // only static methods
+ }
+
+ public static int getMinimumPeriodInHours(Configuration conf) {
+ int hours = DEFAULT_MINIMUM_PERIOD_IN_HOURS;
+ if (conf != null) {
+ hours = conf.getInt(PROP_KEY_MINIMUM_PERIOD_IN_HOURS, DEFAULT_MINIMUM_PERIOD_IN_HOURS);
+ }
+ return hours;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.purge;
+
+import org.apache.commons.configuration.PropertiesConfiguration;
+import org.junit.Test;
+import static org.hamcrest.core.Is.is;
+
+import static org.junit.Assert.assertThat;
+
+public class PurgeUtilsTest {
+
+ @Test
+ public void shouldReturnDefaultMinimumPeriod() {
+ assertThat(PurgeUtils.getMinimumPeriodInHours(new PropertiesConfiguration()), is(PurgeUtils.DEFAULT_MINIMUM_PERIOD_IN_HOURS));
+ }
+
+ @Test
+ public void shouldReturnMinimumPeriod() {
+ PropertiesConfiguration conf = new PropertiesConfiguration();
+ conf.setProperty(PurgeUtils.PROP_KEY_MINIMUM_PERIOD_IN_HOURS, "9");
+ assertThat(PurgeUtils.getMinimumPeriodInHours(conf), is(9));
+ }
+}