.setEntity("contract", this);
List<RateScript> rateScripts = (List<RateScript>) rateScriptQuery
.list();
RateScript lastRateScript = rateScripts.get(rateScripts.size() - 1);
if (HhStartDate.isAfter(startDate, lastRateScript.getFinishDate())) {
throw new UserException("For the contract " + getId() + " called "
+ getName() + ", the start date " + startDate
+ " is after the last rate script.");
}
RateScript coveredRateScript = (RateScript) Hiber
.session()
.createQuery(
"from RateScript script where script.contract = :contract and script.startDate.date <= :startDate and (script.finishDate is null or script.finishDate.date >= :startDate)")
.setEntity("contract", this)
.setTimestamp("startDate", startDate.getDate()).uniqueResult();
HhStartDate finishDate = null;
if (coveredRateScript == null) {
finishDate = rateScripts.get(0).getStartDate().getPrevious();
} else {
if (coveredRateScript.getStartDate().equals(
coveredRateScript.getFinishDate())) {
throw new UserException(
"The start date falls on a rate script which is only half an hour in length, and so cannot be subdivided further.");
}
if (startDate.equals(coveredRateScript.getStartDate())) {
throw new UserException(
"The start date is the same as the start date of an existing rate script.");
}
finishDate = coveredRateScript.getFinishDate();
coveredRateScript.setFinishDate(startDate.getPrevious());
}