Skip to main content
summaryrefslogtreecommitdiffstats
blob: 71e2c99f4ed669236ff313ca9ac34b6dfb0efa37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.skynet.core.artifact;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.EventModType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.relation.RelationLink;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.utility.AbstractDbTxOperation;
import org.eclipse.osee.framework.skynet.core.utility.ArtifactJoinQuery;
import org.eclipse.osee.framework.skynet.core.utility.ConnectionHandler;
import org.eclipse.osee.framework.skynet.core.utility.JoinUtility;
import org.eclipse.osee.framework.skynet.core.utility.TransactionJoinQuery;
import org.eclipse.osee.jdbc.JdbcConnection;
import org.eclipse.osee.jdbc.JdbcStatement;

/**
 * @author Ryan D. Brooks
 */
public class PurgeArtifacts extends AbstractDbTxOperation {

   private static final String SELECT_ITEM_GAMMAS =
      "SELECT /*+ ordered */ txs.gamma_id, txs.transaction_id, aj.branch_id FROM osee_join_artifact aj, %s item, osee_txs txs WHERE aj.query_id = ? AND %s AND item.gamma_id = txs.gamma_id AND aj.branch_id = txs.branch_id";

   private static final String COUNT_ARTIFACT_VIOLATIONS =
      "SELECT art.art_id, txs.branch_id FROM osee_join_artifact aj, osee_artifact art, osee_txs txs WHERE aj.query_id = ? AND aj.art_id = art.art_id AND art.gamma_id = txs.gamma_id AND txs.branch_id = aj.branch_id";

   private static final String DELETE_FROM_TXS_USING_JOIN_TRANSACTION =
      "DELETE FROM osee_txs txs WHERE EXISTS (select 1 from osee_join_transaction jt WHERE jt.query_id = ? AND jt.branch_id = txs.branch_id AND jt.gamma_id = txs.gamma_id AND jt.transaction_id = txs.transaction_id)";

   private static final String DELETE_FROM_TX_DETAILS_USING_JOIN_TRANSACTION =
      "DELETE FROM osee_tx_details txd WHERE EXISTS (select 1 from osee_join_transaction jt WHERE jt.query_id = ? AND jt.branch_id = txd.branch_id AND jt.transaction_id = txd.transaction_id AND not exists (select * from osee_txs txs where jt.branch_id = txs.branch_id and jt.transaction_id = txs.transaction_id))";

   private final List<Artifact> artifactsToPurge;
   private boolean success;
   private final boolean recurseChildrenBranches;

   public PurgeArtifacts(Collection<? extends Artifact> artifactsToPurge) throws OseeCoreException {
      this(artifactsToPurge, false);
   }

   public PurgeArtifacts(Collection<? extends Artifact> artifactsToPurge, boolean recurseChildrenBranches) throws OseeCoreException {
      super(ConnectionHandler.getJdbcClient(), "Purge Artifact", Activator.PLUGIN_ID);
      this.artifactsToPurge = new LinkedList<>(artifactsToPurge);
      this.success = false;
      this.recurseChildrenBranches = recurseChildrenBranches;
   }

