
package com.engineous.system.drm.ldlev;

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

/**
 * This class declares shared constants and implements
 * shared methods used by a Load Leveler installation.
 * <p>
 * The main function of this class is to define the names
 * of all Load Leveler resources and implement the methods
 * which convert resource definitions between component XML
 * strings and name-to-value mappings.  These are used in
 * the LoadLevelerEnabler and LoadLevelerOptionsPanel
 * classes; putting them in this class keeps these two
 * classes from depending on each other.
 */
public class LoadLevelerUtils {

	// These constants define the available Load Leveler resources.
	public static final String LDLEV_RESOURCE_JOB_NAME = "jobname";
	public static final String LDLEV_RESOURCE_CLASS = "class";
	public static final String LDLEV_RESOURCE_ACCOUNT = "account";
	public static final String LDLEV_RESOURCE_PARALLEL_TASKS = "paralleltasks";
	public static final String LDLEV_RESOURCE_MAX_MEMORY = "maxmemory";
	public static final String LDLEV_RESOURCE_START_DATE = "startdate";
	public static final String LDLEV_RESOURCE_RUN_LIMIT = "runlimit";
	public static final String LDLEV_RESOURCE_WORKING_DIR = "workingdir";

	/**
	 * Private constructor for an LoadLevelerUtils object.
	 * No instance of this class need be created.
	 */
	private LoadLevelerUtils() {
	}

	/**
	 * This method implements the 'load' half of the Load Leveler
	 * resource settings protocol.  The given string is parsed into
	 * a list of lines of tab-separated name-value pairs, where each
	 * name must be one of the LDLEV_RESOURCE_xxxx constants.  It
	 * returns the parsed string as a name-value map.
	 * @param s
	 * @return Map<String,String>
	 */
	public static Map<String,String> fromString(String s) {

		// Begin by setting defaults:
		Map<String,String> newMap = new HashMap<String,String>();
		newMap.put(LDLEV_RESOURCE_JOB_NAME, "");
		newMap.put(LDLEV_RESOURCE_CLASS, "");
		newMap.put(LDLEV_RESOURCE_ACCOUNT, "");
		newMap.put(LDLEV_RESOURCE_PARALLEL_TASKS, "");
		newMap.put(LDLEV_RESOURCE_MAX_MEMORY, "");
		newMap.put(LDLEV_RESOURCE_START_DATE, "");
		newMap.put(LDLEV_RESOURCE_RUN_LIMIT, "");
		newMap.put(LDLEV_RESOURCE_WORKING_DIR, "");

		// Extract lines of tab-separated name-value pairs:
		if (s != null) {
			for (String setting : s.split("\\n")) {
				int iTab = setting.indexOf('\t');
				if (iTab > 0) {
					String resourceName = setting.substring(0,iTab);
					if (newMap.containsKey(resourceName)) {
						newMap.put(resourceName,setting.substring(iTab + 1));
					}
				}
			}
		}

		// Replace the old settings en masse:
		return newMap;
	}

	
	/**
	 * This method implements the 'store' half of the Load Leveler
	 * resource settings protocol.  It packs the entries of the
	 * given map into a string as a list of lines of tab-separated
	 * name-value pairs.  Only non-trivial values are recorded in
	 * this string.
	 * @param resourceNameMap Map<String,String>
	 * @return String
	 */
	public static String toString(Map<String,String> resourceNameMap) {

		if (resourceNameMap != null) {
			StringBuilder sb = new StringBuilder();
			for (Map.Entry<String,String> entry : resourceNameMap.entrySet()) {
				String resourceSetting = entry.getValue();

				// Save only nonempty settings:
				if (resourceSetting.length() > 0) {
					sb.append(entry.getKey()).append('\t').append(resourceSetting).append('\n');
				}
			}

			return sb.toString();
		}
		else {
			return "";
		}
	}
}
