private void storeMessage(ActivityData data, BAMDataServiceAdmin dsAdmin) {
if (data.getServiceName() != null && data.getOperationName() != null && data.getActivityId() != null) {
if (!data.getOperationName().equals("") && !data.getServiceName().equals("")
&& !data.getActivityId().equals("")) {
MessageDO messageDO;
// TODO timestamp Fix
if (data.getTimeStamp() != null) {
if (data.getTimeStamp().equals("")) {
messageDO = new MessageDO(data.getMessageId(), BAMCalendar.getInstance(Calendar.getInstance())
.getBAMTimestamp(), data.getRemoteIPAddress(), data.getUserAgent());
} else {
messageDO = new MessageDO(data.getMessageId(), data.getTimeStamp(), data.getRemoteIPAddress(),
data.getUserAgent());
}
} else {
messageDO = new MessageDO(data.getMessageId(), BAMCalendar.getInstance(Calendar.getInstance())
.getBAMTimestamp(), data.getRemoteIPAddress(), data.getUserAgent());
}
// For the tenanting serverDO should be get passing server name
// and tenant ID
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);
}
}
messageDO.setServerId(serverDO.getId());
} catch (BAMException e) {
log.error("Could not retrieve server from DB ", e);
}
ServiceDO serviceDO = null;
try {
String key = messageDO.getServerId() + "-" + data.getServiceName();
if (serviceMap.containsKey(key)) {
serviceDO = serviceMap.get(key);
} else {
serviceDO = dsAdmin.getService(messageDO.getServerId(), data.getServiceName());
if (serviceDO == null) {
serviceDO = new ServiceDO();
serviceDO.setServerID(serverDO.getId());
serviceDO.setName(data.getServiceName());
dsAdmin.addService(serviceDO);
serviceDO = dsAdmin.getService(messageDO.getServerId(), data.getServiceName());
}
serviceMap.put(key, serviceDO);
}
messageDO.setServiceId(serviceDO.getId());
} catch (BAMException e) {
log.error("Could not retrieve service from DB ", e);
}
OperationDO operationDO;
try {
String key = messageDO.getServiceId() + "-" + data.getOperationName();
if (operationMap.containsKey(key)) {
operationDO = operationMap.get(key);
} else {
operationDO = dsAdmin.getOperation(messageDO.getServiceId(), data.getOperationName());
if (operationDO == null) {
operationDO = new OperationDO();
operationDO.setServiceID(serviceDO.getId());
operationDO.setName(data.getOperationName());
dsAdmin.addOperation(operationDO);
operationDO = dsAdmin.getOperation(messageDO.getServiceId(), data.getOperationName());
}
operationMap.put(key, operationDO);
}
messageDO.setOperationId(operationDO.getOperationID());
} catch (BAMException e) {
log.error("Could not retrieve operation from DB ", e);
}
try {
ActivityDO activity = null;
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);
}
messageDO.setActivityKeyId(activity.getActivityKeyId());
} catch (BAMException e) {
log.error("Could not retrieve activity from DB ", e);
}
try {
MessageDO msg = dsAdmin.getMessage(data.getMessageId(), messageDO.getOperationId(), messageDO
.getActivityKeyId());
if (msg != null) {
messageDO.setMessageKeyId(msg.getMessageKeyId());
} else {
dsAdmin.addMessageData(messageDO);
if (log.isDebugEnabled()) {
log.debug("Message is saved");
}