package com.onaro.sanscreen.client.view.actions.migration; import java.awt.event.ActionEvent; import java.util.Collections; import java.util.List; import java.util.ResourceBundle; import com.onaro.sanscreen.client.SessionsHelper; import com.onaro.sanscreen.client.error.CentralErrorHandler; import com.onaro.sanscreen.client.view.ActionFactory; import com.onaro.sanscreen.client.view.ViewDirector; import com.onaro.sanscreen.client.view.actions.SingleSelectionAction; import com.onaro.sanscreen.client.view.selection.ObjectSelectionModel; import com.onaro.sanscreen.client.view.selection.SelectedObject; import com.onaro.sanscreen.server.migration.query.data.MigrationTaskStateType; import com.onaro.sanscreen.server.migration.sessions.MigrationException; import com.onaro.sanscreen.server.migration.sessions.MigrationSession; import com.onaro.util.IllegalInitException; import com.onaro.util.jfc.RoleRestrictedAction; /** * Change the state of some tasks. * A StateMachine in the state enum determines what are the next available states */ public class ChangeMigrationTaskStateAction extends SingleSelectionAction implements RoleRestrictedAction { private static final long serialVersionUID = 1L; private MigrationTaskStateType changeToState; private String stateName; public ChangeMigrationTaskStateAction(ActionFactory.ActionInitInfo actionInitInfo, ResourceBundle resources, ViewDirector targetView) throws IllegalInitException{ super(actionInitInfo, resources, targetView); String name = getName(); //the name of the action is used to set the state of the action stateName = name.substring(name.lastIndexOf('.') + 1); changeToState = MigrationTaskStateType.valueOf(stateName.toUpperCase()); } @Override public void setEnableBySelection(ObjectSelectionModel selection){ setEnabled(shouldBeEnabled(selection)); } //action is enabled if the task can move to the selected state. private boolean shouldBeEnabled(ObjectSelectionModel selection){ if(selection.size() == 1){ SelectedObject selectedObject = getSelection(); return MigrationUtil.getState(selectedObject).canChangeTo(changeToState); } return false; } public void actionPerformed(ActionEvent evt) { try { assert getSelection() != null : "this action should be enabled only when one row is selected"; //$NON-NLS-1$ List list = Collections.singletonList(getSelection().getObjIdLong()); MigrationSession session = SessionsHelper.getInstance().getMigrationSession(); session.setMigrationTaskState(list, changeToState); } catch (SessionsHelper.SessionsException e) { CentralErrorHandler.handle("Failed to set state for switch migration tasks", e); //$NON-NLS-1$ } catch (MigrationException e) { CentralErrorHandler.handle("Failed to set state for switch migration tasks", e); //$NON-NLS-1$ } } }