/*
 * Copyright (c) 2013 Rising Oak LLC.
 *
 * Distributed under the MIT license: http://opensource.org/licenses/MIT
 */

package com.offbytwo.jenkins.model;

import static com.google.common.collect.Collections2.filter;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.HttpResponseException;

import com.google.common.base.Predicate;

public class BuildWithDetails extends Build {
    List actions;
    boolean building;
    String description;
    int duration;
    String fullDisplayName;
    String id;
    long timestamp;
    BuildResult result;
    List<Artifact> artifacts;

    public List<Artifact> getArtifacts() {
		return artifacts;
	}

	public void setArtifacts(List<Artifact> artifacts) {
		this.artifacts = artifacts;
	}

	public boolean isBuilding() {
        return building;
    }

    public String getDescription() {
        return description;
    }

    public int getDuration() {
        return duration;
    }

    public String getFullDisplayName() {
        return fullDisplayName;
    }

    public String getId() {
        return id;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public BuildResult getResult() {
        return result;
    }

    public List getActions() {
		return actions;
	}

	public void setActions(List actions) {
        this.actions = actions;
    }

    public Map<String, String> getParameters() {
        Collection parameters = filter(actions, new Predicate<Map<String, Object>>() {
            @Override
            public boolean apply(Map<String, Object> action) {
                return action.containsKey("parameters");
            }
        });

        Map<String, String> params = new HashMap<String, String>();

        if (parameters != null && !parameters.isEmpty()) {
            for(Map<String, String> param : ((Map<String, List<Map<String, String>>>) parameters.toArray()[0]).get("parameters")) {
                String key = param.get("name");
                String value = param.get("value");
                params.put(key, value);
            }
        }

        return params;
    }
    
    public InputStream downloadArtifact(Artifact a) throws HttpResponseException, IOException, URISyntaxException {
    	//We can't just put the artifact's relative path at the end of the url string,
    	//as there could be characters that need to be escaped.
    	URI uri = new URI(getUrl());
    	String artifactPath = uri.getPath()+"artifact/"+a.getRelativePath();
    	URI artifactUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(),uri.getPort(),artifactPath,"", "");
    	return client.getFile(artifactUri);
    }
}

@@ -163,15 +163,8 @@
 ort 
-static 
 com.
@@ -181,35 +181,22 @@
 mon.
-collect.Collections2.fil
+base.Predica
 te
-r
 ;%0A%0Ai
@@ -419,94 +419,60 @@
 ort 
-org.apache.http.client.HttpResponseException;%0A%0Aimport com.google.common.base.Predica
+static com.google.common.collect.Collections2.fil
 te
+r
 ;%0A%0Ap
@@ -755,26 +755,32 @@
 tifacts() %7B%0A
-%09%09
+        
 return artif
@@ -789,101 +789,19 @@
 ts;%0A
-%09%7D%0A%0A%09public void setArtifacts(List%3CArtifact%3E artifacts) %7B%0A%09%09this.artifacts = artifacts;%0A%09%7D%0A%0A%09
+    %7D%0A%0A    
 publ
@@ -1295,92 +1295,22 @@
 ) %7B%0A
-%09%09return actions;%0A%09%7D%0A%0A%09public void setActions(List actions) %7B%0A        this.actions =
+        return
  act
@@ -1770,16 +1770,17 @@
      for
+ 
 (Map%3CStr
@@ -2081,20 +2081,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -2144,31 +2144,8 @@
 rows
- HttpResponseException,
  IOE
@@ -2179,17 +2179,20 @@
 n %7B%0A    
-%09
+    
 //We can
@@ -2266,17 +2266,20 @@
 ng,%0A    
-%09
+    
 //as the
@@ -2326,25 +2326,28 @@
 scaped.%0A    
-%09
+    
 URI uri = ne
@@ -2367,17 +2367,20 @@
 ));%0A    
-%09
+    
 String a
@@ -2406,17 +2406,19 @@
 etPath()
-+
+ + 
 %22artifac
@@ -2420,17 +2420,19 @@
 tifact/%22
-+
+ + 
 a.getRel
@@ -2448,17 +2448,20 @@
 ();%0A    
-%09
+    
 URI arti
@@ -2528,16 +2528,17 @@
 tHost(),
+ 
 uri.getP
@@ -2543,16 +2543,17 @@
 tPort(),
+ 
 artifact
@@ -2557,16 +2557,17 @@
 actPath,
+ 
 %22%22, %22%22);
@@ -2575,9 +2575,12 @@
     
-%09
+    
 retu

clean up build with details