Skip to main content
summaryrefslogtreecommitdiffstats
blob: d634d4c21ae0d757cc52af7f722aa1546d86c723 (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
/*******************************************************************************
 * Copyright (c) 2009 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.httpRequests;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.TransactionToken;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.model.event.DefaultBasicUuidRelation;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.event.res.AttributeEventModificationType;
import org.eclipse.osee.framework.skynet.core.AccessPolicy;
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.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.change.AttributeChange;
import org.eclipse.osee.framework.skynet.core.change.Change;
import org.eclipse.osee.framework.skynet.core.change.RelationChange;
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.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEventType;
import org.eclipse.osee.framework.skynet.core.event.model.EventBasicGuidRelation;
import org.eclipse.osee.framework.skynet.core.event.model.EventModifiedBasicGuidArtifact;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.internal.ServiceUtil;
import org.eclipse.osee.framework.skynet.core.relation.RelationEventType;
import org.eclipse.osee.framework.skynet.core.revision.ChangeManager;
import org.eclipse.osee.framework.skynet.core.revision.LoadChangeType;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.jaxrs.client.JaxRsExceptions;
import org.eclipse.osee.orcs.rest.client.OseeClient;
import org.eclipse.osee.orcs.rest.model.BranchCommitOptions;
import org.eclipse.osee.orcs.rest.model.BranchEndpoint;
import org.eclipse.osee.orcs.rest.model.Transaction;

/**
 * @author Megumi Telles
 * @author Ryan D. Brooks
 */
public final class CommitBranchHttpRequestOperation extends AbstractOperation {
   private final ArtifactId committer;
   private final BranchId sourceBranch;
   private final BranchId destinationBranch;
   private final boolean isArchiveAllowed;
   private final boolean skipChecksAndEvents;

   public CommitBranchHttpRequestOperation(ArtifactId committer, BranchId sourceBranch, BranchId destinationBranch, boolean isArchiveAllowed, boolean skipChecksAndEvents) {
      super("Commit " + sourceBranch, Activator.PLUGIN_ID);
      this.committer = committer;
      this.sourceBranch = sourceBranch;
      this.destinationBranch = destinationBranch;
      this.isArchiveAllowed = isArchiveAllowed;
      this.skipChecksAndEvents = skipChecksAndEvents;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws OseeCoreException {
      BranchState currentState = BranchManager.getState(sourceBranch);
      BranchManager.getBranch(sourceBranch).setBranchState(BranchState.COMMIT_IN_PROGRESS); // the server changes the state in the database to COMMIT_IN_PROGRESS

      BranchEvent branchEvent = new BranchEvent(BranchEventType.Committing, sourceBranch, destinationBranch);
      OseeEventManager.kickBranchEvent(getClass(), branchEvent);

      OseeClient client = ServiceUtil.getOseeClient();
      BranchEndpoint proxy = client.getBranchEndpoint();

      BranchCommitOptions options = new BranchCommitOptions();
      options.setArchive(isArchiveAllowed);
      options.setCommitter(committer);
      try {
         Response response = proxy.commitBranch(sourceBranch, destinationBranch, options);
         if (Status.CREATED.getStatusCode() == response.getStatus()) {
            BranchManager.setState(sourceBranch, BranchState.COMMITTED);
            Long txId = getTransactionId(response);
            handleResponse(txId, monitor, sourceBranch, destinationBranch);
         }
      } catch (Exception ex) {
         BranchManager.setState(sourceBranch, currentState);
         OseeEventManager.kickBranchEvent(getClass(), new BranchEvent(BranchEventType.CommitFailed, sourceBranch));
         throw JaxRsExceptions.asOseeException(ex);
      }
   }

   private long getTransactionId(Response response) {
      long toReturn = -1;
      if (response.hasEntity()) {
         Transaction tx = response.readEntity(Transaction.class);
         toReturn = tx.getTxId().getId();
      } else {
         URI location = response.getLocation();
         if (location != null) {
            String path = location.toASCIIString();
            int index = path.lastIndexOf("txs/");
            if (index > 0 && index < path.length()) {
               String value = path.substring(index);
               if (Strings.isNumeric(value)) {
                  toReturn = Integer.parseInt(value);
               }
            }
         }
      }
      return toReturn;
   }

   private void handleResponse(Long newTxId, IProgressMonitor monitor, BranchId sourceBranch, BranchId destinationBranch) throws OseeCoreException {
      TransactionToken newTransaction = TransactionToken.valueOf(newTxId, destinationBranch);
      AccessPolicy accessPolicy = ServiceUtil.getAccessPolicy();
      accessPolicy.removePermissions(sourceBranch);

      // Update commit artifact cache with new information
      Artifact associatedArtifact = BranchManager.getAssociatedArtifact(sourceBranch);
      if (!associatedArtifact.equals(SystemUser.OseeSystem)) {
         TransactionManager.cacheCommittedArtifactTransaction(associatedArtifact, newTransaction);
      }

      BranchManager.reloadBranch(sourceBranch);

      if (!skipChecksAndEvents) {
         Collection<Change> changes = new ArrayList<>();
         IOperation operation = ChangeManager.comparedToPreviousTx(newTransaction, changes);
         doSubWork(operation, monitor, 1.0);
         handleArtifactEvents(newTransaction, changes);
      }

      OseeEventManager.kickBranchEvent(getClass(),
         new BranchEvent(BranchEventType.Committed, sourceBranch, destinationBranch));
   }

   private void handleArtifactEvents(TransactionToken newTransaction, Collection<Change> changes) throws OseeCoreException {
      ArtifactEvent artifactEvent = new ArtifactEvent(newTransaction);
      Map<Integer, EventModifiedBasicGuidArtifact> artEventMap = new HashMap<>();
      Set<Artifact> artifacts = new HashSet<>();

      for (Change change : changes) {
         LoadChangeType changeType = change.getChangeType();
         switch (changeType) {
            case artifact:
               // Don't do anything.  When kicking Persist event to all clients we need only to create the artifact changed based on the Changed Attributes
               break;
            case relation:
               RelationChange relChange = (RelationChange) change;
               RelationEventType relationEventType =
                  change.getModificationType().isDeleted() ? RelationEventType.Deleted : change.getModificationType().isUnDeleted() ? RelationEventType.Undeleted : RelationEventType.Added;

               DefaultBasicUuidRelation defaultBasicGuidRelation = new DefaultBasicUuidRelation(relChange.getBranchId(),
                  relChange.getRelationType().getGuid(), relChange.getItemId().getId().intValue(),
                  relChange.getGamma().getId().intValue(), relChange.getChangeArtifact().getBasicGuidArtifact(),
                  relChange.getEndTxBArtifact().getBasicGuidArtifact());
               EventBasicGuidRelation event =
                  new EventBasicGuidRelation(relationEventType, relChange.getArtId().getId().intValue(),
                     relChange.getBArtId().getId().intValue(), defaultBasicGuidRelation);
               event.setRationale(relChange.getRationale());
               artifactEvent.getRelations().add(event);
               break;
            case attribute:
               // Only reload items that were already in the active cache
               ArtifactId artifactId = change.getArtId();
               Artifact artifact = ArtifactCache.getActive(artifactId, newTransaction.getBranch());
               if (artifact != null) {
                  artifacts.add(artifact);
               }

               Artifact changedArtifact = change.getChangeArtifact();
               if (changedArtifact != null) {

                  EventModifiedBasicGuidArtifact artEvent = artEventMap.get(artifactId.getId().intValue());
                  if (artEvent == null) {
                     artEvent = new EventModifiedBasicGuidArtifact(newTransaction.getBranch(),
                        change.getArtifactType().getGuid(), changedArtifact.getGuid(),
                        new ArrayList<org.eclipse.osee.framework.skynet.core.event.model.AttributeChange>());
                     artifactEvent.getArtifacts().add(artEvent);
                  }

                  AttributeChange attributeChange = (AttributeChange) change;
                  org.eclipse.osee.framework.skynet.core.event.model.AttributeChange attrChangeEvent =
                     new org.eclipse.osee.framework.skynet.core.event.model.AttributeChange();
                  attrChangeEvent.setAttrTypeGuid(attributeChange.getAttributeType().getGuid());
                  attrChangeEvent.setGammaId(attributeChange.getGamma().getId().intValue());
                  attrChangeEvent.setAttributeId(attributeChange.getAttrId().getId().intValue());
                  attrChangeEvent.setModTypeGuid(
                     AttributeEventModificationType.getType(attributeChange.getModificationType()).getGuid());

                  Attribute<?> attribute = changedArtifact.getAttributeById(attributeChange.getAttrId().getId(), true);
                  if (attribute != null) {
                     for (Object obj : attribute.getAttributeDataProvider().getData()) {
                        if (obj == null) {
                           attrChangeEvent.getData().add("");
                        } else if (obj instanceof String) {
                           attrChangeEvent.getData().add((String) obj);
                        } else {
                           OseeLog.log(Activator.class, Level.SEVERE,
                              "Unhandled data type " + obj.getClass().getSimpleName());
                        }
                     }
                  }
                  artEvent.getAttributeChanges().add(attrChangeEvent);
               }
               break;
            default:
               break;
         }
      }

      ArtifactQuery.reloadArtifacts(artifacts);
      OseeEventManager.kickPersistEvent(getClass(), artifactEvent);
   }

}

Back to the top