Skip to main content
summaryrefslogtreecommitdiffstats
blob: 34e3d9d50cebf05374979373efea1b38c08d737e (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
/*******************************************************************************
 * 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.ui.skynet.artifact;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeManager;
import org.eclipse.osee.framework.skynet.core.relation.order.IRelationSorter;
import org.eclipse.osee.framework.skynet.core.relation.order.RelationOrderData;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.util.ArtifactPasteConfiguration;

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

   private final List<Artifact> itemsToCopy;
   private final Artifact destination;
   private final ArtifactPasteConfiguration config;
   private final ArtifactNameConflictHandler nameConflictHandler;

   public ArtifactPasteOperation(ArtifactPasteConfiguration config, Artifact destination, List<Artifact> itemsToCopy, ArtifactNameConflictHandler nameConflictHandler) {
      super("Paste Artifact(s)", Activator.PLUGIN_ID);
      this.itemsToCopy = itemsToCopy;
      this.destination = destination;
      this.config = config;
      this.nameConflictHandler = nameConflictHandler;
   }

   @Override
   protected void doWork(final IProgressMonitor monitor) throws Exception {
      if (destination == null) {
         throw new OseeArgumentException("Destination Artifact cannot be null.");
      }
      if (!itemsToCopy.isEmpty()) {
         double workAmount = 0.80;
         final Artifact itemToCopy = itemsToCopy.get(0);
         if (itemToCopy.hasParent() && destination.equals(itemToCopy.getParent())) {

            // Prevent Pasting multiples if pasting onto same parent
            if (itemToCopy instanceof User) {
               return;
            }

            Object object = nameConflictHandler.resolve(itemToCopy);
            String changedName = object != null ? object.toString() : null;
            if (!Strings.isValid(changedName) || itemToCopy.getName().equals(changedName)) {
               throw new OperationCanceledException();
            } else {
               Artifact newArtifact = pasteArtifact(monitor, workAmount, config, destination, itemToCopy);
               newArtifact.setName(changedName);
            }
         } else {
            workAmount = workAmount / itemsToCopy.size();
            for (Artifact item : itemsToCopy) {
               pasteArtifact(monitor, workAmount, config, destination, item);
            }
         }
         destination.persist(getClass().getSimpleName());
         monitor.worked(calculateWork(0.20));
      } else {
         monitor.worked(calculateWork(1.0));
      }
   }

   private void pasteRelationOrder(ArtifactPasteConfiguration config, Artifact source, Artifact newArtifact, List<Artifact> copiedChildren) throws OseeCoreException {
      if (config.isKeepRelationOrderSettings()) {
         IRelationTypeSide relationTypeSide = CoreRelationTypes.Default_Hierarchical__Child;
         RelationOrderData data = RelationManager.createRelationOrderData(source);
         String order =
            data.getCurrentSorterGuid(RelationTypeManager.getType(relationTypeSide), relationTypeSide.getSide());
         IRelationSorter sorter = RelationManager.getSorterProvider().getRelationOrder(order);
         if (RelationOrderBaseTypes.USER_DEFINED == sorter.getSorterId()) {
            newArtifact.setRelationOrder(relationTypeSide, copiedChildren);
         } else {
            newArtifact.setRelationOrder(relationTypeSide, sorter.getSorterId());
         }
      }
   }

   private Artifact pasteArtifact(IProgressMonitor monitor, double workAmount, ArtifactPasteConfiguration config, Artifact destination, Artifact source) throws OseeCoreException {
      boolean workComplete = true;
      Artifact newArtifact = null;
      // We do not support duplicating user artifacts.
      if (!(source instanceof User)) {
         newArtifact = source.duplicate(destination.getBranch());
         destination.addChild(newArtifact);
         List<Artifact> copiedChildren = new ArrayList<>();
         if (config.isIncludeChildrenOfCopiedElements()) {
            Collection<Artifact> children = source.getChildren();
            if (!children.isEmpty()) {
               workComplete = false;
               double stepAmount = workAmount / children.size();
               for (Artifact sourceChild : children) {
                  copiedChildren.add(pasteArtifact(monitor, stepAmount, config, newArtifact, sourceChild));
               }
            }
         }

         if (config.isKeepRelationOrderSettings()) {
            pasteRelationOrder(config, source, newArtifact, copiedChildren);
         }
      }
      if (workComplete) {
         monitor.worked(calculateWork(workAmount));
      }
      return newArtifact;
   }
}

Back to the top