Extensis Logo SUPPORT | FORUMS | KNOWLEDGE BASE

Return Original Asset


#1

This particular Code Sample will connect to the Portfolio Server and download all of the completed Jobs. This example is in JAVA, but will be available in PHP and C# as well.

Downloading the results of a completed Job
Once a Job has been completed, it is available to download for the next twenty-four hours. This code sample will check for completed jobs and download the results.

String SERVER = "127.0.0.1";
String PORT = "8090";
String DOWNLOADFOLDER = "/Volumes/Server/Downloads/";

// Check for Job IDs and get the status for each Job.
List<String> myJobIds = service.getJobIDs(sessionId); 
List<JobStatus> statuses = service.getStatusForJobs(sessionId, myJobIds);
if (statuses.size() > 0 ) {
	for ( JobStatus perStatus : statuses ) {
   		String downloadLocation = "";
   		String downloadFilename = "";
   		List fields = perStatus.getJobDetails();
// We are only interested in completed jobs.
		if (perStatus.getStatus() == JobStatusType.COMPLETE) {
			boolean proceed = true;
			for (Attribute att : fields) {
				if (att.getName().equals("downloadLocation")) {
					downloadLocation = att.getValue();
				} else if (att.getName().equals("downloadFilename")) {
					downloadFilename = att.getValue();
// This script is skipping jobs that have errors.
				} else if (att.getName().equals("filesWithErrors") && ! att.getValue().equals("0")) {
					proceed = false;
				}
			}
// If the downloadLocation is blank, then there is nothing to download.
			if (downloadLocation == "") 
				proceed = false;
			if (downloadFilename.equals(""))
				downloadFilename = "files.zip";
			if (proceed) {
				URL download = null;
				try {
					String url = "http://" + SERVER + ":" + PORT + downloadLocation + "&sessionId=" + sessionId;
					download = new URL(url);
				} catch (MalformedURLException e) {
					System.out.println("Error: " + e.getMessage());
				}
				try {
					ReadableByteChannel rbc = null;
					FileOutputStream fos = new FileOutputStream(DOWNLOADFOLDER + downloadFilename);
					rbc = Channels.newChannel(download.openStream());
					fos.getChannel().transferFrom(rbc, 0, 1 << 24);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

It should be a fairly simple matter of adjusting this code to download the results of a specific Job that downloads a single file.

If you have any questions, feel free to post them.