conviz.gr.server.ConvizResultTab.java Source code

Java tutorial

Introduction

Here is the source code for conviz.gr.server.ConvizResultTab.java

Source

/**
 * Copyright 2013 Roman Soroka
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package conviz.gr.server;

import conviz.gr.common.ConvizConstants;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.serverSide.artifacts.BuildArtifact;
import jetbrains.buildServer.serverSide.artifacts.BuildArtifacts;
import jetbrains.buildServer.serverSide.artifacts.BuildArtifactsViewMode;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import jetbrains.buildServer.web.openapi.ViewLogTab;
import jetbrains.buildServer.web.reportTabs.ReportTabUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static conviz.gr.common.ConvizConstants.*;

/**
 * @author: Roman Soroka
 * Date: 1/7/13
 * Time: 9:35 AM
 */
public class ConvizResultTab extends ViewLogTab {

    public ConvizResultTab(@NotNull PagePlaces pagePlaces, @NotNull SBuildServer server,
            @NotNull PluginDescriptor pluginDescriptor) {
        super(ConvizConstants.RESULT_TAB_TITLE, ConvizConstants.PLUGIN_TAB_ID, pagePlaces, server);
        setIncludeUrl(pluginDescriptor.getPluginResourcesPath("viewConvizRunResult.jsp"));
    }

    @Override
    protected void fillModel(final @NotNull Map<String, Object> model, @NotNull HttpServletRequest request,
            @NotNull SBuild build) {
        try {
            List<ResultVIewBean> diagrams = new ArrayList<ResultVIewBean>();
            List<ResultVIewBean> reports = new ArrayList<ResultVIewBean>();

            final List<BuildArtifact> reportArtifacts = new ArrayList<BuildArtifact>();

            build.getArtifacts(BuildArtifactsViewMode.VIEW_ALL)
                    .iterateArtifacts(new BuildArtifacts.BuildArtifactsProcessor() {
                        @NotNull
                        @Override
                        public Continuation processBuildArtifact(@NotNull BuildArtifact artifact) {
                            if (artifact.getName().matches(REPORT_FILE_PREFIX + ".*" + REPORT_FILE_SUFFIX)) {
                                reportArtifacts.add(artifact);
                            }
                            return Continuation.CONTINUE;
                        }
                    });

            for (BuildArtifact report : reportArtifacts) {

                String reportText = FileCopyUtils
                        .copyToString(new BufferedReader(new InputStreamReader(report.getInputStream())));
                Document doc = DocumentHelper.parseText(reportText);

                for (Element diagram : new ArrayList<Element>(doc.getRootElement().elements(REPORT_ELEMENT_IMG))) {
                    diagrams.add(new ResultVIewBean(diagram.elementText(REPORT_EL_ID),
                            diagram.elementText(REPORT_EL_FILE)));
                }

                for (Element customReport : new ArrayList<Element>(
                        doc.getRootElement().elements(REPORT_ELEMENT_HTML))) {
                    reports.add(new ResultVIewBean(customReport.elementText(REPORT_EL_ID),
                            customReport.elementText(REPORT_EL_FILE)));
                }

            }

            model.put("diagrams", diagrams);
            model.put("reports", reports);

        } catch (Exception e) {
            LoggerFactory.getLogger(this.getClass()).error("ConViz failed to construct display tab view: ", e);
            model.put("conviz.view.error", true);
            model.put("conviz.error.text", e.toString());
        }
    }

    @Override
    protected boolean isAvailable(@NotNull HttpServletRequest request, @NotNull SBuild build) {

        return super.isAvailable(request, build) && ReportTabUtil.isAvailable(build, FLAG_FILE);
    }
}