Search in sources :

Example 26 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class HistoricalData method get.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#get(java.lang.String)
    */
@Override
protected Object get(String id, Map<String, String> query) throws Exception {
    NXCSession session = getSession();
    AbstractObject obj = getObject();
    long dciId = 0;
    try {
        dciId = Long.parseLong(id);
    } catch (NumberFormatException e) {
        dciId = session.dciNameToId(obj.getObjectId(), id);
    }
    if (obj == null || dciId == 0 || !(obj instanceof DataCollectionTarget))
        throw new NXCException(RCC.INVALID_OBJECT_ID);
    String timeFrom = query.get("from");
    String timeTo = query.get("to");
    String timeInteval = query.get("timeInterval");
    String itemCount = query.get("itemCount");
    DciData data = null;
    if (timeFrom != null || timeTo != null) {
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(parseLong(timeFrom, 0) * 1000), new Date(parseLong(timeTo, System.currentTimeMillis() / 1000) * 1000), parseInt(itemCount, 0), false);
    } else if (timeInteval != null) {
        Date now = new Date();
        long from = now.getTime() - parseLong(timeInteval, 0) * 1000;
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(from), new Date(), parseInt(itemCount, 0), false);
    } else if (itemCount != null) {
        data = session.getCollectedData(obj.getObjectId(), dciId, null, null, parseInt(itemCount, 0), false);
    } else {
        Date now = new Date();
        // one hour
        long from = now.getTime() - 3600000;
        data = session.getCollectedData(obj.getObjectId(), dciId, new Date(from), now, parseInt(itemCount, 0), false);
    }
    return new ResponseContainer("values", data);
}
Also used : NXCSession(org.netxms.client.NXCSession) AbstractObject(org.netxms.client.objects.AbstractObject) DataCollectionTarget(org.netxms.client.objects.DataCollectionTarget) DciData(org.netxms.client.datacollection.DciData) ResponseContainer(org.netxms.websvc.json.ResponseContainer) NXCException(org.netxms.client.NXCException) Date(java.util.Date)

Example 27 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class AbstractObjectHandler method getObject.

/**
 * Get object from URL entity ID part
 *
 * @return object
 * @throws Exception if object cannot be found or request is invalid
 */
protected AbstractObject getObject() throws Exception {
    String entityId = (String) getRequest().getAttributes().get("object-id");
    NXCSession session = getSession();
    if (!session.isObjectsSynchronized())
        session.syncObjects();
    AbstractObject object;
    try {
        long objectId = Long.parseLong(entityId);
        object = session.findObjectById(objectId);
    } catch (NumberFormatException e) {
        UUID objectGuid = UUID.fromString(entityId);
        object = session.findObjectByGUID(objectGuid);
    }
    if (object == null)
        throw new NXCException(RCC.INVALID_OBJECT_ID);
    return object;
}
Also used : NXCSession(org.netxms.client.NXCSession) AbstractObject(org.netxms.client.objects.AbstractObject) UUID(java.util.UUID) NXCException(org.netxms.client.NXCException)

Example 28 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class Alarms method getCollection.

/* (non-Javadoc)
    * @see org.netxms.websvc.handlers.AbstractHandler#getCollection(org.json.JSONObject)
    */
@Override
protected Object getCollection(Map<String, String> query) throws Exception {
    NXCSession session = getSession();
    Collection<Alarm> alarms = session.getAlarms().values();
    String queryGuid = query.get("objectGuid");
    if (queryGuid != null) {
        UUID objectGuid = UUID.fromString(queryGuid);
        if (!session.isObjectsSynchronized())
            session.syncObjects();
        AbstractObject object = session.findObjectByGUID(objectGuid);
        if (object == null)
            throw new NXCException(RCC.INVALID_OBJECT_ID);
        Iterator<Alarm> iterator = alarms.iterator();
        while (iterator.hasNext()) {
            Alarm alarm = iterator.next();
            if (alarm.getSourceObjectId() != object.getObjectId()) {
                iterator.remove();
            }
        }
    }
    return new ResponseContainer("alarms", alarms);
}
Also used : NXCSession(org.netxms.client.NXCSession) Alarm(org.netxms.client.events.Alarm) AbstractObject(org.netxms.client.objects.AbstractObject) UUID(java.util.UUID) ResponseContainer(org.netxms.websvc.json.ResponseContainer) NXCException(org.netxms.client.NXCException)

