}
if(udpValues != null){
// an udp value is never erased for a kpi once memorized, that is because by user interface integer have no null value and boolean too
// these are current UdpValues; for each:
for (Iterator iterator = udpValues.iterator(); iterator.hasNext();) {
UdpValue udpValue = (UdpValue) iterator.next();
// the tow ids of relationship; Kpi / Model and Udp
Integer udpId = udpValue.getUdpId();
// search if KpiValue is already present, in that case update otherwise insert
SbiUdpValue sbiUdpValue = null;
SbiUdpValue sbiUdpValueToClose = null;
UdpValue already = DAOFactory.getUdpDAOValue().loadByReferenceIdAndUdpId(idObject, udpValue.getUdpId(), family.toUpperCase());
boolean inserting = true;
boolean openNewOne = true;
if(already == null){
sbiUdpValue = new SbiUdpValue();
}
else{
inserting = false;
// check if value has changed, if not so don't open a new one
SbiUdpValue sbiUdpValueRetrieved = (SbiUdpValue) aSession.load(SbiUdpValue.class,already.getUdpValueId());
if(udpValue.getValue() != null && udpValue.getValue().equals(already.getValue())){
// same value as before, simple update
openNewOne = false;
sbiUdpValue = sbiUdpValueRetrieved;
}
else{
// new value, close preceding open a new one
sbiUdpValueToClose = sbiUdpValueRetrieved;
sbiUdpValue = new SbiUdpValue();
}
}
// fill SbiUdpValue values
sbiUdpValue.setLabel(udpValue.getLabel());
sbiUdpValue.setName(udpValue.getName());
sbiUdpValue.setProg(udpValue.getProg());
sbiUdpValue.setFamily(udpValue.getFamily());
sbiUdpValue.setReferenceId(idObject);
SbiUdp hibUdp = (SbiUdp) aSession.load(SbiUdp.class,
udpId);
sbiUdpValue.setSbiUdp(hibUdp);
sbiUdpValue.setValue(udpValue.getValue());
if(inserting){
logger.debug("Inserting Udp association between udp "+udpValue.getLabel() + " referencing family " + udpValue.getFamily() +
" with id "+ udpValue.getReferenceId() + "with value "+sbiUdpValue.getValue());
sbiUdpValue.setBeginTs(new Date());
DAOFactory.getUdpDAOValue().insert(aSession, sbiUdpValue);
logger.debug("value to Udp "+hibUdp.getLabel()+ " has been inserted");
}
else{
// the update must close the previous record and open a new one, but only if value has changed
if(openNewOne){
logger.debug("Close previous udp value and open Udp association between udp "+udpValue.getLabel() + " referencing family " + udpValue.getFamily() +
" with id "+ udpValue.getReferenceId() + "with value "+sbiUdpValue.getValue());
// close previous one
sbiUdpValueToClose.setBeginTs(already.getBeginTs());
sbiUdpValueToClose.setEndTs(new Date());
DAOFactory.getUdpDAOValue().update(aSession, sbiUdpValueToClose);
// insert new one
sbiUdpValue.setBeginTs(new Date());
DAOFactory.getUdpDAOValue().insert(aSession, sbiUdpValue);
}
else{
logger.debug("Update without closing Udp association between udp "+udpValue.getLabel() + " referencing family " + udpValue.getFamily() +
" with id "+ udpValue.getReferenceId() + "with value "+sbiUdpValue.getValue());
// just update fields no new opening
sbiUdpValue.setBeginTs(already.getBeginTs());
DAOFactory.getUdpDAOValue().update(aSession, sbiUdpValue);
}
logger.debug("value to Udp "+hibUdp.getLabel()+ " has been updated; associated to a "+family);
}