Skip to main content
summaryrefslogtreecommitdiffstats
blob: 33a3d92a71f70cb465fcb6b02f1e6698560a2e73 (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
/*******************************************************************************
 * 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.framework.skynet.core.internal.event.handlers;

import java.util.Collection;
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.event.DefaultBasicGuidArtifact;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.messaging.event.res.AttributeEventModificationType;
import org.eclipse.osee.framework.messaging.event.res.msgs.RemotePersistEvent1;
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.Attribute;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.ChangeArtifactType;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.event.EventUtil;
import org.eclipse.osee.framework.skynet.core.event.FrameworkEventUtil;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.AttributeChange;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidRelation;
import org.eclipse.osee.framework.skynet.core.event.model.EventChangeTypeBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.EventModType;
import org.eclipse.osee.framework.skynet.core.event.model.EventModifiedBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.skynet.core.internal.event.EventHandlerRemote;
import org.eclipse.osee.framework.skynet.core.internal.event.Transport;
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;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactRemoteEventHandler implements EventHandlerRemote<RemotePersistEvent1> {

   @Override
   public void handle(Transport transport, Sender sender, RemotePersistEvent1 remoteEvent) throws OseeCoreException {
      RemotePersistEvent1 event1 = remoteEvent;
      ArtifactEvent transEvent = FrameworkEventUtil.getPersistEvent(event1);
      updateArtifacts(sender, transEvent.getArtifacts(), remoteEvent.getTransactionId());
      updateRelations(sender, transEvent.getRelations());
      transport.send(sender, transEvent);
   }

   private void updateArtifacts(Sender sender, Collection<EventBasicGuidArtifact> artifacts, int transactionId) throws OseeCoreException {
      // Don't crash on any one artifact update problem (no update method throughs exceptions)
      for (EventBasicGuidArtifact guidArt : artifacts) {
         EventUtil.eventLog(String.format("REM: updateArtifact -> [%s]", guidArt));
         EventModType eventModType = guidArt.getModType();
         if (BranchManager.branchExists(guidArt.getBranchGuid())) {
            switch (eventModType) {
               case Added:
                  // Handle Added Artifacts
                  // Nothing to do for added cause they're not in cache yet.  Apps will load if they need them.
                  // do nothing cause not in cache
                  break;
               case Modified:
                  updateModifiedArtifact((EventModifiedBasicGuidArtifact) guidArt, transactionId);
                  break;
               case ChangeType:
                  ChangeArtifactType.handleRemoteChangeType((EventChangeTypeBasicGuidArtifact) guidArt);
                  break;
               case Deleted:
               case Purged:
                  updateDeletedArtifact(guidArt);
                  break;
               default:
                  // Unknown mod type
                  EventUtil.eventLog(String.format("REM: updateArtifacts - Unhandled mod type [%s]",
                     guidArt.getModType()));
                  break;
            }
         }
      }
   }

   private void updateDeletedArtifact(DefaultBasicGuidArtifact guidArt) {
      try {
         Artifact artifact = ArtifactCache.getActive(guidArt);
         if (artifact == null) {
            // do nothing, artifact not in cache, so don't need to update
         } else if (!artifact.isHistorical()) {
            artifact.internalSetDeletedFromRemoteEvent();
         }
      } catch (OseeCoreException ex) {
         EventUtil.eventLog("REM: updateDeletedArtifact", ex);
      }
   }

   private void updateModifiedArtifact(EventModifiedBasicGuidArtifact guidArt, int transactionId) {
      try {
         Artifact artifact = ArtifactCache.getActive(guidArt);
         if (artifact == null) {
            // do nothing, artifact not in cache, so don't need to update
         } else if (!artifact.isHistorical()) {
            artifact.setTransactionId(transactionId);
            for (AttributeChange attrChange : guidArt.getAttributeChanges()) {
               if (!OseeEventManager.getPreferences().isEnableRemoteEventLoopback()) {
                  ModificationType modificationType =
                     AttributeEventModificationType.getType(attrChange.getModTypeGuid()).getModificationType();
                  AttributeType attributeType = AttributeTypeManager.getTypeByGuid(attrChange.getAttrTypeGuid());
                  try {
                     Attribute<?> attribute = artifact.getAttributeById(attrChange.getAttributeId(), true);
                     // Attribute already exists (but may be deleted), process update
                     // Process MODIFIED / DELETED attribute
                     if (attribute != null) {
                        if (attribute.isDirty()) {
                           EventUtil.eventLog(String.format("%s's attribute %d [/n%s/n] has been overwritten.",
                              artifact.getSafeName(), attribute.getId(), attribute.toString()));
                        }
                        try {
                           if (modificationType == null) {
                              EventUtil.eventLog(String.format(
                                 "REM: updateModifiedArtifact - Can't get mod type for %s's attribute %d.",
                                 artifact.getArtifactTypeName(), attrChange.getAttributeId()));
                              continue;
                           }
                           if (modificationType.isDeleted()) {
                              attribute.internalSetModType(modificationType, false, false);
                           } else {
                              attribute.getAttributeDataProvider().loadData(
                                 attrChange.getData().toArray(new Object[attrChange.getData().size()]));
                           }
                           attribute.setNotDirty();
                           attribute.internalSetGammaId(attrChange.getGammaId());
                        } catch (OseeCoreException ex) {
                           EventUtil.eventLog(
                              String.format("REM: Exception updating %s's attribute %d [/n%s/n].",
                                 artifact.getSafeName(), attribute.getId(), attribute.toString()), ex);
                        }
                     }
                     // Otherwise, attribute needs creation
                     // Process NEW attribute
                     else {
                        if (modificationType == null) {
                           EventUtil.eventLog(String.format("REM: Can't get mod type for %s's attribute %d.",
                              artifact.getArtifactTypeName(), attrChange.getAttributeId()));
                           continue;
                        }
                        artifact.internalInitializeAttribute(attributeType, attrChange.getAttributeId(),
                           attrChange.getGammaId(), modificationType, false,
                           attrChange.getData().toArray(new Object[attrChange.getData().size()]));
                     }
                  } catch (OseeCoreException ex) {
                     EventUtil.eventLog(
                        String.format("REM: Exception updating %s's attribute change for attributeTypeId %d.",
                           artifact.getSafeName(), attributeType.getId()), ex);
                  }
               }
            }
         }
      } catch (OseeCoreException ex) {
         EventUtil.eventLog("REM: updateModifiedArtifact", ex);
      }
   }

   private void updateRelations(Sender sender, Collection<EventBasicGuidRelation> relations) {
      for (EventBasicGuidRelation guidArt : relations) {
         // Don't crash on any one relation update problem
         try {
            EventUtil.eventLog(String.format("REM: updateRelation -> [%s]", guidArt));

            Branch branch = BranchManager.getBranch(guidArt.getArtA());
            RelationType relationType = RelationTypeManager.getTypeByGuid(guidArt.getRelTypeGuid());
            Artifact aArtifact = ArtifactCache.getActive(guidArt.getArtA());
            Artifact bArtifact = ArtifactCache.getActive(guidArt.getArtB());
            // Nothing in cache, ignore this relation only
            if (aArtifact == null && bArtifact == null) {
               continue;
            }
            boolean aArtifactLoaded = aArtifact != null;
            boolean bArtifactLoaded = bArtifact != null;

            if (aArtifactLoaded || bArtifactLoaded) {
               RelationLink relation =
                  RelationManager.getLoadedRelationById(guidArt.getRelationId(), guidArt.getArtAId(),
                     guidArt.getArtBId(), branch);

               RelationEventType eventType = guidArt.getModType();
               switch (eventType) {
                  case Added:
                     if (relation == null || relation.getModificationType() == ModificationType.DELETED || relation.getModificationType() == ModificationType.ARTIFACT_DELETED) {
                        relation =
                           RelationManager.getOrCreate(guidArt.getArtAId(), guidArt.getArtBId(), branch, relationType,
                              guidArt.getRelationId(), guidArt.getGammaId(), guidArt.getRationale(),
                              ModificationType.NEW);
                     }
                     break;
                  case ModifiedRationale:
                     if (relation != null) {
                        relation.internalSetRationale(guidArt.getRationale());
                        relation.setNotDirty();
                     }
                     break;
                  case Deleted:
                  case Purged:
                     if (relation != null) {
                        relation.internalRemoteEventDelete();
                     }
                     break;
                  case Undeleted:
                     if (relation != null) {
                        relation.undelete();
                        relation.setNotDirty();
                     }
                     break;
                  default:
                     EventUtil.eventLog(String.format("REM: updateRelations - Unhandled mod type [%s]", eventType));
                     break;
               }
            }
         } catch (OseeCoreException ex) {
            EventUtil.eventLog("REM: updateRelations", ex);
         }
      }
   }
}

Back to the top