Skip to main content
summaryrefslogtreecommitdiffstats
blob: 411ce7344751948322bc12d2542683f38c124575 (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
/*******************************************************************************
 * 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.define.operations;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.threading.ThreadedWorkerExecutor;
import org.eclipse.osee.framework.core.threading.ThreadedWorkerFactory;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.ChecksumUtil;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.ote.define.artifacts.ArtifactTestRunOperator;
import org.eclipse.osee.ote.define.parser.BaseOutfileParser;
import org.eclipse.osee.ote.define.utilities.OutfileDataCollector;
import org.eclipse.osee.ote.define.utilities.OutfileParserExtensionManager;

/**
 * @author Roberto E. Escobar
 */
public class OutfileToArtifactOperation {
   private final IOseeBranch branch;
   private final List<URI> filesToImport;
   private final List<Artifact> results;
   private final List<URI> filesWithErrors;

   public OutfileToArtifactOperation(IOseeBranch branch, URI... filesToImport) {
      this.branch = branch;
      this.filesToImport = Arrays.asList(filesToImport);
      this.results = new ArrayList<>();
      this.filesWithErrors = new ArrayList<>();
   }

   public void execute(final IProgressMonitor monitor) throws Exception {
      this.results.clear();
      monitor.setTaskName("Outfiles to Artifact Conversion...");
      final Artifact parent = getParentArtifact();

      ThreadedWorkerFactory<Object> outfileToArtifactFactory = new ThreadedWorkerFactory<Object>() {

         @Override
         public int getWorkSize() {
            return filesToImport.size();
         }

         @Override
         public Callable<Object> createWorker(int startIndex, int endIndex) {
            return new OutfileToArtifactCallable(monitor, parent, filesToImport.subList(startIndex, endIndex));
         }

      };

      ThreadedWorkerExecutor<Object> executor = new ThreadedWorkerExecutor<>(outfileToArtifactFactory, false);
      executor.executeWorkersBlocking();
   }

   private Artifact getParentArtifact() throws OseeCoreException {
      Artifact root = OseeSystemArtifacts.getDefaultHierarchyRootArtifact(branch);
      Artifact testFolder = OseeSystemArtifacts.getOrCreateArtifact(CoreArtifactTypes.Folder, "Test", (Branch) branch);
      if (!root.isRelated(CoreRelationTypes.Default_Hierarchical__Child, testFolder)) {
         root.addChild(testFolder);
         root.persist("New Test Folder");
      }
      Artifact scriptFolder =
         OseeSystemArtifacts.getOrCreateArtifact(CoreArtifactTypes.Folder, "Test Script Results", (Branch) branch);
      if (!testFolder.isRelated(CoreRelationTypes.Default_Hierarchical__Child, scriptFolder)) {
         testFolder.addChild(scriptFolder);
         testFolder.persist("New Test Script Results Folder");
      }
      return scriptFolder;
   }

   private void addChecksum(ArtifactTestRunOperator operator, URL targetURL) throws Exception {
      InputStream inputStream = null;
      try {
         inputStream = targetURL.openStream();
         String checkSum = ChecksumUtil.createChecksumAsString(inputStream, ChecksumUtil.MD5);
         operator.setChecksum(checkSum);
      } finally {
         if (inputStream != null) {
            inputStream.close();
         }
      }
   }

   private OutfileDataCollector getOutfileData(IProgressMonitor monitor, URL fileToImport) throws Exception {
      OutfileDataCollector collector = new OutfileDataCollector();
      BaseOutfileParser outfileParser = OutfileParserExtensionManager.getInstance().getOutfileParserFor(fileToImport);
      synchronized (outfileParser) {
         outfileParser.registerListener(collector);
         outfileParser.execute(monitor, fileToImport);
         outfileParser.deregisterListener(collector);
      }
      return collector;
   }

   public Artifact[] getResults() {
      return results.toArray(new Artifact[results.size()]);
   }

   public URI[] getUnparseableFiles() {
      return filesWithErrors.toArray(new URI[filesWithErrors.size()]);
   }

   private class OutfileToArtifactCallable implements Callable<Object> {

      private final IProgressMonitor monitor;
      private final Artifact parent;
      private final List<URI> filesToImport;

      public OutfileToArtifactCallable(IProgressMonitor monitor, Artifact parent, List<URI> filesToImport) {
         this.monitor = monitor;
         this.parent = parent;
         this.filesToImport = filesToImport;
      }

      @Override
      public Object call() throws Exception {
         for (URI targetUri : filesToImport) {
            ArtifactTestRunOperator operator = null;
            try {
               operator = ArtifactTestRunOperator.getNewArtifactWithOperator(branch);

               OutfileDataCollector collector = getOutfileData(monitor, targetUri.toURL());
               collector.populate(operator.getTestRunArtifact(), parent);

               String path = targetUri.toURL().toString();
               operator.setLocalOutfileURI(path);
               operator.setOutfileExtension(Lib.getExtension(path));
               addChecksum(operator, targetUri.toURL());
               synchronized (results) {
                  results.add(operator.getTestRunArtifact());
               }

               if (monitor.isCanceled() == true) {
                  break;
               }
            } catch (Exception ex) {
               if (operator.getTestRunArtifact() == null) {
                  throw new Exception(
                     "Unable to create Test Run Artifact. Make sure type information exists in the selected branch.");
               }
               synchronized (filesWithErrors) {
                  filesWithErrors.add(targetUri);
               }
            }
            operator = null;
            monitor.worked(1);
         }
         return null;
      }

   }
}

Back to the top