com.clustercontrol.jobmanagement.view.action.StartJobAction.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.jobmanagement.view.action.StartJobAction.java

Source

/*
    
Copyright (C) 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.
    
 */

package com.clustercontrol.jobmanagement.view.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.menus.UIElement;

import com.clustercontrol.bean.Property;
import com.clustercontrol.bean.PropertyDefineConstant;
import com.clustercontrol.jobmanagement.OperationMessage;
import com.clustercontrol.jobmanagement.action.OperationJob;
import com.clustercontrol.jobmanagement.bean.JobOperationConstant;
import com.clustercontrol.jobmanagement.composite.DetailComposite;
import com.clustercontrol.jobmanagement.composite.HistoryComposite;
import com.clustercontrol.jobmanagement.composite.NodeDetailComposite;
import com.clustercontrol.jobmanagement.dialog.JobOperationDialog;
import com.clustercontrol.jobmanagement.util.JobEndpointWrapper;
import com.clustercontrol.jobmanagement.view.JobDetailView;
import com.clustercontrol.jobmanagement.view.JobHistoryView;
import com.clustercontrol.jobmanagement.view.JobNodeDetailView;
import com.clustercontrol.util.HinemosMessage;
import com.clustercontrol.util.Messages;
import com.clustercontrol.ws.jobmanagement.InvalidRole_Exception;

/**
 * [][][]????<BR>
 *
 * @version 5.0.0
 * @since 1.0.0
 */
public abstract class StartJobAction extends AbstractHandler implements IElementUpdater {

    // 
    private static Log m_log = LogFactory.getLog(StartJobAction.class);

    /** ID */
    public static final String ID = StartJobAction.class.getName();

    /**  */
    private IWorkbenchPart viewPart;

    /**
     * Dispose
     */
    @Override
    public void dispose() {
        this.viewPart = null;
    }

    /**
     * ?
     *
     * @param managerName ???
     * @param sessionId ID
     * @param jobunitId ?ID
     * @param jobId ID
     * @param facilityId ID
     * @return ?
     *
     */
    private Property getStartProperty(String managerName, String sessionId, String jobunitId, String jobId,
            String facilityId) {
        Locale locale = Locale.getDefault();
        //ID
        Property session = new Property(JobOperationConstant.SESSION, Messages.getString("session.id", locale),
                PropertyDefineConstant.EDITOR_TEXT);
        //ID
        Property jobUnit = new Property(JobOperationConstant.JOB_UNIT, Messages.getString("jobunit.id", locale),
                PropertyDefineConstant.EDITOR_TEXT);
        //ID
        Property job = new Property(JobOperationConstant.JOB, Messages.getString("job.id", locale),
                PropertyDefineConstant.EDITOR_TEXT);
        //ID
        Property facility = new Property(JobOperationConstant.FACILITY, Messages.getString("facility.id", locale),
                PropertyDefineConstant.EDITOR_TEXT);
        //
        Property control = new Property(JobOperationConstant.CONTROL, Messages.getString("control", locale),
                PropertyDefineConstant.EDITOR_SELECT);

        List<Integer> values = null;
        try {
            JobEndpointWrapper wrapper = JobEndpointWrapper.getWrapper(managerName);
            values = wrapper.getAvailableStartOperation(sessionId, jobunitId, jobId, facilityId);
        } catch (InvalidRole_Exception e) {
            MessageDialog.openInformation(null, Messages.getString("message"),
                    Messages.getString("message.accesscontrol.16"));
            throw new InternalError("values is null.");
        } catch (Exception e) {
            m_log.warn("getStartProperty(), " + e.getMessage(), e);
            MessageDialog.openError(null, Messages.getString("failed"),
                    Messages.getString("message.hinemos.failure.unexpected") + ", "
                            + HinemosMessage.replace(e.getMessage()));
            throw new InternalError("values is null.");
        }

        // type?String??
        List<String> valuesStr = new ArrayList<String>();
        for (Integer controlType : values) {
            valuesStr.add(OperationMessage.typeToString(controlType));
        }

        //?
        if (values.size() >= 1) {
            Object controlValues[][] = { valuesStr.toArray(), valuesStr.toArray() };
            control.setSelectValues(controlValues);
            control.setValue(valuesStr.get(0));
        } else {
            Object controlValues[][] = { { "" }, { "" } };
            control.setSelectValues(controlValues);
            control.setValue("");
        }

        session.setValue(sessionId);
        jobUnit.setValue(jobunitId);
        job.setValue(jobId);
        if (facilityId != null && facilityId.length() > 0) {
            facility.setValue(facilityId);
        } else {
            facility.setValue("");
        }

        //??/??
        session.setModify(PropertyDefineConstant.MODIFY_NG);
        jobUnit.setModify(PropertyDefineConstant.MODIFY_NG);
        job.setModify(PropertyDefineConstant.MODIFY_NG);
        facility.setModify(PropertyDefineConstant.MODIFY_NG);
        control.setModify(PropertyDefineConstant.MODIFY_OK);

        Property property = new Property(null, null, "");

        // ??
        property.removeChildren();
        property.addChildren(session);
        property.addChildren(jobUnit);
        property.addChildren(job);
        if (facilityId != null && facilityId.length() > 0) {
            property.addChildren(facility);
        }
        property.addChildren(control);

        return property;
    }

