if (data.getServiceName() != null && data.getOperationName() != null && data.getActivityId() != null) {
if (!data.getOperationName().equals("") && !data.getServiceName().equals("")
&& !data.getActivityId().equals("")) {
if (data.getProperties() != null && data.getProperties().size() > 0) {
MessagePropertyDO messagePropertyDO;
String dbtime = null; //need to check the timestamp of the different events
messagePropertyDO = new MessagePropertyDO();
ServerDO serverDO = null;
try {
if (serverMap.containsKey(data.getServerName())) {
serverDO = serverMap.get(data.getServerName());
} else {
serverDO = dsAdmin.getServer(data.getServerName());
if (serverDO == null) {
log.error("Server is not in the Database " + data.getServerName());
return;
} else {
serverMap.put(data.getServerName(), serverDO);
}
}
messagePropertyDO.setServerId(serverDO.getId());
} catch (BAMException e) {
log.error("Could not retrieve server from DB ", e);
}
ServiceDO serviceDO = null;
try {
String key = messagePropertyDO.getServerId() + "-" + data.getServiceName();
if (serviceMap.containsKey(key)) {
serviceDO = serviceMap.get(key);
} else {
serviceDO = dsAdmin.getService(messagePropertyDO.getServerId(), data.getServiceName());
if (serviceDO == null) {
serviceDO = new ServiceDO();
serviceDO.setServerID(serverDO.getId());
serviceDO.setName(data.getServiceName());
dsAdmin.addService(serviceDO);
serviceDO = dsAdmin.getService(messagePropertyDO.getServerId(), data.getServiceName());
}
serviceMap.put(key, serviceDO);
}
messagePropertyDO.setServiceId(serviceDO.getId());
} catch (BAMException e) {
log.error("Could not retrieve service from DB ", e);
}
OperationDO operationDO = null;
try {
String key = messagePropertyDO.getServiceId() + "-" + data.getOperationName();
if (operationMap.containsKey(key)) {
operationDO = operationMap.get(key);
} else {
operationDO = dsAdmin.getOperation(messagePropertyDO.getServiceId(), data.getOperationName());
if (operationDO == null) {
operationDO = new OperationDO();
operationDO.setServiceID(serviceDO.getId());
operationDO.setName(data.getOperationName());
dsAdmin.addOperation(operationDO);
operationDO = dsAdmin.getOperation(messagePropertyDO.getServiceId(), data.getOperationName());
}
operationMap.put(key, operationDO);
}
messagePropertyDO.setOperationId(operationDO.getOperationID());
} catch (BAMException e) {
log.error("Could not retrieve operation from DB ", e);
}
ActivityDO activity = null;
try {
if (activityMap.containsKey(data.getActivityId())) {
activity = activityMap.get(data.getActivityId());
} else {
activity = dsAdmin.getActivityForActivityID(data.getActivityId());
if (activity == null) {
activity = new ActivityDO();
activity.setActivityId(data.getActivityId());
activity.setName(data.getActivityName());
activity.setDescription(data.getActivityDescription());
dsAdmin.addActivity(activity);
activity = dsAdmin.getActivityForActivityID(data.getActivityId());
}
activityMap.put(data.getActivityId(), activity);
}
messagePropertyDO.setActivityKeyId(activity.getActivityKeyId());
} catch (BAMException e) {
log.error("Could not retrieve activity from DB ", e);
}
try {
MessageDO msg = dsAdmin.getMessage(data.getMessageId(), messagePropertyDO
.getOperationId(), messagePropertyDO.getActivityKeyId());
if (msg != null) {
return; // If message is already present avoid adding duplicate messages
} else {
MessageDO mesg = new MessageDO();
if (activity != null) {
mesg.setMessageId(data.getMessageId());
mesg.setActivityKeyId(messagePropertyDO.getActivityKeyId());
mesg.setOperationId(messagePropertyDO.getOperationId());
mesg.setTimestamp(data.getTimeStamp());
mesg.setIPAddress(data.getRemoteIPAddress());
mesg.setUserAgent(data.getUserAgent());
//TODO: Data Services supports returning the ID
dsAdmin.addMessage(mesg);
messagePropertyDO.setMessageKeyId(dsAdmin.getMessage(
data.getMessageId(),
messagePropertyDO
.getOperationId(),
messagePropertyDO
.getActivityKeyId())
.getMessageKeyId());
dbtime = data.getTimeStamp();
}
}
} catch (BAMException e) {
log.error("Could not retrieve message from DB", e);
}
try {
// MessagePropertyDO property = dsAdmin.getPropertyofMessage(
// messagePropertyDO.getMessageKeyId(),
// messagePropertyDO.getActivityKeyId(), messagePropertyDO.getKey());
// if (property == null) {
// process properties and get arraylist out of it
Map<String, String> propertyList = new HashMap<String, String>();
for (String actProperty : data.getProperties().keySet()) {
propertyList.put(actProperty, data.getProperties().get(actProperty));
}
ArrayList<String> keyList = new ArrayList<String>();
ArrayList<String> valueList = new ArrayList<String>();
Set<String> keys = propertyList.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = propertyList.get(key);
keyList.add(key);
valueList.add(value);
}
String valueArray[] = new String[keyList.size()];
String keyArray[] = new String[keyList.size()];
keyList.toArray(keyArray);
valueList.toArray(valueArray);
messagePropertyDO.setKeyArray(keyArray);
messagePropertyDO.setValueArray(valueArray);
dsAdmin.addMessageProperty(messagePropertyDO);
if (log.isDebugEnabled()) {
log.debug("Message Properties are saved");
}