Skip to main content
summaryrefslogtreecommitdiffstats
blob: 839ae398877f9463a868e4901227bd6df7caeacb (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
/*******************************************************************************
 * 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.framework.core.dsl.ui.integration.internal;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.dsl.integration.util.OseeDslSegmentParser;
import org.eclipse.osee.framework.core.dsl.integration.util.OseeDslSegmentParser.OseeDslSegment;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
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.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;

/**
 * @author Roberto E. Escobar
 */
public class OseeDslArtifactUpdateOperation extends AbstractOperation {

   private final File file;
   private final OseeDslSegmentParser parser;

   public OseeDslArtifactUpdateOperation(OseeDslSegmentParser parser, File file) {
      super("OseeDsl Artifact Update", DslUiIntegrationConstants.PLUGIN_ID);
      this.file = file;
      this.parser = parser;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      String source = getSource();
      Collection<OseeDslSegment> segments = parser.getSegments(source);
      if (segments.isEmpty()) {
         throw new OseeStateException("No tagged segments Found");
      } else {
         double workPercentage = 0.80 * 1.0 / segments.size();
         int workAmount = calculateWork(workPercentage);

         Map<Branch, SkynetTransaction> transactionMap = new HashMap<Branch, SkynetTransaction>();
         for (OseeDslSegment segment : segments) {
            int startAt = segment.start();
            int endAt = segment.end();

            String data = source.substring(startAt, endAt);
            addChanges(transactionMap, segment.getBranchUuid(), segment.getArtifactGuid(), data);
            monitor.worked(workAmount);
         }
         monitor.setTaskName("Persist...");
         for (SkynetTransaction transaction : transactionMap.values()) {
            transaction.execute();
         }
         monitor.worked(calculateWork(0.20));
      }
   }

   protected String getSource() throws IOException {
      return Lib.fileToString(file);
   }

   protected void addChanges(Map<Branch, SkynetTransaction> transactionMap, long branchUuid, String artifactGuid, String data) throws OseeCoreException {
      Branch branch = BranchManager.getBranchByGuid(branchUuid);
      SkynetTransaction transaction = transactionMap.get(branch);
      if (transaction == null) {
         transaction = TransactionManager.createTransaction(branch, "OseeDslArtifactUpdate");
         transactionMap.put(branch, transaction);
      }
      Artifact artifact = ArtifactQuery.getArtifactFromId(artifactGuid, branch);
      artifact.setSoleAttributeFromString(CoreAttributeTypes.GeneralStringData, data);
      artifact.persist(transaction);
   }

}

Back to the top