Skip to main content
summaryrefslogtreecommitdiffstats
blob: ccdd2576a63f05b298f10e7c5df84173bd0816db (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*******************************************************************************
 * 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.ote.ui.define.viewers.data;

import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.jface.viewers.DecorationOverlayIcon;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.nebula.widgets.xviewer.XViewer;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.skynet.core.attribute.DateAttribute;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IArtifactEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.ArtifactEvent;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.ui.skynet.ArtifactImageManager;
import org.eclipse.osee.framework.ui.swt.Displays;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.framework.ui.swt.OverlayImage;
import org.eclipse.osee.framework.ui.swt.OverlayImage.Location;
import org.eclipse.osee.ote.define.AUTOGEN.OteAttributeTypes;
import org.eclipse.osee.ote.define.artifacts.ArtifactTestRunOperator;
import org.eclipse.osee.ote.ui.define.OteDefineImage;
import org.eclipse.osee.ote.ui.define.OteUiDefinePlugin;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.TreeColumn;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactItem extends DataItem implements IXViewerItem, IArtifactEventListener {
   private static Image FROM_LOCAL_WS_COMMIT_ALLOWED_IMAGE = null;
   private static Image FROM_DATABASE_IMAGE = null;
   private static Image FROM_LOCAL_WS_COMMIT_NOT_ALLOWED_IMAGE = null;
   private static Image INVALID_SCRIPT_IMAGE = null;
   private static boolean isFullDescriptionMode = true;

   private final XViewer xViewer;
   private Artifact artifact;
   private ArtifactTestRunOperator operator;
   private String key;

   public ArtifactItem(XViewer xViewer, Artifact artifact, DataItem parent) throws OseeArgumentException {
      super(parent);
      this.xViewer = xViewer;
      setArtifact(artifact);
      OseeEventManager.addListener(this);
   }

   @Override
   public Artifact getData() {
      return artifact;
   }

   @Override
   public void dispose() {
      OseeEventManager.removeListener(this);
      super.dispose();
   }

   private void setArtifact(Artifact artifact) throws OseeArgumentException {
      this.artifact = artifact;
      this.operator = new ArtifactTestRunOperator(artifact);
      try {
         this.key =
            String.format("%s:%s:%s", getOperator().getChecksum(), getOperator().isFromLocalWorkspace(),
               getOperator().hasNotBeenCommitted());
      } catch (Exception ex) {
         this.key = "";
      }
   }

   public XViewer getXViewer() {
      return xViewer;
   }

   public ArtifactTestRunOperator getOperator() {
      return operator;
   }

   @Override
   public String getLabel(int index) {
      String toReturn = "";
      Artifact artifact = getData();
      try {
         if (artifact != null && artifact.isDeleted() != true) {

            if (index <= xViewer.getTree().getColumns().length - 1) {

               TreeColumn treeCol = xViewer.getTree().getColumns()[index];
               String colName = treeCol.getText();
               if (colName.equals("HRID")) {
                  toReturn = artifact.getHumanReadableId();
               } else if (colName.equals("GUID")) {
                  toReturn = artifact.getGuid();
               } else if (colName.equals("Artifact_Type")) {
                  toReturn = artifact.getArtifactTypeName();
                  if (getOperator().isFromLocalWorkspace()) {
                     toReturn += " (Local)";
                  }
               } else if (colName.equals("Status")) {
                  try {
                     toReturn = getOperator().getTestResultStatus();
                  } catch (Exception ex) {
                     OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
                     toReturn = ex.getLocalizedMessage();
                  }
               } else if (colName.equals("Disposition")) {
                  //Special case for the Disposition Artifact
                  String name = artifact.getSoleAttributeValueAsString(CoreAttributeTypes.Name, "");
                  if (name != "") {
                     try {
                        Artifact dispoArtifact =
                           ArtifactQuery.getArtifactFromTypeAndName(CoreArtifactTypes.TestRunDisposition, name,
                              artifact.getBranch());
                        if (dispoArtifact != null) {
                           return dispoArtifact.getSoleAttributeValueAsString(OteAttributeTypes.TestDisposition, "");
                        }
                     } catch (ArtifactDoesNotExist ex) {
                        //ignore if not defined
                     }
                  }
                  return "";
               } else {
                  AttributeType attributeType = AttributeTypeManager.getType(colName);
                  if (artifact.isAttributeTypeValid(attributeType)) {
                     if (AttributeTypeManager.isBaseTypeCompatible(DateAttribute.class, attributeType)) {
                        Date date = null;
                        try {
                           date = artifact.getSoleAttributeValue(attributeType);
                        } catch (Exception ex) {
                           // Do Nothing;
                        }
                        if (date != null) {
                           toReturn = new DateAttribute().MMDDYYHHMM.format(date);
                        } else {
                           toReturn = "NOT SET";
                        }
                     } else {
                        toReturn = artifact.getAttributesToString(attributeType);
                        if (colName.equals("Name")) {
                           toReturn = getArtifactName(toReturn);
                        }
                     }
                  }
               }
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
         toReturn = ex.getLocalizedMessage();
      }
      return toReturn;
   }

   private String getArtifactName(String rawName) throws OseeCoreException {
      String name = rawName;
      if (isFullDescriptionModeEnabled() != true) {
         String[] qualifiers = rawName.split("\\.");
         name = qualifiers[qualifiers.length - 1];
      }
      return String.format("%s%s [%s]", getOperator().isFromLocalWorkspace() ? "> " : "", name,
         getOperator().getChecksum());
   }

   @Override
   public Image getImage() {
      Image toReturn = null;
      try {
         Artifact artifact = getData();
         if (artifact != null && artifact.isDeleted() != true) {
            if (areImagesInitialized() != true) {
               initializeImages();
            }
            ArtifactTestRunOperator operator = getOperator();
            if (operator.isFromLocalWorkspace() == true) {
               if (operator.isCommitAllowed() == true) {
                  toReturn = FROM_LOCAL_WS_COMMIT_ALLOWED_IMAGE;
               } else {
                  if (operator.isScriptRevisionValid() != true) {
                     toReturn = INVALID_SCRIPT_IMAGE;
                  } else {
                     toReturn = FROM_LOCAL_WS_COMMIT_NOT_ALLOWED_IMAGE;
                  }
               }
            } else {
               toReturn = FROM_DATABASE_IMAGE;
            }
         }
      } catch (Exception ex) {
         OseeLog.log(OteUiDefinePlugin.class, Level.SEVERE, ex);
      }
      return toReturn;
   }

   private boolean areImagesInitialized() {
      return FROM_LOCAL_WS_COMMIT_ALLOWED_IMAGE != null && FROM_DATABASE_IMAGE != null && FROM_LOCAL_WS_COMMIT_NOT_ALLOWED_IMAGE != null;
   }

   private void initializeImages() {
      Artifact artifact = getData();
      Image defaultImage = ArtifactImageManager.getImage(artifact);
      DecorationOverlayIcon overlay = null;
      if (FROM_LOCAL_WS_COMMIT_ALLOWED_IMAGE == null) {
         OverlayImage overlayImage =
            new OverlayImage(defaultImage, ImageManager.getImageDescriptor(OteDefineImage.ADDITION), Location.BOT_RIGHT);
         FROM_LOCAL_WS_COMMIT_ALLOWED_IMAGE = overlayImage.createImage();
      }
      if (FROM_LOCAL_WS_COMMIT_NOT_ALLOWED_IMAGE == null) {
         overlay =
            new DecorationOverlayIcon(defaultImage, ImageManager.getImageDescriptor(OteDefineImage.CONFAUTO_OV),
               IDecoration.BOTTOM_RIGHT);
         FROM_LOCAL_WS_COMMIT_NOT_ALLOWED_IMAGE = overlay.createImage();
      }
      if (FROM_DATABASE_IMAGE == null) {
         overlay =
            new DecorationOverlayIcon(defaultImage, ImageManager.getImageDescriptor(OteDefineImage.VERSION_CONTROLLED),
               IDecoration.BOTTOM_RIGHT);
         FROM_DATABASE_IMAGE = overlay.createImage();
      }
      if (INVALID_SCRIPT_IMAGE == null) {
         overlay =
            new DecorationOverlayIcon(defaultImage, ImageManager.getImageDescriptor(OteDefineImage.OBSTRUCTED),
               IDecoration.BOTTOM_RIGHT);
         INVALID_SCRIPT_IMAGE = overlay.createImage();
      }
   }

   @Override
   public Object getKey() {
      return key;
   }

   public static void setFullDescriptionModeEnabled(boolean isEnabled) {
      ArtifactItem.isFullDescriptionMode = isEnabled;
   }

   public static boolean isFullDescriptionModeEnabled() {
      return ArtifactItem.isFullDescriptionMode;
   }

   //   @Override
   //   public void handleTransactionEvent(Sender sender, TransactionEvent transData) {
   //      if (artifact.isDeleted()) {
   //         return;
   //      }
   //
   ////      transData.getTransactions()
   //      TransactionChange change = null;
   //      change.
   //      if (transData.isDeleted(artifact)) {
   //         Displays.ensureInDisplayThread(new Runnable() {
   //            @Override
   //            public void run() {
   //               if (!xViewer.getTree().isDisposed()) {
   //                  xViewer.remove(this);
   //               }
   //               dispose();
   //            }
   //         });
   //         return;
   //      }
   //      if (transData.isRelAddedChangedDeleted(artifact) || transData.isChanged(artifact)) {
   //         Displays.ensureInDisplayThread(new Runnable() {
   //            @Override
   //            public void run() {
   //               if (!xViewer.getTree().isDisposed()) {
   //                  xViewer.remove(this);
   //               } else {
   //                  xViewer.update(this, null);
   //               }
   //            }
   //         });
   //      }
   //   }

   @Override
   public List<? extends IEventFilter> getEventFilters() {
      return null;
   }

   @Override
   public void handleArtifactEvent(ArtifactEvent artifactEvent, Sender sender) {
      if (artifact.isDeleted()) {
         return;
      }
      if (artifactEvent.isDeletedPurged(artifact)) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               if (!xViewer.getTree().isDisposed()) {
                  xViewer.remove(this);
               }
               dispose();
            }
         });
         return;
      }
      if (artifactEvent.isRelAddedChangedDeleted(artifact) || artifactEvent.isModified(artifact)) {
         Displays.ensureInDisplayThread(new Runnable() {
            @Override
            public void run() {
               if (!xViewer.getTree().isDisposed()) {
                  xViewer.remove(this);
               } else {
                  xViewer.update(this, null);
               }
            }
         });
      }
   }

}

Back to the top