package org.jboss.pnc.core.builder;

import org.jboss.logging.Logger;
import org.jboss.pnc.model.BuildDriverStatus;
import org.jboss.pnc.model.BuildConfiguration;
import org.jboss.pnc.model.BuildRecord;
import org.jboss.pnc.spi.builddriver.BuildResult;
import org.jboss.pnc.spi.datastore.Datastore;
import org.jboss.pnc.spi.datastore.DatastoreException;

import javax.inject.Inject;
import java.io.StringWriter;

/**
 * Created by <a href="mailto:matejonnet@gmail.com">Matej Lazar</a> on 2014-12-15.
 */
public class DatastoreAdapter {

    private Datastore datastore;

    private static final Logger log = Logger.getLogger(DatastoreAdapter.class);

    @Inject
    public DatastoreAdapter(Datastore datastore) {
        this.datastore = datastore;
    }

    public void storeResult(BuildTask buildTask, BuildResult completedBuild) throws DatastoreException {
        try {
            BuildConfiguration buildConfiguration = buildTask.getBuildConfiguration();

            BuildRecord buildRecord = new BuildRecord();
            buildRecord.setBuildLog(completedBuild.getBuildLog());
            buildRecord.setStatus(completedBuild.getBuildDriverStatus());
            buildRecord.setBuildConfiguration(buildConfiguration);
            // Additional information needed for historical purpose
            buildRecord.setBuildScript(buildConfiguration.getBuildScript());
            buildRecord.setPatchesUrl(buildConfiguration.getPatchesUrl());
            buildRecord.setSourceUrl(buildConfiguration.getScmUrl());

            log.debugf("Storing results of %s to datastore.", buildConfiguration.getName());
            datastore.storeCompletedBuild(buildRecord);
        } catch (Exception e) {
            throw new DatastoreException("Error storing the result to datastore.", e);
        }
    }

    public void storeResult(BuildTask buildTask, Throwable e) throws DatastoreException {
        BuildConfiguration buildConfiguration = buildTask.getBuildConfiguration();

        BuildRecord buildRecord = new BuildRecord();
        StringWriter stackTraceWriter = new StringWriter();
        buildRecord.setStatus(BuildDriverStatus.UNKNOWN); //TODO set error status
        buildRecord.setBuildLog(stackTraceWriter.toString());
        buildRecord.setBuildConfiguration(buildConfiguration);
        // Additional information needed for historical purpose
        buildRecord.setBuildScript(buildConfiguration.getBuildScript());
        buildRecord.setPatchesUrl(buildConfiguration.getPatchesUrl());
        buildRecord.setSourceUrl(buildConfiguration.getScmUrl());
        log.debugf("Storing ERROR result of %s to datastore. Error: %s", buildConfiguration.getName(), e);
        datastore.storeCompletedBuild(buildRecord);
    }

    public boolean isBuildConfigurationBuilt() {
        return false; //TODO
    }
}

@@ -95,28 +95,29 @@
 el.Build
-DriverStatus
+Configuration
 ;%0Aimport
@@ -138,37 +138,36 @@
 .model.Build
-Configuration
+DriverStatus
 ;%0Aimport org
@@ -376,16 +376,44 @@
 Inject;%0A
+import java.io.PrintWriter;%0A
 import j
@@ -2126,141 +2126,505 @@
     
-buildRecord.setStatus(BuildDriverStatus.UNKNOWN); //TODO set error status%0A        buildRecord.setBuildLog(stackTraceWriter.toString()
+PrintWriter stackTracePrinter = new PrintWriter(stackTraceWriter);%0A        e.printStackTrace(stackTracePrinter);%0A        buildRecord.setStatus(BuildDriverStatus.UNKNOWN); //TODO set error status. Is it ok to store DBS (Jenkins), if we are storing BSD than UNKNOWN is the right one%0A%0A        String errorMessage = %22Last build status: %22 + buildTask.getStatus().toString() + %22%5Cn%22;%0A        errorMessage += %22Caught exception: %22 + stackTraceWriter.toString();%0A        buildRecord.setBuildLog(errorMessage
 );%0A 

Write error stack trace to database.