List of usage examples for org.hibernate.criterion Restrictions or
public static LogicalExpression or(Criterion lhs, Criterion rhs)
From source file:com.eucalyptus.blockstorage.async.SnapshotTransferCleaner.java
License:Open Source License
private void deleteExpiredUploads() { try (TransactionResource snapTran = Entities.transactionFor(SnapshotUploadInfo.class)) { Criterion criterion = Restrictions.and( Restrictions.or(Restrictions.like("state", SnapshotUploadState.cleaned), Restrictions.like("state", SnapshotUploadState.uploaded)), Restrictions.le("purgeTime", System.currentTimeMillis())); List<SnapshotUploadInfo> snapshotUploadInfoList = Entities.query(new SnapshotUploadInfo(), Boolean.FALSE, criterion, Collections.EMPTY_MAP); for (SnapshotUploadInfo snapUploadInfo : snapshotUploadInfoList) { LOG.debug("Deleting expired entity from DB " + snapUploadInfo); Map<String, String> parameters = Maps.newHashMap(); parameters.put("snapshotId", snapUploadInfo.getSnapshotId()); parameters.put("bucketName", snapUploadInfo.getBucketName()); parameters.put("keyName", snapUploadInfo.getKeyName()); if (snapUploadInfo.getUploadId() != null) { parameters.put("uploadId", snapUploadInfo.getUploadId()); Entities.deleteAllMatching(SnapshotPart.class, "WHERE snapshot_id = :snapshotId AND bucket_name = :bucketName AND key_name = :keyName AND upload_id = :uploadId", parameters);/*from ww w . jav a 2s . c o m*/ } else { Entities.deleteAllMatching(SnapshotPart.class, "WHERE snapshot_id = :snapshotId AND bucket_name = :bucketName AND key_name = :keyName", parameters); } Entities.delete(snapUploadInfo); } snapTran.commit(); } catch (Exception e) { LOG.debug("Error deleting expired snapshot upload info entities" + e); } }
From source file:com.eucalyptus.cloudformation.entity.StackEntityManager.java
License:Open Source License
public static List<StackEntity> describeStacks(String accountId, String stackNameOrId) { List<StackEntity> returnValue = Lists.newArrayList(); try (TransactionResource db = Entities.transactionFor(StackEntity.class)) { Criteria criteria = Entities.createCriteria(StackEntity.class) .add(Restrictions.eq("accountId", accountId)); if (stackNameOrId != null) { // stack name or id can be stack name on non-deleted stacks or stack id on any stack criteria.add(Restrictions.or( Restrictions.and(Restrictions.eq("recordDeleted", Boolean.FALSE), Restrictions.eq("stackName", stackNameOrId)), Restrictions.eq("stackId", stackNameOrId))); } else {//from ww w . ja v a 2s .c o m criteria.add(Restrictions.eq("recordDeleted", Boolean.FALSE)); } returnValue = criteria.list(); db.commit(); } return returnValue; }
From source file:com.eucalyptus.cloudformation.entity.StackEntityManager.java
License:Open Source License
public static StackEntity getNonDeletedStackByNameOrId(String stackNameOrId, String accountId) { StackEntity stackEntity = null;// w w w . j a v a2 s . c o m try (TransactionResource db = Entities.transactionFor(StackEntity.class)) { Criteria criteria = Entities.createCriteria(StackEntity.class) .add(Restrictions.eq("accountId", accountId)) // stack name or id can be stack name on non-deleted stacks or stack id on any stack .add(Restrictions.or(Restrictions.eq("stackName", stackNameOrId), Restrictions.eq("stackId", stackNameOrId))) .add(Restrictions.eq("recordDeleted", Boolean.FALSE)); List<StackEntity> entityList = criteria.list(); if (entityList != null && !entityList.isEmpty()) { stackEntity = entityList.get(0); } db.commit(); } return stackEntity; }
From source file:com.eucalyptus.cloudformation.entity.StackEntityManager.java
License:Open Source License
public static StackEntity getAnyStackByNameOrId(String stackNameOrId, String accountId) { StackEntity stackEntity = null;// www . j av a 2s . c o m try (TransactionResource db = Entities.transactionFor(StackEntity.class)) { Criteria criteria = Entities.createCriteria(StackEntity.class) .add(Restrictions.eq("accountId", accountId)) // stack name or id can be stack name on non-deleted stacks or stack id on any stack .add(Restrictions.or( Restrictions.and(Restrictions.eq("recordDeleted", Boolean.FALSE), Restrictions.eq("stackName", stackNameOrId)), Restrictions.eq("stackId", stackNameOrId))); List<StackEntity> entityList = criteria.list(); if (entityList != null && !entityList.isEmpty()) { stackEntity = entityList.get(0); } db.commit(); } return stackEntity; }
From source file:com.eucalyptus.cloudformation.entity.StackEventEntityManager.java
License:Open Source License
public static ArrayList<StackEvent> getStackEventsByNameOrId(String stackNameOrId, String accountId) { ArrayList<StackEvent> returnValue = Lists.newArrayList(); try (TransactionResource db = Entities.transactionFor(StackEventEntity.class)) { Criteria criteria = Entities.createCriteria(StackEventEntity.class) .add(Restrictions.eq("accountId", accountId)) .add(Restrictions.or(Restrictions.eq("stackId", stackNameOrId), Restrictions.eq("stackName", stackNameOrId))); // don't even care if it is deleted List<StackEventEntity> results = criteria.list(); if (results != null) { for (StackEventEntity stackEventEntity : results) { returnValue.add(stackEventEntityToStackEvent(stackEventEntity)); }/*from ww w . j a v a 2 s . c o m*/ } db.commit(); } return returnValue; }
From source file:com.eucalyptus.cloudformation.entity.StackResourceEntityManager.java
License:Open Source License
public static StackResourceEntity describeStackResource(String accountId, String stackNameOrId, String logicalResourceId) throws CloudFormationException { StackResourceEntity matchingStackResourceEntity = null; String stackId = null;//ww w. j ava 2 s. c om try (TransactionResource db = Entities.transactionFor(StackResourceEntity.class)) { // There is some weirdness in this request. The stack name represents either the stack name of the // non-deleted stack or the stack id of the deleted or non-deleted stack. Criteria criteria = Entities.createCriteria(StackResourceEntity.class) .add(Restrictions.eq("accountId", accountId)) .add(Restrictions.or( Restrictions.and(Restrictions.eq("recordDeleted", Boolean.FALSE), Restrictions.eq("stackName", stackNameOrId)), Restrictions.eq("stackId", stackNameOrId))) .add(Restrictions.ne("resourceStatus", StackResourceEntity.Status.NOT_STARTED)); // placeholder, AWS doesn't return these List<StackResourceEntity> result = criteria.list(); if (result == null || result.isEmpty()) { // TODO: in theory the stack may exist but with no resources. Either way though there is an error, so this is ok. throw new ValidationErrorException("Stack with name " + stackNameOrId + " does not exist"); } for (StackResourceEntity stackResourceEntity : result) { if (stackId == null) { stackId = stackResourceEntity.getStackId(); } else if (!stackId.equals(stackResourceEntity.getStackId())) { throw new InternalFailureException("Got results from more than one stack"); } if (logicalResourceId.equals(stackResourceEntity.getLogicalResourceId())) { if (matchingStackResourceEntity != null) { throw new InternalFailureException("More than one record exists for Resource " + logicalResourceId + " on stack " + stackId); } else { matchingStackResourceEntity = stackResourceEntity; } } } if (matchingStackResourceEntity == null) { throw new ValidationErrorException( "Resource " + logicalResourceId + " does not exist for stack " + stackId); } db.commit(); } return matchingStackResourceEntity; }
From source file:com.eucalyptus.cloudformation.entity.StackResourceEntityManager.java
License:Open Source License
public static List<StackResourceEntity> describeStackResources(String accountId, String stackNameOrId, String physicalResourceId, String logicalResourceId) { List<StackResourceEntity> returnValue = null; String stackId = null;/* www .ja va2 s .c o m*/ try (TransactionResource db = Entities.transactionFor(StackResourceEntity.class)) { // There is some weirdness in this request. The stack name represents either the stack name of the // non-deleted stack or the stack id of the deleted or non-deleted stack. Criteria criteria = Entities.createCriteria(StackResourceEntity.class) .add(Restrictions.eq("accountId", accountId)); if (stackNameOrId != null) { criteria.add(Restrictions.or( Restrictions.and(Restrictions.eq("recordDeleted", Boolean.FALSE), Restrictions.eq("stackName", stackNameOrId)), Restrictions.eq("stackId", stackNameOrId))); } if (logicalResourceId != null) { criteria.add(Restrictions.eq("logicalResourceId", logicalResourceId)); } if (physicalResourceId != null) { criteria.add(Restrictions.eq("physicalResourceId", logicalResourceId)); } criteria.add(Restrictions.ne("resourceStatus", StackResourceEntity.Status.NOT_STARTED)); // placeholder, AWS doesn't return these returnValue = criteria.list(); db.commit(); } return returnValue; }
From source file:com.eucalyptus.compute.vpc.VpcWorkflow.java
License:Open Source License
/** * Delete NAT gateways that have timed out in a terminal state (failed or deleted) */// w ww .j a va 2 s. com private void natGatewayTimeout() { List<String> timedOutNatGateways = Collections.emptyList(); try (final TransactionResource tx = Entities.transactionFor(NatGateway.class)) { timedOutNatGateways = natGateways.list(null, Restrictions.and( Restrictions.or(Example.create(NatGateway.exampleWithState(NatGateway.State.failed)), Example.create(NatGateway.exampleWithState(NatGateway.State.deleted))), Restrictions.lt("lastUpdateTimestamp", new Date(System.currentTimeMillis() - NatGateways.EXPIRY_AGE))), Collections.<String, String>emptyMap(), Predicates.<NatGateway>alwaysTrue(), CloudMetadatas.<NatGateway>toDisplayName()); } catch (final Exception e) { logger.error("Error listing timed out NAT gateways", e); } for (final String natGatewayId : timedOutNatGateways) { try (final TransactionResource tx = Entities.transactionFor(NatGateway.class)) { final NatGateway natGateway = natGateways.lookupByName(null, natGatewayId, Functions.<NatGateway>identity()); logger.info("Deleting NAT gateway " + natGateway.getDisplayName() + " with state " + natGateway.getState()); natGateways.delete(natGateway); tx.commit(); } catch (final VpcMetadataNotFoundException e) { logger.info("NAT gateway " + natGatewayId + " not found for deletion"); } catch (final Exception e) { logger.error("Error deleting timed out NAT gateway " + natGatewayId, e); } } }
From source file:com.eucalyptus.objectstorage.DbObjectManagerImpl.java
License:Open Source License
/** * Returns the ObjectEntities that have failed or are marked for deletion */// w w w .ja va2 s . com @Override public List<ObjectEntity> getFailedOrDeleted() throws Exception { try { // Return the latest version based on the created date. EntityTransaction db = Entities.get(ObjectEntity.class); try { ObjectEntity searchExample = new ObjectEntity(); Criteria search = Entities.createCriteria(ObjectEntity.class); List results = search.add(Example.create(searchExample)) .add(Restrictions.or(ObjectEntity.QueryHelpers.getDeletedRestriction(), ObjectEntity.QueryHelpers.getFailedRestriction())) .list(); db.commit(); return (List<ObjectEntity>) results; } finally { if (db != null && db.isActive()) { db.rollback(); } } } catch (NoSuchElementException e) { // Swallow this exception } catch (Exception e) { LOG.error("Error fetching failed or deleted object records"); throw e; } return new ArrayList<ObjectEntity>(0); }
From source file:com.eucalyptus.objectstorage.metadata.DbMpuPartMetadataManagerImpl.java
License:Open Source License
@Override public long getTotalSize(Bucket bucket) throws Exception { try (TransactionResource trans = Entities.transactionFor(PartEntity.class)) { Criteria queryCriteria = Entities.createCriteria(PartEntity.class) .add(Restrictions.or(Restrictions.eq("state", ObjectState.creating), Restrictions.eq("state", ObjectState.extant))) .setProjection(Projections.sum("size")); if (bucket != null) { queryCriteria = getSearchByBucket(queryCriteria, bucket); }// w w w . j av a 2s . c o m queryCriteria.setReadOnly(true); final Number count = (Number) queryCriteria.uniqueResult(); return count == null ? 0 : count.longValue(); } catch (Throwable e) { LOG.error("Error getting part total size for bucket " + bucket.getBucketName(), e); throw new Exception(e); } }