List of usage examples for org.hibernate ScrollMode FORWARD_ONLY
ScrollMode FORWARD_ONLY
To view the source code for org.hibernate ScrollMode FORWARD_ONLY.
Click Source Link
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private ScrollableResults getCloseInventoryLines(Set<String> orgs) { StringBuffer where = new StringBuffer(); where.append(" as il"); where.append(" where exists (select 1 from " + CostingRuleInit.ENTITY_NAME + " as cri"); where.append(" where cri." + CostingRuleInit.PROPERTY_CLOSEINVENTORY + " = il." + InventoryCountLine.PROPERTY_PHYSINVENTORY + ")"); where.append(" and il." + InventoryCountLine.PROPERTY_ORGANIZATION + ".id IN (:orgs)"); where.append(" order by " + InventoryCountLine.PROPERTY_PRODUCT + ", il." + InventoryCountLine.PROPERTY_BOOKQUANTITY); OBQuery<InventoryCountLine> iclQry = OBDal.getInstance().createQuery(InventoryCountLine.class, where.toString());// www . j ava 2 s . c om iclQry.setNamedParameter("orgs", orgs); iclQry.setFilterOnActive(false); iclQry.setFilterOnReadableClients(false); iclQry.setFilterOnReadableOrganization(false); iclQry.setFetchSize(1000); return iclQry.scroll(ScrollMode.FORWARD_ONLY); }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private ScrollableResults getLegacyCostScroll(String clientId, Set<String> naturalTree) { StringBuffer where = new StringBuffer(); where.append(" as c"); where.append(" where c." + Costing.PROPERTY_CLIENT + ".id = :client"); where.append(" and exists (select 1 from " + MaterialTransaction.ENTITY_NAME + " as trx"); where.append(" where trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)"); where.append(" and trx." + MaterialTransaction.PROPERTY_TRANSACTIONCOST + " is null"); where.append(" and trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " >= c." + Costing.PROPERTY_STARTINGDATE); where.append(" and trx." + MaterialTransaction.PROPERTY_PRODUCT + " = c." + Costing.PROPERTY_PRODUCT); where.append(" and trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " -1 < (c." + Costing.PROPERTY_ENDINGDATE + ") "); where.append(" )"); where.append(" and " + Costing.PROPERTY_COST + " is not null"); where.append(" order by " + Costing.PROPERTY_PRODUCT + ", " + Costing.PROPERTY_STARTINGDATE + ", " + Costing.PROPERTY_ENDINGDATE + " desc"); OBQuery<Costing> costingQry = OBDal.getInstance().createQuery(Costing.class, where.toString()); costingQry.setFilterOnReadableClients(false); costingQry.setFilterOnReadableOrganization(false); costingQry.setFilterOnActive(false); costingQry.setNamedParameter("client", clientId); costingQry.setNamedParameter("orgs", naturalTree); costingQry.setFetchSize(1000);//from w ww. ja va 2s . c o m return costingQry.scroll(ScrollMode.FORWARD_ONLY); }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private void updateTrxLegacyCosts(Costing _cost, int standardPrecision, Set<String> naturalTree) { log4j.debug("****** UpdateTrxLegacyCosts"); Costing cost = OBDal.getInstance().get(Costing.class, _cost.getId()); StringBuffer where = new StringBuffer(); where.append(MaterialTransaction.PROPERTY_PRODUCT + ".id = :product"); where.append(" and " + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)"); where.append(" and " + MaterialTransaction.PROPERTY_MOVEMENTDATE + " >= :dateFrom"); where.append(" and " + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :dateTo"); OBQuery<MaterialTransaction> trxQry = OBDal.getInstance().createQuery(MaterialTransaction.class, where.toString());//from www . j av a2 s .c om trxQry.setFilterOnActive(false); trxQry.setFilterOnReadableClients(false); trxQry.setFilterOnReadableOrganization(false); trxQry.setNamedParameter("product", DalUtil.getId(cost.getProduct())); trxQry.setNamedParameter("orgs", naturalTree); trxQry.setNamedParameter("dateFrom", cost.getStartingDate()); trxQry.setNamedParameter("dateTo", cost.getEndingDate()); trxQry.setFetchSize(1000); ScrollableResults trxs = trxQry.scroll(ScrollMode.FORWARD_ONLY); int i = 0; try { while (trxs.next()) { MaterialTransaction trx = (MaterialTransaction) trxs.get(0); log4j.debug("********** UpdateTrxLegacyCosts process trx:" + trx.getIdentifier()); if (trx.getGoodsShipmentLine() != null && trx.getGoodsShipmentLine().getShipmentReceipt() .getAccountingDate().compareTo(trx.getMovementDate()) != 0) { // Shipments with accounting date different than the movement date gets the cost valid on // the accounting date. BigDecimal unitCost = new BigDecimal( new ProductInfo(cost.getProduct().getId(), new DalConnectionProvider(false)) .getProductItemCost( OBDateUtils.formatDate(trx.getGoodsShipmentLine().getShipmentReceipt() .getAccountingDate()), null, "AV", new DalConnectionProvider(false), OBDal.getInstance().getConnection())); BigDecimal trxCost = unitCost.multiply(trx.getMovementQuantity().abs()) .setScale(standardPrecision, BigDecimal.ROUND_HALF_UP); trx.setTransactionCost(trxCost); } else { trx.setTransactionCost(cost.getCost().multiply(trx.getMovementQuantity().abs()) .setScale(standardPrecision, BigDecimal.ROUND_HALF_UP)); } trx.setCurrency(cost.getCurrency()); trx.setCostCalculated(true); trx.setCostingStatus("CC"); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); cost = OBDal.getInstance().get(Costing.class, cost.getId()); } i++; } } finally { trxs.close(); } log4j.debug("****** UpdateTrxLegacyCosts updated:" + i); }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
/** * Initializes with zero cost those transactions that haven't been calculated by previous methods * because they don't have any cost available. This transactions are checked by the alert rule. * But if this alert is deactivated the process continues forcing to initialize the transactions * with zero cost./* w ww .j a va 2 s . com*/ */ private void updateWithZeroCostRemainingTrx() { log4j.debug("****** updateWithCeroRemainingTrx"); StringBuffer where = new StringBuffer(); where.append(MaterialTransaction.PROPERTY_TRANSACTIONCOST + " is null"); where.append(" order by " + MaterialTransaction.PROPERTY_ORGANIZATION); OBQuery<MaterialTransaction> trxsQry = OBDal.getInstance().createQuery(MaterialTransaction.class, where.toString()); trxsQry.setFilterOnActive(false); trxsQry.setFilterOnReadableClients(false); trxsQry.setFilterOnReadableOrganization(false); trxsQry.setFetchSize(1000); ScrollableResults trxs = trxsQry.scroll(ScrollMode.FORWARD_ONLY); int i = 0; String orgId = ""; String curId = ""; try { while (trxs.next()) { MaterialTransaction trx = (MaterialTransaction) trxs.get(0); if (!orgId.equals(DalUtil.getId(trx.getOrganization()))) { orgId = (String) DalUtil.getId(trx.getOrganization()); Currency cur = FinancialUtils.getLegalEntityCurrency(trx.getOrganization()); curId = cur.getId(); } trx.setTransactionCost(BigDecimal.ZERO); trx.setCurrency((Currency) OBDal.getInstance().getProxy(Currency.ENTITY_NAME, curId)); trx.setCostCalculated(true); trx.setCostingStatus("CC"); OBDal.getInstance().save(trx); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } i++; } } finally { trxs.close(); } log4j.debug("****** updateWithCeroRemainingTrx updated:" + i); }
From source file:org.openbravo.costing.CostingRuleProcess.java
License:Open Source License
private void initializeOldTrx(Set<String> childOrgs, Date date) { StringBuffer where = new StringBuffer(); where.append(" where " + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)"); where.append(" and " + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :date"); OBQuery<MaterialTransaction> trxQry = OBDal.getInstance().createQuery(MaterialTransaction.class, where.toString());//from www .ja v a 2s . c o m trxQry.setFilterOnReadableOrganization(false); trxQry.setNamedParameter("orgs", childOrgs); trxQry.setNamedParameter("date", date); trxQry.setFetchSize(1000); ScrollableResults trxs = trxQry.scroll(ScrollMode.FORWARD_ONLY); int i = 1; try { while (trxs.next()) { MaterialTransaction trx = (MaterialTransaction) trxs.get(0); TransactionCost transactionCost = OBProvider.getInstance().get(TransactionCost.class); transactionCost.setInventoryTransaction(trx); transactionCost.setCostDate(trx.getTransactionProcessDate()); transactionCost.setClient(trx.getClient()); transactionCost.setOrganization(trx.getOrganization()); transactionCost.setCost(BigDecimal.ZERO); transactionCost.setCurrency(trx.getClient().getCurrency()); transactionCost.setAccountingDate(trx.getGoodsShipmentLine() != null ? trx.getGoodsShipmentLine().getShipmentReceipt().getAccountingDate() : trx.getMovementDate()); List<TransactionCost> trxCosts = trx.getTransactionCostList(); trxCosts.add(transactionCost); trx.setTransactionCostList(trxCosts); trx.setCostCalculated(true); trx.setCostingStatus("CC"); trx.setTransactionCost(BigDecimal.ZERO); trx.setCurrency(trx.getClient().getCurrency()); OBDal.getInstance().save(trx); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } i++; } } finally { trxs.close(); } }
From source file:org.openbravo.costing.CostingRuleProcess.java
License:Open Source License
private ScrollableResults getStockLines(Set<String> childOrgs, Date date) { StringBuffer select = new StringBuffer(); select.append("select trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ATTRIBUTESETVALUE + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_UOM + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ORDERUOM + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_STORAGEBIN + ".id"); select.append(", loc." + Locator.PROPERTY_WAREHOUSE + ".id"); select.append(", sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ")"); select.append(", sum(trx." + MaterialTransaction.PROPERTY_ORDERQUANTITY + ")"); select.append(" from " + MaterialTransaction.ENTITY_NAME + " as trx"); select.append(" join trx." + MaterialTransaction.PROPERTY_STORAGEBIN + " as loc"); select.append(" where trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)"); if (date != null) { select.append(" and trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE + " < :date"); }/*w w w .j a va 2 s . c o m*/ select.append(" and trx." + MaterialTransaction.PROPERTY_PRODUCT + ".productType = 'I'"); select.append(" and trx." + MaterialTransaction.PROPERTY_PRODUCT + ".stocked = true"); select.append(" group by trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ATTRIBUTESETVALUE + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_UOM + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ORDERUOM + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_STORAGEBIN + ".id"); select.append(", loc." + Locator.PROPERTY_WAREHOUSE + ".id"); select.append(" having "); select.append(" sum(trx." + MaterialTransaction.PROPERTY_MOVEMENTQUANTITY + ") <> 0"); select.append(" or sum(trx." + MaterialTransaction.PROPERTY_ORDERQUANTITY + ") <> 0"); select.append(" order by loc." + Locator.PROPERTY_WAREHOUSE + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_PRODUCT + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_STORAGEBIN + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ATTRIBUTESETVALUE + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_UOM + ".id"); select.append(", trx." + MaterialTransaction.PROPERTY_ORDERUOM + ".id"); Query stockLinesQry = OBDal.getInstance().getSession().createQuery(select.toString()); stockLinesQry.setParameterList("orgs", childOrgs); if (date != null) { stockLinesQry.setTimestamp("date", date); } stockLinesQry.setFetchSize(1000); ScrollableResults stockLines = stockLinesQry.scroll(ScrollMode.FORWARD_ONLY); return stockLines; }
From source file:org.openbravo.costing.CostingRuleProcess.java
License:Open Source License
private void updateInitInventoriesTrxDate(Date startingDate, String ruleId) { StringBuffer where = new StringBuffer(); where.append(" as trx"); where.append(" join trx." + MaterialTransaction.PROPERTY_PHYSICALINVENTORYLINE + " as il"); where.append(" where il." + InventoryCountLine.PROPERTY_PHYSINVENTORY + ".id IN ("); where.append(" select cri." + CostingRuleInit.PROPERTY_INITINVENTORY + ".id"); where.append(" from " + CostingRuleInit.ENTITY_NAME + " as cri"); where.append(" where cri." + CostingRuleInit.PROPERTY_COSTINGRULE + ".id = :cr"); where.append(" )"); OBQuery<MaterialTransaction> trxQry = OBDal.getInstance().createQuery(MaterialTransaction.class, where.toString());//from www.j a v a2s. com trxQry.setNamedParameter("cr", ruleId); trxQry.setFetchSize(1000); ScrollableResults trxs = trxQry.scroll(ScrollMode.FORWARD_ONLY); int i = 0; while (trxs.next()) { MaterialTransaction trx = (MaterialTransaction) trxs.get(0); trx.setTransactionProcessDate(startingDate); OBDal.getInstance().save(trx); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } } trxs.close(); }
From source file:org.openbravo.costing.CostingServer.java
License:Open Source License
private void checkCostAdjustments() { TrxType trxType = TrxType.getTrxType(transaction); boolean adjustmentAlreadyCreated = false; if (trxType == TrxType.InventoryClosing) { OBDal.getInstance().refresh(transaction.getPhysicalInventoryLine().getPhysInventory()); if (transaction.getPhysicalInventoryLine().getPhysInventory().getCostingRuleInitCloseInventoryList() .size() > 0) {/*from ww w. j a va2 s.c o m*/ // Closing inventories from costing rule process are not automatically adjusted. return; } } boolean checkPriceCorrectionTrxs = false; boolean checkNegativeStockCorrectionTrxs = false; // check if price correction is needed try { checkPriceCorrectionTrxs = Preferences.getPreferenceValue( CostAdjustmentUtils.ENABLE_AUTO_PRICE_CORRECTION_PREF, true, OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext.getOBContext().getRole(), null).equals("Y"); } catch (PropertyException e1) { checkPriceCorrectionTrxs = false; } if (checkPriceCorrectionTrxs && transaction.isCheckpricedifference()) { JSONObject message = PriceDifferenceProcess.processPriceDifferenceTransaction(transaction); if (message.has("documentNo")) { adjustmentAlreadyCreated = true; } } // check if landed cost need to be processed if (trxType == TrxType.Receipt || trxType == TrxType.ReceiptReturn || trxType == TrxType.ReceiptNegative) { StringBuffer where = new StringBuffer(); where.append(" as lc"); where.append(" where not exists "); where.append(" (select 1 from " + MaterialTransaction.ENTITY_NAME + " mtrans"); where.append(" join mtrans." + MaterialTransaction.PROPERTY_GOODSSHIPMENTLINE + " iol"); where.append(" where iol." + ShipmentInOutLine.PROPERTY_SHIPMENTRECEIPT + ".id = :inoutId"); where.append(" and mtrans." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + "= false"); where.append(" )"); where.append(" and lc." + LandedCostCost.PROPERTY_LANDEDCOST + " is null"); where.append(" and lc." + LandedCostCost.PROPERTY_GOODSSHIPMENT + ".id = :inoutId"); OBQuery<LandedCostCost> qry = OBDal.getInstance().createQuery(LandedCostCost.class, where.toString()); qry.setNamedParameter("inoutId", transaction.getGoodsShipmentLine().getShipmentReceipt().getId()); ScrollableResults lcLines = qry.scroll(ScrollMode.FORWARD_ONLY); try { LandedCost landedCost = null; while (lcLines.next()) { if (landedCost == null) { final DocumentType docType = FIN_Utility.getDocumentType(organization, strCategoryLandedCost); final String docNo = FIN_Utility.getDocumentNo(docType, strTableLandedCost); landedCost = OBProvider.getInstance().get(LandedCost.class); landedCost.setReferenceDate(new Date()); landedCost.setDocumentType(docType); landedCost.setDocumentNo(docNo); landedCost.setOrganization(organization); OBDal.getInstance().save(landedCost); LCReceipt lcReceipt = OBProvider.getInstance().get(LCReceipt.class); lcReceipt.setLandedCost(landedCost); lcReceipt.setOrganization(organization); lcReceipt.setGoodsShipment(transaction.getGoodsShipmentLine().getShipmentReceipt()); OBDal.getInstance().save(lcReceipt); } final LandedCostCost landedCostCost = (LandedCostCost) lcLines.get()[0]; landedCostCost.setLandedCost(landedCost); landedCost.getLandedCostCostList().add(landedCostCost); OBDal.getInstance().save(landedCost); OBDal.getInstance().save(landedCostCost); } if (landedCost != null) { OBDal.getInstance().flush(); JSONObject message = LandedCostProcess.doProcessLandedCost(landedCost); if (message.has("documentNo")) { adjustmentAlreadyCreated = true; } if (message.get("severity") != "success") { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingLandedCost@") + ": " + landedCost.getDocumentNo() + " - " + message.getString("text")); } } } catch (JSONException e) { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingLandedCost@")); } finally { lcLines.close(); } } if (getCostingRule().isBackdatedTransactionsFixed() && transaction.getMovementDate().after(CostingUtils.getCostingRuleStartingDate(getCostingRule())) && CostAdjustmentUtils.isNeededBackdatedCostAdjustment(transaction, getCostingRule().isWarehouseDimension(), CostingUtils.getCostingRuleStartingDate(getCostingRule()))) { // BDT= Backdated transaction CostAdjustment costAdjustmentHeader = CostAdjustmentUtils .insertCostAdjustmentHeader(transaction.getOrganization(), "BDT"); CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(transaction, costAdjustmentHeader, null, Boolean.TRUE, transaction.getMovementDate()); cal.setBackdatedTrx(Boolean.TRUE); OBDal.getInstance().save(cal); OBDal.getInstance().flush(); JSONObject message = CostAdjustmentProcess.doProcessCostAdjustment(costAdjustmentHeader); try { if (message.get("severity") != "success") { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingCostAdj@") + ": " + costAdjustmentHeader.getDocumentNo() + " - " + message.getString("text")); } adjustmentAlreadyCreated = true; } catch (JSONException ignore) { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingCostAdj@")); } } // check if negative stock correction should be done try { checkNegativeStockCorrectionTrxs = Preferences.getPreferenceValue( CostAdjustmentUtils.ENABLE_NEGATIVE_STOCK_CORRECTION_PREF, true, OBContext.getOBContext().getCurrentClient(), OBContext.getOBContext().getCurrentOrganization(), OBContext.getOBContext().getUser(), OBContext.getOBContext().getRole(), null).equals("Y"); } catch (PropertyException e1) { checkNegativeStockCorrectionTrxs = false; } boolean modifiesAvg = AverageAlgorithm.modifiesAverage(TrxType.getTrxType(transaction)); BigDecimal currentStock = CostAdjustmentUtils.getStockOnTransactionDate(getOrganization(), transaction, getCostingAlgorithm().costDimensions, transaction.getProduct().isProduction(), costingRule.isBackdatedTransactionsFixed()); // the stock previous to transaction was negative if (checkNegativeStockCorrectionTrxs && currentStock.compareTo(transaction.getMovementQuantity()) < 0 && modifiesAvg && !adjustmentAlreadyCreated) { CostAdjustment costAdjustmentHeader = CostAdjustmentUtils .insertCostAdjustmentHeader(transaction.getOrganization(), "NSC"); // NSC= Negative Stock Correction Date acctDate = transaction.getMovementDate(); CostAdjustmentLine cal = CostAdjustmentUtils.insertCostAdjustmentLine(transaction, costAdjustmentHeader, null, Boolean.TRUE, acctDate); cal.setNegativeStockCorrection(Boolean.TRUE); cal.setUnitCost(Boolean.FALSE); OBDal.getInstance().save(cal); OBDal.getInstance().flush(); JSONObject message = CostAdjustmentProcess.doProcessCostAdjustment(costAdjustmentHeader); try { if (!"success".equals(message.get("severity"))) { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingCostAdj@") + ": " + costAdjustmentHeader.getDocumentNo() + " - " + message.getString("text")); } } catch (JSONException e) { throw new OBException(OBMessageUtils.parseTranslation("@ErrorProcessingCostAdj@")); } } // update trxCost after cost adjustments transaction = OBDal.getInstance().get(MaterialTransaction.class, transaction.getId()); trxCost = CostAdjustmentUtils.getTrxCost(transaction, false, getCostCurrency()); }
From source file:org.openbravo.costing.FixBackdatedTransactionsProcess.java
License:Open Source License
private ScrollableResults getTransactions(Set<String> childOrgs, Date startDate, Date endDate) { StringBuffer select = new StringBuffer(); select.append("select trx as trx"); select.append(" from " + MaterialTransaction.ENTITY_NAME + " as trx"); select.append(" where trx." + MaterialTransaction.PROPERTY_ORGANIZATION + ".id in (:orgs)"); select.append(" and trx." + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true"); select.append(" and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " >= (:startDate)"); if (endDate != null) { select.append(" and trx." + MaterialTransaction.PROPERTY_TRANSACTIONPROCESSDATE + " < (:endDate)"); }/* www. j av a 2 s . co m*/ select.append(" order by trx." + MaterialTransaction.PROPERTY_MOVEMENTDATE); Query stockLinesQry = OBDal.getInstance().getSession().createQuery(select.toString()); stockLinesQry.setParameterList("orgs", childOrgs); stockLinesQry.setTimestamp("startDate", startDate); if (endDate != null) { stockLinesQry.setTimestamp("endDate", endDate); } stockLinesQry.setFetchSize(1000); ScrollableResults stockLines = stockLinesQry.scroll(ScrollMode.FORWARD_ONLY); return stockLines; }
From source file:org.openbravo.costing.InventoryAmountUpdateProcess.java
License:Open Source License
@Override protected JSONObject execute(Map<String, Object> parameters, String data) { JSONObject result = new JSONObject(); JSONObject errorMessage = new JSONObject(); OBContext.setAdminMode(true);/*from w w w . j a va 2 s . c o m*/ try { final JSONObject jsonData = new JSONObject(data); String orgId = jsonData.getString("inpadOrgId"); String invAmtUpdId = jsonData.getString("M_Ca_Inventoryamt_ID"); InventoryAmountUpdate invAmtUpd = OBDal.getInstance().get(InventoryAmountUpdate.class, invAmtUpdId); final OBCriteria<InventoryAmountUpdateLine> qLines = OBDal.getInstance() .createCriteria(InventoryAmountUpdateLine.class); qLines.add(Restrictions.eq(InventoryAmountUpdateLine.PROPERTY_CAINVENTORYAMT, invAmtUpd)); ScrollableResults scrollLines = qLines.scroll(ScrollMode.FORWARD_ONLY); try { int cnt = 0; while (scrollLines.next()) { final InventoryAmountUpdateLine line = (InventoryAmountUpdateLine) scrollLines.get()[0]; String lineId = line.getId(); CostingRule rule = CostingUtils.getCostDimensionRule( OBDal.getInstance().get(Organization.class, orgId), line.getReferenceDate()); String ruleId = rule.getId(); OrganizationStructureProvider osp = OBContext.getOBContext() .getOrganizationStructureProvider(rule.getClient().getId()); final Set<String> childOrgs = osp.getChildTree(rule.getOrganization().getId(), true); if (!rule.isWarehouseDimension()) { createInventories(lineId, null, ruleId, childOrgs, line.getReferenceDate()); } else { createInventories(lineId, line.getWarehouse(), ruleId, childOrgs, line.getReferenceDate()); } if ((cnt++ % 10) == 0) { OBDal.getInstance().flush(); // clear session after each line iteration because the number of objects read in memory // is big OBDal.getInstance().getSession().clear(); } } invAmtUpd = OBDal.getInstance().get(InventoryAmountUpdate.class, invAmtUpdId); invAmtUpd.setProcessed(true); OBDal.getInstance().save(invAmtUpd); OBDal.getInstance().flush(); try { // to ensure that the closed inventory is created before opening inventory Thread.sleep(2000); } catch (InterruptedException e) { log.error("Error waiting between processing close an open inventories", e); } StringBuffer where = new StringBuffer(); where.append(" as inv"); where.append(" where exists ("); where.append(" select 1 from " + InvAmtUpdLnInventories.ENTITY_NAME + " invAmtUpd"); where.append(" where invAmtUpd." + InvAmtUpdLnInventories.PROPERTY_CAINVENTORYAMTLINE + "." + InventoryAmountUpdateLine.PROPERTY_CAINVENTORYAMT + ".id =:invAmtUpdId"); where.append(" and invAmtUpd." + InvAmtUpdLnInventories.PROPERTY_INITINVENTORY + "= inv)"); OBQuery<InventoryCount> qry = OBDal.getInstance().createQuery(InventoryCount.class, where.toString()); qry.setNamedParameter("invAmtUpdId", invAmtUpdId); ScrollableResults invLines = qry.scroll(ScrollMode.FORWARD_ONLY); try { while (invLines.next()) { final InventoryCount inventory = (InventoryCount) invLines.get()[0]; new InventoryCountProcess().processInventory(inventory, false, true); } } finally { invLines.close(); } } finally { scrollLines.close(); } errorMessage.put("severity", "success"); errorMessage.put("text", OBMessageUtils.messageBD("Success")); result.put("message", errorMessage); } catch (Exception e) { OBDal.getInstance().rollbackAndClose(); log.error(e.getMessage(), e); try { Throwable ex = DbUtility.getUnderlyingSQLException(e); String message = OBMessageUtils.translateError(ex.getMessage()).getMessage(); errorMessage = new JSONObject(); errorMessage.put("severity", "error"); errorMessage.put("title", OBMessageUtils.messageBD("Error")); errorMessage.put("text", message); result.put("message", errorMessage); } catch (Exception ignore) { } } finally { OBContext.restorePreviousMode(); } return result; }