Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4863d0beb78359b9e4babdf16829eda47e826357 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*******************************************************************************
 * 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.skynet.core.event;

import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import net.jini.core.entry.Entry;
import net.jini.core.lookup.ServiceItem;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jini.discovery.EclipseJiniClassloader;
import org.eclipse.osee.framework.jini.discovery.IServiceLookupListener;
import org.eclipse.osee.framework.jini.discovery.ServiceDataStore;
import org.eclipse.osee.framework.jini.service.core.SimpleFormattedEntry;
import org.eclipse.osee.framework.jini.util.OseeJini;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.event.skynet.ASkynetEventListener;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetArtifactEvent;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEvent;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEventListener;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetEventService;
import org.eclipse.osee.framework.messaging.event.skynet.ISkynetRelationLinkEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkAccessControlArtifactsEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkArtifactChangeTypeEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkArtifactDeletedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkArtifactModifiedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkArtifactPurgeEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkBroadcastEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkCommitBranchEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkDeletedBranchEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkNewBranchEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkPurgeBranchEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkRelationLinkCreatedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkRelationLinkDeletedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkRelationLinkRationalModifiedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkRenameBranchEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.NetworkTransactionDeletedEvent;
import org.eclipse.osee.framework.messaging.event.skynet.event.SkynetAttributeChange;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactCache;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactModType;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
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.event2.AccessControlEvent;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.relation.RelationEventType;
import org.eclipse.osee.framework.skynet.core.relation.RelationLink;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeManager;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.skynet.core.utility.LoadedArtifacts;
import org.eclipse.osee.framework.ui.plugin.event.UnloadedArtifact;
import org.eclipse.osee.framework.ui.plugin.event.UnloadedRelation;

/**
 * Manages remote events from the SkynetEventService.
 * 
 * @author Jeff C. Phillips
 * @author Donald G. Dunne
 */
public class RemoteEventManager {
   private static String ACCEPTABLE_SERVICE;

   private static final RemoteEventManager instance = new RemoteEventManager();

   private final InternalSkynetEventManager internalSkynetEventManager;
   private final ISkynetEventListener clientEventListener;
   private ISkynetEventListener clientEventListenerRemoteReference;

   private RemoteEventManager() {
      super();
      if (OseeEventManager.isOldEvents()) {
         OseeLog.log(Activator.class, Level.INFO, "REM1 Enabled");
         internalSkynetEventManager = new InternalSkynetEventManager();
         clientEventListener = new EventListener();
         checkJiniRegistration();
      } else {
         OseeLog.log(Activator.class, Level.INFO, "REM1 Disabled");
         internalSkynetEventManager = null;
         clientEventListener = null;
      }
   }

   private void checkJiniRegistration() {
      if (OseeEventManager.isNewEvents()) {
         return;
      }
      if (clientEventListenerRemoteReference == null) {
         try {
            // We need to trigger authentication before attempting to get database information from client session manager.
            UserManager.getUser();
            ACCEPTABLE_SERVICE =
               ClientSessionManager.getDataStoreName() + ":" + ClientSessionManager.getDataStoreLoginName();
            clientEventListenerRemoteReference =
               (ISkynetEventListener) OseeJini.getRemoteReference(clientEventListener);
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
            clientEventListenerRemoteReference = null;
         }

         if (clientEventListenerRemoteReference != null) {
            ServiceDataStore.getEclipseInstance(EclipseJiniClassloader.getInstance()).addListener(
               internalSkynetEventManager, ISkynetEventService.class);
         }
      }
   }

   private static ISkynetEventListener getClientEventListenerRemoteReference() {
      instance.checkJiniRegistration();
      return instance.clientEventListenerRemoteReference;
   }

   private static InternalSkynetEventManager getEventServiceManager() {
      return instance.internalSkynetEventManager;
   }

   public static void deregisterFromRemoteEventManager() {
      if (OseeEventManager.isNewEvents()) {
         return;
      }
      ServiceDataStore.getEclipseInstance(EclipseJiniClassloader.getInstance()).removeListener(getEventServiceManager());
      getEventServiceManager().reset();
   }

   public static void kick(Collection<ISkynetEvent> events) {
      kick(events.toArray(new ISkynetEvent[events.size()]));
   }

   public static boolean isConnected() {
      return OseeEventManager.isOldEvents() && getEventServiceManager().isValid();
   }

