package de.hsadmin.core.qserv;

import java.util.Map;

import de.hsadmin.mods.dom.Domain;
import de.hsadmin.mods.pac.Hive;

public class CreateFileProcessor extends AbstractProcessor {

	private static final long serialVersionUID = -2947609532975712444L;

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

	public CreateFileProcessor(String templateName, Hive hive, String targetPath, String owner, String group, String modeMask, boolean overwriteTarget) throws ProcessorException {
		VelocityProcessor templateProcessor = new VelocityProcessor(templateName, hive, targetPath, overwriteTarget);
		compoundProcessor = createCompound(targetPath, owner, group, modeMask, templateProcessor);
	}

	private Processor createCompound(String targetPath, String owner, String group,
			String modeMask, Processor 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();
	}

}