Example 29 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class DataCollectionEditor method convertToTemplate.

/**
 * Convert selected item(s) to template items
 */
private void convertToTemplate() {
    final ObjectSelectionDialog dlg = new ObjectSelectionDialog(getSite().getShell(), null, ObjectSelectionDialog.createTemplateSelectionFilter());
    dlg.showFilterToolTip(false);
    if (dlg.open() != Window.OK)
        return;
    AbstractObject[] objects = dlg.getSelectedObjects(Template.class);
    if (objects.length == 0)
        return;
    final Template template = (Template) objects[0];
    IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
    Iterator<?> it = selection.iterator();
    final long[] dciList = new long[selection.size()];
    for (int i = 0; (i < dciList.length) && it.hasNext(); i++) dciList[i] = ((DataCollectionObject) it.next()).getId();
    new ConsoleJob(Messages.get().DataCollectionEditor_ConvertJob_TitlePrefix + object.getObjectName() + Messages.get().DataCollectionEditor_ConvertJob_TitleSuffix, this, Activator.PLUGIN_ID, null) {

        @Override
        protected void runInternal(IProgressMonitor monitor) throws Exception {
            monitor.beginTask(Messages.get(getDisplay()).DataCollectionEditor_ConvertJob_TaskName, 4);
            boolean needApply = true;
            for (long id : template.getChildIdList()) {
                if (id == dciConfig.getNodeId()) {
                    needApply = false;
                    break;
                }
            }
            monitor.worked(1);
            dciConfig.copyObjects(template.getObjectId(), dciList);
            for (long id : dciList) dciConfig.deleteObject(id);
            dciConfig.close();
            monitor.worked(1);
            if (needApply) {
                boolean success = false;
                int retries = 5;
                do {
                    try {
                        session.applyTemplate(template.getObjectId(), dciConfig.getNodeId());
                        success = true;
                    } catch (NXCException e) {
                        if (e.getErrorCode() != RCC.COMPONENT_LOCKED)
                            throw e;
                        Thread.sleep(200);
                    }
                    retries--;
                } while (!success && (retries > 0));
            }
            monitor.worked(1);
            boolean success = false;
            int retries = 5;
            do {
                try {
                    Thread.sleep(500);
                    dciConfig.open();
                    success = true;
                } catch (NXCException e) {
                    if (e.getErrorCode() != RCC.COMPONENT_LOCKED)
                        throw e;
                }
                retries--;
            } while (!success && (retries > 0));
            runInUIThread(new Runnable() {

                @Override
                public void run() {
                    viewer.setInput(dciConfig.getItems());
                }
            });
            monitor.done();
        }

        @Override
        protected String getErrorMessage() {
            return Messages.get().DataCollectionEditor_ConvertJob_ErrorPrefix + object.getObjectName() + Messages.get().DataCollectionEditor_ConvertJob_ErrorSuffix;
        }
    }.start();
}
Also used : DataCollectionObject(org.netxms.client.datacollection.DataCollectionObject) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) PartInitException(org.eclipse.ui.PartInitException) NXCException(org.netxms.client.NXCException) NXCException(org.netxms.client.NXCException) Template(org.netxms.client.objects.Template) ObjectSelectionDialog(org.netxms.ui.eclipse.objectbrowser.dialogs.ObjectSelectionDialog) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AbstractObject(org.netxms.client.objects.AbstractObject) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob)

Example 30 with NXCException

use of org.netxms.client.NXCException in project netxms by netxms.

the class HistoricalGraphView method saveGraph.

/**
 * Save this graph as predefined
 */
