use of org.apache.ofbiz.entity.util.EntityQuery in project ofbiz-framework by apache.
the class ShipmentServices method updateShipmentsFromStaging.
public static Map<String, Object> updateShipmentsFromStaging(DispatchContext dctx, Map<String, ? extends Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Map<String, String> shipmentMap = new HashMap<String, String>();
EntityQuery eq = EntityQuery.use(delegator).from("OdbcPackageIn").orderBy("shipmentId", "shipmentPackageSeqId", "voidIndicator");
try (EntityListIterator eli = eq.queryIterator()) {
GenericValue pkgInfo;
while ((pkgInfo = eli.next()) != null) {
String packageSeqId = pkgInfo.getString("shipmentPackageSeqId");
String shipmentId = pkgInfo.getString("shipmentId");
// locate the shipment package
GenericValue shipmentPackage = EntityQuery.use(delegator).from("ShipmentPackage").where("shipmentId", shipmentId, "shipmentPackageSeqId", packageSeqId).queryOne();
if (shipmentPackage != null) {
if ("00001".equals(packageSeqId)) {
// only need to do this for the first package
GenericValue rtSeg = null;
rtSeg = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001").queryOne();
if (rtSeg == null) {
rtSeg = delegator.makeValue("ShipmentRouteSegment", UtilMisc.toMap("shipmentId", shipmentId, "shipmentRouteSegmentId", "00001"));
try {
delegator.create(rtSeg);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
rtSeg.set("actualStartDate", pkgInfo.get("shippedDate"));
rtSeg.set("billingWeight", pkgInfo.get("billingWeight"));
rtSeg.set("actualCost", pkgInfo.get("shippingTotal"));
rtSeg.set("trackingIdNumber", pkgInfo.get("trackingNumber"));
delegator.store(rtSeg);
}
Map<String, Object> pkgCtx = new HashMap<String, Object>();
pkgCtx.put("shipmentId", shipmentId);
pkgCtx.put("shipmentPackageSeqId", packageSeqId);
// first update the weight of the package
GenericValue pkg = null;
pkg = EntityQuery.use(delegator).from("ShipmentPackage").where(pkgCtx).queryOne();
if (pkg == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ProductShipmentPackageNotFound", UtilMisc.toMap("shipmentPackageSeqId", packageSeqId, "shipmentId", shipmentId), locale));
}
pkg.set("weight", pkgInfo.get("packageWeight"));
delegator.store(pkg);
// need if we are the first package (only) update the route seg info
pkgCtx.put("shipmentRouteSegmentId", "00001");
GenericValue pkgRtSeg = null;
pkgRtSeg = EntityQuery.use(delegator).from("ShipmentPackageRouteSeg").where(pkgCtx).queryOne();
if (pkgRtSeg == null) {
pkgRtSeg = delegator.makeValue("ShipmentPackageRouteSeg", pkgCtx);
try {
delegator.create(pkgRtSeg);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
pkgRtSeg.set("trackingCode", pkgInfo.get("trackingNumber"));
pkgRtSeg.set("boxNumber", pkgInfo.get("shipmentPackageSeqId"));
pkgRtSeg.set("packageServiceCost", pkgInfo.get("packageTotal"));
delegator.store(pkgRtSeg);
shipmentMap.put(shipmentId, pkgInfo.getString("voidIndicator"));
}
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// update the status of each shipment
for (Map.Entry<String, String> entry : shipmentMap.entrySet()) {
String shipmentId = entry.getKey();
String voidInd = entry.getValue();
Map<String, Object> shipCtx = new HashMap<String, Object>();
shipCtx.put("shipmentId", shipmentId);
if ("Y".equals(voidInd)) {
shipCtx.put("statusId", "SHIPMENT_CANCELLED");
} else {
shipCtx.put("statusId", "SHIPMENT_SHIPPED");
}
shipCtx.put("userLogin", userLogin);
Map<String, Object> shipResp = null;
try {
shipResp = dispatcher.runSync("updateShipment", shipCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(shipResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(shipResp));
}
// remove the shipment info
Map<String, Object> clearResp = null;
try {
clearResp = dispatcher.runSync("clearShipmentStaging", UtilMisc.<String, Object>toMap("shipmentId", shipmentId, "userLogin", userLogin));
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(clearResp)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(clearResp));
}
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.util.EntityQuery in project ofbiz-framework by apache.
the class ProductUtilServices method makeStandAloneFromSingleVariantVirtuals.
public static Map<String, Object> makeStandAloneFromSingleVariantVirtuals(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
Locale locale = (Locale) context.get("locale");
String errMsg = null;
Debug.logInfo("Starting makeStandAloneFromSingleVariantVirtuals", module);
DynamicViewEntity dve = new DynamicViewEntity();
dve.addMemberEntity("PVIRT", "Product");
dve.addMemberEntity("PVA", "ProductAssoc");
dve.addViewLink("PVIRT", "PVA", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("productId", "productId")));
dve.addAlias("PVIRT", "productId", null, null, null, Boolean.TRUE, null);
dve.addAlias("PVIRT", "salesDiscontinuationDate", null, null, null, null, null);
dve.addAlias("PVA", "productAssocTypeId", null, null, null, null, null);
dve.addAlias("PVA", "fromDate", null, null, null, null, null);
dve.addAlias("PVA", "thruDate", null, null, null, null, null);
dve.addAlias("PVA", "productIdToCount", "productIdTo", null, null, null, "count-distinct");
EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp))), EntityOperator.AND);
EntityCondition havingCond = EntityCondition.makeCondition("productIdToCount", EntityOperator.EQUALS, Long.valueOf(1));
EntityQuery eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(condition).having(havingCond);
try (EntityListIterator eliOne = eq.queryIterator()) {
List<GenericValue> valueList = eliOne.getCompleteList();
Debug.logInfo("Found " + valueList.size() + " virtual products with one variant to turn into a stand alone product.", module);
int numWithOneOnly = 0;
for (GenericValue value : valueList) {
// has only one variant period, is it valid? should already be discontinued if not
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one variant move all info from virtual to variant and remove virtual, make variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.TRUE, "userLogin", userLogin));
numWithOneOnly++;
if (numWithOneOnly % 100 == 0) {
Debug.logInfo("Made " + numWithOneOnly + " virtual products with only one valid variant stand-alone products.", module);
}
}
}
EntityCondition conditionWithDates = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp)), EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp), EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))), EntityOperator.AND);
eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(conditionWithDates).having(havingCond);
try (EntityListIterator eliMulti = eq.queryIterator()) {
List<GenericValue> valueMultiList = eliMulti.getCompleteList();
Debug.logInfo("Found " + valueMultiList.size() + " virtual products with one VALID variant to pull the variant from to make a stand alone product.", module);
int numWithOneValid = 0;
for (GenericValue value : valueMultiList) {
// has only one valid variant
String productId = value.getString("productId");
List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
// verify the query; tested on a bunch, looks good
if (paList.size() != 1) {
Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
} else {
// for all virtuals with one valid variant move info from virtual to variant, put variant in categories from virtual, remove virtual from all categories but leave "family" otherwise intact, mark variant as not a variant
dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.FALSE, "userLogin", userLogin));
numWithOneValid++;
if (numWithOneValid % 100 == 0) {
Debug.logInfo("Made " + numWithOneValid + " virtual products with one valid variant stand-alone products.", module);
}
}
}
Debug.logInfo("Found virtual products with one valid variant: " + numWithOneValid + ", with one variant only: " + numWithOneOnly, module);
} catch (GenericEntityException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
} catch (GenericEntityException | GenericServiceException e) {
Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
Debug.logError(e, errMsg, module);
return ServiceUtil.returnError(errMsg);
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.util.EntityQuery in project ofbiz-framework by apache.
the class EntitySyncContext method assembleValuesToCreate.
public ArrayList<GenericValue> assembleValuesToCreate() throws SyncDataErrorException {
// first grab all values inserted in the date range, then get the updates (leaving out all values inserted in the data range)
// make it an ArrayList to easily merge in sorted lists
ArrayList<GenericValue> valuesToCreate = new ArrayList<GenericValue>();
if (this.nextCreateTxTime != null && (this.nextCreateTxTime.equals(currentRunEndTime) || this.nextCreateTxTime.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 valuesToCreate;
}
// Debug.logInfo("Getting values to create; 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 knownNextCreateTime = this.nextEntityCreateTxTime.get(modelEntity.getEntityName());
if (knownNextCreateTime != null && (knownNextCreateTime.equals(currentRunEndTime) || knownNextCreateTime.after(currentRunEndTime))) {
// Debug.logInfo("In assembleValuesToCreate found knownNextCreateTime [" + knownNextCreateTime + "] after currentRunEndTime [" + currentRunEndTime + "], so skipping time per period for entity [" + modelEntity.getEntityName() + "]", module);
entitiesSkippedForKnownNext++;
continue;
}
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin(7200);
} catch (GenericTransactionException e) {
throw new SyncDataErrorException("Unable to begin JTA transaction", e);
}
try {
EntityCondition findValCondition = EntityCondition.makeCondition(EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunStartTime), EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.LESS_THAN, currentRunEndTime));
EntityQuery eq = EntityQuery.use(delegator).from(modelEntity.getEntityName()).where(findValCondition).orderBy(ModelEntity.CREATE_STAMP_TX_FIELD, ModelEntity.CREATE_STAMP_FIELD);
long valuesPerEntity = 0;
try (EntityListIterator eli = eq.queryIterator()) {
// get the values created within the current time range
GenericValue nextValue = null;
while ((nextValue = eli.next()) != null) {
// find first value in valuesToCreate list, starting with the current insertBefore value, that has a CREATE_STAMP_TX_FIELD after the nextValue.CREATE_STAMP_TX_FIELD, then do the same with CREATE_STAMP_FIELD
while (insertBefore < valuesToCreate.size() && valuesToCreate.get(insertBefore).getTimestamp(ModelEntity.CREATE_STAMP_TX_FIELD).before(nextValue.getTimestamp(ModelEntity.CREATE_STAMP_TX_FIELD))) {
insertBefore++;
}
while (insertBefore < valuesToCreate.size() && valuesToCreate.get(insertBefore).getTimestamp(ModelEntity.CREATE_STAMP_FIELD).before(nextValue.getTimestamp(ModelEntity.CREATE_STAMP_FIELD))) {
insertBefore++;
}
valuesToCreate.add(insertBefore, nextValue);
valuesPerEntity++;
}
} catch (GenericEntityException e) {
try {
TransactionUtil.rollback(beganTransaction, "Entity Engine error in assembleValuesToCreate", e);
} catch (GenericTransactionException e2) {
Debug.logWarning(e2, "Unable to call rollback()", module);
}
throw new SyncDataErrorException("Error getting values to create from the datasource", e);
}
// 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.CREATE_STAMP_TX_FIELD, EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition(ModelEntity.CREATE_STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunEndTime));
eq = EntityQuery.use(delegator).from(modelEntity.getEntityName()).where(findNextCondition).orderBy(ModelEntity.CREATE_STAMP_TX_FIELD);
GenericValue firstVal = null;
try (EntityListIterator eliNext = eq.queryIterator()) {
// get the first element and it's tx time value...
firstVal = eliNext.next();
} catch (GenericEntityException e) {
try {
TransactionUtil.rollback(beganTransaction, "Entity Engine error in assembleValuesToCreate", e);
} catch (GenericTransactionException e2) {
Debug.logWarning(e2, "Unable to call rollback()", module);
}
throw new SyncDataErrorException("Error getting values to create from the datasource", e);
}
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.nextCreateTxTime == null || nextTxTime.before(this.nextCreateTxTime)) {
this.nextCreateTxTime = nextTxTime;
Debug.logInfo("EntitySync: Set nextCreateTxTime to [" + nextTxTime + "]", module);
}
Timestamp curEntityNextTxTime = this.nextEntityCreateTxTime.get(modelEntity.getEntityName());
if (curEntityNextTxTime == null || nextTxTime.before(curEntityNextTxTime)) {
this.nextEntityCreateTxTime.put(modelEntity.getEntityName(), nextTxTime);
Debug.logInfo("EntitySync: Set nextEntityCreateTxTime to [" + nextTxTime + "] for the entity [" + modelEntity.getEntityName() + "]", module);
}
}
} catch (Throwable t) {
try {
TransactionUtil.rollback(beganTransaction, "Throwable error in assembleValuesToCreate", t);
} catch (GenericTransactionException e2) {
Debug.logWarning(e2, "Unable to call rollback()", module);
}
throw new SyncDataErrorException("Caught runtime error while getting values to create", t);
}
try {
TransactionUtil.commit(beganTransaction);
} catch (GenericTransactionException e) {
throw new SyncDataErrorException("Commit transaction failed", e);
}
}
if (entitiesSkippedForKnownNext > 0) {
if (Debug.infoOn())
Debug.logInfo("In assembleValuesToCreate skipped [" + entitiesSkippedForKnownNext + "/" + entityModelToUseList + "] entities for the time period ending at [" + currentRunEndTime + "] because of next known create times", module);
}
// TEST SECTION: leave false for normal use
boolean logValues = false;
if (logValues && valuesToCreate.size() > 0) {
StringBuilder toCreateInfo = new StringBuilder();
for (GenericValue valueToCreate : valuesToCreate) {
toCreateInfo.append("\n-->[");
toCreateInfo.append(valueToCreate.get(ModelEntity.CREATE_STAMP_TX_FIELD));
toCreateInfo.append(":");
toCreateInfo.append(valueToCreate.get(ModelEntity.CREATE_STAMP_FIELD));
toCreateInfo.append("] ");
toCreateInfo.append(valueToCreate.getPrimaryKey());
}
Debug.logInfo(toCreateInfo.toString(), module);
}
// this calculation is false, so it needs to be nullified
if (valuesToCreate.size() > 0) {
this.nextCreateTxTime = null;
}
return valuesToCreate;
}
use of org.apache.ofbiz.entity.util.EntityQuery in project ofbiz-framework by apache.
the class ServiceUtil method purgeOldJobs.
public static Map<String, Object> purgeOldJobs(DispatchContext dctx, Map<String, ? extends Object> context) {
Locale locale = (Locale) context.get("locale");
Debug.logWarning("purgeOldJobs service invoked. This service is obsolete - the Job Scheduler will purge old jobs automatically.", module);
String sendPool = null;
Calendar cal = Calendar.getInstance();
try {
sendPool = ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool();
int daysToKeep = ServiceConfigUtil.getServiceEngine().getThreadPool().getPurgeJobDays();
cal.add(Calendar.DAY_OF_YEAR, -daysToKeep);
} catch (GenericConfigException e) {
Debug.logWarning(e, "Exception thrown while getting service configuration: ", module);
return returnError(UtilProperties.getMessage(ServiceUtil.resource, "ServiceExceptionThrownWhileGettingServiceConfiguration", UtilMisc.toMap("errorString", e), locale));
}
Delegator delegator = dctx.getDelegator();
Timestamp purgeTime = new Timestamp(cal.getTimeInMillis());
// create the conditions to query
EntityCondition pool = EntityCondition.makeCondition("poolId", sendPool);
List<EntityExpr> finExp = UtilMisc.toList(EntityCondition.makeCondition("finishDateTime", EntityOperator.NOT_EQUAL, null));
finExp.add(EntityCondition.makeCondition("finishDateTime", EntityOperator.LESS_THAN, purgeTime));
List<EntityExpr> canExp = UtilMisc.toList(EntityCondition.makeCondition("cancelDateTime", EntityOperator.NOT_EQUAL, null));
canExp.add(EntityCondition.makeCondition("cancelDateTime", EntityOperator.LESS_THAN, purgeTime));
EntityCondition cancelled = EntityCondition.makeCondition(canExp);
EntityCondition finished = EntityCondition.makeCondition(finExp);
EntityCondition doneCond = EntityCondition.makeCondition(UtilMisc.toList(cancelled, finished), EntityOperator.OR);
// always suspend the current transaction; use the one internally
Transaction parent = null;
try {
if (TransactionUtil.getStatus() != TransactionUtil.STATUS_NO_TRANSACTION) {
parent = TransactionUtil.suspend();
}
// lookup the jobs - looping 1000 at a time to avoid problems with cursors
// also, using unique transaction to delete as many as possible even with errors
boolean noMoreResults = false;
boolean beganTx1 = false;
while (!noMoreResults) {
// current list of records
List<GenericValue> curList = null;
try {
// begin this transaction
beganTx1 = TransactionUtil.begin();
EntityQuery eq = EntityQuery.use(delegator).select("jobId").from("JobSandbox").where(EntityCondition.makeCondition(UtilMisc.toList(doneCond, pool))).cursorScrollInsensitive().maxRows(1000);
try (EntityListIterator foundJobs = eq.queryIterator()) {
curList = foundJobs.getPartialList(1, 1000);
}
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot obtain job data from datasource", module);
try {
TransactionUtil.rollback(beganTx1, e.getMessage(), e);
} catch (GenericTransactionException e1) {
Debug.logWarning(e1, module);
}
return ServiceUtil.returnError(e.getMessage());
} finally {
try {
TransactionUtil.commit(beganTx1);
} catch (GenericTransactionException e) {
Debug.logWarning(e, module);
}
}
// remove each from the list in its own transaction
if (UtilValidate.isNotEmpty(curList)) {
for (GenericValue job : curList) {
String jobId = job.getString("jobId");
boolean beganTx2 = false;
try {
beganTx2 = TransactionUtil.begin();
job.remove();
} catch (GenericEntityException e) {
Debug.logInfo("Cannot remove job data for ID: " + jobId, module);
try {
TransactionUtil.rollback(beganTx2, e.getMessage(), e);
} catch (GenericTransactionException e1) {
Debug.logWarning(e1, module);
}
} finally {
try {
TransactionUtil.commit(beganTx2);
} catch (GenericTransactionException e) {
Debug.logWarning(e, module);
}
}
}
} else {
noMoreResults = true;
}
}
// Now JobSandbox data is cleaned up. Now process Runtime data and remove the whole data in single shot that is of no need.
boolean beganTx3 = false;
GenericValue runtimeData = null;
List<GenericValue> runtimeDataToDelete = new LinkedList<>();
long jobsandBoxCount = 0;
try {
// begin this transaction
beganTx3 = TransactionUtil.begin();
EntityQuery eq = EntityQuery.use(delegator).select("runtimeDataId").from("RuntimeData");
try (EntityListIterator runTimeDataIt = eq.queryIterator()) {
while ((runtimeData = runTimeDataIt.next()) != null) {
EntityCondition whereCondition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("runtimeDataId", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("runtimeDataId", EntityOperator.EQUALS, runtimeData.getString("runtimeDataId"))), EntityOperator.AND);
jobsandBoxCount = EntityQuery.use(delegator).from("JobSandbox").where(whereCondition).queryCount();
if (BigDecimal.ZERO.compareTo(BigDecimal.valueOf(jobsandBoxCount)) == 0) {
runtimeDataToDelete.add(runtimeData);
}
}
}
// Now we are ready to delete runtimeData, we can safely delete complete list that we have recently fetched i.e runtimeDataToDelete.
delegator.removeAll(runtimeDataToDelete);
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot obtain runtime data from datasource", module);
try {
TransactionUtil.rollback(beganTx3, e.getMessage(), e);
} catch (GenericTransactionException e1) {
Debug.logWarning(e1, module);
}
return ServiceUtil.returnError(e.getMessage());
} finally {
try {
TransactionUtil.commit(beganTx3);
} catch (GenericTransactionException e) {
Debug.logWarning(e, module);
}
}
} catch (GenericTransactionException e) {
Debug.logError(e, "Unable to suspend transaction; cannot purge jobs!", module);
return ServiceUtil.returnError(e.getMessage());
} finally {
if (parent != null) {
try {
TransactionUtil.resume(parent);
} catch (GenericTransactionException e) {
Debug.logWarning(e, module);
}
}
}
return ServiceUtil.returnSuccess();
}
use of org.apache.ofbiz.entity.util.EntityQuery in project ofbiz-framework by apache.
the class WebToolsServices method entityExportAll.
public static Map<String, Object> entityExportAll(DispatchContext dctx, Map<String, ? extends Object> context) {
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
// mandatory
String outpath = (String) context.get("outpath");
Timestamp fromDate = (Timestamp) context.get("fromDate");
Integer txTimeout = (Integer) context.get("txTimeout");
if (txTimeout == null) {
txTimeout = Integer.valueOf(7200);
}
List<String> results = new LinkedList<String>();
if (UtilValidate.isNotEmpty(outpath)) {
File outdir = new File(outpath);
if (!outdir.exists()) {
outdir.mkdir();
}
if (outdir.isDirectory() && outdir.canWrite()) {
Set<String> passedEntityNames;
try {
ModelReader reader = delegator.getModelReader();
Collection<String> ec = reader.getEntityNames();
passedEntityNames = new TreeSet<String>(ec);
} catch (Exception exc) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityImportErrorRetrievingEntityNames", locale));
}
int fileNumber = 1;
for (String curEntityName : passedEntityNames) {
long numberWritten = 0;
ModelEntity me = delegator.getModelEntity(curEntityName);
if (me instanceof ModelViewEntity) {
results.add("[" + fileNumber + "] [vvv] " + curEntityName + " skipping view entity");
continue;
}
List<EntityCondition> conds = new LinkedList<EntityCondition>();
if (UtilValidate.isNotEmpty(fromDate)) {
conds.add(EntityCondition.makeCondition("createdStamp", EntityOperator.GREATER_THAN_EQUAL_TO, fromDate));
}
EntityQuery eq = EntityQuery.use(delegator).from(curEntityName).where(conds).orderBy(me.getPkFieldNames());
try {
boolean beganTx = TransactionUtil.begin();
// Don't bother writing the file if there's nothing to put into it
try (EntityListIterator values = eq.queryIterator()) {
GenericValue value = values.next();
if (value != null) {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outdir, curEntityName + ".xml")), "UTF-8")))) {
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.println("<entity-engine-xml>");
do {
value.writeXmlText(writer, "");
numberWritten++;
if (numberWritten % 500 == 0) {
TransactionUtil.commit(beganTx);
beganTx = TransactionUtil.begin();
}
} while ((value = values.next()) != null);
writer.println("</entity-engine-xml>");
} catch (UnsupportedEncodingException | FileNotFoundException e) {
results.add("[" + fileNumber + "] [xxx] Error when writing " + curEntityName + ": " + e);
}
results.add("[" + fileNumber + "] [" + numberWritten + "] " + curEntityName + " wrote " + numberWritten + " records");
} else {
results.add("[" + fileNumber + "] [---] " + curEntityName + " has no records, not writing file");
}
TransactionUtil.commit(beganTx);
} catch (GenericEntityException entityEx) {
results.add("[" + fileNumber + "] [xxx] Error when writing " + curEntityName + ": " + entityEx);
continue;
}
fileNumber++;
} catch (GenericTransactionException e) {
Debug.logError(e, module);
results.add(e.getLocalizedMessage());
}
}
} else {
results.add("Path not found or no write access.");
}
} else {
results.add("No path specified, doing nothing.");
}
// send the notification
Map<String, Object> resp = UtilMisc.<String, Object>toMap("results", results);
return resp;
}
Aggregations