}else{
if(behaviour.equalsIgnoreCase("timeIntervalDefault")){
if(dateIntervalFrom!=null && dateIntervalTo!=null){
value = DAOFactory.getKpiDAO().getKpiValueFromInterval(kpiI.getKpiInstanceId(), dateIntervalFrom, dateIntervalTo, r, grantNodeToUse);
if (value==null){
IDataSet dataSet = DAOFactory.getKpiDAO().getDsFromKpiId(kpiI.getKpi());
logger.info("Retrieved the Dataset to be calculated: " + (dataSet!=null ? dataSet.getId():"null"));
value = getNewKpiValue(dataSet, kpiI, r, miId, grantNodeToUse);
}
}else{
value = null;
}
}else if(behaviour.equalsIgnoreCase("timeIntervalForceRecalculation")){
if(dateIntervalFrom!=null && dateIntervalTo!=null){
DAOFactory.getKpiDAO().deleteKpiValueFromInterval(kpiI.getKpiInstanceId(), dateIntervalFrom, dateIntervalTo, r, grantNodeToUse);
IDataSet dataSet = DAOFactory.getKpiDAO().getDsFromKpiId(kpiI.getKpi());
logger.info("Retrieved the Dataset to be calculated: " + (dataSet!=null ? dataSet.getId():"null"));
value = getNewKpiValue(dataSet, kpiI, r, miId, grantNodeToUse);
}else{
value = null;
}
}else{
if(behaviour.equalsIgnoreCase("default") || //If the behaviour is default
(behaviour.equalsIgnoreCase("recalculate") && kpiI.getPeriodicityId()!=null)){//or the behaviour is recalculate and the kpiinstance has a setted periodicity
// the old value still valid is retrieved
logger.debug("Trying to retrieve the old value still valid");
value = DAOFactory.getKpiDAO().getKpiValue(kpiI.getKpiInstanceId(), dateOfKPI, r, grantNodeToUse);
logger.debug("Old KpiValue retrieved");
}
if(value!=null && value.getEndDate()!=null && kpiI.getPeriodicityId()!=null){
GregorianCalendar c2 = new GregorianCalendar();
c2.setTime(value.getEndDate());
int year = c2.get(1);
if(year==9999){
no_period_to_period = true;
logger.debug("The periodicity was null and now exists");
}
}
if (value==null || //If the retrieved value is null
no_period_to_period || //or the periodicity was before null and now is setted
behaviour.equalsIgnoreCase("force_recalculation") || //or the behaviour is force_recalculation
(behaviour.equalsIgnoreCase("recalculate") && kpiI.getPeriodicityId()==null) ) {// or the behaviour is recalculate and the kpiinstance hasn't a setted periodicity
//a new value is calculated
logger.debug("Old value not valid anymore or recalculation forced");
IDataSet dataSet = DAOFactory.getKpiDAO().getDsFromKpiId(kpiI.getKpi());
logger.info("Retrieved the Dataset to be calculated: " + (dataSet!=null ? dataSet.getId():"null"));
value = getNewKpiValue(dataSet, kpiI, r, miId, grantNodeToUse);
}else if(behaviour.equalsIgnoreCase("display")){//diplay behaviour
logger.debug("Display behaviour");
value = DAOFactory.getKpiDAO().getDisplayKpiValue(kpiI.getKpiInstanceId(), dateOfKPI, r, grantNodeToUse);