Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2849171671a28eb16e2ed2075efa8f9e43fcca02 (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
/*******************************************************************************
 * Copyright (c) 2013 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.blam.operation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.jdk.core.type.CountingMap;
import org.eclipse.osee.framework.jdk.core.type.MutableInteger;
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.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;

/**
 * @author Angel Avila
 */

public class AttributeCheckOperation extends AbstractOperation {

   private final List<Artifact> folders;
   private final IAttributeType attribute;
   private boolean changeValues;
   private final boolean multipleValuesRequested;
   private boolean multipleValuesFound;

   public AttributeCheckOperation(OperationLogger logger, List<Artifact> artifacts, IAttributeType attribute, boolean changeValues, boolean multipleRequested) {
      super("Attribute Check ", Activator.PLUGIN_ID, logger);
      this.folders = artifacts;
      this.attribute = attribute;
      this.changeValues = changeValues;
      this.multipleValuesRequested = multipleRequested;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws OseeCoreException {
      for (Artifact folder : folders) {
         if (!monitor.isCanceled()) {
            logf("\n\n**************CHECKING IN [%s]***************", folder);
            List<Artifact> allDescendants = folder.getDescendants();
            String popularValue = findMostPopularAttributeValue(allDescendants, monitor);
            compareAllChildrenWithPopularValue(allDescendants, popularValue, folder, monitor);
         }
      }
   }

   private String findMostPopularAttributeValue(List<Artifact> allDescendants, IProgressMonitor monitor) throws OseeCoreException {
      CountingMap<String> countingMap = new CountingMap<String>();

      for (Artifact child : allDescendants) {
         if (!monitor.isCanceled()) {
            List<String> attributeValues = child.getAttributeValues(attribute);
            for (String attributeValue : attributeValues) {
               if (multipleValuesRequested) {
                  if (attributeValues.size() > 1) {
                     multipleValuesFound = true;
                  }
               }
               countingMap.put(attributeValue);
            }
         }
      }

      Set<Entry<String, MutableInteger>> counts = countingMap.getCounts();
      String mostPopular = "";
      int mostPopularCount = 0;

      for (Entry<String, MutableInteger> count : counts) {
         if (!monitor.isCanceled()) {
            int countValueInt = count.getValue().getValue();
            if (countValueInt > mostPopularCount && !mostPopular.equals(count.getKey())) {
               mostPopular = count.getKey();
               mostPopularCount = countValueInt;
            }
         }
      }

      return mostPopular;
   }

   @SuppressWarnings("unchecked")
   private void compareAllChildrenWithPopularValue(List<Artifact> allDescendants, String popularValue, Artifact folder, IProgressMonitor monitor) throws OseeCoreException {
      if (!allDescendants.isEmpty()) {
         IOseeBranch tempBranch = allDescendants.get(0).getBranch();
         Branch branch = BranchManager.getBranch(tempBranch);
         SkynetTransaction transaction = TransactionManager.createTransaction(branch, "Attribute Check Blam");
         if (!branch.getBranchType().isWorkingBranch()) {
            changeValues = false;
            logf("\n\nCANNOT MAKE ANY CHANGES BECAUSE ARTIFACTS ARE NOT ON A WORKING BRANCH");
         }

         if (multipleValuesRequested != multipleValuesFound) {
            changeValues = false;
            logf("=============WARNING==============");
            logf("NO CHANGES WILL BE MADE\n");
            if (multipleValuesRequested) {
               logf(
                  "No artifacts with multiple values for [%s] were found under the selected folder(s).  All attributes will be expected to have exactly ONE value.",
                  attribute);
               logf(String.format(
                  "To allow this blam to set multiple values, please change an artifact under the folder [%s] to contain multiple values using Artifact Editor.",
                  folder));
            }
            if (multipleValuesFound) {
               logf(String.format(
                  "Artifacts with multiple values for [%s] were found under the selected folder [%s], however, the \'Multiple values allowed for this attribute?\' check box was not selected.",
                  attribute, folder));
            }
         }

         for (Artifact art : allDescendants) {
            if (!monitor.isCanceled()) {
               List<String> attributeValues = art.getAttributeValues(attribute);
               List<String> newValues = attributeValues;
               if (!attributeValues.isEmpty()) {
                  boolean changesNeeded = false;
                  if (multipleValuesRequested && !attributeValues.contains(popularValue)) {
                     logf("[%s] does not contain the value [%s] for [%s]", art, popularValue, attribute);
                     newValues.add(popularValue);
                     changesNeeded = true;
                  } else if (!multipleValuesRequested && !attributeValues.equals(Collections.singletonList(popularValue))) {
                     logf("[%s]'s value for [%s] is NOT equal to [%s]", art, attribute, popularValue);
                     newValues = Arrays.asList(popularValue);
                     changesNeeded = true;
                  }
                  if (changeValues && changesNeeded) {
                     art.setAttributeValues(attribute, newValues);
                     art.persist(transaction);
                     logf("------>Value of attribute [%s] in [%s] changed from [%s] to [%s]", attribute, art,
                        attributeValues, newValues);
                  }
               } else {
                  logf("[%s] has NO values for [%s].  No changes made", art, attribute);
               }
            }
         }
         if (changeValues) {
            transaction.execute();
         }

      }
   }
}

Back to the top