    /**
     * [][][]?????????????
     * <p>
     * <ol>
     * <li>[][]????IDID????</li>
     * <li>[]?????IDIDID????</li>
     * <li>?????</li>
     * <li>[]???</li>
     * <li>[]??????</li>
     * <li>??????</li>
     * </ol>
     *
     * @see org.eclipse.core.commands.IHandler#execute
     * @see com.clustercontrol.jobmanagement.view.JobHistoryView
     * @see com.clustercontrol.jobmanagement.view.JobDetailView
     * @see com.clustercontrol.jobmanagement.view.JobNodeDetailView
     * @see com.clustercontrol.jobmanagement.dialog.JobOperationDialog
     * @see com.clustercontrol.jobmanagement.action.OperationJob#operationJob(Property)
     */
    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        String managerName = null;
        String sessionId = null;
        String jobunitId = null;
        String jobId = null;
        String facilityId = null;

        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);

        // ???
        this.viewPart = HandlerUtil.getActivePart(event);

        if (viewPart instanceof JobHistoryView) { // ????
            JobHistoryView jobHistoryView = null;
            try {
                jobHistoryView = (JobHistoryView) viewPart.getAdapter(JobHistoryView.class);
            } catch (Exception e) {
                m_log.info("execute " + e.getMessage());
                return null;
            }

            if (jobHistoryView == null) {
                m_log.info("execute: job history view is null");
            } else {
                HistoryComposite historyComposite = jobHistoryView.getComposite();
                //????
                managerName = historyComposite.getManagerName();
                //ID?
                sessionId = historyComposite.getSessionId();
                //ID?
                jobunitId = historyComposite.getJobunitId();
                //ID?
                jobId = historyComposite.getJobId();
            }
        } else if (viewPart instanceof JobDetailView) { // ????
            JobDetailView jobDetailView = null;
            try {
                jobDetailView = (JobDetailView) viewPart.getAdapter(JobDetailView.class);
            } catch (Exception e) {
                m_log.info("execute " + e.getMessage());
                return null;
            }

            if (jobDetailView == null) {
                m_log.info("execute: job detail view is null");
            } else {
                DetailComposite detailComposite = jobDetailView.getComposite();
                //????
                managerName = detailComposite.getManagerName();
                //ID?
                sessionId = detailComposite.getSessionId();
                //ID?
                jobunitId = detailComposite.getJobunitId();
                //ID?
                jobId = detailComposite.getJobId();
            }
        } else if (viewPart instanceof JobNodeDetailView) { // ????
            JobNodeDetailView jobNodeDetailView = null;
            try {
                jobNodeDetailView = (JobNodeDetailView) viewPart.getAdapter(JobNodeDetailView.class);
            } catch (Exception e) {
                m_log.info("execute " + e.getMessage());
                return null;
            }

            if (jobNodeDetailView == null) {
                m_log.info("execute: job node detail view is null");
            } else {
                NodeDetailComposite nodeDetailComposite = jobNodeDetailView.getComposite();
                //????
                managerName = nodeDetailComposite.getManagerName();
                //ID?
                sessionId = nodeDetailComposite.getSessionId();
                //ID?
                jobunitId = nodeDetailComposite.getJobunitId();
                //ID?
                jobId = nodeDetailComposite.getJobId();
                //ID?
                facilityId = nodeDetailComposite.getFacilityId();
                if (facilityId == null) {
                    sessionId = null;
                    jobunitId = null;
                    jobId = null;
                }
            }
        }

        if (sessionId != null && sessionId.length() > 0 && jobunitId != null && jobunitId.length() > 0
                && jobId != null && jobId.length() > 0) {

            JobOperationDialog dialog = new JobOperationDialog(window.getShell());

            //
            dialog.setProperty(getStartProperty(managerName, sessionId, jobunitId, jobId, facilityId));
            dialog.setTitleText(Messages.getString("job") + "[" + Messages.getString("start") + "]");

            //
            if (dialog.open() == IDialogConstants.OK_ID) {
                //
                OperationJob operation = new OperationJob();
                operation.operationJob(managerName, dialog.getProperty());
            }
        }
        return null;
    }

    @Override
    public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
        IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        // page may not start at state restoring
        if (null != window) {
            IWorkbenchPage page = window.getActivePage();
            if (null != page) {
                IWorkbenchPart part = page.getActivePart();

                boolean editEnable = false;
                if (part instanceof JobHistoryView) {
                    // Enable button when 1 item is selected
                    JobHistoryView view = (JobHistoryView) part;
                    if (view.getSelectedNum() > 0) {
                        editEnable = true;
                    }
                } else if (part instanceof JobDetailView) {
                    JobDetailView view = (JobDetailView) part;
                    if (view.getSelectedNum() > 0) {
                        editEnable = true;
                    }
                } else if (part instanceof JobNodeDetailView) {
                    JobNodeDetailView view = (JobNodeDetailView) part;
                    if (view.getSelectedNum() > 0) {
                        editEnable = true;
                    }
                }
                this.setBaseEnabled(editEnable);
            } else {
                this.setBaseEnabled(false);
            }
        }
    }

}