Skip to main content
summaryrefslogtreecommitdiffstats
blob: e8cb1e9b645ad81d168ab782351414d6482b84d5 (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
/*******************************************************************************
 * 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.blam.operation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.io.CharBackedInputStream;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ExcelXmlWriter;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ISheetWriter;
import org.eclipse.osee.framework.plugin.core.util.AIFile;
import org.eclipse.osee.framework.plugin.core.util.OseeData;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;
import org.eclipse.osee.framework.ui.skynet.blam.AbstractBlam;
import org.eclipse.osee.framework.ui.skynet.blam.VariableMap;
import org.eclipse.swt.program.Program;

/**
 * @author Ryan D. Brooks
 */
public class ExportArtifacts extends AbstractBlam {
   private CharBackedInputStream charBak;
   private ISheetWriter excelWriter;
   private IAttributeType[] attributeColumns;
   private AttributeType nameAttributeType;
   private static final int NUM_FIXED_COLUMNS = 3;

   @Override
   public String getName() {
      return "Export Artifacts";
   }

   @Override
   public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception {
      init();

      List<Artifact> parentArtifacts = variableMap.getArtifacts("artifacts");
      List<Artifact> artifacts;

      if (variableMap.getBoolean("Include Children")) {
         artifacts = new ArrayList<Artifact>(400);
         for (Artifact artifact : parentArtifacts) {
            artifacts.add(artifact);
            artifacts.addAll(artifact.getDescendants());
         }
      } else {
         artifacts = parentArtifacts;
      }

      mapAttributeTypeToColumn(artifacts);

      String[] row = new String[attributeColumns.length + NUM_FIXED_COLUMNS];
      excelWriter.startSheet("Artifacts", row.length);
      excelWriter.writeCell("GUID");
      excelWriter.writeCell("Artifact Type");
      excelWriter.writeCell("Name");
      for (IAttributeType attributeType : attributeColumns) {
         excelWriter.writeCell(attributeType.getName());
      }
      excelWriter.endRow();

      for (Artifact artifact : artifacts) {
         Arrays.fill(row, null);
         row[0] = artifact.getGuid();
         row[1] = artifact.getArtifactTypeName();
         row[2] = artifact.getName();
         for (IAttributeType attributeType : artifact.getAttributeTypes()) {
            if (!attributeType.equals(nameAttributeType)) {
               String value = artifact.getAttributesToString(attributeType);
               if (!value.equals("")) {
                  row[NUM_FIXED_COLUMNS + Arrays.binarySearch(attributeColumns, attributeType)] = value;
               }
            }
         }
         excelWriter.writeRow(row);
      }

      excelWriter.endSheet();
      excelWriter.endWorkbook();
      IFile iFile = OseeData.getIFile("artifacts" + Lib.getDateTimeString() + ".xml");
      AIFile.writeToFile(iFile, charBak);
      Program.launch(iFile.getLocation().toOSString());
   }

   private void mapAttributeTypeToColumn(List<Artifact> artifacts) throws OseeCoreException {
      HashSet<IAttributeType> attributeTypes = new HashSet<IAttributeType>();

      for (Artifact artifact : artifacts) {
         for (IAttributeType attributeType : artifact.getAttributeTypes()) {
            attributeTypes.add(attributeType);
         }
      }

      attributeTypes.remove(nameAttributeType);
      attributeColumns = attributeTypes.toArray(new AttributeType[attributeTypes.size()]);
      Arrays.sort(attributeColumns);
   }

   private void init() throws IOException, OseeCoreException {
      nameAttributeType = AttributeTypeManager.getType("Name");
      charBak = new CharBackedInputStream();
      excelWriter = new ExcelXmlWriter(charBak.getWriter());
   }

   @Override
   public String getXWidgetsXml() {
      return "<xWidgets><XWidget xwidgetType=\"XCheckBox\" horizontalLabel=\"true\" labelAfter=\"true\" displayName=\"Include Children\" /><XWidget xwidgetType=\"XListDropViewer\" displayName=\"artifacts\" /></xWidgets>";
   }

   @Override
   public String getDescriptionUsage() {
      return "Select parameters below and click the play button at the top right.";
   }

   @Override
   public Collection<String> getCategories() {
      return Arrays.asList("Util");
   }
}

Back to the top