Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2e207a01cafba781060ee6bde4e2ec603bcb30f3 (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
/*******************************************************************************
 * 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.action;

import java.net.URI;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osee.framework.core.client.OseeClientProperties;
import org.eclipse.osee.framework.core.model.TransactionRecord;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.change.AttributeChange;
import org.eclipse.osee.framework.skynet.core.change.Change;
import org.eclipse.osee.framework.ui.plugin.util.AWorkbench;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.commandHandlers.Handlers;
import org.eclipse.osee.framework.ui.skynet.compare.CompareHandler;
import org.eclipse.osee.framework.ui.skynet.compare.CompareItem;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.jaxrs.client.JaxRsClient;
import org.eclipse.osee.jaxrs.client.JaxRsExceptions;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;

/**
 * @author Donald G. Dunne
 */
public class WasIsCompareEditorAction extends Action {

   private static String ATTRIBUTE_TRANSACTIONS_QUERY_DESC =
      "SELECT txs.transaction_id, txs.gamma_id FROM osee_attribute atr, osee_txs txs WHERE atr.attr_id = ? AND atr.gamma_id = txs.gamma_id AND txs.branch_id = ? order by gamma_id desc";

   public WasIsCompareEditorAction() {
      super("View Was/Is Comparison");
   }

   @Override
   public ImageDescriptor getImageDescriptor() {
      return ImageManager.getImageDescriptor(FrameworkImage.COMPARE_DOCUMENTS);
   }

   @Override
   public void run() {
      try {
         ISelection selection =
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
         if (selection instanceof IStructuredSelection) {
            IStructuredSelection structuredSelection = (IStructuredSelection) selection;

            List<Change> localChanges = Handlers.getArtifactChangesFromStructuredSelection(structuredSelection);
            if (localChanges.isEmpty() || localChanges.size() > 1) {
               AWorkbench.popup("Can only show Was/Is for single selection");
               return;
            }
            Change change = localChanges.iterator().next();
            List<TransactionRecord> transactionsFromStructuredSelection =
               Handlers.getTransactionsFromStructuredSelection(structuredSelection);
            int transactionId = transactionsFromStructuredSelection.iterator().next().getId();
            List<Artifact> artifactsFromStructuredSelection =
               Handlers.getArtifactsFromStructuredSelection(structuredSelection);
            Artifact artifact = artifactsFromStructuredSelection.iterator().next();

            String was = change.getWasValue();
            int attrId = ((AttributeChange) change).getAttrId();
            Integer previousTransaction = getPreviousTransaction(artifact.getBranchUuid(), attrId, transactionId);
            if (!Strings.isValid(was) && (change instanceof AttributeChange)) {
               if (previousTransaction > 0) {
                  was = loadAttributeValue(attrId, previousTransaction, artifact);
               }
            }

            String is = change.getIsValue();
            if (!Strings.isValid(is) && (change instanceof AttributeChange)) {
               is = loadAttributeValue(attrId, transactionId, artifact);
            }
            CompareHandler compareHandler =
               new CompareHandler(String.format("Compare [%s]", change), new CompareItem(String.format(
                  "Was [Transaction: %d]", previousTransaction), was, System.currentTimeMillis()), new CompareItem(
                  String.format("Is [Transaction: %s]", transactionId), is, System.currentTimeMillis()), null);
            compareHandler.compare();
         }
      } catch (Exception ex) {
         OseeLog.log(getClass(), OseeLevel.SEVERE_POPUP, ex);
      }
   }

   private Integer getPreviousTransaction(long branchUuid, int attrId, int transactionId) {
      Integer previousTransaction = 0;
      boolean found = false;
      IOseeStatement chStmt = ConnectionHandler.getStatement(ConnectionHandler.getConnection(), true);
      try {
         chStmt.runPreparedQuery(ATTRIBUTE_TRANSACTIONS_QUERY_DESC, attrId, branchUuid);
         while (chStmt.next()) {
            int transaction = chStmt.getInt("transaction_id");
            if (found) {
               return transaction;
            }
            if (transaction == transactionId) {
               found = true;
            }
         }
      } finally {
         chStmt.close();
      }
      return previousTransaction;
   }

   private String loadAttributeValue(int attrId, int transactionId, Artifact artifact) {
      String appServer = OseeClientProperties.getOseeApplicationServer();
      URI uri =
         UriBuilder.fromUri(appServer).path("orcs").path("branch").path(String.valueOf(artifact.getBranchUuid())).path(
            "artifact").path(artifact.getGuid()).path("attribute").path(String.valueOf(attrId)).path("version").path(
            String.valueOf(transactionId)).build();
      try {
         return JaxRsClient.newClient().target(uri).request(MediaType.TEXT_PLAIN).get(String.class);
      } catch (Exception ex) {
         throw JaxRsExceptions.asOseeException(ex);
      }

   }

   private static ISelectionProvider getSelectionProvider() {
      ISelectionProvider selectionProvider = null;
      IWorkbench workbench = PlatformUI.getWorkbench();
      if (!workbench.isStarting() && !workbench.isClosing()) {
         IWorkbenchPage page = AWorkbench.getActivePage();
         if (page != null) {
            IWorkbenchPart part = page.getActivePart();
            if (part != null) {
               IWorkbenchSite site = part.getSite();
               if (site != null) {
                  selectionProvider = site.getSelectionProvider();
               }
            }
         }
      }
      return selectionProvider;
   }

   public static boolean isEnabledStatic() {
      if (PlatformUI.getWorkbench().isClosing()) {
         return false;
      }
      boolean isEnabled = false;

      ISelectionProvider selectionProvider = getSelectionProvider();
      if (selectionProvider != null) {
         ISelection selection = selectionProvider.getSelection();
         if (selection instanceof IStructuredSelection) {
            isEnabled = ((IStructuredSelection) selection).size() == 1;
         }
      }
      return isEnabled;
   }
}

Back to the top