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();
}
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;
}
Aggregations