com.jpattern.core.textfiles.FileCreateCommand Maven / Gradle / Ivy
package com.jpattern.core.textfiles;
import com.jpattern.core.command.NullCommand;
import com.jpattern.core.command.ACommand;
import com.jpattern.core.command.ICommand;
import com.jpattern.core.command.ICommandResult;
import com.jpattern.logger.ILogger;
import com.jpattern.shared.result.ErrorMessage;
/**
*
* @author Francesco Cina'
*
* 10/giu/2010
*/
public class FileCreateCommand extends ACommand {
private static final long serialVersionUID = 1L;
private ILogger logger;
private FileWrapper fileWrapper;
private StringBuffer fileContent;
private StringBuffer fileName;
private IResource resource;
public FileCreateCommand(IResource resource, StringBuffer fileName, StringBuffer fileContent, FileWrapper fileWrapper) {
this( resource, fileName, fileContent, fileWrapper, new NullCommand() );
}
public FileCreateCommand(IResource resource, StringBuffer fileName, StringBuffer fileContent, FileWrapper fileWrapper, ICommand aSuccessor) {
super(aSuccessor);
this.resource = resource;
this.fileName = fileName;
this.fileContent = fileContent;
this.fileWrapper = fileWrapper;
}
@Override
protected void internalRollBack(ICommandResult result) {
}
@Override
protected void result(ICommandResult result) {
logger = getProvider().getLoggerService().logger(FileCreateCommand.class);
logger.info("result", "Creating file " + fileName.toString() + " in " + resource.getPath());
try {
IFile newFile = resource.create( fileName.toString(), fileContent.toString() );
fileWrapper.setFile(newFile);
if ( newFile.exists() ){
logger.debug("result", "Created file " + newFile.getPath());
}
else {
logger.error("result", "Error creating file " + fileName);
result.addErrorMessage( new ErrorMessage("FileCreateCommand", "Error creating file " + fileName) );
}
} catch (Exception e) {
logger.error("result", "Error creating file " + fileName, e);
result.addErrorMessage( new ErrorMessage("FileCreateCommand", "Error creating file " + fileName + ": " + e.getMessage()) );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy