Skip to main content
summaryrefslogtreecommitdiffstats
blob: 646d7109608048c50036ea1f30c7f89e91f0e890 (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
/*******************************************************************************
 * Copyright (c) 2012 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.ats.core.client.workflow;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.core.client.internal.Activator;
import org.eclipse.osee.ats.core.client.util.AtsUtilCore;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.util.XResultData;
import org.eclipse.osee.framework.jdk.core.util.AXml;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;

/**
 * This operation will take source workflows and convert state names. Use this BLAM when Work Definition changes require
 * state re-name or removal. This BLAM will fix old workflows. This works by reading the attributes on the workflow and
 * search/replacing the fromStateName to the toStateName. NOTE: ATS log will show name changes, but not OSEE history.";
 * 
 * @author Donald G. Dunne
 */
public class ConvertWorkflowStatesOperation extends AbstractOperation {

   private final Collection<? extends AbstractWorkflowArtifact> workflows;
   private final Map<String, String> fromStateToStateMap;
   private final XResultData rd;
   private final boolean persist;
   private final Pattern pattern = Pattern.compile("^[0-9A-Za-z-_ ]+$");

   /**
    * @param fromStateToStateMap mapping of pairs of fromStateName and toStateName strings
    * @param persist true if changes are to be persisted to database
    * @param rd will contain results of changes which should be reviewed before persisting to DB
    */
   public ConvertWorkflowStatesOperation(Map<String, String> fromStateToStateMap, Collection<? extends AbstractWorkflowArtifact> workflows, boolean persist, XResultData rd) {
      super("Convert ATS Workflow States", Activator.PLUGIN_ID);
      this.fromStateToStateMap = fromStateToStateMap;
      this.workflows = workflows;
      this.persist = persist;
      this.rd = rd;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      SkynetTransaction transaction = null;
      if (persist) {
         transaction = TransactionManager.createTransaction(AtsUtilCore.getAtsBranchToken(), getName());
      }
      if (fromStateToStateMap.isEmpty()) {
         rd.logError("Must enter FromToState pairs");
      } else if (!stateNamesAreValid(fromStateToStateMap, rd)) {
         return;
      } else if (workflows.isEmpty()) {
         rd.logError("No workflows entered");
      } else {
         try {
            for (AbstractWorkflowArtifact awa : workflows) {
               convertCurrentState(awa);
               convertStates(awa);
               convertCompletedFromState(awa);
               convertCancelledFromState(awa);
               convertLogStates(awa);
               if (persist) {
                  awa.persist(transaction);
               }
            }
         } catch (OseeCoreException ex) {
            rd.logError(ex.getLocalizedMessage() + " (see error log)");
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      if (persist) {
         transaction.execute();
      }
   }

   private boolean stateNamesAreValid(Map<String, String> fromStateToStateMap, XResultData rd) {
      boolean valid = true;
      for (Entry<String, String> fromStateNameToStateName : fromStateToStateMap.entrySet()) {
         String fromStateName = fromStateNameToStateName.getKey();
         if (!stateNameIsValid(fromStateName, rd)) {
            valid = false;
         }
         String toStateName = fromStateNameToStateName.getValue();
         if (!stateNameIsValid(toStateName, rd)) {
            valid = false;
         }
      }
      return valid;
   }

   private boolean stateNameIsValid(String stateName, XResultData rd) {
      Matcher m = pattern.matcher(stateName);
      if (!m.find()) {
         rd.logErrorWithFormat(
            "State name must be alpha-numeric with dashes, spaces or underscores.  Invalid for [%s]", stateName);
         return false;
      }
      return true;
   }

   private void convertLogStates(AbstractWorkflowArtifact awa) throws OseeCoreException {
      String logStr = awa.getSoleAttributeValue(AtsAttributeTypes.Log, "");
      String resultLogStr = logStr;
      for (Entry<String, String> fromToState : fromStateToStateMap.entrySet()) {
         String fromStr = fromToState.getKey();
         String toStr = fromToState.getValue();
         resultLogStr = resultLogStr.replaceAll("state=\"" + fromStr + "\"", "state=\"" + toStr + "\"");
      }
      if (!logStr.equals(resultLogStr)) {
         awa.setSoleAttributeValue(AtsAttributeTypes.Log, resultLogStr);
         rd.logWithFormat("Converted <can't display>\n", AXml.xmlToText(logStr), AXml.xmlToText(resultLogStr));
      }
   }

   @SuppressWarnings("deprecation")
   private void convertStateAttributes(AbstractWorkflowArtifact awa, IAttributeType attrType) throws OseeCoreException {
      for (Attribute<Object> attribute : awa.getAttributes(attrType)) {
         for (Entry<String, String> fromToState : fromStateToStateMap.entrySet()) {
            if (((String) attribute.getValue()).startsWith(fromToState.getKey() + ";")) {
               String fromStr = ((String) attribute.getValue());
               String toStr = fromStr.replaceFirst(fromToState.getKey() + ";", fromToState.getValue() + ";");
               attribute.setValue(toStr);
               rd.logWithFormat("Convert [%s] \n   [%s] to \n   [%s]\n", attrType.getName(), fromStr, toStr);
            }
         }
      }
   }

   private void convertCurrentState(AbstractWorkflowArtifact awa) throws OseeCoreException {
      convertStateAttributes(awa, AtsAttributeTypes.CurrentState);
   }

   private void convertStates(AbstractWorkflowArtifact awa) throws OseeCoreException {
      convertStateAttributes(awa, AtsAttributeTypes.State);
   }

   private void convertCompletedFromState(AbstractWorkflowArtifact awa) throws OseeCoreException {
      convertExactMatchAttributeValue(awa, AtsAttributeTypes.CompletedFromState);
   }

   private void convertCancelledFromState(AbstractWorkflowArtifact awa) throws OseeCoreException {
      convertExactMatchAttributeValue(awa, AtsAttributeTypes.CancelledFromState);
   }

   private void convertExactMatchAttributeValue(AbstractWorkflowArtifact awa, IAttributeType attrType) throws OseeCoreException {
      List<Attribute<Object>> attributes = awa.getAttributes(attrType);
      if (attributes != null && !attributes.isEmpty()) {
         for (Attribute<Object> attribute : attributes) {
            for (Entry<String, String> fromToState : fromStateToStateMap.entrySet()) {
               if (attribute.getValue().equals(fromToState.getKey())) {
                  String fromStr = ((String) attribute.getValue());
                  String toStr = fromToState.getValue();
                  attribute.setValue(toStr);
                  rd.logWithFormat("Convert [%s] \n   [%s] to \n   [%s]\n", attrType.getName(), fromStr, toStr);
               }
            }
         }
      }
   }
}

Back to the top