Search in sources :

Example 1 with SearchBuilder

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;
        }
    });
}
Also used : SearchBuilder(com.cloud.utils.db.SearchBuilder) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) SearchCriteria(com.cloud.utils.db.SearchCriteria) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ControlledEntity(org.apache.cloudstack.acl.ControlledEntity) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.cloud.utils.db.DB)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 DB (com.cloud.utils.db.DB)1 SearchBuilder (com.cloud.utils.db.SearchBuilder)1 SearchCriteria (com.cloud.utils.db.SearchCriteria)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ControlledEntity (org.apache.cloudstack.acl.ControlledEntity)1