Skip to main content
summaryrefslogtreecommitdiffstats
blob: 54078fc963cfd917a3732cb32819ae730f02d011 (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
/*******************************************************************************
 * Copyright (c) 2013 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.ats.rest.internal.build.report;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.ats.rest.internal.build.report.model.AtsElementData;
import org.eclipse.osee.ats.rest.internal.build.report.model.AtsWorkflowData;
import org.eclipse.osee.ats.rest.internal.build.report.parser.ArtIdParser;
import org.eclipse.osee.ats.rest.internal.build.report.parser.AtsAbstractSAXParser.AtsDataHandler;
import org.eclipse.osee.ats.rest.internal.build.report.parser.AtsWorkflowDataParser;
import org.eclipse.osee.ats.rest.internal.build.report.resources.BuildTraceReportResource;
import org.eclipse.osee.ats.rest.internal.build.report.table.BuildTraceTable;
import org.eclipse.osee.ats.rest.internal.build.report.table.BuildTraceTable.VerifierUriProvider;
import org.eclipse.osee.ats.rest.internal.build.report.util.InputFilesUtil;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.search.QueryBuilder;
import org.eclipse.osee.orcs.search.QueryFactory;

/**
 * @author John Misinco
 * @author Megumi Telles
 */
public class BuildTraceReport {

   private static final Pair<String, String> EMPTY_PAIR = new Pair<String, String>("", "");

   private int getPort(UriInfo uriInfo) {
      URI baseUri = uriInfo.getAbsolutePath();
      return baseUri.getPort();
   }

   public void getBuildArchive(OutputStream output, OrcsApi orcsApi, Log logger, String fileName, String programId, String buildId, String programName, String buildName, UriInfo uriInfo) {
      ByteArrayOutputStream tableStream = new ByteArrayOutputStream();
      final ZipOutputStream zout = new ZipOutputStream(output);

      UriBuilder uriBuilder =
         uriInfo.getBaseUriBuilder().path(BuildTraceReportResource.RESOURCE_BASE).host("localhost");

      int port = getPort(uriInfo);
      if (port > -1) {
         uriBuilder.port(port);
      }

      String supportFilesUrl = uriBuilder.build().toString();
      ArchiveCollector dataCollector = new ArchiveCollector(supportFilesUrl, logger);
      createTraceReport(buildId, programName, buildName, AtsElementData.ARCHIVE_REPORT_TEMPLATE, tableStream,
         dataCollector, orcsApi, logger);
      try {
         zout.putNextEntry(new ZipEntry(fileName + ".html"));
         tableStream.writeTo(zout);
         zout.closeEntry();
         dataCollector.writeArchive(zout);
      } catch (IOException ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         Lib.close(zout);
      }
   }

   public void getBuildReport(OutputStream output, OrcsApi orcsApi, Log logger, String programId, String buildId, String programName, String buildName) {
      createTraceReport(buildId, programName, buildName, AtsElementData.CHANGE_REPORT_URL_TEMPLATE, output, null,
         orcsApi, logger);
   }

   private void createTraceReport(final String buildId, final String programName, final String buildName, String urlTemplate, OutputStream output, final ArchiveCollector dataCollector, OrcsApi orcsApi, final Log logger) {
      final QueryFactory queryFactory = orcsApi.getQueryFactory(null);
      final IOseeBranch branch = getBaselineBranch(buildId, queryFactory);
      final List<Pair<String, String>> buildNamesToUrls = getBuildNameUrlPairs(buildId, queryFactory);

      final BuildTraceTable buildTraceTable = new BuildTraceTable(output, urlTemplate, new VerifierUriProvider() {

         @Override
         public List<Pair<String, String>> getBuildToUrlPairs(String verifierName) {
            List<Pair<String, String>> toReturn = new LinkedList<Pair<String, String>>();
            String fullPath = convertVerifierNameToPath(verifierName);
            if (Strings.isValid(fullPath)) {
               for (Pair<String, String> buildToUrl : buildNamesToUrls) {
                  String url = String.format(buildToUrl.getSecond(), fullPath);
                  URI loadSourceResource =
                     UriBuilder.fromPath("..").path("sourceFile").queryParam("url", url).queryParam("offline",
                        dataCollector != null).build();
                  toReturn.add(new Pair<String, String>(buildToUrl.getFirst(), loadSourceResource.toString()));
               }
            } else {
               for (int i = 0; i < getColumnCount(); i++) {
                  toReturn.add(EMPTY_PAIR);
               }
            }
            if (dataCollector != null) {
               dataCollector.onBuildToUrlPairs(verifierName, toReturn);
            }
            return toReturn;
         }

         @Override
         public int getColumnCount() {
            return buildNamesToUrls.size();
         }
      });
      buildTraceTable.initializeTraceReportTable(programName, buildName);
      AtsDataHandler<AtsWorkflowData> handler = new AtsDataHandler<AtsWorkflowData>() {

         @Override
         public void handleData(AtsWorkflowData data) {
            if (data.getWorkflowBuildId().equals(buildId)) {
               String pcrId = data.getWorkflowPcrId();
               try {
                  Collection<Integer> artIds = ArtIdParser.getArtIds(pcrId);
                  Map<ArtifactReadable, Iterable<ArtifactReadable>> requirementsToTests =
                     new LinkedHashMap<ArtifactReadable, Iterable<ArtifactReadable>>();

                  if (Conditions.hasValues(artIds)) {
                     ResultSet<ArtifactReadable> requirements =
                        queryFactory.fromBranch(branch).andLocalIds(artIds).getResults();

                     for (ArtifactReadable requirement : requirements) {
                        ResultSet<ArtifactReadable> verifiers =
                           requirement.getRelated(CoreRelationTypes.Verification__Verifier);
                        requirementsToTests.put(requirement, verifiers);
                     }

                  }
                  buildTraceTable.addRpcrToTable(pcrId, requirementsToTests);

               } catch (OseeCoreException ex) {
                  logger.error(ex, "Error handling AtsWorkflowData");
               }
               if (dataCollector != null) {
                  dataCollector.onPcrId(pcrId);
               }
            }
         }
      };

      AtsWorkflowDataParser parser = new AtsWorkflowDataParser(InputFilesUtil.getWorkflowFile(), handler);
      parser.parseDocument();
      buildTraceTable.close();
   }

   private IOseeBranch getBaselineBranch(String buildId, QueryFactory queryFactory) {
      QueryBuilder builder = queryFactory.fromBranch(AtsUtilCore.getAtsBranch());
      ArtifactReadable buildArt = builder.andGuid(buildId).getResults().getExactlyOne();
      ResultSet<? extends AttributeReadable<String>> branchUuids =
         buildArt.getAttributes(AtsAttributeTypes.BaselineBranchUuid);
      Conditions.checkNotNull(branchUuids, "branchUuids");
      String baselineBranchUuid = branchUuids.getExactlyOne().getValue();
      IOseeBranch branch = TokenFactory.createBranch(Long.valueOf(baselineBranchUuid), "TraceReport Branch");
      return branch;
   }

   private List<Pair<String, String>> getBuildNameUrlPairs(String buildId, QueryFactory queryFactory) {
      List<Pair<String, String>> toReturn = new LinkedList<Pair<String, String>>();
      QueryBuilder builder = queryFactory.fromBranch(AtsUtilCore.getAtsBranch());
      ArtifactReadable buildArt = builder.andGuid(buildId).getResults().getExactlyOne();
      String buildToUrlPairs = buildArt.getSoleAttributeAsString(AtsAttributeTypes.TestToSourceLocator, "");
      if (Strings.isValid(buildToUrlPairs)) {
         for (String pair : buildToUrlPairs.split("\n")) {
            String[] tokens = pair.trim().split("@");
            String build = tokens[0];
            String url = tokens[1];
            toReturn.add(new Pair<String, String>(build, url));
         }
      }
      return toReturn;
   }

   private String convertVerifierNameToPath(String verifierName) {
      int lastDot = verifierName.lastIndexOf(".");
      if (lastDot == -1) {
         return Strings.EMPTY_STRING;
      }
      String packageName = verifierName.substring(0, lastDot);
      List<String> paths = new LinkedList<String>();
      paths.add(packageName);
      paths.add("src");
      for (String token : packageName.split("\\.")) {
         paths.add(token);
      }
      paths.add(verifierName.substring(lastDot + 1) + ".java");
      return Collections.toString("/", paths);
   }

}

Back to the top