package org.jetbrains.teamcity.buildChainExport.controllers;

import jetbrains.buildServer.controllers.BaseController;
import jetbrains.buildServer.serverSide.*;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.web.openapi.WebControllerManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.teamcity.buildChainExport.formats.DotFormat;
import org.jetbrains.teamcity.buildChainExport.formats.GmlFormat;
import org.jetbrains.teamcity.buildChainExport.formats.GraphFormat;
import org.jetbrains.teamcity.buildChainExport.nodes.BuildTypeNode;
import org.jetbrains.teamcity.buildChainExport.nodes.PromotionNode;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class ExportChainController extends BaseController {
  private final BuildPromotionManager myBuildPromotionManager;
  @NotNull private final ProjectManager myProjectManager;
  private Map<String, GraphFormat> myFormats = new HashMap<String, GraphFormat>();

  public ExportChainController(@NotNull BuildPromotionManager buildPromotionManager,
                               @NotNull WebControllerManager webControllerManager,
                               @NotNull ProjectManager projectManager) {
    myBuildPromotionManager = buildPromotionManager;
    myProjectManager = projectManager;
    webControllerManager.registerController("/exportBuildChain.html", this);

    myFormats.put("dot", new DotFormat());
    myFormats.put("gml", new GmlFormat());
  }

  @Nullable
  @Override
  protected ModelAndView doHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response) throws Exception {
    String fileFormat = StringUtil.emptyIfNull(request.getParameter("format"));
    GraphFormat format = myFormats.get(fileFormat.toLowerCase());
    if (format == null) {
      return simpleView("Supported formats: " + myFormats.keySet());
    }

    String result;

    String promoId = request.getParameter("promotionId");
    if (promoId != null) {
      BuildPromotion promotion = myBuildPromotionManager.findPromotionById(Long.parseLong(promoId));
      if (promotion == null) return simpleView("Build promotion does not exist: " + promoId);
      result = format.export(new PromotionNode(promotion));
    } else {
      String buildTypeId = request.getParameter("buildTypeId");
      if (buildTypeId == null){
        return simpleView("Neither 'promotionId' nor 'buildTypeId' parameters are specified");
      }
      final SBuildType buildType = myProjectManager.findBuildTypeByExternalId(buildTypeId);
      if (buildType == null){
        return simpleView("Build configuration with id '" + buildTypeId + "' is not found");
      }
      String useDummy = request.getParameter("useDummy");
      if (useDummy == null || (!Boolean.valueOf(useDummy))){
        result = format.export(new BuildTypeNode(buildType));
      }else{
        //Object here is to make it possible to compile the code against both TeamCity 8.0 and 8.1
        final Object dummyPromotion = myBuildPromotionManager.createDummyPromotion((BuildTypeEx)buildType);
        result = format.export(new PromotionNode((BuildPromotion)dummyPromotion));
      }
    }

    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();
    writer.print(result);
    writer.close();
    return null;
  }
}

@@ -67,98 +67,451 @@
 rt j
-etbrains.buildServer.controllers.BaseController;%0Aimport jetbrains.buildServer.serverSide.*
+ava.io.PrintWriter;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport jetbrains.buildServer.controllers.BaseController;%0Aimport jetbrains.buildServer.serverSide.BuildPromotion;%0Aimport jetbrains.buildServer.serverSide.BuildPromotionManager;%0Aimport jetbrains.buildServer.serverSide.ProjectManager;%0Aimport jetbrains.buildServer.serverSide.SBuildType
 ;%0Aim
@@ -1096,178 +1096,8 @@
 w;%0A%0A
-import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.io.PrintWriter;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 publ
@@ -2719,32 +2719,33 @@
 dTypeId == null)
+ 
 %7B%0A        return
@@ -2953,16 +2953,17 @@
 == null)
+ 
 %7B%0A      
@@ -3067,493 +3067,61 @@
     
-String useDummy = request.getParameter(%22useDummy%22);%0A      if (useDummy == null %7C%7C (!Boolean.valueOf(useDummy)))%7B%0A        result = format.export(new BuildTypeNode(buildType));%0A      %7Delse%7B%0A        //Object here is to make it possible to compile the code against both TeamCity 8.0 and 8.1%0A        final Object dummyPromotion = myBuildPromotionManager.createDummyPromotion((BuildTypeEx)buildType);%0A        result = format.export(new PromotionNode((BuildPromotion)dummyPromotion));%0A      %7D
+result = format.export(new BuildTypeNode(buildType));
 %0A   

Drop "useDummy" parameter as relying on not stable enough API (according to pull request comment)