/*******************************************************************************
 * Copyright (c) 2016 Matt Day, Cisco and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************/
package com.cisco.matday.ucsd.hp3par.reports.volume.actions;

import org.apache.log4j.Logger;

import com.cisco.matday.ucsd.hp3par.account.HP3ParCredentials;
import com.cisco.matday.ucsd.hp3par.account.inventory.HP3ParInventory;
import com.cisco.matday.ucsd.hp3par.exceptions.HP3ParVolumeException;
import com.cisco.matday.ucsd.hp3par.rest.cpg.json.CPGResponseMember;
import com.cisco.matday.ucsd.hp3par.rest.json.HP3ParRequestStatus;
import com.cisco.matday.ucsd.hp3par.rest.volumes.json.VolumeResponseMember;
import com.cisco.matday.ucsd.hp3par.tasks.volumes.EditVolumeConfig;
import com.cisco.matday.ucsd.hp3par.tasks.volumes.HP3ParVolumeExecute;
import com.cloupia.model.cIM.ConfigTableAction;
import com.cloupia.model.cIM.ReportContext;
import com.cloupia.service.cIM.inframgr.forms.wizard.Page;
import com.cloupia.service.cIM.inframgr.forms.wizard.PageIf;
import com.cloupia.service.cIM.inframgr.forms.wizard.WizardSession;
import com.cloupia.service.cIM.inframgr.reports.simplified.CloupiaPageAction;

/**
 * Creates an action button to edit a volume
 *
 * @author Matt Day
 *
 */
public class EditVolumeAction extends CloupiaPageAction {

	private static Logger logger = Logger.getLogger(CreateVolumeCopyAction.class);

	// need to provide a unique string to identify this form and action
	private static final String FORM_ID = "com.cisco.matday.ucsd.hp3par.reports.actions.EditVolumeActionForm";
	private static final String ACTION_ID = "com.cisco.matday.ucsd.hp3par.reports.actions.EditVolumeActionAction";
	private static final String LABEL = "Edit";
	private static final String DESCRIPTION = "Edit Volume";

	@Override
	public void definePage(Page page, ReportContext context) {
		// Use the same form (config) as the Create Volume custom task
		page.bind(FORM_ID, EditVolumeConfig.class);
	}

	/**
	 * This sets up the initial fields and provides default values (in this case
	 * the account name)
	 */
	@Override
	public void loadDataToPage(Page page, ReportContext context, WizardSession session) throws Exception {
		String query = context.getId();
		EditVolumeConfig form = new EditVolumeConfig();
		/*
		 * Unlike CreateVolumeAction, this returns true on isSelectionRequired()
		 *
		 * This means the context is whatever's in column 0 of the table. In
		 * this case it's in the format:
		 *
		 * accountName;volumeName;cpgName;copyCpgName
		 */
		final String volume = query.split(";")[1];
		final String volumeName = volume.split("@")[2];
		final HP3ParCredentials login = new HP3ParCredentials(context);

		// Populate the copy CPG field if it's already set
		try {
			String copyCpg = query.split(";")[3];
			if (!copyCpg.equals("")) {
				// Have to do an API lookup as we need the ID which isn't in the
				// volume REST response:
				CPGResponseMember cpg = HP3ParInventory.getCpgInfo(login, copyCpg);
				// Build it in the format for the CPG table:
				// ID@AccountName@CPGName
				copyCpg = cpg.getId() + "@" + login.getAccountName() + "@" + cpg.getName();
				form.setCopyCpg(copyCpg);
			}
		}
		catch (@SuppressWarnings("unused") ArrayIndexOutOfBoundsException e) {
			// Do nothing if this field is null
		}

		// Pre-populate the account, volume and CPG fields:
		form.setVolume(volume);

		VolumeResponseMember volInfo = HP3ParInventory.getVolumeInfo(login, volumeName);

		// Pre-populate volume name

		form.setNewVolumeName(volumeName);

		form.setComment(volInfo.getComment());

		// Set the account and volume fields to read-only (I couldn't find this
		// documented anywhere, maybe there's a better way to do it?)
		page.getFlist().getByFieldId(FORM_ID + ".volume").setEditable(false);

		session.getSessionAttributes().put(FORM_ID, form);
		page.marshallFromSession(FORM_ID);

	}

	/**
	 * This should do basic error checking (UCSD will enforce mandatory fields)
	 * and attempt to execute the task.
	 *
	 * Throwing an exception here will show the message as an error dialogue.
	 */
	@Override
	public int validatePageData(Page page, ReportContext context, WizardSession session) throws Exception {
		Object obj = page.unmarshallToSession(FORM_ID);
		EditVolumeConfig config = (EditVolumeConfig) obj;

		// Get credentials from the current context
		HP3ParCredentials c = new HP3ParCredentials(context);
		HP3ParRequestStatus s = HP3ParVolumeExecute.edit(c, config);

		// Throwing an exception fails the submit and shows the error in the
		// window
		if (!s.isSuccess()) {
			logger.warn("Failed to edit volume: " + s.getError());
			throw new HP3ParVolumeException("Failed to edit volume: " + s.getError());
		}

		// Set the text for the "OK" prompt and return successfully
		page.setPageMessage("Volume edited OK");

		return PageIf.STATUS_OK;
	}

	@Override
	public boolean isDoubleClickAction() {
		return false;
	}

	@Override
	public boolean isDrilldownAction() {
		return false;
	}

	@Override
	public boolean isMultiPageAction() {
		return false;
	}

	@Override
	public boolean isSelectionRequired() {
		return true;
	}

	@Override
	public String getActionId() {
		return ACTION_ID;
	}

	@Override
	public int getActionType() {
		return ConfigTableAction.ACTION_TYPE_POPUP_FORM;
	}

	@Override
	public String getDescription() {
		return DESCRIPTION;
	}

	@Override
	public String getFormId() {
		return FORM_ID;
	}

	@Override
	public String getLabel() {
		return LABEL;
	}

	@Override
	public String getTitle() {
		return LABEL;
	}

}

@@ -3921,27 +3921,23 @@
 if (
-!
 copyCpg
-.equals(%22%22)
+ != null
 ) %7B%0A

Copy CPG is no longer null