package dk.statsbiblioteket.medieplatform.autonomous;

import java.util.HashMap;
import java.util.Map;

/**
 * Container for the result information for a result of invoking the call method on a autonomous components.
 */
public class CallResult {
    private final Map<Batch,ResultCollector> results = new HashMap<>();
    private final String errorMessage;

    /**
     * Create a result for a fatal error.
     * @param errorMessage Describes the error.
     */
    public CallResult(String errorMessage) {
        this.errorMessage = errorMessage;
    }


    public CallResult() {
        errorMessage = null;
    }

    public void addResult(Batch batch, ResultCollector resultCollector) {
        results.put(batch, resultCollector);
    }

    @Override
    public String toString() {
        StringBuilder resultString = new StringBuilder();
        resultString.append(getErrorMessage() + "\n");
        for (Map.Entry<Batch, ResultCollector> result : results.entrySet()) {
            if (result.getValue().isSuccess()) {
                resultString.append("Worked on " + result.getKey() + " successfully\n");
            } else {
                resultString.append("Failed to process " + result.getKey() + "\n");
            }

        }
        return resultString.toString();
    }

    /**
     * Will return 0 if the supplied map doesn't contains any failures. The following int values indicates failures:<br>
     *     1: A batch check found a failure.
     *     2: A call invocation had to exit because of an unrecoverable problem.
     * </br>
     */
    public int containsFailures() {
        if (fatalErrorEncountered()) return 2;
        for (Map.Entry<Batch, ResultCollector> result : results.entrySet()) {
            if (!result.getValue().isSuccess()) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * If the call invocation generated a fatal error, a message describing the error will be returned.
     */
    public String getErrorMessage() {
        return errorMessage;
    }

    /**
     * Returns <code>true</code> if a fatal error was encountered preventing the call() method from completing.
     */
    public boolean fatalErrorEncountered() {
        return errorMessage != null;
    }
}

@@ -100,19 +100,16 @@
 ap;%0A%0A/**
-%0A *
  Contain
@@ -206,17 +206,16 @@
 ponents.
-%0A
  */%0Apubl
@@ -264,16 +264,17 @@
 p%3CBatch,
+ 
 ResultCo
@@ -395,24 +395,31 @@
 atal error.%0A
+     *%0A
      * @para
@@ -893,19 +893,46 @@
 essage()
- + 
+)%0A                    .append(
 %22%5Cn%22);%0A 
@@ -1033,32 +1033,55 @@
 esult.getValue()
+%0A                      
 .isSuccess()) %7B%0A
@@ -1132,147 +1132,471 @@
 on %22
- + result.getKey() + %22 successfully%5Cn%22);%0A            %7D else %7B%0A                resultString.append(%22Failed to process %22 + result.getKey() + 
+)%0A                            .append(%0A                                    result.getKey()%0A                                          .getFullID())%0A                            .append(%22 successfully%5Cn%22);%0A            %7D else %7B%0A                resultString.append(%22Failed to process %22)%0A                            .append(%0A                                    result.getKey()%0A                                          .getFullID())%0A                            .append(
 %22%5Cn%22
@@ -1784,16 +1784,23 @@
 ndicates
+%0A     *
  failure
@@ -1812,20 +1812,16 @@
 %3E%0A     *
-    
  1: A ba
@@ -1853,20 +1853,16 @@
 .%0A     *
-    
  2: A ca
@@ -2022,17 +2022,41 @@
 ()) 
-return 2;
+%7B%0A            return 2;%0A        %7D
 %0A   
@@ -2164,16 +2164,40 @@
 tValue()
+%0A                       
 .isSucce
@@ -2286,23 +2286,16 @@
 %0A    /**
-%0A     *
  If the 
@@ -2383,21 +2383,16 @@
 eturned.
-%0A    
  */%0A    
@@ -2468,23 +2468,16 @@
 %0A    /**
-%0A     *
  Returns
@@ -2573,21 +2573,16 @@
 pleting.
-%0A    
  */%0A    

USe the batch id in the result print