use of com.cloud.utils.db.SearchBuilder in project cloudstack by apache.
the class AutoScaleManagerImpl method checkValidityAndPersist.
@DB
protected AutoScalePolicyVO checkValidityAndPersist(final AutoScalePolicyVO autoScalePolicyVOFinal, final List<Long> conditionIds) {
final int duration = autoScalePolicyVOFinal.getDuration();
final int quietTime = autoScalePolicyVOFinal.getQuietTime();
if (duration < 0) {
throw new InvalidParameterValueException("duration is an invalid value: " + duration);
}
if (quietTime < 0) {
throw new InvalidParameterValueException("quiettime is an invalid value: " + quietTime);
}
return Transaction.execute(new TransactionCallback<AutoScalePolicyVO>() {
@Override
public AutoScalePolicyVO doInTransaction(TransactionStatus status) {
AutoScalePolicyVO autoScalePolicyVO = _autoScalePolicyDao.persist(autoScalePolicyVOFinal);
if (conditionIds != null) {
SearchBuilder<ConditionVO> conditionsSearch = _conditionDao.createSearchBuilder();
conditionsSearch.and("ids", conditionsSearch.entity().getId(), Op.IN);
conditionsSearch.done();
SearchCriteria<ConditionVO> sc = conditionsSearch.create();
sc.setParameters("ids", conditionIds.toArray(new Object[0]));
List<ConditionVO> conditions = _conditionDao.search(sc, null);
ControlledEntity[] sameOwnerEntities = conditions.toArray(new ControlledEntity[conditions.size() + 1]);
sameOwnerEntities[sameOwnerEntities.length - 1] = autoScalePolicyVO;
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true, sameOwnerEntities);
if (conditionIds.size() != conditions.size()) {
// TODO report the condition id which could not be found
throw new InvalidParameterValueException("Unable to find the condition specified");
}
ArrayList<Long> counterIds = new ArrayList<Long>();
for (ConditionVO condition : conditions) {
if (counterIds.contains(condition.getCounterid())) {
throw new InvalidParameterValueException("atleast two conditions in the conditionids have the same counter. It is not right to apply two different conditions for the same counter");
}
counterIds.add(condition.getCounterid());
}
/* For update case remove the existing mappings and create fresh ones */
_autoScalePolicyConditionMapDao.removeByAutoScalePolicyId(autoScalePolicyVO.getId());
for (Long conditionId : conditionIds) {
AutoScalePolicyConditionMapVO policyConditionMapVO = new AutoScalePolicyConditionMapVO(autoScalePolicyVO.getId(), conditionId);
_autoScalePolicyConditionMapDao.persist(policyConditionMapVO);
}
}
return autoScalePolicyVO;
}
});
}
Aggregations