   @Override
   protected void doTxWork(IProgressMonitor monitor, JdbcConnection connection) throws OseeCoreException {
      if (artifactsToPurge == null || artifactsToPurge.isEmpty()) {
         return;
      }

      checkPurgeValid(connection);

      // now load the artifacts to be purged
      Set<Artifact> childreArtifactsToPurge = new HashSet<>();
      for (Artifact art : artifactsToPurge) {
         childreArtifactsToPurge.addAll(art.getDescendants(DeletionFlag.INCLUDE_DELETED));
      }
      artifactsToPurge.addAll(childreArtifactsToPurge);

      ArtifactJoinQuery artJoin2 = JoinUtility.createArtifactJoinQuery(getJdbcClient());
      try {
         for (Artifact art : artifactsToPurge) {
            artJoin2.add(art.getArtId(), art.getBranchId());
         }
         artJoin2.store(connection);

         int queryId = artJoin2.getQueryId();

         TransactionJoinQuery txJoin = JoinUtility.createTransactionJoinQuery(getJdbcClient());

         insertSelectItems(txJoin, connection, "osee_relation_link",
            "(aj.art_id = item.a_art_id OR aj.art_id = item.b_art_id)", queryId);
         insertSelectItems(txJoin, connection, "osee_attribute", "aj.art_id = item.art_id", queryId);
         insertSelectItems(txJoin, connection, "osee_artifact", "aj.art_id = item.art_id", queryId);

         try {
            txJoin.store(connection);
            getJdbcClient().runPreparedUpdate(connection, DELETE_FROM_TXS_USING_JOIN_TRANSACTION, txJoin.getQueryId());
            getJdbcClient().runPreparedUpdate(connection, DELETE_FROM_TX_DETAILS_USING_JOIN_TRANSACTION,
               txJoin.getQueryId());
         } finally {
            txJoin.delete(connection);
         }

         for (Artifact artifact : artifactsToPurge) {
            ArtifactCache.deCache(artifact);
            RelationManager.deCache(artifact);
            artifact.internalSetDeleted();
            for (RelationLink rel : artifact.getRelationsAll(DeletionFlag.EXCLUDE_DELETED)) {
               rel.markAsPurged();
            }
            for (Attribute<?> attr : artifact.internalGetAttributes()) {
               attr.markAsPurged();
            }
         }
         success = true;
      } finally {
         artJoin2.delete(connection);
      }
   }

   @Override
   protected void handleTxFinally(IProgressMonitor monitor) throws OseeCoreException {
      if (success) {
         Set<EventBasicGuidArtifact> artifactChanges = new HashSet<>();
         for (Artifact artifact : artifactsToPurge) {
            artifactChanges.add(new EventBasicGuidArtifact(EventModType.Purged, artifact));
         }
         // Kick Local and Remote Events
         ArtifactEvent artifactEvent = new ArtifactEvent(artifactsToPurge.iterator().next().getBranch());
         for (EventBasicGuidArtifact guidArt : artifactChanges) {
            artifactEvent.getArtifacts().add(guidArt);
         }
         OseeEventManager.kickPersistEvent(PurgeArtifacts.class, artifactEvent);
      }
   }

   public void insertSelectItems(TransactionJoinQuery txJoin, JdbcConnection connection, String tableName, String artifactJoinSql, int queryId) throws OseeCoreException {
      String query = String.format(SELECT_ITEM_GAMMAS, tableName, artifactJoinSql);
      JdbcStatement chStmt = getJdbcClient().getStatement(connection);
      try {
         chStmt.runPreparedQuery(query, queryId);
         while (chStmt.next()) {
            txJoin.add(chStmt.getLong("gamma_id"), chStmt.getLong("transaction_id"), chStmt.getLong("branch_id"));
         }
      } finally {
         chStmt.close();
      }
   }

   private void checkPurgeValid(JdbcConnection connection) {
      ArtifactJoinQuery artJoin = JoinUtility.createArtifactJoinQuery(getJdbcClient());
      for (Artifact art : artifactsToPurge) {
         for (IOseeBranch branch : BranchManager.getChildBranches(art.getBranch(), true)) {
            artJoin.add(art.getArtId(), branch.getUuid());
         }
      }
      if (!artJoin.isEmpty()) {
         try {
            artJoin.store(connection);
            JdbcStatement chStmt = getJdbcClient().getStatement(connection);
            try {
               chStmt.runPreparedQuery(COUNT_ARTIFACT_VIOLATIONS, artJoin.getQueryId());
               boolean failed = false;
               StringBuilder sb = new StringBuilder();
               while (chStmt.next()) {
                  int artId = chStmt.getInt("art_id");
                  long branchUuid = chStmt.getLong("branch_id");
                  if (recurseChildrenBranches) {
                     BranchId branch = BranchId.valueOf(branchUuid);
                     Artifact artifactFromId = ArtifactQuery.getArtifactFromId(artId, branch);
                     artifactsToPurge.add(artifactFromId);
                  } else {
                     failed = true;
                     sb.append("ArtifactId[");
                     sb.append(artId);
                     sb.append("] BranchId[");
                     sb.append(branchUuid);
                     sb.append("]\n");
                  }
               }
               if (failed) {
                  throw new OseeCoreException(
                     "Unable to purge because the following artifacts exist on child branches.\n%s", sb.toString());
               }
            } finally {
               chStmt.close();
            }
         } finally {
            artJoin.delete(connection);
         }
      }
   }

}

Back to the top