summaryrefslogtreecommitdiffstats
blob: 36f2eb0a4a2cd4f9d1bbf2f5164a020144890bc6 (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
/*******************************************************************************
 * 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.branch.management.internal;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.branch.management.IOseeBranchService;
import org.eclipse.osee.framework.branch.management.change.ComputeNetChangeOperation;
import org.eclipse.osee.framework.branch.management.change.LoadDeltasBetweenBranches;
import org.eclipse.osee.framework.branch.management.change.LoadDeltasBetweenTxsOnTheSameBranch;
import org.eclipse.osee.framework.branch.management.commit.CommitDbOperation;
import org.eclipse.osee.framework.branch.management.creation.CreateBranchOperation;
import org.eclipse.osee.framework.branch.management.purge.BranchOperation;
import org.eclipse.osee.framework.branch.management.purge.IBranchOperationFactory;
import org.eclipse.osee.framework.branch.management.purge.PurgeBranchOperationFactory;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.TransactionVersion;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.message.BranchCommitRequest;
import org.eclipse.osee.framework.core.message.BranchCommitResponse;
import org.eclipse.osee.framework.core.message.BranchCreationRequest;
import org.eclipse.osee.framework.core.message.BranchCreationResponse;
import org.eclipse.osee.framework.core.message.ChangeBranchArchiveStateRequest;
import org.eclipse.osee.framework.core.message.ChangeBranchStateRequest;
import org.eclipse.osee.framework.core.message.ChangeBranchTypeRequest;
import org.eclipse.osee.framework.core.message.ChangeItem;
import org.eclipse.osee.framework.core.message.ChangeReportRequest;
import org.eclipse.osee.framework.core.message.ChangeReportResponse;
import org.eclipse.osee.framework.core.message.PurgeBranchRequest;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.TransactionDelta;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.TransactionCache;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.operation.CompositeOperation;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.OperationLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.core.services.IOseeCachingServiceProvider;
import org.eclipse.osee.framework.core.services.IOseeModelFactoryServiceProvider;
import org.eclipse.osee.framework.database.IOseeDatabaseServiceProvider;

/**
 * @author Jeff C. Phillips
 * @author Megumi Telles
 * @author Ryan D. Brooks
 * @author Roberto E. Escobar
 * @author Andrew M. Finkbeiner
 */
public class OseeBranchService implements IOseeBranchService {
   private final IOseeDatabaseServiceProvider oseeDatabaseProvider;
   private final IOseeCachingServiceProvider cachingService;
   private final IOseeModelFactoryServiceProvider modelFactory;

   public OseeBranchService(IOseeDatabaseServiceProvider oseeDatabaseProvider, IOseeCachingServiceProvider cachingService, IOseeModelFactoryServiceProvider modelFactory) {
      this.oseeDatabaseProvider = oseeDatabaseProvider;
      this.cachingService = cachingService;
      this.modelFactory = modelFactory;
   }

   @Override
   public void commitBranch(IProgressMonitor monitor, BranchCommitRequest branchCommitData, BranchCommitResponse response) throws OseeCoreException {
      int userId = branchCommitData.getUserArtId();
      BranchCache branchCache = cachingService.getOseeCachingService().getBranchCache();
      TransactionCache transactionCache = cachingService.getOseeCachingService().getTransactionCache();

      Branch sourceBranch = branchCache.getById(branchCommitData.getSourceBranchId());
      Branch destinationBranch = branchCache.getById(branchCommitData.getDestinationBranchId());
      Branch mergeBranch = branchCache.findMergeBranch(sourceBranch, destinationBranch);

      TransactionRecord sourceTx = transactionCache.getTransaction(sourceBranch, TransactionVersion.HEAD);
      TransactionRecord destinationTx = transactionCache.getTransaction(destinationBranch, TransactionVersion.HEAD);

      TransactionDelta txDelta = new TransactionDelta(sourceTx, destinationTx);
      TransactionRecord mergeTx =
         mergeBranch != null ? transactionCache.getTransaction(mergeBranch, TransactionVersion.HEAD) : null;

      List<ChangeItem> changes = new ArrayList<ChangeItem>();

      List<IOperation> ops = new ArrayList<IOperation>();
      ops.add(new LoadDeltasBetweenBranches(oseeDatabaseProvider, txDelta, mergeTx, changes));
      ops.add(new ComputeNetChangeOperation(changes));
      ops.add(new CommitDbOperation(oseeDatabaseProvider.getOseeDatabaseService(), branchCache, userId, sourceBranch,
         destinationBranch, mergeBranch, changes, response, modelFactory));

      String opName =
         String.format("Commit: [%s]->[%s]", sourceBranch.getShortName(), destinationBranch.getShortName());
      IOperation op = new CompositeOperation(opName, Activator.PLUGIN_ID, ops);
      Operations.executeWorkAndCheckStatus(op, monitor);

      TransactionRecord newTransaction = response.getTransaction();
      newTransaction.setBranchCache(branchCache);
      transactionCache.cache(newTransaction);
      if (branchCommitData.isArchiveAllowed()) {
         sourceBranch.setArchived(true);
         branchCache.storeItems(sourceBranch);
      }
   }

   @Override
   public IOperation createBranch(IProgressMonitor monitor, BranchCreationRequest request, BranchCreationResponse response) throws OseeCoreException {
      return new CreateBranchOperation(oseeDatabaseProvider.getOseeDatabaseService(), modelFactory,
         cachingService.getOseeCachingService(), request, response);
   }

   @Override
   public IOperation getChanges(IProgressMonitor monitor, ChangeReportRequest request, ChangeReportResponse response) throws OseeCoreException {
      TransactionCache txCache = cachingService.getOseeCachingService().getTransactionCache();
      TransactionRecord srcTx = txCache.getOrLoad(request.getSourceTx());
      TransactionRecord destTx = txCache.getOrLoad(request.getDestinationTx());

      TransactionDelta txDelta = new TransactionDelta(srcTx, destTx);

      List<IOperation> ops = new ArrayList<IOperation>();
      if (txDelta.areOnTheSameBranch()) {
         ops.add(new LoadDeltasBetweenTxsOnTheSameBranch(oseeDatabaseProvider, txDelta, response.getChangeItems()));
      } else {
         TransactionRecord mergeTx = getMergeTransaction(srcTx, destTx);
         ops.add(new LoadDeltasBetweenBranches(oseeDatabaseProvider, txDelta, mergeTx, response.getChangeItems()));
      }
      ops.add(new ComputeNetChangeOperation(response.getChangeItems()));
      ops.add(new AddArtifactChangeData(response.getChangeItems()));

      String opName = String.format("Gathering changes");
      return new CompositeOperation(opName, Activator.PLUGIN_ID, ops);
   }

   private TransactionRecord getMergeTransaction(TransactionRecord sourceTx, TransactionRecord destinationTx) throws OseeCoreException {
      BranchCache cache = cachingService.getOseeCachingService().getBranchCache();
      Branch mergeBranch = cache.findMergeBranch(sourceTx.getBranch(), destinationTx.getBranch());
      return mergeBranch != null ? cachingService.getOseeCachingService().getTransactionCache().getTransaction(
         mergeBranch, TransactionVersion.HEAD) : null;
   }

   @Override
   public IOperation purge(IProgressMonitor monitor, PurgeBranchRequest request, OperationLogger logger) throws OseeCoreException {
      BranchCache branchCache = cachingService.getOseeCachingService().getBranchCache();
      Branch branch = branchCache.getById(request.getBranchId());
      IBranchOperationFactory factory =
         new PurgeBranchOperationFactory(logger, branchCache, oseeDatabaseProvider.getOseeDatabaseService());

      return new BranchOperation(logger, factory, branch);
   }

   @Override
   public IOperation updateBranchArchiveState(IProgressMonitor monitor, ChangeBranchArchiveStateRequest request) {
      return createBranchChangeOp(monitor, "Branch Archive State Change", request.getBranchId(), null, null,
         request.getState());
   }

   @Override
   public IOperation updateBranchState(IProgressMonitor monitor, ChangeBranchStateRequest request) {
      return createBranchChangeOp(monitor, "Branch State Change", request.getBranchId(), request.getState(), null, null);
   }

   @Override
   public IOperation updateBranchType(IProgressMonitor monitor, ChangeBranchTypeRequest request) {
      return createBranchChangeOp(monitor, "Branch Type Change", request.getBranchId(), null, request.getType(), null);
   }

   private IOperation createBranchChangeOp(IProgressMonitor monitor, String opName, final int branchId, final BranchState branchState, final BranchType branchType, final BranchArchivedState archivedState) {
      IOperation operation = new AbstractOperation(opName, Activator.PLUGIN_ID) {

         @Override
         protected void doWork(IProgressMonitor monitor) throws Exception {
            BranchCache branchCache = cachingService.getOseeCachingService().getBranchCache();
            Branch branch = branchCache.getById(branchId);

            if (branchType != null) {
               branch.setBranchType(branchType);
            }
            if (branchState != null) {
               branch.setBranchState(branchState);
            }
            if (archivedState != null) {
               branch.setArchived(archivedState.isArchived());
            }
            branchCache.storeItems(branch);
         }
      };
      return operation;
   }

   @Override
   public IOperation createSystemRootBranch(IProgressMonitor monitor) throws OseeCoreException {
      //      boolean doesSystemRootExist =
      //            cachingService.getOseeCachingService().getBranchCache().existsByGuid(CoreBranches.SYSTEM_ROOT.getGuid());
      //      Conditions.checkExpressionFailOnTrue(doesSystemRootExist, "System Root branch already exists.");

      final int NULL_PARENT_BRANCH_ID = -1;
      BranchCreationResponse response = new BranchCreationResponse(-1);
      BranchCreationRequest request =
         new BranchCreationRequest(BranchType.SYSTEM_ROOT, 1, NULL_PARENT_BRANCH_ID,
            CoreBranches.SYSTEM_ROOT.getGuid(), CoreBranches.SYSTEM_ROOT.getName(), -1, -1,
            CoreBranches.SYSTEM_ROOT.getName() + " Creation", -1, -1);
      return createBranch(monitor, request, response);
   }
}