timestampParams = new Object[] { instance.getReportStartTime(), instance.getReportEndTime() };
}
// For each point.
for (PointInfo pointInfo : points) {
DataPointVO point = pointInfo.getPoint();
int dataType = point.getPointLocator().getDataTypeId();
DataValue startValue = null;
if (!instance.isFromInception()) {
// Get the value just before the start of the report
PointValueTime pvt = pointValueDao.getPointValueBefore(point.getId(), instance.getReportStartTime());
if (pvt != null)
startValue = pvt.getValue();
// Make sure the data types match
if (DataTypes.getDataType(startValue) != dataType)
startValue = null;
}
// Insert the reportInstancePoints record
String name = Functions.truncate(point.getName(), 100);
int reportPointId = doInsert(
REPORT_INSTANCE_POINTS_INSERT,
new Object[] { instance.getId(), point.getDeviceName(), name, pointInfo.getPoint().getXid(), dataType,
DataTypes.valueToString(startValue),
SerializationHelper.writeObject(point.getTextRenderer()), pointInfo.getColour(),
pointInfo.getWeight(), boolToChar(pointInfo.isConsolidatedChart()), pointInfo.getPlotType() },
new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.BLOB,
Types.VARCHAR, Types.FLOAT, Types.CHAR, Types.INTEGER });
// Insert the reportInstanceData records
String insertSQL = "insert into reportInstanceData " //
+ " select id, " + reportPointId + ", pointValue, ts from pointValues " //
+ " where dataPointId=? and dataType=? " //
+ StringUtils.replaceMacro(timestampSql, "field", "ts");
count += ejt.update(insertSQL, appendParameters(timestampParams, point.getId(), dataType));
// Insert the reportInstanceDataAnnotations records
ejt.update(
"insert into reportInstanceDataAnnotations " //
+ " (pointValueId, reportInstancePointId, textPointValueShort, textPointValueLong, sourceMessage) " //
+ " select rd.pointValueId, rd.reportInstancePointId, pva.textPointValueShort, " //
+ " pva.textPointValueLong, pva.sourceMessage " //
+ " from reportInstanceData rd " //
+ " join reportInstancePoints rp on rd.reportInstancePointId = rp.id " //
+ " join pointValueAnnotations pva on rd.pointValueId = pva.pointValueId " //
+ " where rp.id = ?", new Object[] { reportPointId });
// Insert the reportInstanceEvents records for the point.
if (instance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
String eventSQL = "insert into reportInstanceEvents " //
+ " (eventId, reportInstanceId, typeName, subtypeName, typeRef1, typeRef2, activeTs, " //
+ " rtnApplicable, rtnTs, rtnCause, alarmLevel, message, ackTs, ackUsername, " //
+ " alternateAckSource)" //
+ " select e.id, " + instance.getId() + ", e.typeName, e.subtypeName, e.typeRef1, " //
+ " e.typeRef2, e.activeTs, e.rtnApplicable, e.rtnTs, e.rtnCause, e.alarmLevel, " //
+ " e.message, e.ackTs, u.username, e.alternateAckSource " //
+ " from events e join userEvents ue on ue.eventId=e.id " //
+ " left join users u on e.ackUserId=u.id " //
+ " where ue.userId=? " //
+ " and e.typeName=? " //
+ " and e.typeRef1=? ";
if (instance.getIncludeEvents() == ReportVO.EVENTS_ALARMS)
eventSQL += "and e.alarmLevel > 0 ";
eventSQL += StringUtils.replaceMacro(timestampSql, "field", "e.activeTs");
ejt.update(
eventSQL,
appendParameters(timestampParams, instance.getUserId(), EventType.EventTypeNames.DATA_POINT,
point.getId()));
}
// Insert the reportInstanceUserComments records for the point.
if (instance.isIncludeUserComments()) {
String commentSQL = "insert into reportInstanceUserComments " //
+ " (reportInstanceId, username, commentType, typeKey, ts, commentText)" //
+ " select " + instance.getId() + ", u.username, " + UserComment.TYPE_POINT + ", " //
+ reportPointId + ", uc.ts, uc.commentText " //
+ " from userComments uc " //
+ " left join users u on uc.userId=u.id " //
+ " where uc.commentType=" + UserComment.TYPE_POINT //
+ " and uc.typeKey=? ";
// Only include comments made in the duration of the report.
commentSQL += StringUtils.replaceMacro(timestampSql, "field", "uc.ts");
ejt.update(commentSQL, appendParameters(timestampParams, point.getId()));
}
}
// Insert the reportInstanceUserComments records for the selected events
if (instance.isIncludeUserComments()) {