Skip to main content
summaryrefslogtreecommitdiffstats
blob: 84ac8d3664cba061f35607e96e1d632a309e17c1 (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
/*******************************************************************************
 * 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 static org.eclipse.osee.framework.core.enums.DeletionFlag.EXCLUDE_DELETED;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
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.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;

/**
 * @author Ryan D. Brooks
 * @author Roberto E. Escobar
 */
public class RepeatEnumerationAttributeValues extends DatabaseHealthOperation {

   private final static String FIND_REPEAT_ENUMS =
      "select DISTINCT(art1.guid), att1.art_id, att1.value, att1.attr_type_id from osee_attribute att1, osee_attribute att2, osee_txs txs1, osee_txs txs2, osee_artifact art1 where att1.gamma_id = txs1.gamma_id and txs1.branch_id = ? and att2.gamma_id = txs2.gamma_id and txs2.branch_id = ? and att1.art_id = att2.art_id and att1.attr_id <> att2.attr_id and att1.value = att2.value and txs1.tx_current = " + TxChange.CURRENT.getValue() + " and txs2.tx_current = " + TxChange.CURRENT.getValue() + " and att1.attr_type_id = att2.attr_type_id and art1.art_id = att1.art_id order by att1.art_id, att1.attr_type_id, att1.value";

   public RepeatEnumerationAttributeValues() {
      super("Repeat Enumeration Attribute Values");
   }

   @Override
   public String getFixTaskName() {
      return "Delete Repeat Enumeration Attribute Values";
   }

   @Override
   protected void doHealthCheck(IProgressMonitor monitor) throws Exception {
      HashCollection<Branch, AttrData> attributesWithErrors = new HashCollection<Branch, AttrData>();
      List<Branch> branches = BranchManager.getBaselineBranches();
      if (branches.isEmpty()) {
         throw new OseeStateException("no branches found");
      }
      for (Branch branch : branches) {
         Collection<AttrData> datas = getRepeatEnumeratedAttrs(monitor, branch);
         if (!datas.isEmpty()) {
            attributesWithErrors.put(branch, datas);
         }
      }
      monitor.worked(calculateWork(0.40));

      appendToDetails(AHTML.beginMultiColumnTable(100, 1));
      appendToDetails(AHTML.addHeaderRowMultiColumnTable(new String[] {"GUID", "ATTR TYPE ID", "VALUE"}));
      for (Branch branch : attributesWithErrors.keySet()) {
         appendToDetails(AHTML.addRowSpanMultiColumnTable(branch.getName(), 3));
         for (AttrData attrData : attributesWithErrors.getValues(branch)) {
            appendToDetails(AHTML.addRowMultiColumnTable(new String[] {
               attrData.getArtifactGuid(),
               AttributeTypeManager.getType(attrData.getAttributeTypeId()).getName(),
               attrData.getValue()}));
         }
      }
      appendToDetails(AHTML.endMultiColumnTable());

      monitor.worked(calculateWork(0.10));
      checkForCancelledStatus(monitor);

      setItemsToFix(attributesWithErrors.size());
      checkForCancelledStatus(monitor);
      if (isFixOperationEnabled() && hadItemsToFix()) {
         for (Branch branch : attributesWithErrors.keySet()) {
            Collection<AttrData> attributeData = attributesWithErrors.getValues(branch);
            List<String> artifactGuids = new ArrayList<String>(attributeData.size());
            for (AttrData attrData : attributeData) {
               artifactGuids.add(attrData.getArtifactGuid());
            }

            ArtifactQuery.getArtifactListFromIds(artifactGuids, branch, EXCLUDE_DELETED); // bulk load for speed
            SkynetTransaction transaction =
               new SkynetTransaction(branch, "Delete Repeat Attribute Values for" + branch.getShortName());
            for (AttrData attrData : attributeData) {
               Artifact artifact = ArtifactQuery.getArtifactFromId(attrData.getArtifactGuid(), branch);
               AttributeType attributeType = AttributeTypeManager.getType(attrData.getAttributeTypeId());
               if (attributeType.isEnumerated()) {
                  artifact.setAttributeValues(attributeType, artifact.getAttributesToStringList(attributeType));
                  transaction.addArtifactAndAttributes(artifact);
               }
            }
            transaction.execute();
         }
      }
      monitor.worked(calculateWork(0.40));

      getSummary().append(String.format("[%s] Repeat Enumeration Attribute Values found\n", getItemsToFixCount()));
      monitor.worked(calculateWork(0.10));
   }

   @Override
   public String getCheckDescription() {
      return "Searches for attributes of the same artifact having the same values in top level branches";
   }

   @Override
   public String getFixDescription() {
      return "Deletes the repeat attribute values using a transaction directly on the branch";
   }

   private Set<AttrData> getRepeatEnumeratedAttrs(IProgressMonitor monitor, Branch branch) throws OseeDataStoreException {
      Set<AttrData> attrData = new HashSet<AttrData>();
      IOseeStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(FIND_REPEAT_ENUMS, branch.getId(), branch.getId());
         while (chStmt.next()) {
            checkForCancelledStatus(monitor);
            attrData.add(new AttrData(chStmt.getString("guid"), chStmt.getInt("attr_type_id"),
               chStmt.getString("value")));
         }
      } finally {
         chStmt.close();
      }
      return attrData;
   }

   private final class AttrData {
      private final String artifactGuid;
      private final int attributeTypeId;
      private final String value;

      public AttrData(String artifactGuid, int attributeTypeId, String value) {
         super();
         this.artifactGuid = artifactGuid;
         this.attributeTypeId = attributeTypeId;
         this.value = value;
      }

      public String getArtifactGuid() {
         return artifactGuid;
      }

      public int getAttributeTypeId() {
         return attributeTypeId;
      }

      public String getValue() {
         return value;
      }

      @Override
      public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + (artifactGuid == null ? 0 : artifactGuid.hashCode());
         result = prime * result + attributeTypeId;
         result = prime * result + (value == null ? 0 : value.hashCode());
         return result;
      }

      @Override
      public boolean equals(Object obj) {
         if (this == obj) {
            return true;
         }
         AttrData other = (AttrData) obj;
         if (artifactGuid == null) {
            if (other.artifactGuid != null) {
               return false;
            }
         } else if (!artifactGuid.equals(other.artifactGuid)) {
            return false;
         }
         if (attributeTypeId != other.attributeTypeId) {
            return false;
         }
         if (value == null) {
            if (other.value != null) {
               return false;
            }
         } else if (!value.equals(other.value)) {
            return false;
         }
         return true;
      }
   }
}

Back to the top