Search in sources :

Example 1 with SelectInstallTargetDialog

use of org.netxms.ui.eclipse.policymanager.dialogs.SelectInstallTargetDialog in project netxms by netxms.

the class InstallPolicy method run.

/* (non-Javadoc)
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
@Override
public void run(IAction action) {
    int initialMode = SelectInstallTargetDialog.INSTALL_ON_CURRENT;
    for (AgentPolicy p : currentSelection) {
        if (p.hasChildren()) {
            initialMode = SelectInstallTargetDialog.INSTALL_ON_SELECTED;
            break;
        }
    }
    final SelectInstallTargetDialog dlg = new SelectInstallTargetDialog(shell, initialMode);
    if (dlg.open() == Window.OK) {
        final NXCSession session = (NXCSession) ConsoleSharedData.getSession();
        for (final AgentPolicy policy : currentSelection) {
            new ConsoleJob(String.format(Messages.get().InstallPolicy_JobName, policy.getObjectName()), null, Activator.PLUGIN_ID, null) {

                @Override
                protected void runInternal(IProgressMonitor monitor) throws Exception {
                    final AbstractObject[] nodeList = (dlg.getInstallMode() == SelectInstallTargetDialog.INSTALL_ON_SELECTED) ? dlg.getSelectedObjects() : policy.getChildsAsArray();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < nodeList.length; i++) {
                        try {
                            session.deployAgentPolicy(policy.getObjectId(), nodeList[i].getObjectId());
                        } catch (NXCException e) {
                            if (e.getErrorCode() == CommonRCC.INCOMPATIBLE_OPERATION || e.getErrorCode() == CommonRCC.INTERNAL_ERROR || e.getErrorCode() == CommonRCC.ACCESS_DENIED || e.getErrorCode() == CommonRCC.INVALID_OBJECT_ID) {
                                if (sb.length() == 0)
                                    sb.append("Could not apply policy to the node(s):\n");
                                sb.append(nodeList[i].getObjectName());
                                sb.append(" - ");
                                sb.append(e.getLocalizedMessage());
                                sb.append("\n");
                            } else {
                                throw (e);
                            }
                        }
                    }
                    if (sb.length() != 0) {
                        final String error = sb.toString();
                        runInUIThread(new Runnable() {

                            @Override
                            public void run() {
                                MessageDialog.openError(shell, "Error", error);
                            }
                        });
                    }
                }

                @Override
                protected String getErrorMessage() {
                    return String.format(Messages.get().InstallPolicy_JobError, policy.getObjectName());
                }
            }.start();
        }
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AgentPolicy(org.netxms.client.objects.AgentPolicy) NXCSession(org.netxms.client.NXCSession) SelectInstallTargetDialog(org.netxms.ui.eclipse.policymanager.dialogs.SelectInstallTargetDialog) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) NXCException(org.netxms.client.NXCException) NXCException(org.netxms.client.NXCException)

Aggregations

IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NXCException (org.netxms.client.NXCException)1 NXCSession (org.netxms.client.NXCSession)1 AgentPolicy (org.netxms.client.objects.AgentPolicy)1 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)1 SelectInstallTargetDialog (org.netxms.ui.eclipse.policymanager.dialogs.SelectInstallTargetDialog)1