/////////////////////////////////////////////////////////////////////////
//
// © University of Southampton IT Innovation Centre, 2008
//
// Copyright in this library belongs to the University of Southampton
// University Road, Highfield, Southampton, UK, SO17 1BJ
//
// This software may not be used, sold, licensed, transferred, copied
// or reproduced in whole or in part in any manner or form or in or
// on any media by any person other than in accordance with the terms
// of the Licence Agreement supplied with the software, or otherwise
// without the prior written consent of the copyright owners.
//
// This software is distributed WITHOUT ANY WARRANTY, without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE, except where stated in the Licence Agreement supplied with
// the software.
//
//	Created By :			Mark McArdle
//	Created Date :			2008-02-28
//	Created for Project :		CRISP
//
/////////////////////////////////////////////////////////////////////////
//
//  Dependencies : none
//
/////////////////////////////////////////////////////////////////////////
//
//	Last commit info :  $Author: mm $
//                          $Date: 2008-03-03 13:55:54 +0000 (Mon, 03 Mar 2008) $
//                          $Revision: 9049 $
//
/////////////////////////////////////////////////////////////////////////

package uk.ac.soton.itinnovation.grid.client.tutorial;

import org.w3c.dom.Document;
import uk.ac.soton.itinnovation.grid.service.types.jsdl.JobDescription;
import uk.ac.soton.itinnovation.grid.service.types.metadata.ApplicationMetadata;
import uk.ac.soton.itinnovation.grid.service.types.metadata.DataStager;

/**
 *
 * @author mark
 */
public class JobHelpers {

    public static JobDescription getJobDescription(String jobName,Document metaDataDoc){
        ApplicationMetadata metadata = new ApplicationMetadata(metaDataDoc);

        JobDescription jsdl = new JobDescription(jobName,metadata.getInfo().getUri());
        
        // Add the stagers to the JSDL
        for (DataStager stager : metadata.getInputs()){
            addStagers(jsdl, stager, stager.getDefaultSize());
        }
        for (DataStager stager : metadata.getOutputs()){
            addStagers(jsdl, stager, stager.getDefaultSize());
        }
        
        // Parse the arguments string and add them to the JSDL
        String[] args = {};
        for (String arg : args){
            jsdl.getApplication().getPosixApplication().addArgument(arg);
        }
        return jsdl;
    }
    
    private static void addStagers(JobDescription jsdl, DataStager stager, int multiplicity) {
        if (stager.isArray()) {
            for (int i=0 ; i<multiplicity ; ++i)
                jsdl.addStager(stager.getName() + "-" + i);
        } else if (multiplicity != 0)
            jsdl.addStager(stager.getName());
    }
    
}
