List of usage examples for org.hibernate.criterion Restrictions isNotNull
public static Criterion isNotNull(String propertyName)
From source file:com.eucalyptus.blockstorage.StorageUtil.java
License:Open Source License
private static long getBlockStorageTotalSize(final String partition, final String partitionProperty, final String sizeProperty, final Class<? extends UserMetadata<State>> sizedType) { long size = -1; final EntityTransaction db = Entities.get(sizedType); try {// ww w. ja v a2s. co m size = Objects.firstNonNull((Number) Entities.createCriteria(sizedType) .add(Restrictions.in("state", EnumSet.of(State.EXTANT, State.BUSY))) .add(partition == null ? Restrictions.isNotNull(partitionProperty) : // Get size for all partitions. Restrictions.eq(partitionProperty, partition)) .setProjection(Projections.sum(sizeProperty)).setReadOnly(true).uniqueResult(), 0).longValue(); db.commit(); } catch (Exception e) { LOG.error(e); db.rollback(); } return size; }
From source file:com.eucalyptus.compute.common.internal.vm.VmInstance.java
License:Open Source License
public static Criterion nonNullNodeCriterion() { return Restrictions.isNotNull("runtimeState.serviceTag"); }
From source file:com.eucalyptus.compute.vpc.VpcWorkflow.java
License:Open Source License
/** * Release resources for failed NAT gateways */// ww w. j a va 2 s . c o m private void natGatewayFailureCleanup() { List<String> failedNatGatewayIds = Collections.emptyList(); try (final TransactionResource tx = Entities.transactionFor(NatGateway.class)) { failedNatGatewayIds = natGateways.list(null, Restrictions.and(Example.create(NatGateway.exampleWithState(NatGateway.State.failed)), Restrictions.isNotNull("networkInterfaceId")), Collections.<String, String>emptyMap(), Predicates.<NatGateway>alwaysTrue(), CloudMetadatas.<NatGateway>toDisplayName()); } catch (final Exception e) { logger.error("Error listing failed NAT gateways for cleanup", e); } for (final String natGatewayId : failedNatGatewayIds) { try (final TransactionResource tx = Entities.transactionFor(NatGateway.class)) { final NatGateway natGateway = natGateways.lookupByName(null, natGatewayId, Functions.<NatGateway>identity()); releaseNatGatewayResources(natGateway); tx.commit(); } catch (Exception e) { if (PersistenceExceptions.isStaleUpdate(e)) { logger.debug("Conflict updating NAT gateway " + natGatewayId + " for cleanup (will retry)"); } else { logger.error("Error cleaning up failed NAT gateway " + natGatewayId, e); } } } }
From source file:com.eucalyptus.walrus.WalrusFSManager.java
License:Open Source License
private String getMultipartData(ObjectInfo objectInfo, GetObjectType request, GetObjectResponseType response) throws WalrusException { //get all parts PartInfo searchPart = new PartInfo(request.getBucket(), request.getKey()); searchPart.setCleanup(false);/*ww w . j a v a 2s. com*/ searchPart.setUploadId(objectInfo.getUploadId()); List<PartInfo> parts; EntityTransaction db = Entities.get(PartInfo.class); try { Criteria partCriteria = Entities.createCriteria(PartInfo.class); partCriteria.setReadOnly(true); partCriteria.add(Example.create(searchPart)); partCriteria.add(Restrictions.isNotNull("partNumber")); partCriteria.addOrder(Order.asc("partNumber")); parts = partCriteria.list(); if (parts.size() == 0) { throw new InternalErrorException( "No parts found corresponding to uploadId: " + objectInfo.getUploadId()); } } finally { db.rollback(); } if (request.getInlineData()) { if ((objectInfo.getSize() * 4) > WalrusProperties.MAX_INLINE_DATA_SIZE) { throw new InlineDataTooLargeException(request.getBucket() + "/" + request.getKey()); } String base64Data = ""; for (PartInfo part : parts) { byte[] bytes = new byte[102400]; int bytesRead = 0, offset = 0; try { FileIO fileIO = storageManager.prepareForRead(part.getBucketName(), part.getObjectName()); while ((bytesRead = fileIO.read(offset)) > 0) { ByteBuffer buffer = fileIO.getBuffer(); if (buffer != null) { buffer.get(bytes, 0, bytesRead); base64Data += new String(bytes, 0, bytesRead); offset += bytesRead; } } fileIO.finish(); } catch (Exception e) { LOG.error(e, e); throw new InternalErrorException(e); } } return Hashes.base64encode(base64Data); } else { response.setHasStreamingData(true); // support for large objects storageManager.getMultipartObject(response, parts, request.getIsCompressed()); return null; } }
From source file:com.eucalyptus.walrus.WalrusFSManager.java
License:Open Source License
public CompleteMultipartUploadResponseType completeMultipartUpload(CompleteMultipartUploadType request) throws WalrusException { CompleteMultipartUploadResponseType reply = (CompleteMultipartUploadResponseType) request.getReply(); Context ctx = Contexts.lookup(); Account account = ctx.getAccount();//from ww w.j ava 2 s .c om String bucketName = request.getBucket(); String objectKey = request.getKey(); List<Part> requestParts = request.getParts(); EntityWrapper<BucketInfo> db = EntityWrapper.get(BucketInfo.class); BucketInfo bucketInfo = new BucketInfo(bucketName); List<BucketInfo> bucketList = db.queryEscape(bucketInfo); if (bucketList.size() > 0) { BucketInfo bucket = bucketList.get(0); try { // Find the manifest entity PartInfo searchManifest = new PartInfo(bucketName, objectKey); searchManifest.setUploadId(request.getUploadId()); searchManifest.setCleanup(Boolean.FALSE); EntityWrapper<PartInfo> dbPart = db.recast(PartInfo.class); Criteria partCriteria = dbPart.createCriteria(PartInfo.class); partCriteria.add(Example.create(searchManifest)); partCriteria.add(Restrictions.isNull("partNumber")); List<PartInfo> found = partCriteria.list(); if (found.size() == 0) { db.rollback(); throw new NoSuchUploadException(request.getUploadId()); } if (found.size() > 1) { db.rollback(); throw new InternalErrorException( "Multiple manifests found for same uploadId: " + request.getUploadId()); } PartInfo foundManifest = found.get(0); if (foundManifest != null) { long oldBucketSize = 0L; long oldObjectSize = 0L; //check if we can write to the object if it already exists String versionId = null; if (bucket.isVersioningEnabled()) { versionId = foundManifest.getVersionId(); } else { versionId = WalrusProperties.NULL_VERSION_ID; ObjectInfo searchObject = new ObjectInfo(bucketName, objectKey); searchObject.setVersionId(versionId); EntityWrapper<ObjectInfo> dbObject = db.recast(ObjectInfo.class); try { ObjectInfo foundObject = dbObject.getUniqueEscape(searchObject); if (!foundObject.canWrite(account.getAccountNumber())) { // Found existing object, but don't have write // access db.rollback(); throw new AccessDeniedException("Key", objectKey); } //check if we can write ACP if (foundManifest.getGrants().size() > 0 && (!foundObject.canWriteACP(account.getAccountNumber()))) { db.rollback(); throw new AccessDeniedException("Key", objectKey); } oldObjectSize = foundObject.getSize() == null ? 0L : foundObject.getSize(); // Fix for EUCA-2275: // If an existing object is overwritten, the size // difference must be taken into account. Size of the // already existing object was ignored before oldBucketSize = -oldObjectSize; } catch (AccessDeniedException ex) { throw ex; } catch (EucalyptusCloudException ex) { // No existing object found //empty catch? We need to throw or catch a more specific exception here. EucalyptusCloudException is not //specific enough and could be raised in a variety of cases. } } // Look for the parts PartInfo searchPart = new PartInfo(bucketName, objectKey); searchPart.setUploadId(request.getUploadId()); partCriteria = dbPart.createCriteria(PartInfo.class); partCriteria.add(Example.create(searchManifest)); partCriteria.add(Restrictions.isNotNull("partNumber")); List<PartInfo> foundParts = partCriteria.list(); String eTagString = ""; long size = 0; if (foundParts != null && foundParts.size() > 0) { if (requestParts != null && requestParts.size() > foundParts.size()) { throw new InternalErrorException( "One or more parts has not been uploaded yet. Either upload the part or fix the manifest. Upload Id: " + request.getUploadId()); } else { // Create a hashmap Map<Integer, PartInfo> partsMap = new HashMap<Integer, PartInfo>(foundParts.size()); for (PartInfo foundPart : foundParts) { partsMap.put(foundPart.getPartNumber(), foundPart); } PartInfo lookupPart = null; for (Part requestPart : requestParts) { if ((lookupPart = partsMap.get(requestPart.getPartNumber())) != null) { lookupPart.setCleanup(Boolean.FALSE); eTagString += lookupPart.getEtag(); size += lookupPart.getSize(); partsMap.remove(lookupPart.getPartNumber()); } else { throw new InvalidPartException("Part Number: " + requestPart.getPartNumber() + " upload id: " + request.getUploadId()); } } MessageDigest digest = Digest.MD5.get(); digest.update(eTagString.getBytes()); final String eTag = "uuid-" + Hashes.bytesToHex(digest.digest()); foundManifest.setEtag(eTag); foundManifest.setCleanup(Boolean.FALSE); if (!ctx.hasAdministrativePrivileges() && !Permissions.canAllocate(PolicySpec.VENDOR_S3, PolicySpec.S3_RESOURCE_OBJECT, bucketName, PolicySpec.S3_PUTOBJECT, ctx.getUser(), oldBucketSize + size)) { // dbObject.rollback(); LOG.error("Quota exceeded for WalrusBackend putObject"); throw new EntityTooLargeException("Key", objectKey); } ObjectInfo objectInfo = new ObjectInfo(bucketName, objectKey); objectInfo.setOwnerId(account.getAccountNumber()); objectInfo.setCleanup(false); objectInfo.setEtag(eTag); objectInfo.setUploadId(foundManifest.getUploadId()); objectInfo.setDeleted(false); objectInfo.setSize(size); objectInfo.setLastModified(new Date()); objectInfo.setVersionId(versionId); objectInfo.setStorageClass(foundManifest.getStorageClass()); objectInfo.setContentDisposition(foundManifest.getContentDisposition()); objectInfo.setContentType(foundManifest.getContentType()); objectInfo.setLast(true); objectInfo.setDeleted(false); objectInfo.updateGrants(foundManifest); objectInfo.setMetaData(foundManifest.cloneMetaData()); objectInfo.setLastModified(new Date()); EntityWrapper<ObjectInfo> dbOject = db.recast(ObjectInfo.class); dbOject.add(objectInfo); //cleanup unused parts Set<Integer> keys = partsMap.keySet(); for (Integer key : keys) { partsMap.get(key).markForCleanup(); } db.commit(); reply.setEtag(foundManifest.getEtag()); // TODO figure out etag correctly reply.setLocation("WalrusBackend" + foundManifest.getBucketName() + "/" + foundManifest.getObjectKey()); reply.setBucket(foundManifest.getBucketName()); reply.setKey(foundManifest.getObjectKey()); reply.setLastModified(objectInfo.getLastModified()); //fire cleanup firePartsCleanupTask(foundManifest.getBucketName(), request.getKey(), request.getUploadId()); } } else { throw new InvalidPartException("No parts found for: " + request.getUploadId()); } } else { throw new NoSuchUploadException(request.getUploadId()); } } catch (Exception ex) { db.rollback(); throw new InternalErrorException(ex); } } else { db.rollback(); throw new NoSuchBucketException(bucketName); } reply.setLocation(request.getBucket() + '/' + request.getKey()); reply.setBucket(request.getBucket()); reply.setKey(request.getKey()); return reply; }
From source file:com.evolveum.midpoint.repo.sql.query.matcher.Matcher.java
License:Apache License
protected Criterion basicMatch(ItemRestrictionOperation operation, String propertyName, Object value, boolean ignoreCase) throws QueryException { Criterion criterion;// w ww. ja v a 2 s .c o m switch (operation) { case EQ: if (value == null) { criterion = Restrictions.isNull(propertyName); } else { criterion = Restrictions.eq(propertyName, value); } break; case GT: criterion = Restrictions.gt(propertyName, value); break; case GE: criterion = Restrictions.ge(propertyName, value); break; case LT: criterion = Restrictions.lt(propertyName, value); break; case LE: criterion = Restrictions.le(propertyName, value); break; case NOT_NULL: criterion = Restrictions.isNotNull(propertyName); break; case NULL: criterion = Restrictions.isNull(propertyName); break; case STARTS_WITH: criterion = Restrictions.like(propertyName, (String) value, MatchMode.START); break; case ENDS_WITH: criterion = Restrictions.like(propertyName, (String) value, MatchMode.END); break; case SUBSTRING: criterion = Restrictions.like(propertyName, (String) value, MatchMode.ANYWHERE); break; default: throw new QueryException("Unknown operation '" + operation + "'."); } if (ignoreCase && (value instanceof String) && (criterion instanceof SimpleExpression)) { criterion = ((SimpleExpression) criterion).ignoreCase(); } return criterion; }
From source file:com.floreantpos.model.dao.PosTransactionDAO.java
License:Open Source License
public List<PosTransaction> findUnauthorizedTransactions(User owner) { Session session = null;//w w w. j a v a 2 s. com try { session = getSession(); Criteria criteria = session.createCriteria(getReferenceClass()); criteria.add(Restrictions.eq(PosTransaction.PROP_CAPTURED, Boolean.FALSE)); criteria.add(Restrictions.eq(PosTransaction.PROP_AUTHORIZABLE, Boolean.TRUE)); criteria.add(Restrictions.or(Restrictions.isNull(PosTransaction.PROP_VOIDED), Restrictions.eq(PosTransaction.PROP_VOIDED, Boolean.FALSE))); criteria.add(Restrictions.eq(PosTransaction.PROP_TRANSACTION_TYPE, TransactionType.CREDIT.name())); criteria.add(Restrictions.isNotNull(PosTransaction.PROP_TICKET)); if (owner != null) { criteria.add(Restrictions.eq(PosTransaction.PROP_USER, owner)); } return criteria.list(); } finally { closeSession(session); } }
From source file:com.floreantpos.model.dao.PosTransactionDAO.java
License:Open Source License
public List<PosTransaction> findAuthorizedTransactions(User owner) { Session session = null;/* w w w . j a v a 2s . c o m*/ try { session = getSession(); Criteria criteria = session.createCriteria(CreditCardTransaction.class); criteria.add(Restrictions.eq(PosTransaction.PROP_CAPTURED, Boolean.TRUE)); criteria.add(Restrictions.or(Restrictions.isNull(PosTransaction.PROP_VOIDED), Restrictions.eq(PosTransaction.PROP_VOIDED, Boolean.FALSE))); criteria.add(Restrictions.isNotNull(PosTransaction.PROP_TICKET)); //criteria.add(Restrictions.eq(PosTransaction.PROP_DRAWER_RESETTED, Boolean.FALSE)); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -1); Date startOfDay = DateUtil.startOfDay(calendar.getTime()); Date endOfDay = DateUtil.endOfDay(new Date()); //show credit card transactions of last 2 days criteria.add(Restrictions.between(PosTransaction.PROP_TRANSACTION_TIME, startOfDay, endOfDay)); return criteria.list(); } finally { closeSession(session); } }
From source file:com.floreantpos.model.dao.PosTransactionDAO.java
License:Open Source License
public List<? extends PosTransaction> findTransactions(Terminal terminal, Class transactionClass, Date from, Date to) {//w w w . ja va 2 s . c o m Session session = null; try { session = getSession(); Criteria criteria = session.createCriteria(transactionClass); criteria.add(Restrictions.isNotNull(PosTransaction.PROP_TICKET)); if (terminal != null) { criteria.add(Restrictions.eq(PosTransaction.PROP_TERMINAL, terminal)); } if (from != null && to != null) { //criteria.add(Restrictions.ge(PosTransaction.PROP_TRANSACTION_TIME, from)); //criteria.add(Restrictions.le(PosTransaction.PROP_TRANSACTION_TIME, to)); } return criteria.list(); } finally { closeSession(session); } }
From source file:com.floreantpos.model.dao.PosTransactionDAO.java
License:Open Source License
public List<? extends PosTransaction> findTransactions(Class transactionClass, Date from, Date to) { Session session = null;//w w w. j a va 2s. com try { session = getSession(); Criteria criteria = session.createCriteria(transactionClass); criteria.add(Restrictions.isNotNull(PosTransaction.PROP_TICKET)); if (from != null && to != null) { criteria.add(Restrictions.ge(PosTransaction.PROP_TRANSACTION_TIME, from)); criteria.add(Restrictions.le(PosTransaction.PROP_TRANSACTION_TIME, to)); } return criteria.list(); } finally { closeSession(session); } }