Skip to main content
summaryrefslogtreecommitdiffstats
blob: 35ce71f9ba35d32c832ed9e004b4b644bac11812 (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
/*******************************************************************************
 * 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.ui.skynet.dbHealth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.util.AHTML;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactIdWithoutVersionsCheck extends DatabaseHealthOperation {

   private static final String GET_INVALID_A_ART_IDS =
      "select item.a_art_id as artId, item.rel_link_id as itemId from osee_relation_link item where NOT EXISTS (select oav.art_id from osee_artifact oav where oav.art_id = item.a_art_id)";

   private static final String GET_INVALID_B_ART_IDS =
      "select item.b_art_id as artId, item.rel_link_id as itemId from osee_relation_link item where NOT EXISTS (select oav.art_id from osee_artifact oav where oav.art_id = item.b_art_id)";

   private static final String GET_INVALID_ATTR_IDS_ART_IDS =
      "select item.art_id as artId, item.attr_id as itemId from osee_attribute item where NOT EXISTS (select oav.art_id from osee_artifact oav where oav.art_id = item.art_id)";

   private static final String GET_INVALID_ACL_ART_IDS =
      "select item.art_id as artId from osee_artifact_acl item where NOT EXISTS (select oav.art_id from osee_artifact oav where oav.art_id = item.art_id)";

   public ArtifactIdWithoutVersionsCheck() {
      super("Artifact Id Without osee_artifact Table Entry");
   }

   @Override
   protected void doHealthCheck(IProgressMonitor monitor) throws Exception {
      Set<Integer> allInvalidArtIds = new HashSet<Integer>();
      List<ItemEntry> itemsToDelete = new ArrayList<ItemEntry>();

      itemsToDelete.add(new ItemEntry("osee_relation_link", "rel_link_id", "a_art_id", //
         getInvalidEntries(monitor, allInvalidArtIds, GET_INVALID_A_ART_IDS, true)));

      itemsToDelete.add(new ItemEntry("osee_relation_link", "rel_link_id", "b_art_id", //
         getInvalidEntries(monitor, allInvalidArtIds, GET_INVALID_B_ART_IDS, true)));

      itemsToDelete.add(new ItemEntry("osee_attribute", "attr_id", "art_id", //
         getInvalidEntries(monitor, allInvalidArtIds, GET_INVALID_ATTR_IDS_ART_IDS, true)));

      itemsToDelete.add(new ItemEntry("osee_artifact_acl", "art_id", "art_id", //
         getInvalidEntries(monitor, allInvalidArtIds, GET_INVALID_ACL_ART_IDS, false)));

      int beforeArtifactCheck = allInvalidArtIds.size();

      setItemsToFix(allInvalidArtIds.size());
      createReport(monitor, beforeArtifactCheck, getItemsToFixCount(), itemsToDelete);

      if (isFixOperationEnabled() && getItemsToFixCount() > 0) {
         for (ItemEntry entry : itemsToDelete) {
            if (!entry.invalids.isEmpty()) {
               String deleteSql = String.format("delete from %s where %s = ?", entry.table, entry.itemIdName);
               List<Object[]> dataList = new ArrayList<Object[]>();
               for (Integer item : entry.invalids) {
                  dataList.add(new Object[] {item});
               }
               ConnectionHandler.runBatchUpdate(deleteSql, dataList);
            }
         }
      }
      getSummary().append(String.format("Found %s invalid artIds referenced\n", getItemsToFixCount()));
      monitor.worked(calculateWork(0.50));
   }

   private void createReport(IProgressMonitor monitor, int totalBeforeCheck, int totalArtIds, List<ItemEntry> itemsToDelete) throws IOException {
      appendToDetails(AHTML.beginMultiColumnTable(100, 1));
      appendToDetails(AHTML.beginMultiColumnTable(100, 1));
      appendToDetails(AHTML.addHeaderRowMultiColumnTable(new String[] {"TABLE", "REFERENCED_BY", "TOTAL INVALIDS"}));
      for (ItemEntry entry : itemsToDelete) {
         appendToDetails(AHTML.addRowMultiColumnTable(new String[] {
            entry.table,
            entry.invalidField,
            String.valueOf(entry.invalids.size())}));
      }
      appendToDetails(AHTML.endMultiColumnTable());
      monitor.worked(calculateWork(0.10));
      checkForCancelledStatus(monitor);
   }

   private Set<Integer> getInvalidEntries(IProgressMonitor monitor, Set<Integer> allInvalidArtIds, String query, boolean hasItemId) throws OseeDataStoreException {
      Set<Integer> toReturn = new HashSet<Integer>();
      IOseeStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(query);
         while (chStmt.next()) {
            if (hasItemId) {
               toReturn.add(chStmt.getInt("itemId"));
            }
            allInvalidArtIds.add(chStmt.getInt("artId"));
         }
      } finally {
         chStmt.close();
      }
      monitor.worked(calculateWork(0.10));
      checkForCancelledStatus(monitor);
      return hasItemId ? toReturn : allInvalidArtIds;
   }

   private final class ItemEntry {
      private final String table;
      private final String itemIdName;
      private final String invalidField;
      private final Set<Integer> invalids;

      public ItemEntry(String table, String itemIdName, String invalidField, Set<Integer> invalids) {
         super();
         this.table = table;
         this.itemIdName = itemIdName;
         this.invalidField = invalidField;
         this.invalids = invalids;
      }

   }

   @Override
   public String getCheckDescription() {
      return "Verifies that artifact entries in the relation, attribute and artifact tables have a valid entry in the osee_artifact table.";
   }

   @Override
   public String getFixDescription() {
      return "Removes invalid data from the corresponding tables, however does not clean up any addressing that might be left behind.";
   }
}

Back to the top