use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class EntitySyncContext method assembleValuesToStore.
public ArrayList<GenericValue> assembleValuesToStore() throws SyncDataErrorException {
// simulate two ordered lists and merge them on-the-fly for faster combined sorting
// make it an ArrayList to easily merge in sorted lists
ArrayList<GenericValue> valuesToStore = new ArrayList<GenericValue>();
if (this.nextUpdateTxTime != null && (this.nextUpdateTxTime.equals(currentRunEndTime) || this.nextUpdateTxTime.after(currentRunEndTime))) {
// this means that for all entities in this pack we found on the last pass that there would be nothing for this one, so just return nothing...
return valuesToStore;
}
// Debug.logInfo("Getting values to store; currentRunStartTime=" + currentRunStartTime + ", currentRunEndTime=" + currentRunEndTime, module);
int entitiesSkippedForKnownNext = 0;
// iterate through entities, get all records with tx stamp in the current time range, put all in a single list
for (ModelEntity modelEntity : entityModelToUseList) {
int insertBefore = 0;
// first test to see if we know that there are no records for this entity in this time period...
Timestamp knownNextUpdateTime = this.nextEntityUpdateTxTime.get(modelEntity.getEntityName());
if (knownNextUpdateTime != null && (knownNextUpdateTime.equals(currentRunEndTime) || knownNextUpdateTime.after(currentRunEndTime))) {
entitiesSkippedForKnownNext++;
continue;
}
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin(7200);
} catch (GenericTransactionException e) {
throw new SyncDataErrorException("Unable to begin JTA transaction", e);
}
try {
// get all values that were updated, but NOT created in the current time range; if no info on created stamp, that's okay we'll include it here because it won't have been included in the valuesToCreate list
EntityCondition createdBeforeStartCond = EntityCondition.makeCondition(EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.LESS_THAN, currentRunStartTime));
EntityCondition findValCondition = EntityCondition.makeCondition(EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunStartTime), EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.LESS_THAN, currentRunEndTime), createdBeforeStartCond);
EntityListIterator eli = EntityQuery.use(delegator).from(modelEntity.getEntityName()).where(findValCondition).orderBy(ModelEntity.STAMP_TX_FIELD, ModelEntity.STAMP_FIELD).queryIterator();
GenericValue nextValue = null;
long valuesPerEntity = 0;
while ((nextValue = eli.next()) != null) {
// find first value in valuesToStore list, starting with the current insertBefore value, that has a STAMP_TX_FIELD after the nextValue.STAMP_TX_FIELD, then do the same with STAMP_FIELD
while (insertBefore < valuesToStore.size() && valuesToStore.get(insertBefore).getTimestamp(ModelEntity.STAMP_TX_FIELD).before(nextValue.getTimestamp(ModelEntity.STAMP_TX_FIELD))) {
insertBefore++;
}
while (insertBefore < valuesToStore.size() && valuesToStore.get(insertBefore).getTimestamp(ModelEntity.STAMP_FIELD).before(nextValue.getTimestamp(ModelEntity.STAMP_FIELD))) {
insertBefore++;
}
valuesToStore.add(insertBefore, nextValue);
valuesPerEntity++;
}
eli.close();
// if we didn't find anything for this entity, find the next value's Timestamp and keep track of it
if (valuesPerEntity == 0) {
Timestamp startCheckStamp = new Timestamp(System.currentTimeMillis() - syncEndBufferMillis);
EntityCondition findNextCondition = EntityCondition.makeCondition(EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunEndTime), EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.LESS_THAN, currentRunEndTime));
EntityListIterator eliNext = EntityQuery.use(delegator).from(modelEntity.getEntityName()).where(findNextCondition).orderBy(ModelEntity.STAMP_TX_FIELD).queryIterator();
// get the first element and it's tx time value...
GenericValue firstVal = eliNext.next();
eliNext.close();
Timestamp nextTxTime;
if (firstVal != null) {
nextTxTime = firstVal.getTimestamp(ModelEntity.CREATE_STAMP_TX_FIELD);
} else {
// no results? well, then it's safe to say that up to the pre-querytime (minus the buffer, as usual) we are okay
nextTxTime = startCheckStamp;
}
if (this.nextUpdateTxTime == null || nextTxTime.before(this.nextUpdateTxTime)) {
this.nextUpdateTxTime = nextTxTime;
Debug.logInfo("EntitySync: Set nextUpdateTxTime to [" + nextTxTime + "]", module);
}
Timestamp curEntityNextTxTime = this.nextEntityUpdateTxTime.get(modelEntity.getEntityName());
if (curEntityNextTxTime == null || nextTxTime.before(curEntityNextTxTime)) {
this.nextEntityUpdateTxTime.put(modelEntity.getEntityName(), nextTxTime);
Debug.logInfo("EntitySync: Set nextEntityUpdateTxTime to [" + nextTxTime + "] for the entity [" + modelEntity.getEntityName() + "]", module);
}
}
} catch (GenericEntityException e) {
try {
TransactionUtil.rollback(beganTransaction, "Entity Engine error in assembleValuesToStore", e);
} catch (GenericTransactionException e2) {
Debug.logWarning(e2, "Unable to call rollback()", module);
}
throw new SyncDataErrorException("Error getting values to store from the datasource", e);
} catch (Throwable t) {
try {
TransactionUtil.rollback(beganTransaction, "General error in assembleValuesToStore", t);
} catch (GenericTransactionException e2) {
Debug.logWarning(e2, "Unable to call rollback()", module);
}
throw new SyncDataErrorException("Caught runtime error while getting values to store", t);
}
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException e) {
throw new SyncDataErrorException("Commit transaction failed", e);
}
}
if (entitiesSkippedForKnownNext > 0) {
if (Debug.infoOn())
Debug.logInfo("In assembleValuesToStore skipped [" + entitiesSkippedForKnownNext + "/" + entityModelToUseList + "] entities for the time period ending at [" + currentRunEndTime + "] because of next known update times", module);
}
// TEST SECTION: leave false for normal use
boolean logValues = false;
if (logValues && valuesToStore.size() > 0) {
StringBuilder toStoreInfo = new StringBuilder();
for (GenericValue valueToStore : valuesToStore) {
toStoreInfo.append("\n-->[");
toStoreInfo.append(valueToStore.get(ModelEntity.STAMP_TX_FIELD));
toStoreInfo.append(":");
toStoreInfo.append(valueToStore.get(ModelEntity.STAMP_FIELD));
toStoreInfo.append("] ");
toStoreInfo.append(valueToStore.getPrimaryKey());
}
Debug.logInfo(toStoreInfo.toString(), module);
}
// this calculation is false, so it needs to be nullified
if (valuesToStore.size() > 0) {
this.nextUpdateTxTime = null;
}
return valuesToStore;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class EntityCount method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
try {
Delegator delegator = getDelegator(methodContext);
String entityName = this.entityNameFse.expandString(methodContext.getEnvMap());
ModelEntity modelEntity = delegator.getModelEntity(entityName);
EntityCondition whereEntityCondition = null;
if (this.whereCondition != null) {
whereEntityCondition = this.whereCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity));
}
EntityCondition havingEntityCondition = null;
if (this.havingCondition != null) {
havingEntityCondition = this.havingCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity));
}
long count = EntityQuery.use(delegator).from(entityName).where(whereEntityCondition).having(havingEntityCondition).queryCount();
this.countFma.put(methodContext.getEnvMap(), count);
} catch (GeneralException e) {
String errMsg = "Exception thrown while performing entity count: " + e.getMessage();
Debug.logWarning(e, errMsg, module);
simpleMethod.addErrorMessage(methodContext, errMsg);
return false;
}
return true;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class FindByAnd method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
boolean useIterator = "true".equals(useIteratorFse.expandString(methodContext.getEnvMap()));
List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap());
Delegator delegator = getDelegator(methodContext);
try {
EntityCondition whereCond = null;
Map<String, ? extends Object> fieldMap = mapFma.get(methodContext.getEnvMap());
if (fieldMap != null) {
whereCond = EntityCondition.makeCondition(fieldMap);
}
if (useIterator) {
listFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelectListFma.get(methodContext.getEnvMap()))).from(entityName).where(whereCond).orderBy(orderByNames).queryIterator());
} else {
listFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelectListFma.get(methodContext.getEnvMap()))).from(entityName).where(whereCond).orderBy(orderByNames).cache(useCache).queryList());
}
} catch (GenericEntityException e) {
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
Debug.logWarning(e, errMsg, module);
simpleMethod.addErrorMessage(methodContext, errMsg);
return false;
}
return true;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class JobManager method poll.
/**
* Scans the JobSandbox entity and returns a list of jobs that are due to run.
* Returns an empty list if there are no jobs due to run.
* This method is called by the {@link JobPoller} polling thread.
*/
protected List<Job> poll(int limit) {
assertIsRunning();
// The rest of this method logs exceptions and does not throw them.
// The idea is to keep the JobPoller working even when a database
// connection is not available (possible on a saturated server).
DispatchContext dctx = getDispatcher().getDispatchContext();
if (dctx == null) {
Debug.logWarning("Unable to locate DispatchContext object; not running job!", module);
return Collections.emptyList();
}
// basic query
List<EntityExpr> expressions = UtilMisc.toList(EntityCondition.makeCondition("runTime", EntityOperator.LESS_THAN_EQUAL_TO, UtilDateTime.nowTimestamp()), EntityCondition.makeCondition("startDateTime", EntityOperator.EQUALS, null), EntityCondition.makeCondition("cancelDateTime", EntityOperator.EQUALS, null), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null));
// limit to just defined pools
List<String> pools = null;
try {
pools = getRunPools();
} catch (GenericConfigException e) {
Debug.logWarning(e, "Unable to get run pools - not running job: ", module);
return Collections.emptyList();
}
List<EntityExpr> poolsExpr = UtilMisc.toList(EntityCondition.makeCondition("poolId", EntityOperator.EQUALS, null));
if (!pools.isEmpty()) {
for (String poolName : pools) {
poolsExpr.add(EntityCondition.makeCondition("poolId", EntityOperator.EQUALS, poolName));
}
}
List<Job> poll = new ArrayList<>(limit);
// make the conditions
EntityCondition baseCondition = EntityCondition.makeCondition(expressions);
EntityCondition poolCondition = EntityCondition.makeCondition(poolsExpr, EntityOperator.OR);
EntityCondition mainCondition = EntityCondition.makeCondition(UtilMisc.toList(baseCondition, poolCondition));
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
if (!beganTransaction) {
Debug.logWarning("Unable to poll JobSandbox for jobs; unable to begin transaction.", module);
return poll;
}
try (EntityListIterator jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("runTime").queryIterator()) {
GenericValue jobValue = jobsIterator.next();
while (jobValue != null) {
// Claim ownership of this value. Using storeByCondition to avoid a race condition.
List<EntityExpr> updateExpression = UtilMisc.toList(EntityCondition.makeCondition("jobId", EntityOperator.EQUALS, jobValue.get("jobId")), EntityCondition.makeCondition("runByInstanceId", EntityOperator.EQUALS, null));
int rowsUpdated = delegator.storeByCondition("JobSandbox", UtilMisc.toMap("runByInstanceId", instanceId), EntityCondition.makeCondition(updateExpression));
if (rowsUpdated == 1) {
poll.add(new PersistedServiceJob(dctx, jobValue, null));
if (poll.size() == limit) {
break;
}
}
jobValue = jobsIterator.next();
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
TransactionUtil.commit(beganTransaction);
} catch (Throwable t) {
String errMsg = "Exception thrown while polling JobSandbox: ";
try {
TransactionUtil.rollback(beganTransaction, errMsg, t);
} catch (GenericEntityException e) {
Debug.logWarning(e, "Exception thrown while rolling back transaction: ", module);
}
Debug.logWarning(t, errMsg, module);
return Collections.emptyList();
}
if (poll.isEmpty()) {
// No jobs to run, see if there are any jobs to purge
Calendar cal = Calendar.getInstance();
try {
int daysToKeep = ServiceConfigUtil.getServiceEngine().getThreadPool().getPurgeJobDays();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
} catch (GenericConfigException e) {
Debug.logWarning(e, "Unable to get purge job days: ", module);
return Collections.emptyList();
}
Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
List<EntityExpr> finExp = UtilMisc.toList(EntityCondition.makeCondition("finishDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("finishDateTime", EntityOperator.LESS_THAN, purgeTime));
List<EntityExpr> canExp = UtilMisc.toList(EntityCondition.makeCondition("cancelDateTime", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("cancelDateTime", EntityOperator.LESS_THAN, purgeTime));
EntityCondition doneCond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition(canExp), EntityCondition.makeCondition(finExp)), EntityOperator.OR);
mainCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runByInstanceId", instanceId), doneCond));
beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
if (!beganTransaction) {
Debug.logWarning("Unable to poll JobSandbox for jobs; unable to begin transaction.", module);
return Collections.emptyList();
}
try (EntityListIterator jobsIterator = EntityQuery.use(delegator).from("JobSandbox").where(mainCondition).orderBy("jobId").queryIterator()) {
GenericValue jobValue = jobsIterator.next();
while (jobValue != null) {
poll.add(new PurgeJob(jobValue));
if (poll.size() == limit) {
break;
}
jobValue = jobsIterator.next();
}
} catch (GenericEntityException e) {
Debug.logWarning(e, module);
}
TransactionUtil.commit(beganTransaction);
} catch (Throwable t) {
String errMsg = "Exception thrown while polling JobSandbox: ";
try {
TransactionUtil.rollback(beganTransaction, errMsg, t);
} catch (GenericEntityException e) {
Debug.logWarning(e, "Exception thrown while rolling back transaction: ", module);
}
Debug.logWarning(t, errMsg, module);
return Collections.emptyList();
}
}
return poll;
}
use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.
the class ProductSearchSession method getCountForProductCategory.
/**
* This method returns count of products in a given category (including all sub categories), the constraint being
* applied on current ProductSearchConstraint list in session.
* @param productCategoryId productCategoryId for which the count should be returned.
* @param session Current session.
* @param delegator The delegator object.
* @return The long value of count of products.
*/
public static long getCountForProductCategory(String productCategoryId, HttpSession session, Delegator delegator) {
String visitId = VisitHandler.getVisitId(session);
ProductSearchContext productSearchContext = new ProductSearchContext(delegator, visitId);
List<ProductSearchConstraint> productSearchConstraintList = ProductSearchOptions.getConstraintList(session);
if (UtilValidate.isNotEmpty(productSearchConstraintList)) {
productSearchContext.addProductSearchConstraints(productSearchConstraintList);
}
productSearchContext.finishKeywordConstraints();
productSearchContext.finishCategoryAndFeatureConstraints();
DynamicViewEntity dynamicViewEntity = productSearchContext.dynamicViewEntity;
List<EntityCondition> entityConditionList = productSearchContext.entityConditionList;
List<String> fieldsToSelect = new LinkedList<>();
dynamicViewEntity.addMemberEntity("PCMC", "ProductCategoryMember");
dynamicViewEntity.addAlias("PCMC", "pcmcProductCategoryId", "productCategoryId", null, null, null, null);
dynamicViewEntity.addAlias("PCMC", "pcmcFromDate", "fromDate", null, null, null, null);
dynamicViewEntity.addAlias("PCMC", "pcmcThruDate", "thruDate", null, null, null, null);
dynamicViewEntity.addAlias("PCMC", "categoryCount", "productId", null, null, null, "count-distinct");
dynamicViewEntity.addViewLink("PROD", "PCMC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("productId"));
fieldsToSelect.add("categoryCount");
entityConditionList.add(EntityCondition.makeCondition(EntityCondition.makeCondition("pcmcThruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("pcmcThruDate", EntityOperator.GREATER_THAN, productSearchContext.nowTimestamp)));
entityConditionList.add(EntityCondition.makeCondition("pcmcFromDate", EntityOperator.LESS_THAN, productSearchContext.nowTimestamp));
Set<String> productCategoryIdSet = new HashSet<>();
ProductSearch.getAllSubCategoryIds(productCategoryId, productCategoryIdSet, delegator, productSearchContext.nowTimestamp);
entityConditionList.add(EntityCondition.makeCondition("pcmcProductCategoryId", EntityOperator.IN, productCategoryIdSet));
Long categoryCount = Long.valueOf(0);
EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)).from(dynamicViewEntity).where(entityConditionList).orderBy(productSearchContext.orderByList).cursorScrollInsensitive();
try (EntityListIterator eli = eq.queryIterator()) {
GenericValue searchResult = null;
while ((searchResult = eli.next()) != null) {
categoryCount = searchResult.getLong("categoryCount");
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error in product search", module);
}
return categoryCount;
}
Aggregations