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

net.anotheria.anosite.gen.ascustomaction.action.TransferCustomActionDefAction Maven / Gradle / Ivy

There is a newer version: 4.1.2
Show newest version
/**
 ********************************************************************************
 *** TransferCustomActionDefAction.java                                       ***
 *** generated by AnoSiteGenerator (ASG), Version: 2.6.3                      ***
 *** Copyright (C) 2005 - 2010 Anotheria.net, www.anotheria.net               ***
 *** All Rights Reserved.                                                     ***
 ********************************************************************************
 *** Don't edit this code, if you aren't sure                                 ***
 *** that you do exactly know what you are doing!                             ***
 *** It's better to invest time in the generator, as into the generated code. ***
 ********************************************************************************
 */

package net.anotheria.anosite.gen.ascustomaction.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.anotheria.maf.action.ActionForward;
import net.anotheria.maf.action.ActionMapping;
import net.anotheria.maf.bean.FormBean;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.IOException;
import java.io.PrintWriter;
import net.anotheria.anosite.gen.ascustomaction.service.ASCustomActionServiceException;
import net.anotheria.anosite.config.DocumentTransferConfig;
import org.configureme.ConfigurationManager;
import org.json.JSONException;
import net.anotheria.maf.json.JSONResponse;
import org.codehaus.jettison.json.JSONArray;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.ClientResponse;
import net.anotheria.anosite.util.staticutil.JerseyClientUtil;


public class TransferCustomActionDefAction extends BaseCustomActionDefAction{


	private static final int STATUS_OK = 201;
	private static final String ERROR = "error";

	private DocumentTransferConfig config = DocumentTransferConfig.getInstance();

	// Generated by: class net.anotheria.asg.generator.view.action.ModuleActionsGenerator.generateTransferActionMethod

	public ActionForward anoDocExecute(ActionMapping mapping, FormBean af, HttpServletRequest req, HttpServletResponse res) throws Exception{

		JSONResponse response = new JSONResponse();
		if (ConfigurationManager.INSTANCE.getDefaultEnvironment().expandedStringForm().equals("prod")) {
			response.addError(ERROR, "Environment is prod");
			writeTextToResponse(res, response);
			return null;
		}

		String id = getStringParameter(req, PARAM_ID);
		JSONArray data = new JSONArray();
		try {
			getASCustomActionService().fetchCustomActionDef(id, new HashSet(), data);
		} catch (ASCustomActionServiceException e) {
			response.addError(ERROR, "Problem occurred when fetching info about documents -" + e.getMessage());
			writeTextToResponse(res, response);
			return null;
		}

		Client client = JerseyClientUtil.getClientInstance();
		for (String domain :config.getDomains()) {
			WebResource webResource = client.resource(domain + "/api/customactiondef");
			ClientResponse clientResponse = webResource.header("Content-Type", "application/json;charset=utf-8")
			 		.post(ClientResponse.class, data);

			if (clientResponse.getStatus() != STATUS_OK) {
				clientResponse.close();
				response.addError(ERROR, "Couldn't save transferred objects, status expected 201, got status - " + clientResponse.getStatus());
				writeTextToResponse(res, response);
			}

			clientResponse.close();
		}
		return null;
	}

	private void writeTextToResponse(final HttpServletResponse res, final JSONResponse jsonResponse) throws IOException, JSONException {
		res.setCharacterEncoding("UTF-8");
		res.setContentType("application/json");
		PrintWriter writer = res.getWriter();
		writer.write(jsonResponse.toString());
		writer.flush();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy