/**
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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 org.jboss.pnc.rest.restmodel;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.jboss.pnc.model.Artifact;
import org.jboss.pnc.rest.utils.JsonMixInArtifact;
import org.jboss.pnc.rest.utils.JsonOutputConverterMapper;
import org.jboss.pnc.spi.BuildExecutionStatus;
import org.jboss.pnc.spi.BuildResult;
import org.jboss.pnc.spi.builddriver.exception.BuildDriverException;
import org.jboss.pnc.spi.executor.BuildExecutionConfiguration;
import org.jboss.pnc.spi.executor.exceptions.ExecutorException;

import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import java.io.Serializable;
import java.util.Optional;

/**
 * @author <a href="mailto:matejonnet@gmail.com">Matej Lazar</a>
 */
@XmlRootElement(name = "buildResult")
public class BuildResultRest implements Serializable {

    private BuildExecutionConfigurationRest buildExecutionConfiguration;

    private BuildDriverResultRest buildDriverResult;

    private RepositoryManagerResultRest repositoryManagerResult;

    private ExecutorException exception;

    private BuildExecutionStatus failedReasonStatus;

    public BuildResultRest() {
    }

    public BuildResultRest(String serialized) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixInAnnotations(Artifact.class, JsonMixInArtifact.class);
        BuildResultRest buildResultRest = mapper.readValue(serialized, BuildResultRest.class);
        this.buildExecutionConfiguration = buildResultRest.getBuildExecutionConfiguration();
        this.buildDriverResult = buildResultRest.getBuildDriverResult();
        this.repositoryManagerResult = buildResultRest.getRepositoryManagerResult();
        this.exception = buildResultRest.getException();
        this.failedReasonStatus = buildResultRest.getFailedReasonStatus();
    }

    public BuildResultRest(BuildResult buildResult) throws BuildDriverException {

        buildResult.getBuildExecutionConfiguration().ifPresent((configuration) -> {
            buildExecutionConfiguration = new BuildExecutionConfigurationRest(configuration);
        });

        buildResult.getBuildDriverResult().ifPresent((result) -> {
            try {
                buildDriverResult = new BuildDriverResultRest(result);
            } catch (BuildDriverException e) {
                new RuntimeException(e);
            }
        });

        buildResult.getRepositoryManagerResult().ifPresent((result) -> {
            repositoryManagerResult = new RepositoryManagerResultRest(result);
        });

        exception = buildResult.getException().orElse(null);
    }

    public BuildResult toBuildResult() {
        return new BuildResult(
                Optional.ofNullable(buildExecutionConfiguration),
                Optional.ofNullable(buildDriverResult),
                Optional.ofNullable(repositoryManagerResult),
                Optional.ofNullable(exception),
                Optional.ofNullable(failedReasonStatus));
    }

    public void setBuildExecutionConfiguration(BuildExecutionConfigurationRest buildExecutionConfiguration) {
        this.buildExecutionConfiguration = buildExecutionConfiguration;
    }

    public BuildExecutionConfigurationRest getBuildExecutionConfiguration() {
        return buildExecutionConfiguration;
    }

    public BuildDriverResultRest getBuildDriverResult() {
        return buildDriverResult;
    }

    public void setBuildDriverResult(BuildDriverResultRest buildDriverResult) {
        this.buildDriverResult = buildDriverResult;
    }

    public RepositoryManagerResultRest getRepositoryManagerResult() {
        return repositoryManagerResult;
    }

    public void setRepositoryManagerResult(RepositoryManagerResultRest repositoryManagerResult) {
        this.repositoryManagerResult = repositoryManagerResult;
    }

    public ExecutorException getException() {
        return exception;
    }

    public void setException(ExecutorException exception) {
        this.exception = exception;
    }

    public BuildExecutionStatus getFailedReasonStatus() {
        return failedReasonStatus;
    }

    public void setFailedReasonStatus(BuildExecutionStatus failedReasonStatus) {
        this.failedReasonStatus = failedReasonStatus;
    }

    @Override
    public String toString() {
        return JsonOutputConverterMapper.apply(this);
    }
}

@@ -789,17 +789,16 @@
 Mapper;%0A
-%0A
 import o
@@ -1062,18 +1062,8 @@
 ver.
-exception.
 Buil
@@ -1069,25 +1069,22 @@
 ldDriver
-Exception
+Result
 ;%0Aimport
@@ -1106,40 +1106,46 @@
 spi.
-executor.BuildExecutionConfigura
+builddriver.exception.BuildDriverExcep
 tion
@@ -2793,32 +2793,36 @@
    %7D);%0A%0A        
+if (
 buildResult.getB
@@ -2837,33 +2837,33 @@
 erResult().i
-f
+s
 Present(
 (result) -%3E 
@@ -2854,37 +2854,10 @@
 ent(
-(result) -%3E %7B%0A            try
+))
  %7B%0A 
@@ -2863,29 +2863,25 @@
             
-  
-  b
+B
 uildDriverRe
@@ -2885,22 +2885,40 @@
 rResult 
-= new 
+result = buildResult.get
 BuildDri
@@ -2922,35 +2922,31 @@
 DriverResult
-Rest(result
+().get(
 );%0A         
@@ -2952,109 +2952,72 @@
     
-%7D catch (BuildDriverException e) %7B%0A                new RuntimeException(e);%0A            %7D%0A        %7D);
+buildDriverResult = new BuildDriverResultRest(result);%0A        %7D
 %0A%0A  

Fix build driver exception handling.