package org.jenkinsci.plugins.docker.commons.fingerprint;

import hudson.model.Fingerprint;
import hudson.model.Run;
import jenkins.model.Jenkins;

import java.io.IOException;
import java.util.Collection;
import jenkins.model.FingerprintFacet;

/**
 * @author Kohsuke Kawaguchi
 */
public class DockerFingerprints {
    private DockerFingerprints() {} // no instantiation

    /**
     * Gets {@link Fingerprint} for a given docker image.
     */
    public static Fingerprint of(String imageId) throws IOException {
        if (imageId.length()!=64)
            throw new IllegalArgumentException("Expecting 64char full image ID, but got "+imageId);

        return Jenkins.getInstance().getFingerprintMap().get(imageId.substring(0, 32));
    }

    public static void addRunFacet(String imageId, ContainerRecord record, Run<?,?> run) throws IOException {
        Fingerprint f = of(imageId);
        Collection<FingerprintFacet> facets = f.getFacets();
        DockerRunFingerprintFacet runFacet = null;
        for (FingerprintFacet facet : facets) {
            if (facet instanceof DockerRunFingerprintFacet) {
                runFacet = (DockerRunFingerprintFacet) facet;
                break;
            }
        }
        if (runFacet == null) {
            runFacet = new DockerRunFingerprintFacet(f, System.currentTimeMillis(), imageId);
            facets.add(runFacet);
        }
        runFacet.add(record);
        runFacet.addFor(run);
    }

    public static void addFromFacet(String imageId, Run<?,?> run) throws IOException {
        Fingerprint f = of(imageId);
        Collection<FingerprintFacet> facets = f.getFacets();
        DockerFromFingerprintFacet fromFacet = null;
        for (FingerprintFacet facet : facets) {
            if (facet instanceof DockerFromFingerprintFacet) {
                fromFacet = (DockerFromFingerprintFacet) facet;
                break;
            }
        }
        if (fromFacet == null) {
            fromFacet = new DockerFromFingerprintFacet(f, System.currentTimeMillis(), imageId);
            facets.add(fromFacet);
        }
        fromFacet.addFor(run);
    }

}

@@ -52,16 +52,42 @@
 print;%0A%0A
+import hudson.BulkChange;%0A
 import h
@@ -1236,32 +1236,93 @@
     %7D%0A        %7D%0A
+        BulkChange bc = new BulkChange(f);%0A        try %7B%0A    
         if (runF
@@ -1329,32 +1329,36 @@
 acet == null) %7B%0A
+    
             runF
@@ -1439,32 +1439,36 @@
 d);%0A            
+    
 facets.add(runFa
@@ -1465,32 +1465,36 @@
 .add(runFacet);%0A
+    
         %7D%0A      
@@ -1487,32 +1487,36 @@
       %7D%0A        
+    
 runFacet.add(rec
@@ -1521,32 +1521,36 @@
 ecord);%0A        
+   
+ 
 runFacet.addFor(
@@ -1551,24 +1551,103 @@
 ddFor(run);%0A
+            bc.commit();%0A        %7D finally %7B%0A            bc.abort();%0A        %7D%0A
     %7D%0A%0A    p
@@ -2093,32 +2093,93 @@
     %7D%0A        %7D%0A
+        BulkChange bc = new BulkChange(f);%0A        try %7B%0A    
         if (from
@@ -2187,32 +2187,36 @@
 acet == null) %7B%0A
+    
             from
@@ -2299,32 +2299,36 @@
 d);%0A            
+    
 facets.add(fromF
@@ -2334,34 +2334,42 @@
 Facet);%0A        
+   
+ 
 %7D%0A
+    
         fromFace
@@ -2379,21 +2379,100 @@
 ddFor(run);%0A
+            bc.commit();%0A        %7D finally %7B%0A            bc.abort();%0A        %7D%0A
     %7D%0A%0A%7D%0A

Use BulkChange.