Skip to main content
summaryrefslogtreecommitdiffstats
blob: 514acefeef62bcc8faa9f1e0093af1d7fbe1105f (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
 * Copyright (c) 2010 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.editor;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.ats.api.commit.ICommitConfigItem;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.version.IAtsVersion;
import org.eclipse.osee.ats.core.client.action.ActionArtifact;
import org.eclipse.osee.ats.core.client.action.ActionManager;
import org.eclipse.osee.ats.core.client.review.AbstractReviewArtifact;
import org.eclipse.osee.ats.core.client.review.ReviewManager;
import org.eclipse.osee.ats.core.client.task.AbstractTaskableArtifact;
import org.eclipse.osee.ats.core.client.task.TaskArtifact;
import org.eclipse.osee.ats.core.client.team.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.core.client.util.AtsUtilClient;
import org.eclipse.osee.ats.core.client.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.internal.Activator;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IArtifactEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidRelation;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.ui.swt.Displays;

/**
 * Common location for event handling for SMAEditors in order to keep number of registrations and processing to a
 * minimum.
 * 
 * @author Donald G. Dunne
 */
public class SMAEditorArtifactEventManager implements IArtifactEventListener {

   List<ISMAEditorEventHandler> handlers = new CopyOnWriteArrayList<ISMAEditorEventHandler>();
   static SMAEditorArtifactEventManager instance = new SMAEditorArtifactEventManager();

   private SMAEditorArtifactEventManager() {
      OseeEventManager.addListener(this);
   }

   public static void add(ISMAEditorEventHandler iWorldEventHandler) {
      instance.handlers.add(iWorldEventHandler);
   }

   public static void remove(ISMAEditorEventHandler iWorldEventHandler) {
      if (instance != null) {
         instance.handlers.remove(iWorldEventHandler);
      }
   }

   @Override
   public List<? extends IEventFilter> getEventFilters() {
      return AtsUtilClient.getAtsObjectEventFilters();
   }

   @Override
   public void handleArtifactEvent(final ArtifactEvent artifactEvent, Sender sender) {
      for (ISMAEditorEventHandler handler : handlers) {
         if (handler.isDisposed()) {
            handlers.remove(handler);
         }
      }
      try {
         if (!artifactEvent.isForBranch(AtsUtilCore.getAtsBranch())) {
            return;
         }
      } catch (OseeCoreException ex) {
         return;
      }
      for (final ISMAEditorEventHandler handler : handlers) {
         try {
            safelyProcessHandler(artifactEvent, handler);
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, "Error processing event handler - " + handler, ex);
         }
      }
   }

   private void safelyProcessHandler(final ArtifactEvent artifactEvent, final ISMAEditorEventHandler handler) throws OseeCoreException {
      final AbstractWorkflowArtifact awa = handler.getSMAEditor().getAwa();
      boolean refreshed = false;

      if (artifactEvent.isDeletedPurged(awa)) {
         handler.getSMAEditor().closeEditor();
      } else if (
      //
      workflowModifiedOrReloaded(artifactEvent, awa) ||
      //
      workflowRelationIsAddedChangedOrDeleted(artifactEvent, awa) ||
      //
      workflowActionIsModifedOrReloaded(artifactEvent, awa) ||
      //
      workflowActionRelationIsAddedChangedOrDeleted(artifactEvent, awa) ||
      //
      teamWorkflowParallelConfigurationChanged(artifactEvent, awa)
      //
      ) {
         refreshed = true;
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               handler.getSMAEditor().refreshPages();
            }

         });
      } else if (isReloaded(artifactEvent, awa)) {
         SMAEditor.close(Collections.singleton(awa), false);
         if (!awa.isDeleted()) {
            SMAEditor.editArtifact(awa);
         }
      }
      if (!refreshed && awa.isTeamWorkflow() && ReviewManager.hasReviews((TeamWorkFlowArtifact) awa)) {
         try {
            // If related review has made a change, redraw
            for (AbstractReviewArtifact reviewArt : ReviewManager.getReviews((TeamWorkFlowArtifact) awa)) {
               if (artifactEvent.isHasEvent(reviewArt)) {
                  refreshed = true;
                  Displays.ensureInDisplayThread(new Runnable() {
                     @Override
                     public void run() {
                        handler.getSMAEditor().refreshPages();
                     }
                  });
                  // Only refresh editor for first review that has event
                  break;
               }
            }
         } catch (Exception ex) {
            // do nothing
         }
      }
      if (!refreshed && awa.isTeamWorkflow() && ((TeamWorkFlowArtifact) awa).hasTaskArtifacts()) {
         try {
            // If related review has made a change, redraw
            for (TaskArtifact taskArt : ((TeamWorkFlowArtifact) awa).getTaskArtifactsFromCurrentState()) {
               if (artifactEvent.isHasEvent(taskArt)) {
                  refreshed = true;
                  Displays.ensureInDisplayThread(new Runnable() {
                     @Override
                     public void run() {
                        handler.getSMAEditor().refreshPages();
                     }
                  });
                  // Only refresh editor for first task that has event
                  break;
               }
            }
         } catch (Exception ex) {
            // do nothing
         }
      }
      if (!refreshed) {
         try {
            // Since SMAEditor is refreshed when a sibling workflow is changed, need to refresh this
            // list of actionable items when a sibling changes
            for (TeamWorkFlowArtifact teamWf : ActionManager.getTeams(awa.getParentActionArtifact())) {
               ActionArtifact parentAction = teamWf.getParentActionArtifact();
               if (!awa.equals(teamWf) && (artifactEvent.isHasEvent(teamWf) || (parentAction != null && artifactEvent.isRelAddedChangedDeleted(parentAction)))) {
                  refreshed = true;
                  Displays.ensureInDisplayThread(new Runnable() {
                     @Override
                     public void run() {
                        handler.getSMAEditor().refreshPages();
                     }
                  });
                  // Only need to refresh once
                  return;
               }
            }
         } catch (Exception ex) {
            // do nothing
         }
      }

   }

   /**
    * Return true if one of the versions configured for parallel dev had a parallel config relation add, change or
    * delete. Refreshing in this case is necessary so the commit manager will show the updated parallel configuration
    * changes.
    */
   private boolean teamWorkflowParallelConfigurationChanged(ArtifactEvent artifactEvent, AbstractWorkflowArtifact awa) {
      boolean changed = false;
      // Only handle for teamWorkflows
      if (awa instanceof TeamWorkFlowArtifact) {
         TeamWorkFlowArtifact teamWf = (TeamWorkFlowArtifact) awa;
         try {
            // Retrieve all config to commit items for this team Wf, which will contain all parallel version artifacts
            Collection<ICommitConfigItem> configArtifactsConfiguredToCommitTo =
               AtsClientService.get().getBranchService().getConfigArtifactsConfiguredToCommitTo(teamWf);
            for (Object obj : configArtifactsConfiguredToCommitTo) {
               if (obj instanceof IAtsVersion) {
                  IAtsVersion version = (IAtsVersion) obj;
                  for (EventBasicGuidRelation relation : artifactEvent.getRelations()) {
                     // If relation is parallel config and guid is one of parallel configured versions
                     if (relation.is(AtsRelationTypes.ParallelVersion_Child) && (relation.getArtA().getGuid().equals(
                        version.getGuid()) || relation.getArtB().getGuid().equals(version.getGuid()))) {
                        changed = true;
                        break;
                     }
                  }
               }
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      return changed;
   }

   private boolean workflowActionRelationIsAddedChangedOrDeleted(final ArtifactEvent artifactEvent, AbstractWorkflowArtifact awa) {
      boolean result = false;
      if (awa instanceof TeamWorkFlowArtifact) {
         TeamWorkFlowArtifact teamWf = (TeamWorkFlowArtifact) awa;
         try {
            Artifact actionArt = teamWf.getParentActionArtifact();
            result = artifactEvent.isRelAddedChangedDeleted(actionArt);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      return result;
   }

   private boolean workflowActionIsModifedOrReloaded(ArtifactEvent artifactEvent, AbstractWorkflowArtifact awa) {
      boolean result = false;
      if (awa instanceof TeamWorkFlowArtifact) {
         TeamWorkFlowArtifact teamWf = (TeamWorkFlowArtifact) awa;
         try {
            Artifact actionArt = teamWf.getParentActionArtifact();
            result = artifactEvent.isModifiedReloaded(actionArt);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
      return result;
   }

   private boolean workflowRelationIsAddedChangedOrDeleted(final ArtifactEvent artifactEvent, AbstractWorkflowArtifact awa) {
      return artifactEvent.isRelAddedChangedDeleted(awa);
   }

   private boolean workflowModifiedOrReloaded(final ArtifactEvent artifactEvent, AbstractWorkflowArtifact awa) {
      return artifactEvent.isModifiedReloaded(awa);
   }

   private boolean isReloaded(ArtifactEvent artifactEvent, AbstractWorkflowArtifact sma) {
      try {
         if (artifactEvent.isReloaded(sma)) {
            return true;
         }
         if (sma instanceof AbstractTaskableArtifact) {
            for (TaskArtifact taskArt : ((AbstractTaskableArtifact) sma).getTaskArtifacts()) {
               if (artifactEvent.isReloaded(taskArt)) {
                  return true;
               }
            }
         }
         if (sma.isTeamWorkflow()) {
            for (AbstractReviewArtifact reviewArt : ReviewManager.getReviews((TeamWorkFlowArtifact) sma)) {
               if (artifactEvent.isReloaded(reviewArt)) {
                  return true;
               }
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
         return false;
      }
      return false;
   }

}

Back to the top