Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3983381d231eb72432137c0af1a88d33a14b8956 (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
/*******************************************************************************
 * 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.define.traceability.operations;

import java.io.File;
import java.net.URI;
import java.nio.CharBuffer;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.logging.Level;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.nebula.widgets.xviewer.Activator;
import org.eclipse.osee.define.traceability.HierarchyHandler;
import org.eclipse.osee.define.traceability.TestUnitTagger;
import org.eclipse.osee.define.utility.IResourceLocator;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
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 John R. Misinco
 */
public class TraceResourceDropOperation extends AbstractOperation {

   public static interface RenameConfirmer {

      boolean acceptUpdate(Map<Artifact, String> nameUpdateRequired);
   }

   private final Collection<URI> resources;
   private final IResourceLocator locator;
   private final IRelationTypeSide relTypeSide;
   private final Artifact requirement;
   private final boolean persistChanges;
   private final RenameConfirmer confirmer;

   public TraceResourceDropOperation(Collection<URI> resources, IRelationTypeSide relTypeSide, Artifact requirement, IResourceLocator locator, boolean persistChanges, RenameConfirmer confirmer) {
      super("Trace Resource Drop Operation", Activator.PLUGIN_ID);
      this.relTypeSide = relTypeSide;
      this.requirement = requirement;
      this.resources = resources;
      this.locator = locator;
      this.persistChanges = persistChanges;
      this.confirmer = confirmer;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      if (!resources.isEmpty()) {
         IOseeBranch branch = requirement.getBranch();
         SkynetTransaction transaction = null;
         if (persistChanges) {
            transaction = TransactionManager.createTransaction(branch, "TraceResourceDrop");
         }
         HierarchyHandler handler = new HierarchyHandler(transaction);

         Map<Artifact, String> nameUpdateRequired = new TreeMap<Artifact, String>();

         for (URI resource : resources) {
            File file = new File(resource);
            if (!file.isDirectory()) {
               processFile(file, handler, transaction, nameUpdateRequired);
            }
         }

         boolean isOk = true;
         if (!nameUpdateRequired.isEmpty()) {
            if (confirmer != null) {
               isOk = confirmer.acceptUpdate(nameUpdateRequired);
            }

            if (isOk) {
               for (Entry<Artifact, String> entry : nameUpdateRequired.entrySet()) {
                  entry.getKey().setName(entry.getValue());
               }
            }
         }

         if (persistChanges && isOk) {
            requirement.persist(transaction);
            transaction.execute();
         }
      }
   }

   private void processFile(File file, HierarchyHandler handler, SkynetTransaction transaction, Map<Artifact, String> nameUpdateRequired) throws Exception {
      CharBuffer fileBuffer = Lib.fileToCharBuffer(file);
      URI fileUri = file.toURI();
      IFileStore fileStore = EFS.getStore(fileUri);
      String name = locator.getIdentifier(fileStore, fileBuffer).getName();
      TestUnitTagger tagger = TestUnitTagger.getInstance();
      String tag = tagger.getSourceTag(fileUri);
      Artifact testUnitArtifact = null;
      IOseeBranch branch = requirement.getBranch();
      boolean tagSource = false;
      if (GUID.isValid(tag)) {
         try {
            testUnitArtifact = ArtifactQuery.getArtifactFromId(tag, branch);
         } catch (ArtifactDoesNotExist ex) {
            //do nothing
         }

      } else {
         tag = GUID.create();
         tagSource = true;
      }

      if (testUnitArtifact == null) {
         testUnitArtifact = ArtifactTypeManager.addArtifact(CoreArtifactTypes.TestCase, branch, null, tag);
         testUnitArtifact.setName(name);
         handler.addArtifact(testUnitArtifact);
         if (tagSource) {
            tagger.addSourceTag(fileUri, testUnitArtifact.getGuid());
            refreshFile(file.getAbsolutePath());
         }
      }

      if (!testUnitArtifact.getName().equals(name)) {
         nameUpdateRequired.put(testUnitArtifact, name);
      }

      requirement.addRelation(relTypeSide, testUnitArtifact);
      if (persistChanges) {
         testUnitArtifact.persist(transaction);
      }
   }

   private void refreshFile(String uri) {
      IResource eclipseResource = ResourcesPlugin.getWorkspace().getRoot().findMember(uri);
      if (eclipseResource != null) {
         try {
            eclipseResource.refreshLocal(0, new NullProgressMonitor());
         } catch (CoreException ex) {
            OseeLog.log(Activator.class, Level.INFO, "Refreshing resource failed.");
         }
      }
   }

}

Back to the top