view src/main/java/de/mpiwg/indexmeta/web/servlet/methods/JSONSaveCtx.java @ 9:2bc783262919 default tip

Implementation of Servlet
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Wed, 24 Apr 2013 10:34:46 +0200
parents
children
line wrap: on
line source

package de.mpiwg.indexmeta.web.servlet.methods;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;

import de.mpiwg.indexmeta.bo.Contextualization;
import de.mpiwg.indexmeta.services.DataProvider;

public class JSONSaveCtx  extends AbstractServletMethod {

	public static void execute(DataProvider dp,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		JSONObject json = new JSONObject();

		long startExecution = System.currentTimeMillis();

		String indexMetaId = getString(request, p_indexMetaId);
		String elementId = getString(request, p_elementId);
		
		String comment = getString(request, p_comment);
		String content = getString(request, p_content);
		String remoteId = getString(request, p_remoteId);
		String state = getString(request, p_state);
		String type = getString(request, p_type);
		
		if(StringUtils.isEmpty(indexMetaId) || 
				StringUtils.isEmpty(elementId) || 
				StringUtils.isEmpty(type)){
			
			response.sendError(HttpServletResponse.SC_BAD_REQUEST,
					"Parameters 'indexMetaId', 'elementId' and 'type' are null. "
							+ "At least one of them must be not null.");
			return;
		}
		
		Contextualization ctx = dp.getCtx(indexMetaId, elementId);
		
		if(ctx == null){
			ctx = new Contextualization();
			ctx.setIndexMetaId(indexMetaId);
			ctx.setElementId(elementId);
		}
		
		ctx.setComment(comment);
		ctx.setContent(content);
		ctx.setRemoteId(remoteId);
		ctx.setState(state);
		ctx.setType(type);
		
		json.put(RUNTIME, (System.currentTimeMillis() - startExecution));
		
		PrintWriter out = response.getWriter();
		out.print(json.toString());	
		
	}

}