   public static void kick(final ISkynetEvent... events) {
      if (OseeEventManager.isOldEvents() && isConnected()) {
         Job job = new Job("Send Event") {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
               getEventServiceManager().kick(events, getClientEventListenerRemoteReference());
               return Status.OK_STATUS;
            }
         };

         job.schedule();
      }
      /*
       * This will enable a testing loopback that will take the kicked remote events and loop them back as if they came
       * from an external client. It will allow for the testing of the OEM -> REM -> OEM processing. In addition, this
       * onEvent is put in a non-display thread which will test that all handling by applications is properly handled by
       * doing all processing and then kicking off display-thread when need to update ui. SessionId needs to be modified
       * so this client doesn't think the events came from itself.
       */
      if (InternalEventManager.isEnableRemoteEventLoopback()) {
         OseeEventManager.eventLog("REM: Loopback enabled - Returning events as Remote event.");
         Thread thread = new Thread() {
            @Override
            public void run() {
               try {
                  String newSessionId = GUID.create();
                  for (ISkynetEvent event : events) {
                     event.getNetworkSender().sessionId = newSessionId;
                  }
                  instance.clientEventListener.onEvent(events);
               } catch (RemoteException ex) {
                  OseeLog.log(Activator.class, Level.SEVERE, ex);

               }
            }
         };
         thread.start();
      }
   }

   private static class InternalSkynetEventManager implements IServiceLookupListener {
      private ISkynetEventService currentEventService;

      private InternalSkynetEventManager() {
         currentEventService = null;
      }

      public ISkynetEventService getReference() {
         return currentEventService;
      }

      public boolean isValid() {
         return isValidService(currentEventService);
      }

      public void reset() {
         setEventService(null);
      }

      public void kick(ISkynetEvent[] events, ISkynetEventListener... except) {
         try {
            getReference().kick(events, except);
         } catch (ExportException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         } catch (RemoteException ex) {
            disconnectService(ex);
         }
      }

      private boolean isValidService(ISkynetEventService service) {
         boolean result = false;
         try {
            if (service != null) {
               result = service.isAlive();
            }
         } catch (Exception ex) {
            // Do Nothing
            result = false;
         }
         return result;
      }

      private synchronized void setEventService(ISkynetEventService service) {
         if (isValidService(currentEventService)) {
            try {
               currentEventService.deregister(getClientEventListenerRemoteReference());
            } catch (RemoteException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
         currentEventService = service;
      }

      private void disconnectService(Exception e) {
         if (OseeEventManager.isNewEvents()) {
            return;
         }
         OseeLog.log(Activator.class, Level.WARNING, "Skynet Event Service connection lost\n" + e.toString(), e);
         setEventService(null);
         try {
            OseeEventManager.kickLocalRemEvent(instance, RemoteEventServiceEventType.Rem1_DisConnected);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

      private void connectToService(ISkynetEventService service) {
         if (OseeEventManager.isNewEvents()) {
            return;
         }
         try {
            ISkynetEventListener clientListener = getClientEventListenerRemoteReference();
            if (clientListener != null) {
               service.register(clientListener);
               setEventService(service);
               OseeLog.log(Activator.class, Level.INFO,
                  "Skynet Event Service connection established " + ACCEPTABLE_SERVICE);
               OseeEventManager.kickLocalRemEvent(this, RemoteEventServiceEventType.Rem1_Connected);
            } else {
               OseeLog.log(Activator.class, Level.SEVERE, "Client listener reference was null");
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }

      @Override
      public void serviceAdded(ServiceItem serviceItem) {
         if (OseeEventManager.isNewEvents()) {
            return;
         }
         if (serviceItem.service instanceof ISkynetEventService) {
            ISkynetEventService service = (ISkynetEventService) serviceItem.service;
            if (isValidService(service)) {
               // Check if the service is for the database we are using
               for (Entry entry : serviceItem.attributeSets) {
                  if (entry instanceof SimpleFormattedEntry) {
                     SimpleFormattedEntry simpleEntry = (SimpleFormattedEntry) entry;
                     if ("db".equals(simpleEntry.name) && ACCEPTABLE_SERVICE.equals(simpleEntry.value)) {
                        connectToService(service);
                        break;
                     }
                  }
               }
            }
         }
      }

      @Override
      public void serviceChanged(ServiceItem serviceItem) {
         serviceAdded(serviceItem);
      }

      @Override
      public void serviceRemoved(ServiceItem serviceItem) {
         // do nothing
      }
   }

   private static class EventListener extends ASkynetEventListener {
      private static final long serialVersionUID = -3017349745450262540L;
      private static final ISchedulingRule mutexRule = new ISchedulingRule() {

         @Override
         public boolean contains(ISchedulingRule rule) {
            return rule == this;
         }

         @Override
         public boolean isConflicting(ISchedulingRule rule) {
            return rule == this;
         }
      };

      @Override
      public void onEvent(final ISkynetEvent[] events) {

         final List<ArtifactTransactionModifiedEvent> xModifiedEvents =
            new LinkedList<ArtifactTransactionModifiedEvent>();
         Job job = new Job("Receive Event") {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
               //               Arrays.sort(events);

               Sender lastArtifactRelationModChangeSender = null;

               for (ISkynetEvent event : events) {

                  Sender sender = new Sender(event.getNetworkSender());
                  // If the sender's sessionId is the same as this client, then this event was
                  // created in this client and returned by remote event manager; ignore and continue
                  if (sender.isLocal()) {
                     continue;
                  }

                  if (event instanceof NetworkAccessControlArtifactsEvent) {
                     try {
                        AccessControlEvent accessControlEvent = new AccessControlEvent();
                        AccessControlEventType accessControlModType =
                           AccessControlEventType.valueOf(((NetworkAccessControlArtifactsEvent) event).getAccessControlModTypeName());
                        accessControlEvent.setEventType(accessControlModType);
                        NetworkAccessControlArtifactsEvent accessEvent = (NetworkAccessControlArtifactsEvent) event;
                        Integer[] artIds =
                           accessEvent.getArtifactIds().toArray(new Integer[accessEvent.getArtifactIds().size()]);
                        Branch branch = BranchManager.getBranch(accessEvent.getId());
                        for (int x = 0; x < accessEvent.getArtifactIds().size(); x++) {
                           Artifact cachedArt = ArtifactQuery.getArtifactFromId(artIds[x], branch);
                           if (cachedArt != null) {
                              accessControlEvent.getArtifacts().add(cachedArt.getBasicGuidArtifact());
                           }
                        }
                        LoadedArtifacts loadedArtifacts =
                           new LoadedArtifacts(accessEvent.getId(), accessEvent.getArtifactIds(),
                              accessEvent.getArtifactTypeIds());
                        InternalEventManager.kickAccessControlArtifactsEvent(sender, accessControlEvent,
                           loadedArtifacts);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkRenameBranchEvent) {
                     int branchId = ((NetworkRenameBranchEvent) event).getId();
                     try {
                        Branch branch = BranchManager.getBranch(branchId);
                        branch.setName(((NetworkRenameBranchEvent) event).getBranchName());
                        branch.clearDirty();
                        try {
                           InternalEventManager.kickBranchEvent(sender, BranchEventType.Renamed, branchId);
                        } catch (Exception ex) {
                           OseeLog.log(Activator.class, Level.SEVERE, ex);
                        }
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkNewBranchEvent) {
                     int branchId = ((NetworkNewBranchEvent) event).getId();
                     try {
                        InternalEventManager.kickBranchEvent(sender, BranchEventType.Added, branchId);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkDeletedBranchEvent) {
                     int branchId = ((NetworkDeletedBranchEvent) event).getId();
                     try {
                        Branch branch =
                           Activator.getInstance().getOseeCacheService().getBranchCache().getById(branchId);
                        if (branch != null) {
                           branch.setBranchState(BranchState.DELETED);
                           branch.clearDirty();
                        }
                        InternalEventManager.kickBranchEvent(sender, BranchEventType.Deleted, branchId);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkPurgeBranchEvent) {
                     int branchId = ((NetworkPurgeBranchEvent) event).getId();
                     try {
                        BranchCache cache = Activator.getInstance().getOseeCacheService().getBranchCache();
                        Branch branch = cache.getById(branchId);
                        if (branch != null) {
                           cache.decache(branch);
                        }
                        InternalEventManager.kickBranchEvent(sender, BranchEventType.Purged, branchId);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkCommitBranchEvent) {
                     int branchId = ((NetworkCommitBranchEvent) event).getId();
                     try {
                        try {
                           TransactionManager.clearCommitArtifactCacheForAssociatedArtifact(BranchManager.getAssociatedArtifact(BranchManager.getBranch(branchId)));
                        } catch (OseeCoreException ex) {
                           OseeLog.log(Activator.class, Level.SEVERE, ex);
                        }
                        InternalEventManager.kickBranchEvent(sender, BranchEventType.Committed, branchId);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkBroadcastEvent) {
                     try {
                        final BroadcastEventType broadcastEventType =
                           BroadcastEventType.valueOf(((NetworkBroadcastEvent) event).getBroadcastEventTypeName());
                        if (broadcastEventType == null) {
                           OseeLog.log(
                              Activator.class,
                              Level.SEVERE,
                              "Unknown broadcast event type \"" + ((NetworkBroadcastEvent) event).getBroadcastEventTypeName() + "\"",
                              new IllegalArgumentException());
                        } else {
                           InternalEventManager.kickBroadcastEvent(sender, broadcastEventType,
                              ((NetworkBroadcastEvent) event).getUserIds(),
                              ((NetworkBroadcastEvent) event).getMessage());
                        }
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof ISkynetArtifactEvent) {
                     try {
                        updateArtifacts(sender, (ISkynetArtifactEvent) event, xModifiedEvents);
                        lastArtifactRelationModChangeSender = sender;
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof ISkynetRelationLinkEvent) {
                     try {
                        updateRelations(sender, (ISkynetRelationLinkEvent) event, xModifiedEvents);
                        lastArtifactRelationModChangeSender = sender;
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkArtifactChangeTypeEvent) {
                     try {
                        LoadedArtifacts loadedArtifacts =
                           new LoadedArtifacts(((NetworkArtifactChangeTypeEvent) event).getId(),
                              ((NetworkArtifactChangeTypeEvent) event).getArtifactIds(),
                              ((NetworkArtifactChangeTypeEvent) event).getArtifactTypeIds());
                        InternalEventManager.kickArtifactsChangeTypeEvent(sender,
                           ((NetworkArtifactChangeTypeEvent) event).getToArtifactTypeId(), loadedArtifacts);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkArtifactPurgeEvent) {
                     try {
                        LoadedArtifacts loadedArtifacts =
                           new LoadedArtifacts(((NetworkArtifactPurgeEvent) event).getId(),
                              ((NetworkArtifactPurgeEvent) event).getArtifactIds(),
                              ((NetworkArtifactPurgeEvent) event).getArtifactTypeIds());
                        for (Artifact artifact : loadedArtifacts.getLoadedArtifacts()) {
                           //This is because applications may still have a reference to the artifact
                           for (RelationLink link : RelationManager.getRelationsAll(artifact.getArtId(),
                              artifact.getBranch().getId(), false)) {
                              link.internalRemoteEventDelete();
                           }
                           ArtifactCache.deCache(artifact);
                           artifact.internalSetDeleted();
                        }
                        InternalEventManager.kickArtifactsPurgedEvent(sender, loadedArtifacts);
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  } else if (event instanceof NetworkTransactionDeletedEvent) {
                     try {
                        InternalEventManager.kickTransactionsPurgedEvent(sender,
                           ((NetworkTransactionDeletedEvent) event).getTransactionIds());
                     } catch (Exception ex) {
                        OseeLog.log(Activator.class, Level.SEVERE, ex);
                     }
                  }
               }

               if (xModifiedEvents.size() > 0) {
                  /*
                   * Since transaction events are a collection of ArtifactModfied and RelationModified events, create a
                   * new Sender based on the last sender for these events.
                   */
                  Sender transactionSender =
                     new Sender("RemoteEventManager", lastArtifactRelationModChangeSender.getOseeSession());
                  InternalEventManager.kickPersistEvent(transactionSender, xModifiedEvents);
               }

               return Status.OK_STATUS;
            }
         };
         job.setSystem(true);
         job.setUser(false);
         job.setRule(mutexRule);
         job.schedule();
      }

      /**
       * Updates local cache
       * 
       * @param event
       */
      private static void updateArtifacts(Sender sender, ISkynetArtifactEvent event, Collection<ArtifactTransactionModifiedEvent> xModifiedEvents) {
         if (event == null) {
            return;
         }

         try {
            int artId = event.getArtId();
            int artTypeId = event.getArtTypeId();
            List<String> dirtyAttributeName = new LinkedList<String>();

            if (event instanceof NetworkArtifactModifiedEvent) {
               int branchId = ((NetworkArtifactModifiedEvent) event).getId();
               Artifact artifact = ArtifactCache.getActive(artId, branchId);
               if (artifact == null) {
                  UnloadedArtifact unloadedArtifact = new UnloadedArtifact(branchId, artId, artTypeId);
                  xModifiedEvents.add(new ArtifactModifiedEvent(sender, ArtifactModType.Changed, unloadedArtifact));
               } else if (!artifact.isHistorical()) {
                  for (SkynetAttributeChange skynetAttributeChange : ((NetworkArtifactModifiedEvent) event).getAttributeChanges()) {
                     if (!InternalEventManager.isEnableRemoteEventLoopback()) {
                        try {
                           Attribute<?> attribute =
                              artifact.getAttributeById(skynetAttributeChange.getAttributeId(), true);
                           // Attribute already exists (but may be deleted), process update
                           // Process MODIFIED / DELETED attribute
                           if (attribute != null) {
                              if (attribute.isDirty()) {
                                 dirtyAttributeName.add(attribute.getNameValueDescription());
                                 OseeEventManager.eventLog(String.format(
                                    "%s's attribute %d [/n%s/n] has been overwritten.", artifact.getSafeName(),
                                    attribute.getId(), attribute.toString()));
                              }
                              try {
                                 ModificationType modificationType = skynetAttributeChange.getModificationType();
                                 if (modificationType == null) {
                                    OseeLog.log(
                                       Activator.class,
                                       Level.SEVERE,
                                       String.format("MOD1: Can't get mod type for %s's attribute %d.",
                                          artifact.getArtifactTypeName(), skynetAttributeChange.getAttributeId()));
                                    continue;
                                 }
                                 if (modificationType.isDeleted()) {
                                    attribute.internalSetModificationType(modificationType);
                                 } else {
                                    attribute.getAttributeDataProvider().loadData(skynetAttributeChange.getData());
                                 }
                                 attribute.internalSetGammaId(skynetAttributeChange.getGammaId());
                                 attribute.setNotDirty();
                              } catch (OseeCoreException ex) {
                                 OseeEventManager.eventLog(
                                    String.format("Exception updating %s's attribute %d [/n%s/n].",
                                       artifact.getSafeName(), attribute.getId(), attribute.toString()), ex);
                              }
                           }
                           // Otherwise, attribute needs creation
                           // Process NEW attribute
                           else {
                              ModificationType modificationType = skynetAttributeChange.getModificationType();
                              if (modificationType == null) {
                                 OseeLog.log(
                                    Activator.class,
                                    Level.SEVERE,
                                    String.format("MOD2: Can't get mod type for %s's attribute %d.",
                                       artifact.getArtifactTypeName(), skynetAttributeChange.getAttributeId()));
                                 continue;
                              }
                              artifact.internalInitializeAttribute(
                                 AttributeTypeManager.getType(skynetAttributeChange.getTypeId()),
                                 skynetAttributeChange.getAttributeId(), skynetAttributeChange.getGammaId(),
                                 modificationType, false, skynetAttributeChange.getData());
                           }
                        } catch (OseeCoreException ex) {
                           OseeEventManager.eventLog(String.format(
                              "Exception updating %s's attribute change for attributeTypeId %d.",
                              artifact.getSafeName(), skynetAttributeChange.getTypeId()), ex);
                        }
                     }
                  }

                  xModifiedEvents.add(new ArtifactModifiedEvent(sender, ArtifactModType.Changed, artifact,
                     ((NetworkArtifactModifiedEvent) event).getTransactionId(),
                     ((NetworkArtifactModifiedEvent) event).getAttributeChanges()));

               }
            } else if (event instanceof NetworkArtifactDeletedEvent) {
               int branchId = ((NetworkArtifactDeletedEvent) event).getId();
               Artifact artifact = ArtifactCache.getActive(artId, branchId);
               if (artifact == null) {
                  UnloadedArtifact unloadedArtifact = new UnloadedArtifact(branchId, artId, artTypeId);
                  xModifiedEvents.add(new ArtifactModifiedEvent(sender, ArtifactModType.Deleted, unloadedArtifact));
               } else if (!artifact.isHistorical()) {
                  internalHandleRemoteArtifactDeleted(artifact);

                  xModifiedEvents.add(new ArtifactModifiedEvent(sender, ArtifactModType.Deleted, artifact,
                     ((NetworkArtifactDeletedEvent) event).getTransactionId(), new ArrayList<SkynetAttributeChange>()));
               }
            }
         } catch (OseeCoreException ex) {
            OseeEventManager.eventLog("Update Artifacts", ex);
         }
      }

      private static void updateRelations(Sender sender, ISkynetRelationLinkEvent event, Collection<ArtifactTransactionModifiedEvent> xModifiedEvents) {
         if (event == null) {
            return;
         }

         try {
            RelationType relationType = RelationTypeManager.getType(event.getRelTypeId());
            Branch branch = BranchManager.getBranch(event.getId());
            Artifact aArtifact = ArtifactCache.getActive(event.getArtAId(), branch.getId());
            Artifact bArtifact = ArtifactCache.getActive(event.getArtBId(), branch.getId());
            boolean aArtifactLoaded = aArtifact != null;
            boolean bArtifactLoaded = bArtifact != null;

            if (!aArtifactLoaded && !bArtifactLoaded) {
               if (event instanceof NetworkRelationLinkDeletedEvent) {
                  UnloadedRelation unloadedRelation =
                     new UnloadedRelation(branch.getId(), event.getArtAId(), event.getArtATypeId(), event.getArtBId(),
                        event.getArtBTypeId(), event.getRelTypeId());
                  xModifiedEvents.add(new RelationModifiedEvent(sender, RelationEventType.Deleted, unloadedRelation));
               } else if (event instanceof NetworkRelationLinkRationalModifiedEvent) {
                  UnloadedRelation unloadedRelation =
                     new UnloadedRelation(branch.getId(), event.getArtAId(), event.getArtATypeId(), event.getArtBId(),
                        event.getArtBTypeId(), event.getRelTypeId());
                  xModifiedEvents.add(new RelationModifiedEvent(sender, RelationEventType.ModifiedRationale,
                     unloadedRelation));
               } else if (event instanceof NetworkRelationLinkCreatedEvent) {
                  UnloadedRelation unloadedRelation =
                     new UnloadedRelation(branch.getId(), event.getArtAId(), event.getArtATypeId(), event.getArtBId(),
                        event.getArtBTypeId(), event.getRelTypeId());
                  xModifiedEvents.add(new RelationModifiedEvent(sender, RelationEventType.Added, unloadedRelation));
               }
            }
            if (aArtifactLoaded || bArtifactLoaded) {
               if (event instanceof NetworkRelationLinkDeletedEvent) {
                  RelationLink relation =
                     RelationManager.getLoadedRelationById(event.getRelId(), event.getArtAId(), event.getArtBId(),
                        branch, branch);
                  if (relation != null) {
                     relation.internalRemoteEventDelete();

                     xModifiedEvents.add(new RelationModifiedEvent(sender, RelationEventType.Deleted, relation,
                        relation.getBranch(), relation.getRelationType().getName()));
                  }
               } else if (event instanceof NetworkRelationLinkCreatedEvent) {
                  RelationLink relation =
                     RelationManager.getLoadedRelationById(event.getRelId(), event.getArtAId(), event.getArtBId(),
                        branch, branch);

                  if (relation == null || relation.getModificationType() == ModificationType.DELETED) {
                     relation =
                        RelationLink.getOrCreate(event.getArtAId(), event.getArtBId(), branch, branch, relationType,
                           event.getRelId(), event.getGammaId(),
                           ((NetworkRelationLinkCreatedEvent) event).getRationale(), ModificationType.NEW);

                     xModifiedEvents.add(new RelationModifiedEvent(sender, RelationEventType.Added, relation,
                        relation.getBranch(), relation.getRelationType().getName()));
                  }
               }
            }
         } catch (OseeCoreException ex) {
            OseeEventManager.eventLog("Update Relations", ex);
         }
      }
   }

   public static void internalHandleRemoteArtifactDeleted(Artifact artifact) throws OseeCoreException {
      if (artifact == null) {
         return;
      } else {
         artifact.internalSetDeletedFromRemoteEvent();
      }
   }

}

Back to the top