Skip to main content
summaryrefslogtreecommitdiffstats
blob: 451a0129575d6664c8983b1932a3d37e8cc70d86 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*******************************************************************************
 * 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.explorer;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import org.apache.commons.io.FilenameUtils;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.AccessPolicy;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactData;
import org.eclipse.osee.framework.skynet.core.importing.parsers.IArtifactExtractor;
import org.eclipse.osee.framework.skynet.core.importing.parsers.NativeDocumentExtractor;
import org.eclipse.osee.framework.skynet.core.importing.parsers.WholeWordDocumentExtractor;
import org.eclipse.osee.framework.skynet.core.importing.resolvers.IArtifactImportResolver;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.ui.plugin.util.Wizards;
import org.eclipse.osee.framework.ui.skynet.ArtifactStructuredSelection;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactImportOperationFactory;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactImportOperationParameter;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactImportWizard;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactResolverFactory;
import org.eclipse.osee.framework.ui.skynet.Import.ArtifactResolverFactory.ArtifactCreationStrategy;
import org.eclipse.osee.framework.ui.skynet.artifact.ArtifactTransfer;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;
import org.eclipse.osee.framework.ui.skynet.internal.ServiceUtil;
import org.eclipse.osee.framework.ui.skynet.update.InterArtifactExplorerDropHandlerOperation;
import org.eclipse.osee.framework.ui.skynet.util.SkynetDragAndDrop;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.ui.IViewPart;

/**
 * @author Jeff C. Phillips
 */
public class ArtifactExplorerDragAndDrop extends SkynetDragAndDrop {
   private final TreeViewer treeViewer;
   private final IViewPart viewPart;

   private BranchId selectedBranch;

   public ArtifactExplorerDragAndDrop(TreeViewer treeViewer, String viewId, IViewPart viewPart, BranchId selectedBranch) {
      this(treeViewer, viewId, viewPart);
      this.selectedBranch = selectedBranch;
   }

   public ArtifactExplorerDragAndDrop(TreeViewer treeViewer, String viewId, IViewPart viewPart) {
      super(treeViewer.getTree(), treeViewer.getTree(), viewId);

      this.treeViewer = treeViewer;
      this.viewPart = viewPart;
   }

   public void updateBranch(BranchId branch) {
      selectedBranch = branch;
   }

   @Override
   public Artifact[] getArtifacts() {
      IStructuredSelection selection = (IStructuredSelection) treeViewer.getSelection();
      Object[] objects = selection.toArray();
      Artifact[] artifacts = new Artifact[objects.length];

      for (int index = 0; index < objects.length; index++) {
         artifacts[index] = (Artifact) objects[index];
      }

      return artifacts;
   }

   @Override
   public void performDragOver(DropTargetEvent event) {
      event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;

      if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {
         event.detail = DND.DROP_COPY;
      } else if (isValidForArtifactDrop(event)) {
         event.detail = DND.DROP_MOVE;
      } else {
         event.detail = DND.DROP_NONE;
      }
   }

