/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.editor.fckeditor;

import com.liferay.portal.editor.fckeditor.command.Command;
import com.liferay.portal.editor.fckeditor.command.CommandArgument;
import com.liferay.portal.editor.fckeditor.command.CommandFactory;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Ivica Cardic
 */
public class ConnectorAction extends Action {

	@Override
	public ActionForward execute(
			ActionMapping actionMapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
		throws Exception {

		try {
			String command = request.getParameter("Command");
			String type = request.getParameter("Type");
			String sortType = request.getParameter("SortType");

			String ascendingString = request.getParameter("Ascending");

			boolean ascending = GetterUtil.getBoolean(ascendingString);

			String currentFolder = request.getParameter("CurrentFolder");
			String newFolder = ParamUtil.getString(request, "NewFolderName");

			if (_log.isDebugEnabled()) {
				_log.debug("Command " + command);
				_log.debug("Type " + type);
				_log.debug("SortType " + sortType);
				_log.debug("Ascending " + ascending);
				_log.debug("Current folder " + currentFolder);
				_log.debug("New folder " + newFolder);
			}

			ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
				WebKeys.THEME_DISPLAY);

			CommandArgument commandArgument = new CommandArgument(
				command, type, sortType, ascending, currentFolder, newFolder,
				themeDisplay, request);

			Command commandModel = CommandFactory.getCommand(command);

			commandModel.execute(commandArgument, request, response);
		}
		catch (Exception e) {
			_log.error(e, e);
		}

		return null;
	}

	private static Log _log = LogFactoryUtil.getLog(ConnectorAction.class);

}