All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.jpattern.rest.command.JsonToObjectCommand Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
package com.googlecode.jpattern.rest.command;

import org.codehaus.jackson.map.ObjectMapper;

import com.googlecode.jpattern.shared.command.IBaseCommand;
import com.googlecode.jpattern.shared.command.IBaseCommandResult;
import com.googlecode.jpattern.shared.command.NullBaseCommand;
import com.googlecode.jpattern.shared.result.ErrorMessage;
import com.googlecode.jpattern.shared.util.GenericWrapper;

/**
 * 
 * @author Francesco Cina'
 *
 * 11/mag/2011
 */
public class JsonToObjectCommand extends ARestCommand {

	private static final long serialVersionUID = 1L;
	private static ObjectMapper MAPPER = new ObjectMapper();
	private final Class aClass;
	private final GenericWrapper resultWrapper;
	private final StringBuffer jsonString;

	public JsonToObjectCommand(Class aClass, GenericWrapper resultWrapper, StringBuffer jsonString) {
		this(aClass, resultWrapper, jsonString, new NullBaseCommand());
	}
	
	public JsonToObjectCommand(Class aClass, GenericWrapper resultWrapper, StringBuffer jsonString, IBaseCommand previousCommand) {
		super(previousCommand);
		this.aClass = aClass;
		this.resultWrapper = resultWrapper;
		this.jsonString = jsonString;
	}

	@Override
	protected void result(IBaseCommandResult result) {
		try {
			resultWrapper.setValue( MAPPER.readValue(jsonString.toString(), aClass) );			
		} catch (Exception e) {
			result.addErrorMessage(new ErrorMessage(getClass().getName(), e.getMessage()));
			getLogger().error("result", "", e);
		}
	}

	@Override
	protected void internalRollBack(IBaseCommandResult result) {
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy