sbiKpiModelInst.setLabel(kpiModelInstanceLb);
sbiKpiModelInst.setStartDate(kpiModelInstanceStartDate);
sbiKpiModelInst.setEndDate(kpiModelInDateEndDate);
sbiKpiModelInst.setModelUUID(modelUUID);
SbiKpiInstance oldSbiKpiInstance = sbiKpiModelInst
.getSbiKpiInstance();
//default behaviour
boolean newKpiInstanceHistory = true;
boolean deleteOldHistory = false;
boolean dontSaveKpiHistory = false;
if(value.getKpiInstance() != null && !value.getKpiInstance().isSaveKpiHistory()){
dontSaveKpiHistory = true;
}
// new kpiInstance is null
if (value.getKpiInstance() == null) {
newKpiInstanceHistory = false;
deleteOldHistory = true;
}
if (value.getKpiInstance() != null
&& (!value.getKpiInstance().isSaveKpiHistory())) {
newKpiInstanceHistory = false;
dontSaveKpiHistory = true;
}
// old kpiInstance is null and new kpiInstance has a value
if (oldSbiKpiInstance == null && value.getKpiInstance() != null) {
newKpiInstanceHistory = false;
}
// old kpiId is different from new kpiId
if (newKpiInstanceHistory
&& !(areBothNull(oldSbiKpiInstance.getSbiKpi(), value
.getKpiInstance().getKpi()) || (oldSbiKpiInstance
.getSbiKpi() != null && areNullOrEquals(
oldSbiKpiInstance.getSbiKpi().getKpiId(), value
.getKpiInstance().getKpi())))) {
newKpiInstanceHistory = false;
deleteOldHistory = true;
// create new sbiKpiInstance
}
// check if same value is changed
if (newKpiInstanceHistory
&& !((areBothNull(oldSbiKpiInstance.getSbiThreshold(),
value.getKpiInstance().getThresholdId()) || (oldSbiKpiInstance
.getSbiThreshold() != null && areNullOrEquals(
oldSbiKpiInstance.getSbiThreshold()
.getThresholdId(), value.getKpiInstance()
.getThresholdId())))
&& (areBothNull(oldSbiKpiInstance.getChartType(),
value.getKpiInstance().getChartTypeId()) || (oldSbiKpiInstance
.getChartType() != null && areNullOrEquals(
oldSbiKpiInstance.getChartType()
.getValueId(), value
.getKpiInstance().getChartTypeId())))
/*
* TODO && (areBothNull(oldSbiKpiInstance
* .getSbiKpiPeriodicity(), value
* .getKpiInstance().getPeriodicityId()) ||
* (oldSbiKpiInstance .getSbiKpiPeriodicity() != null &&
* areNullOrEquals( oldSbiKpiInstance.getSbiKpiPeriodicity()
* .getIdKpiPeriodicity(), value .getKpiInstance()
* .getPeriodicityId())))
*/
&& areNullOrEquals(oldSbiKpiInstance.getWeight(), value
.getKpiInstance().getWeight()))) {
// create new History
Calendar now = Calendar.getInstance();
SbiKpiInstanceHistory sbiKpiInstanceHistory = new SbiKpiInstanceHistory();
sbiKpiInstanceHistory.setSbiKpiInstance(oldSbiKpiInstance);
sbiKpiInstanceHistory.setSbiThreshold(oldSbiKpiInstance
.getSbiThreshold());
sbiKpiInstanceHistory.setSbiDomains(oldSbiKpiInstance
.getChartType());
sbiKpiInstanceHistory.setWeight(oldSbiKpiInstance.getWeight());
sbiKpiInstanceHistory
.setBeginDt(oldSbiKpiInstance.getBeginDt());
sbiKpiInstanceHistory.setEndDt(now.getTime());
updateSbiCommonInfo4Insert(sbiKpiInstanceHistory);
aSession.save(sbiKpiInstanceHistory);
}
SbiKpiInstance kpiInstanceToCreate = null;
if (value.getKpiInstance() != null) {
if (newKpiInstanceHistory || dontSaveKpiHistory) {
kpiInstanceToCreate = setSbiKpiInstanceFromModelInstance(
aSession, value, oldSbiKpiInstance);
} else {
// create new kpiInstance
kpiInstanceToCreate = new SbiKpiInstance();
Calendar now = Calendar.getInstance();
kpiInstanceToCreate.setBeginDt(now.getTime());
kpiInstanceToCreate = setSbiKpiInstanceFromModelInstance(
aSession, value, kpiInstanceToCreate);
}
updateSbiCommonInfo4Update(kpiInstanceToCreate);
aSession.saveOrUpdate(kpiInstanceToCreate);