Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c23add103a83ab8b0c0409346c767bf1388dd2c6 (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
/*********************************************************************
 * Copyright (c) 2004, 2007 Boeing
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Boeing - initial API and implementation
 **********************************************************************/

package org.eclipse.osee.framework.ui.skynet.dbHealth;

import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.jdk.core.result.XResultData;
import org.eclipse.osee.framework.jdk.core.util.AHTML;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.utility.ConnectionHandler;
import org.eclipse.osee.framework.ui.skynet.results.XResultDataUI;
import org.eclipse.osee.jdbc.JdbcStatement;

/**
 * @author Donald G. Dunne
 */
public class AuthorIdCheck extends DatabaseHealthOperation {

   private static final String GET_AUTHOR_IDS = "select distinct (author) from osee_tx_details";
   private static final String UPDATE_AUTHOR_IDS = "update osee_tx_details set author=? where author=?";

   public AuthorIdCheck() {
      super("Author Id Check");
   }

   @Override
   protected void doHealthCheck(IProgressMonitor monitor) throws Exception {
      monitor.subTask("Querying for AuthorIds");
      displayReport(monitor);
      checkForCancelledStatus(monitor);
      monitor.worked(calculateWork(0.50));
      getSummary().append(String.format("Completed"));
      monitor.worked(calculateWork(0.10));
   }

   private void displayReport(IProgressMonitor monitor) throws Exception {
      XResultData rd = new XResultData();
      try {
         String[] columnHeaders = new String[] {"Item", "Author Id", "Results"};
         rd.log("Errors show in red.");
         rd.addRaw(AHTML.beginMultiColumnTable(100, 1));
         rd.addRaw(AHTML.addHeaderRowMultiColumnTable(columnHeaders));

         Set<Long> authors = new HashSet<>();
         JdbcStatement chStmt1 = ConnectionHandler.getStatement();
         try {
            chStmt1.runPreparedQuery(GET_AUTHOR_IDS);
            while (chStmt1.next()) {
               checkForCancelledStatus(monitor);
               Long author = chStmt1.getLong("author");
               authors.add(author);
            }
         } finally {
            chStmt1.close();
         }
         int num = 0;
         StringBuffer infoSb = new StringBuffer(500);
         for (Long author : authors) {
            System.out.println(String.format("Processing [%d] %d/%d...", author, num++, authors.size()));
            if (author.equals(Long.valueOf(0))) {
               rd.addRaw(AHTML.addRowMultiColumnTable("TX_DETAILS", String.valueOf(author),
                  "Warning: Skipping author == 0; this is ok, but may want to change in future"));
               continue;
            }
            try {
               Artifact artifact = ArtifactQuery.getArtifactFromId(author, COMMON);
               if (artifact == null) {
                  rd.addRaw(
                     AHTML.addRowMultiColumnTable("TX_DETAILS", String.valueOf(author), "Error: Artifact Not Found"));
                  if (isFixOperationEnabled()) {
                     rd.addRaw("Fix needed here");
                  }
               } else if (artifact.isDeleted()) {
                  rd.addRaw(AHTML.addRowMultiColumnTable("TX_DETAILS", String.valueOf(author),
                     "Error: Artifact marked as deleted"));
                  if (isFixOperationEnabled()) {
                     rd.addRaw("Fix needed here");
                  }
               } else {
                  infoSb.append(String.format("Successfully found author [%s] as [%s]\n", String.valueOf(author),
                     artifact.getName()));
               }
            } catch (Exception ex) {
               rd.addRaw(AHTML.addRowMultiColumnTable("TX_DETAILS", String.valueOf(author),
                  "Error: " + ex.getLocalizedMessage()));
               if (isFixOperationEnabled() && ex.getLocalizedMessage().contains("No artifact found with id")) {
                  updateTxAuthor(author);
                  rd.addRaw(String.format("Fix: Updated author [%s] to OSEE System", author));
               }
            }
         }
         rd.addRaw(AHTML.endMultiColumnTable());
         rd.addRaw(infoSb.toString());
         getSummary().append("Processed " + authors.size() + " author ids\n");

      } finally {
         XResultDataUI.report(rd, getName());
      }
   }

   private static void updateTxAuthor(Long author) throws Exception {
      ConnectionHandler.runPreparedUpdate(UPDATE_AUTHOR_IDS, SystemUser.OseeSystem, author);
   }

   @Override
   public String getCheckDescription() {
      return "Verifies that all author art ids match an un-deleted artifact on Common branch (usually a User artifact)";
   }

   @Override
   public String getFixDescription() {
      return "Sets all invalid authors to the \"OSEE System\" user artifact's art_id.";
   }

}

Back to the top