package de.hsadmin.core.qserv;

import java.util.Iterator;
import java.util.Map;

public class CreateFileProcessor extends AbstractProcessor {

	private static final long serialVersionUID = -2947609532975712444L;

	private Processor compoundProcessor;
	
	public CreateFileProcessor(String templateName, Map<String, String> templateValues, String targetPath, String owner, String group, String modeMask, boolean overwriteTarget) throws ProcessorException {
		TemplateProcessor templateProcessor = new TemplateProcessor(templateName, templateValues, targetPath, overwriteTarget);
		compoundProcessor = createCompound(targetPath, owner, group, modeMask, templateProcessor);
	}

	public CreateFileProcessor(String templateName, Map<String, String> templateValues, Iterator<Map<String, String>> iterateValues, String targetPath, String owner, String group, String modeMask, boolean overwriteTarget) throws ProcessorException {
		TemplateProcessor templateProcessor = new TemplateProcessor(templateName, templateValues, iterateValues, targetPath, overwriteTarget);
		compoundProcessor = createCompound(targetPath, owner, group, modeMask, templateProcessor);
	}

	private Processor createCompound(String targetPath, String owner, String group,
			String modeMask, TemplateProcessor templateProcessor) {
		ShellProcessor shellProcessor = 
			new ShellProcessor(
				"chown " + owner + ":" + group + " " + targetPath + " && " +
				"chmod " + modeMask + " " + targetPath);
		return new CompoundProcessor(templateProcessor, shellProcessor);
	}
	
	@Override
	public Object process() throws ProcessorException {
		return compoundProcessor.process();
	}

}