   private boolean isValidForArtifactDrop(DropTargetEvent event) {
      boolean valid = false;
      if (ArtifactTransfer.getInstance().isSupportedType(event.currentDataType)) {

         Artifact dropTarget = getSelectedArtifact(event);
         ArtifactData toBeDropped = ArtifactTransfer.getInstance().nativeToJava(event.currentDataType);
         if (dropTarget != null) {
            try {
               AccessPolicy policy = ServiceUtil.getAccessPolicy();
               Artifact[] artifactsBeingDropped = toBeDropped.getArtifacts();
               List<Artifact> artsOnSameBranchAsDestination = new LinkedList<>();
               BranchId destinationBranch = dropTarget.getBranch();
               for (Artifact art : artifactsBeingDropped) {
                  if (art.isOnBranch(destinationBranch)) {
                     artsOnSameBranchAsDestination.add(art);
                  }
               }
               valid = policy.canRelationBeModified(dropTarget, artsOnSameBranchAsDestination,
                  CoreRelationTypes.Default_Hierarchical__Child, Level.FINE).matched();

               // if we are deparenting ourself, make sure our parent's child side can be modified
               if (valid) {
                  for (Artifact art : artsOnSameBranchAsDestination) {
                     if (art.hasParent()) {
                        valid = policy.canRelationBeModified(art.getParent(), null,
                           CoreRelationTypes.Default_Hierarchical__Child, Level.FINE).matched();
                     }
                     if (!valid) {
                        break;
                     }
                  }
               }
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex);
               valid = false;
            }
         }
      }
      return valid;
   }

   private Artifact getSelectedArtifact(DropTargetEvent event) {
      if (event.item != null && event.item.getData() instanceof Artifact) {
         return (Artifact) event.item.getData();
      }
      return null;
   }

   @Override
   public void performDrop(final DropTargetEvent event) {
      Artifact parentArtifact = getSelectedArtifact(event);

      if (parentArtifact == null && selectedBranch != null) {
         //try Default Root Hierarchy
         try {
            parentArtifact = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(selectedBranch);
         } catch (Exception ex) {
            OseeLog.log(getClass(), OseeLevel.SEVERE_POPUP, ex);
         }
      }

      if (parentArtifact != null) {
         if (ArtifactTransfer.getInstance().isSupportedType(event.currentDataType)) {
            ArtifactData artData = ArtifactTransfer.getInstance().nativeToJava(event.currentDataType);
            final Artifact[] artifactsToBeRelated = artData.getArtifacts();
            if (artifactsToBeRelated != null && artifactsToBeRelated.length > 0 && !artifactsToBeRelated[0].isOnSameBranch(
               parentArtifact)) {
               InterArtifactExplorerDropHandlerOperation interDropHandler =
                  new InterArtifactExplorerDropHandlerOperation(parentArtifact, artifactsToBeRelated, true);
               Operations.executeAsJob(interDropHandler, true);
            } else if (isValidForArtifactDrop(event) && MessageDialog.openQuestion(viewPart.getViewSite().getShell(),
               "Confirm Move",
               "Are you sure you want to make each of the selected artifacts a child of " + parentArtifact.getName() + "?")) {
               try {
                  SkynetTransaction transaction =
                     TransactionManager.createTransaction(parentArtifact.getBranch(), "Artifact explorer drag & drop");
                  // Replace all of the parent relations
                  for (Artifact artifact : artifactsToBeRelated) {
                     Artifact currentParent = artifact.getParent();
                     if (currentParent != null) {
                        currentParent.deleteRelation(CoreRelationTypes.Default_Hierarchical__Child, artifact);
                        currentParent.persist(transaction);
                     }
                     parentArtifact.addChild(RelationOrderBaseTypes.USER_DEFINED, artifact);
                     parentArtifact.persist(transaction);
                  }
                  transaction.execute();
               } catch (OseeCoreException ex) {
                  OseeLog.log(getClass(), OseeLevel.SEVERE_POPUP, ex);
               }
            }
         } else if (FileTransfer.getInstance().isSupportedType(event.currentDataType)) {

            Object object = FileTransfer.getInstance().nativeToJava(event.currentDataType);

            if (object instanceof String[]) {
               String[] items = (String[]) object;
               File importFile = new File(items[0]);

               ArtifactImportWizard wizard = new ArtifactImportWizard();
               wizard.setImportFile(importFile);
               wizard.setDestinationArtifact(parentArtifact);

               String fileName = importFile.getName();
               if (isSameName(parentArtifact, fileName)) {
                  String promptMsg = String.format(
                     "Artifact [%s] has same base file name as [%s]. \n\nDo you want to update the exisiting file? \nIf 'NO' selected, you'll be taken to the Artifact Import Wizard",
                     parentArtifact.getName(), FilenameUtils.getName(fileName));

                  if (MessageDialog.openQuestion(viewPart.getViewSite().getShell(), "Confirm Import", promptMsg)) {

                     IArtifactImportResolver resolver =
                        ArtifactResolverFactory.createResolver(ArtifactCreationStrategy.CREATE_ON_DIFFERENT_ATTRIBUTES,
                           parentArtifact.getArtifactType(), Arrays.asList(CoreAttributeTypes.Name), true, false);
                     try {
                        ArtifactImportOperationParameter parameter = new ArtifactImportOperationParameter();
                        parameter.setSourceFile(importFile);
                        parameter.setDestinationArtifact(parentArtifact.getParent());
                        parameter.setExtractor(getArtifactExtractor(parentArtifact.getArtifactType()));
                        parameter.setResolver(resolver);
                        parameter.setStopOnError(true);

                        IOperation operation = ArtifactImportOperationFactory.completeOperation(parameter);
                        Operations.executeWorkAndCheckStatus(operation);
                     } catch (OseeCoreException ex) {
                        OseeLog.log(getClass(), OseeLevel.SEVERE_POPUP, ex);
                     }

                  } else {
                     Wizards.initAndOpen(wizard, viewPart, new ArtifactStructuredSelection(parentArtifact));
                  }
               } else {
                  Wizards.initAndOpen(wizard, viewPart, new ArtifactStructuredSelection(parentArtifact));
               }
            }
         }
      }
   }

   private boolean isSameName(Artifact art, String fileName) {
      boolean isSame = false;
      if (!art.getArtifactType().equals(CoreArtifactTypes.Folder)) {
         if (art.getName().equals(FilenameUtils.getBaseName(fileName))) {
            isSame = true;
         }
      }
      return isSame;
   }

   private IArtifactExtractor getArtifactExtractor(ArtifactType type) {
      IArtifactExtractor extractor = null;
      if (type.inheritsFrom(CoreArtifactTypes.GeneralDocument)) {
         extractor = new NativeDocumentExtractor();
      } else {
         extractor = new WholeWordDocumentExtractor();
      }
      return extractor;
   }
}

Back to the top