Search in sources :

Example 1 with ObjectContextBase

use of org.netxms.client.objecttools.ObjectContextBase in project netxms by netxms.

the class ExpansionTest method testNodeAndAlarmExpanssion.

public void testNodeAndAlarmExpanssion() throws Exception {
    final NXCSession session = connect();
    session.syncObjects();
    final AbstractNode object = (AbstractNode) session.findObjectById(TestConstants.NODE_ID);
    final Map<Long, Alarm> alarms = session.getAlarms();
    final Map<String, String> inputValues = new HashMap<String, String>();
    final Alarm alarm = alarms.values().iterator().next();
    inputValues.put("Key1", "Value1");
    inputValues.put("Key2", "Value2");
    final List<String> stringsToExpand = new ArrayList<String>();
    stringsToExpand.add("%%%a%A");
    stringsToExpand.add("%g%I");
    stringsToExpand.add("%K%n%U");
    stringsToExpand.add("%(Key1)%(Key2)");
    final List<String> expandedStrings = session.substitureMacross(new ObjectContextBase(object, alarm), stringsToExpand, inputValues);
    assertEquals("%" + object.getPrimaryIP().getHostAddress().toString() + alarm.getMessage(), expandedStrings.get(0));
    assertEquals(object.getGuid().toString() + object.getObjectId(), expandedStrings.get(1));
    assertEquals(alarm.getKey() + object.getObjectName() + session.getUserName(), expandedStrings.get(2));
    session.disconnect();
}
Also used : AbstractNode(org.netxms.client.objects.AbstractNode) HashMap(java.util.HashMap) ObjectContextBase(org.netxms.client.objecttools.ObjectContextBase) Alarm(org.netxms.client.events.Alarm) ArrayList(java.util.ArrayList)

Example 2 with ObjectContextBase

use of org.netxms.client.objecttools.ObjectContextBase in project netxms by netxms.

the class NXCSession method substitureMacross.

/**
 * Substitute macross in many constexts for one string
 *
 * @param context expansion contexts alarm and node
 * @param textToExpand text to be expanded
 * @param inputValues input values provided by used used for %() expansion
 * @return same count and order of strings already expanded
 * @throws IOException if socket I/O error occurs
 * @throws NXCException if NetXMS server returns an error or operation was timed out
 */
public List<String> substitureMacross(ObjectContextBase[] context, String textToExpand, Map<String, String> inputValues) throws IOException, NXCException {
    final NXCPMessage msg = newMessage(NXCPCodes.CMD_EXPAND_MACROS);
    long varId;
    if (inputValues != null) {
        msg.setFieldInt32(NXCPCodes.VID_IN_FIELD_COUNT, inputValues.size());
        varId = NXCPCodes.VID_IN_FIELD_BASE;
        for (Entry<String, String> e : inputValues.entrySet()) {
            msg.setField(varId++, e.getKey());
            msg.setField(varId++, e.getValue());
        }
    }
    msg.setFieldInt32(NXCPCodes.VID_STRING_COUNT, context.length);
    varId = NXCPCodes.VID_EXP_STRING_BASE;
    for (ObjectContextBase c : context) {
        c.fillMessage(msg, varId, textToExpand);
        varId += 5;
    }
    List<String> result = new ArrayList<String>();
    sendMessage(msg);
    NXCPMessage response = waitForRCC(msg.getMessageId());
    varId = NXCPCodes.VID_EXP_STRING_BASE;
    for (int i = 0; i < context.length; i++) {
        result.add(response.getFieldAsString(varId++));
    }
    return result;
}
Also used : ObjectContextBase(org.netxms.client.objecttools.ObjectContextBase) NXCPMessage(org.netxms.base.NXCPMessage) ArrayList(java.util.ArrayList) ConnectionPoint(org.netxms.client.topology.ConnectionPoint) AccessPoint(org.netxms.client.objects.AccessPoint)

Aggregations

ArrayList (java.util.ArrayList)2 ObjectContextBase (org.netxms.client.objecttools.ObjectContextBase)2 HashMap (java.util.HashMap)1 NXCPMessage (org.netxms.base.NXCPMessage)1 Alarm (org.netxms.client.events.Alarm)1 AbstractNode (org.netxms.client.objects.AbstractNode)1 AccessPoint (org.netxms.client.objects.AccessPoint)1 ConnectionPoint (org.netxms.client.topology.ConnectionPoint)1