private void saveGraph(String graphName, String errorMessage, final boolean canBeOverwritten, final boolean asTemplate) {
    if (asTemplate && useMoreThanOneShoucrNode) {
        String templateError = "More than one node is used for template creation.\nThis may cause undefined behaviour.";
        errorMessage = errorMessage == null ? templateError : errorMessage + "\n\n" + templateError;
    }
    SaveGraphDlg dlg = new SaveGraphDlg(getSite().getShell(), graphName, errorMessage, canBeOverwritten);
    int result = dlg.open();
    if (result == Window.CANCEL)
        return;
    final GraphSettings gs = new GraphSettings(0, session.getUserId(), 0, new ArrayList<AccessListElement>(0));
    gs.setName(dlg.getName());
    gs.setConfig(settings);
    if (asTemplate) {
        gs.setFlags(GraphSettings.GRAPH_FLAG_TEMPLATE);
    }
    if (result == SaveGraphDlg.OVERRIDE) {
        new ConsoleJob(Messages.get().HistoricalGraphView_SaveSettings, this, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(IProgressMonitor monitor) throws Exception {
                session.saveGraph(gs, canBeOverwritten);
            }

            @Override
            protected String getErrorMessage() {
                return Messages.get().HistoricalGraphView_SaveSettingsError;
            }
        }.start();
    } else {
        new ConsoleJob(Messages.get().HistoricalGraphView_SaveSettings, this, Activator.PLUGIN_ID, null) {

            @Override
            protected void runInternal(IProgressMonitor monitor) throws Exception {
                try {
                    session.saveGraph(gs, canBeOverwritten);
                } catch (NXCException e) {
                    if (e.getErrorCode() == RCC.OBJECT_ALREADY_EXISTS) {
                        runInUIThread(new Runnable() {

                            @Override
                            public void run() {
                                saveGraph(gs.getName(), Messages.get().HistoricalGraphView_NameAlreadyExist, true, asTemplate);
                            }
                        });
                    } else {
                        if (e.getErrorCode() == RCC.ACCESS_DENIED) {
                            runInUIThread(new Runnable() {

                                @Override
                                public void run() {
                                    saveGraph(gs.getName(), Messages.get().HistoricalGraphView_NameAlreadyExistNoOverwrite, false, asTemplate);
                                }
                            });
                        } else {
                            throw e;
                        }
                    }
                }
            }

            @Override
            protected String getErrorMessage() {
                return Messages.get().HistoricalGraphView_SaveError;
            }
        }.start();
    }
    updateChart();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GraphSettings(org.netxms.client.datacollection.GraphSettings) SaveGraphDlg(org.netxms.ui.eclipse.perfview.dialogs.SaveGraphDlg) ConsoleJob(org.netxms.ui.eclipse.jobs.ConsoleJob) AccessListElement(org.netxms.client.AccessListElement) PartInitException(org.eclipse.ui.PartInitException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NXCException(org.netxms.client.NXCException) NXCException(org.netxms.client.NXCException)

Aggregations

NXCException (org.netxms.client.NXCException)34 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)20 ConsoleJob (org.netxms.ui.eclipse.jobs.ConsoleJob)20 NXCSession (org.netxms.client.NXCSession)16 PartInitException (org.eclipse.ui.PartInitException)13 IOException (java.io.IOException)12 AbstractObject (org.netxms.client.objects.AbstractObject)8 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 List (java.util.List)4 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 Alarm (org.netxms.client.events.Alarm)3 AbstractNode (org.netxms.client.objects.AbstractNode)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 AccessListElement (org.netxms.client.AccessListElement)2 DataCollectionConfiguration (org.netxms.client.datacollection.DataCollectionConfiguration)2 DataCollectionItem (org.netxms.client.datacollection.DataCollectionItem)2 GraphSettings (org.netxms.client.datacollection.GraphSettings)2 AgentPolicy (org.netxms.client.objects.AgentPolicy)2 Node (org.netxms.client.objects.Node)2