diff options
author | jmisinco | 2015-07-14 16:29:55 +0000 |
---|---|---|
committer | Angel Avila | 2015-09-12 00:56:25 +0000 |
commit | cea7338a732b66baddf39d4669ef87106dd27a0b (patch) | |
tree | acc0454476c1a8797f781f154d0f4868c215a0c1 /plugins | |
parent | 91d8f2a68588a6f022d711eace934295ad6b3ed4 (diff) | |
download | org.eclipse.osee-cea7338a732b66baddf39d4669ef87106dd27a0b.tar.gz org.eclipse.osee-cea7338a732b66baddf39d4669ef87106dd27a0b.tar.xz org.eclipse.osee-cea7338a732b66baddf39d4669ef87106dd27a0b.zip |
feature[ats_ATS213170]: Create console command to purge attributes
Change-Id: I26eb77d574595547e4e6c5ca0189e7662ae83aa8
Diffstat (limited to 'plugins')
5 files changed, 253 insertions, 1 deletions
diff --git a/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/IntegrationTestSuite.java b/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/IntegrationTestSuite.java index 21dfa034886..2089b69baae 100644 --- a/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/IntegrationTestSuite.java +++ b/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/IntegrationTestSuite.java @@ -17,7 +17,7 @@ import org.junit.runners.Suite; * @author Roberto E. Escobar */ @RunWith(Suite.class) -@Suite.SuiteClasses({LoaderTest.class, OseeInfoDataAccessorTest.class}) +@Suite.SuiteClasses({LoaderTest.class, OseeInfoDataAccessorTest.class, PurgeAttributeTest.class}) public class IntegrationTestSuite { // Test Suite } diff --git a/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/PurgeAttributeTest.java b/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/PurgeAttributeTest.java new file mode 100644 index 00000000000..232c5041503 --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.db.test/src/org/eclipse/osee/orcs/db/intergration/PurgeAttributeTest.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2015 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.db.intergration; + +import static org.eclipse.osee.orcs.db.intergration.IntegrationUtil.integrationRule; +import java.util.LinkedList; +import java.util.List; +import org.eclipse.osee.jdbc.JdbcClient; +import org.eclipse.osee.jdbc.JdbcService; +import org.eclipse.osee.jdbc.JdbcStatement; +import org.eclipse.osee.orcs.db.internal.callable.PurgeAttributesDatabaseTxCallable; +import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory; +import org.eclipse.osee.orcs.db.mock.OsgiService; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestRule; + +/** + * @author John Misinco + */ +public class PurgeAttributeTest { + + @Rule + public TestRule db = integrationRule(this); + + @OsgiService + public JdbcService jdbcService; + @OsgiService + public SqlJoinFactory sqlJoinFactory; + + @Test + public void testPurgeAttribute() throws Exception { + JdbcClient jdbcClient = jdbcService.getClient(); + JdbcStatement stmt = jdbcClient.getStatement(); + + int prePurgeAttributeCount = getCount(jdbcClient, "osee_attribute"); + int preAttributeRows = getCount(jdbcClient, "osee_attribute where value = 'Software Requirements'"); + + Assert.assertTrue(preAttributeRows > 0); + + int prePurgeTxsCount = getCount(jdbcClient, "osee_txs"); + + stmt.runPreparedQuery("select attr_id from osee_attribute where value = 'Software Requirements'"); + List<Long> toPurge = new LinkedList<Long>(); + while (stmt.next()) { + toPurge.add(stmt.getLong("attr_id")); + } + + PurgeAttributesDatabaseTxCallable callable = + new PurgeAttributesDatabaseTxCallable(null, null, jdbcClient, sqlJoinFactory, toPurge, null); + + callable.call(); + + int postPurgeAttributeCount = getCount(jdbcClient, "osee_attribute"); + int postAttributeRows = getCount(jdbcClient, "osee_attribute where value = 'Software Requirements'"); + int postPurgeTxsCount = getCount(jdbcClient, "osee_txs"); + + Assert.assertEquals(0, postAttributeRows); + Assert.assertEquals(prePurgeAttributeCount - preAttributeRows, postPurgeAttributeCount); + Assert.assertTrue(postPurgeAttributeCount < prePurgeAttributeCount); + Assert.assertTrue(postPurgeTxsCount < prePurgeTxsCount); + } + + private int getCount(JdbcClient jdbcClient, String table) { + JdbcStatement stmt = jdbcClient.getStatement(); + int toReturn = -1; + stmt.runPreparedQuery("select count(1) from " + table); + while (stmt.next()) { + toReturn = stmt.getInt(1); + } + return toReturn; + } +} diff --git a/plugins/org.eclipse.osee.orcs.db/OSGI-INF/purge.attributes.console.command.xml b/plugins/org.eclipse.osee.orcs.db/OSGI-INF/purge.attributes.console.command.xml new file mode 100644 index 00000000000..b55a8b3b41d --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.db/OSGI-INF/purge.attributes.console.command.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"> + <implementation class="org.eclipse.osee.orcs.db.internal.console.PurgeAttributeCommand"/> + <service> + <provide interface="org.eclipse.osee.console.admin.ConsoleCommand"/> + </service> + <reference bind="setLogger" cardinality="1..1" interface="org.eclipse.osee.logger.Log" name="Log" policy="static"/> + <reference bind="setJdbcService" cardinality="1..1" interface="org.eclipse.osee.jdbc.JdbcService" name="JdbcService" policy="static" target="(osgi.binding=orcs.jdbc.service)"/> + <reference bind="setSqlJoinFactory" cardinality="1..1" interface="org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory" name="SqlJoinFactory" policy="static"/> +</scr:component> diff --git a/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/callable/PurgeAttributesDatabaseTxCallable.java b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/callable/PurgeAttributesDatabaseTxCallable.java new file mode 100644 index 00000000000..75b84103046 --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/callable/PurgeAttributesDatabaseTxCallable.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * Copyright (c) 2015 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.db.internal.callable; + +import java.util.Collection; +import org.eclipse.osee.console.admin.Console; +import org.eclipse.osee.framework.jdk.core.type.OseeCoreException; +import org.eclipse.osee.jdbc.JdbcClient; +import org.eclipse.osee.jdbc.JdbcConnection; +import org.eclipse.osee.jdbc.JdbcStatement; +import org.eclipse.osee.jdbc.OseePreparedStatement; +import org.eclipse.osee.logger.Log; +import org.eclipse.osee.orcs.OrcsSession; +import org.eclipse.osee.orcs.db.internal.sql.join.IdJoinQuery; +import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory; + +public final class PurgeAttributesDatabaseTxCallable extends AbstractDatastoreTxCallable<Void> { + private static final String SELECT_ATTRIBUTE_GAMMAS = + "select gamma_id from osee_attribute, osee_join_id where attr_id = id and query_id = ?"; + + private final SqlJoinFactory joinFactory; + private final Collection<Long> idsToPurge; + private final Console console; + + public PurgeAttributesDatabaseTxCallable(Log logger, OrcsSession session, JdbcClient jdbcClient, SqlJoinFactory joinFactory, Collection<Long> idsToPurge, Console console) { + super(logger, session, jdbcClient); + this.joinFactory = joinFactory; + this.idsToPurge = idsToPurge; + this.console = console; + } + + @Override + protected Void handleTxWork(JdbcConnection connection) throws OseeCoreException { + IdJoinQuery idJoin = joinFactory.createIdJoinQuery(); + JdbcStatement chStmt = getJdbcClient().getStatement(connection); + try { + OseePreparedStatement attrBatch = + getJdbcClient().getBatchStatement(connection, "delete from osee_attribute where attr_id = ?"); + OseePreparedStatement txBatch = + getJdbcClient().getBatchStatement(connection, "delete from osee_txs where gamma_id = ?"); + + for (Long id : idsToPurge) { + idJoin.add(id); + attrBatch.addToBatch(id); + } + idJoin.store(connection); + + chStmt.runPreparedQuery(SELECT_ATTRIBUTE_GAMMAS, idJoin.getQueryId()); + while (chStmt.next()) { + txBatch.addToBatch(chStmt.getLong("gamma_id")); + } + + writeToConsole("Deleting gammas from osee_txs..."); + int deleted = txBatch.execute(); + writeToConsole(deleted + " rows deleted."); + + // execute after txBatch + writeToConsole("Deleting attributes from osee_attribute..."); + deleted = attrBatch.execute(); + writeToConsole(deleted + " rows deleted."); + writeToConsole("Operation Finished"); + } finally { + chStmt.close(); + idJoin.delete(connection); + } + return null; + } + + private void writeToConsole(String s) { + if (console != null) { + console.writeln(s); + } + } + +} diff --git a/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/console/PurgeAttributeCommand.java b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/console/PurgeAttributeCommand.java new file mode 100644 index 00000000000..1423a6e0a7e --- /dev/null +++ b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/console/PurgeAttributeCommand.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2015 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.orcs.db.internal.console; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.Callable; +import org.eclipse.osee.console.admin.Console; +import org.eclipse.osee.console.admin.ConsoleParameters; +import org.eclipse.osee.framework.jdk.core.util.Collections; +import org.eclipse.osee.jdbc.JdbcClient; +import org.eclipse.osee.orcs.db.internal.callable.PurgeAttributesDatabaseTxCallable; +import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory; + +/** + * @author John Misinco + */ +public class PurgeAttributeCommand extends AbstractDatastoreConsoleCommand { + + private SqlJoinFactory joinFactory; + + public void setSqlJoinFactory(SqlJoinFactory joinFactory) { + this.joinFactory = joinFactory; + } + + @Override + public String getName() { + return "purge_attribute"; + } + + @Override + public String getDescription() { + return "Purges attribute instances from datastore"; + } + + @Override + public String getUsage() { + return "[force=<TRUE|FALSE>] attr_id=<ATTRIBUTE_ID,...>"; + } + + @Override + public Callable<Void> createCallable(final Console console, final ConsoleParameters params) { + String[] attrIds = params.getArray("attr_id"); + Set<Long> longIds = new HashSet<Long>(); + for (String id : attrIds) { + longIds.add(Long.parseLong(id)); + } + + boolean force = params.getBoolean("force"); + JdbcClient jdbcClient = getJdbcClient(); + if (force) { + + return new PurgeAttributesDatabaseTxCallable(getLogger(), getSession(), jdbcClient, joinFactory, longIds, + console); + } else { + return new Callable<Void>() { + + @Override + public Void call() throws Exception { + console.writeln("Attribute IDs: "); + console.writeln(Collections.toString(", ", longIds)); + console.writeln("Re-run with the force option to execute the command"); + return null; + } + }; + } + } + +} |