/**
 * ConversionHelper.java
 * Created on Jun 30, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.util.schedule.ingest;

import java.io.File;

import com.tandbergtv.watchpoint.pmm.util.PathProperties;

/**
 * This class contains all common methods required by various convertors.
 * 
 * @author spuranik
 */
public class ConversionHelper {

	private static String FILE_NAME_EXTN_SEPARATOR = ".";
	private static String OUTPUT_FILE_SUFFIX = ".xml";

	/**
	 * Returns the folder path based on the type of the file. It makes that directory if it does not
	 * exist.
	 * 
	 * @return output directory path.
	 */
	public static String getOutputPath(String providerId, FileType type) {
		String contentPath = PathProperties.getProperty(PathProperties.CONTENT_PATH);
		String archivePath = "";
		if (type == FileType.PLANNER) {
			archivePath = PathProperties.getProperty(PathProperties.PROCESSED_PLANNER_PATH);
		} else if (type == FileType.PROGRAM_LIST) {
			archivePath = PathProperties.getProperty(PathProperties.PROCESSED_PROGRAM_LIST_PATH);
		}

		String outputPath = contentPath + File.separator + providerId + File.separator
				+ archivePath;
		File outputPathDir = new File(outputPath);
		if (!outputPathDir.exists()) {
			outputPathDir.mkdir();
		}

		return outputPath;
	}

	/**
	 * Removes the file extension and returns the filename for the name provided. 
	 * 
	 * @param name
	 * @return
	 */
	public static String getFilename(String name) {
		File file = new File(name);
		String filename = file.getName();
		int index = filename.lastIndexOf(FILE_NAME_EXTN_SEPARATOR);
		if (index != -1) {
			return filename.substring(0, index);
		}
		return name;
	}
	
	/**
	 * Combines the folder name with the file name and suffixes .xml as the file xtn. 
	 *  
	 * @param folder
	 * @param filename
	 * @return
	 */
	public static String generateFileName(String folder, String filename)
	{
		return folder + File.separator + filename + OUTPUT_FILE_SUFFIX;
	}

}
