List of usage examples for org.hibernate ScrollableResults close
void close();
From source file:org.openbravo.costing.CostAdjustmentProcess.java
License:Open Source License
private void initializeLines(CostAdjustment costAdjustment) { // initialize is related transaction adjusted flag to false OBCriteria<CostAdjustmentLine> critLines = OBDal.getInstance().createCriteria(CostAdjustmentLine.class); critLines.add(Restrictions.eq(CostAdjustmentLine.PROPERTY_COSTADJUSTMENT, costAdjustment)); critLines.add(Restrictions.eq(CostAdjustmentLine.PROPERTY_ISRELATEDTRANSACTIONADJUSTED, true)); ScrollableResults lines = critLines.scroll(ScrollMode.FORWARD_ONLY); long count = 1L; try {// w w w . java 2 s . co m while (lines.next()) { CostAdjustmentLine line = (CostAdjustmentLine) lines.get(0); line.setRelatedTransactionAdjusted(false); OBDal.getInstance().save(line); if (count % 1000 == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } count++; } OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } finally { lines.close(); } }
From source file:org.openbravo.costing.CostAdjustmentProcess.java
License:Open Source License
private void generateTransactionCosts(CostAdjustmentLine costAdjustmentLine) { log.debug("Generate transaction costs of line: {}", costAdjustmentLine.getLineNo()); long t1 = System.currentTimeMillis(); OBCriteria<CostAdjustmentLine> critLines = OBDal.getInstance().createCriteria(CostAdjustmentLine.class); Date referenceDate = costAdjustmentLine.getCostAdjustment().getReferenceDate(); critLines.add(Restrictions.or(// w ww. j av a 2 s. c o m Restrictions.eq(CostAdjustmentLine.PROPERTY_PARENTCOSTADJUSTMENTLINE, costAdjustmentLine), Restrictions.eq("id", costAdjustmentLine.getId()))); ScrollableResults lines = critLines.scroll(ScrollMode.FORWARD_ONLY); try { while (lines.next()) { CostAdjustmentLine line = (CostAdjustmentLine) lines.get(0); if (!line.getTransactionCostList().isEmpty()) { continue; } TransactionCost trxCost = OBProvider.getInstance().get(TransactionCost.class); // TODO: Review this // trxCost.setNewOBObject(true); MaterialTransaction trx = line.getInventoryTransaction(); trxCost.setInventoryTransaction(trx); trxCost.setOrganization(trx.getOrganization()); trxCost.setCostDate(referenceDate); trxCost.setCostAdjustmentLine(line); trxCost.setUnitCost(line.isUnitCost()); Date accountingDate = line.getAccountingDate(); if (accountingDate == null) { accountingDate = trx.getMovementDate(); } trxCost.setAccountingDate(accountingDate); BigDecimal convertedAmt = line.getAdjustmentAmount(); if (!line.getCurrency().getId().equals(trx.getCurrency().getId())) { convertedAmt = FinancialUtils.getConvertedAmount(convertedAmt, line.getCurrency(), trx.getCurrency(), accountingDate, trx.getOrganization(), "C"); } trxCost.setCost(convertedAmt); trxCost.setCurrency(trx.getCurrency()); OBDal.getInstance().save(trxCost); OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } } finally { lines.close(); } log.debug("Transaction costs created. Time {}", System.currentTimeMillis() - t1); }
From source file:org.openbravo.costing.CostingAlgorithmAdjustmentImp.java
License:Open Source License
protected void searchReturnShipments(CostAdjustmentLine _costAdjLine) { CostAdjustmentLine costAdjLine;/* w ww . j a va 2 s. com*/ if (_costAdjLine != null) { costAdjLine = _costAdjLine; } else { costAdjLine = getCostAdjLine(); } ShipmentInOutLine inoutline = costAdjLine.getInventoryTransaction().getGoodsShipmentLine(); BigDecimal costAdjAmt = costAdjLine.getAdjustmentAmount().negate(); int precission = getCostCurrency().getStandardPrecision().intValue(); StringBuffer where = new StringBuffer(); where.append(" as trx"); where.append(" join trx." + MaterialTransaction.PROPERTY_GOODSSHIPMENTLINE + " as iol"); where.append(" join iol." + ShipmentInOutLine.PROPERTY_SALESORDERLINE + " as ol"); where.append(" where ol." + OrderLine.PROPERTY_GOODSSHIPMENTLINE + " = :shipment"); OBQuery<MaterialTransaction> qryTrx = OBDal.getInstance().createQuery(MaterialTransaction.class, where.toString()); qryTrx.setFilterOnReadableOrganization(false); qryTrx.setNamedParameter("shipment", inoutline); ScrollableResults trxs = qryTrx.scroll(ScrollMode.FORWARD_ONLY); try { int counter = 0; while (trxs.next()) { counter++; MaterialTransaction trx = (MaterialTransaction) trxs.get()[0]; if (trx.isCostCalculated()) { BigDecimal adjAmt = costAdjAmt.multiply(trx.getMovementQuantity().abs()) .divide(inoutline.getMovementQuantity().abs(), precission, RoundingMode.HALF_UP); insertCostAdjustmentLine(trx, adjAmt, _costAdjLine); } if (counter % 1000 == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } } } finally { trxs.close(); } }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private void updateLegacyCosts() { log4j.debug("UpdateLegacyCosts"); // Reset costs in m_transaction and m_transaction_cost. Query queryDelete = OBDal.getInstance().getSession() .createQuery("delete from " + TransactionCost.ENTITY_NAME); queryDelete.executeUpdate();/* w w w. j a va 2 s . c o m*/ // FIXME: Update should be done with a loop based on scroll. StringBuffer update = new StringBuffer(); update.append("update " + MaterialTransaction.ENTITY_NAME); update.append(" set " + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = false,"); update.append(" " + MaterialTransaction.PROPERTY_TRANSACTIONCOST + " = null"); update.append(" where " + MaterialTransaction.PROPERTY_TRANSACTIONCOST + " <> 0"); update.append(" or " + MaterialTransaction.PROPERTY_ISCOSTCALCULATED + " = true"); Query updateQry = OBDal.getInstance().getSession().createQuery(update.toString()); updateQry.executeUpdate(); OBDal.getInstance().flush(); fixLegacyCostingCurrency(); for (Client client : getClients()) { OrganizationStructureProvider osp = OBContext.getOBContext() .getOrganizationStructureProvider(client.getId()); String clientId = client.getId(); // Reload client entity after session cleared to avoid No Session error. client = OBDal.getInstance().get(Client.class, clientId); Currency clientCur = client.getCurrency(); int stdPrecission = clientCur.getStandardPrecision().intValue(); log4j.debug("** Processing client: " + client.getIdentifier() + " with currency: " + clientCur.getIdentifier()); for (Organization legalEntity : osp.getLegalEntitiesList()) { log4j.debug("** Processing organization: " + legalEntity.getIdentifier()); Set<String> naturalTree = osp.getNaturalTree(legalEntity.getId()); ScrollableResults legacyCosts = getLegacyCostScroll(clientId, naturalTree); int i = 0; try { while (legacyCosts.next()) { Costing cost = (Costing) legacyCosts.get(0); updateTrxLegacyCosts(cost, stdPrecission, naturalTree); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } } } finally { legacyCosts.close(); } SessionHandler.getInstance().commitAndStart(); } } updateWithZeroCostRemainingTrx(); insertTrxCosts(); insertStandardCosts(); }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private void fixLegacyCostingCurrency() { StringBuffer where = new StringBuffer(); where.append(" as c"); where.append(" join c." + Costing.PROPERTY_CLIENT + " as cl"); where.append(" where c." + Costing.PROPERTY_CURRENCY + " <> cl." + Client.PROPERTY_CURRENCY); final OBQuery<Costing> costQry = OBDal.getInstance().createQuery(Costing.class, where.toString()); costQry.setFilterOnActive(false);/*from ww w. java 2 s . c o m*/ costQry.setFilterOnReadableClients(false); costQry.setFilterOnReadableOrganization(false); costQry.setFetchSize(1000); final ScrollableResults costs = costQry.scroll(ScrollMode.FORWARD_ONLY); int i = 0; try { while (costs.next()) { Costing cost = (Costing) costs.get(0); cost.setCurrency(cost.getClient().getCurrency()); OBDal.getInstance().save(cost); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); } i++; } } finally { costs.close(); } }
From source file:org.openbravo.costing.CostingMigrationProcess.java
License:Open Source License
private void calculateCosts(Organization org) { Currency cur = FinancialUtils.getLegalEntityCurrency(org); String curId = cur.getId();// ww w .j av a 2 s . com Set<String> orgs = OBContext.getOBContext().getOrganizationStructureProvider(org.getClient().getId()) .getChildTree(org.getId(), true); String orgId = org.getId(); int costPrecision = cur.getCostingPrecision().intValue(); int stdPrecision = cur.getStandardPrecision().intValue(); CostingRuleProcess crp = new CostingRuleProcess(); // Update cost of inventories and process starting physical inventories. ScrollableResults icls = getCloseInventoryLines(orgs); String productId = ""; BigDecimal totalCost = BigDecimal.ZERO; BigDecimal totalStock = BigDecimal.ZERO; int i = 0; try { while (icls.next()) { InventoryCountLine icl = (InventoryCountLine) icls.get(0); if (!productId.equals(icl.getProduct().getId())) { productId = icl.getProduct().getId(); HashMap<String, BigDecimal> stock = getCurrentValuedStock(productId, curId, orgs, orgId); totalCost = stock.get("cost"); totalStock = stock.get("stock"); } MaterialTransaction trx = crp.getInventoryLineTransaction(icl); trx.setTransactionProcessDate(DateUtils.addSeconds(trx.getTransactionProcessDate(), -1)); trx.setCurrency(OBDal.getInstance().get(Currency.class, curId)); BigDecimal trxCost = BigDecimal.ZERO; if (totalStock.compareTo(BigDecimal.ZERO) != 0) { trxCost = totalCost.multiply(trx.getMovementQuantity().abs()).divide(totalStock, stdPrecision, BigDecimal.ROUND_HALF_UP); } if (trx.getMovementQuantity().compareTo(totalStock) == 0) { // Last transaction adjusts remaining cost amount. trxCost = totalCost; } trx.setTransactionCost(trxCost); trx.setCostCalculated(true); trx.setCostingStatus("CC"); OBDal.getInstance().save(trx); Currency legalEntityCur = FinancialUtils.getLegalEntityCurrency(trx.getOrganization()); BigDecimal cost = BigDecimal.ZERO; if (BigDecimal.ZERO.compareTo(trx.getMovementQuantity()) != 0) { cost = trxCost.divide(trx.getMovementQuantity().abs(), costPrecision, BigDecimal.ROUND_HALF_UP); } if (!legalEntityCur.equals(cur)) { cost = FinancialUtils.getConvertedAmount(cost, cur, legalEntityCur, new Date(), icl.getOrganization(), FinancialUtils.PRECISION_COSTING); } InventoryCountLine initICL = icl.getRelatedInventory(); initICL.setCost(cost); OBDal.getInstance().save(initICL); totalCost = totalCost.subtract(trxCost); // MovementQty is already negative so add to totalStock to decrease it. totalStock = totalStock.add(trx.getMovementQuantity()); if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); cur = OBDal.getInstance().get(Currency.class, curId); } i++; } } finally { icls.close(); } OBDal.getInstance().flush(); insertTrxCosts(); }
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 w w w .j a v a2 s .c o m 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./*from w ww . ja v a 2 s .c o m*/ */ 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 w w w.j a v a 2 s . com 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
protected void createCostingRuleInits(String ruleId, Set<String> childOrgs, Date date) { CostingRule rule = OBDal.getInstance().get(CostingRule.class, ruleId); ScrollableResults stockLines = getStockLines(childOrgs, date); // The key of the Map is the concatenation of orgId and warehouseId Map<String, String> initLines = new HashMap<String, String>(); Map<String, Long> maxLineNumbers = new HashMap<String, Long>(); InventoryCountLine closingInventoryLine = null; InventoryCountLine openInventoryLine = null; int i = 1;/*from www . j a v a 2 s . c o m*/ try { while (stockLines.next()) { Object[] stockLine = stockLines.get(); String productId = (String) stockLine[0]; String attrSetInsId = (String) stockLine[1]; String uomId = (String) stockLine[2]; String orderUOMId = (String) stockLine[3]; String locatorId = (String) stockLine[4]; String warehouseId = (String) stockLine[5]; BigDecimal qty = (BigDecimal) stockLine[6]; BigDecimal orderQty = (BigDecimal) stockLine[7]; String criId = initLines.get(warehouseId); CostingRuleInit cri = null; if (criId == null) { cri = createCostingRuleInitLine(rule, warehouseId, date); initLines.put(warehouseId, cri.getId()); } else { cri = OBDal.getInstance().get(CostingRuleInit.class, criId); } Long lineNo = (maxLineNumbers.get(criId) == null ? 0L : maxLineNumbers.get(criId)) + 10L; maxLineNumbers.put(criId, lineNo); if (BigDecimal.ZERO.compareTo(qty) < 0) { // Do not insert negative values in Inventory lines, instead reverse the Quantity Count // and the Book Quantity. For example: // Instead of CountQty=0 and BookQty=-5 insert CountQty=5 and BookQty=0 // By doing so the difference between both quantities remains the same and no negative // values have been inserted. openInventoryLine = insertInventoryLine(cri.getInitInventory(), productId, attrSetInsId, uomId, orderUOMId, locatorId, qty, BigDecimal.ZERO, orderQty, BigDecimal.ZERO, lineNo, null); insertInventoryLine(cri.getCloseInventory(), productId, attrSetInsId, uomId, orderUOMId, locatorId, BigDecimal.ZERO, qty, BigDecimal.ZERO, orderQty, lineNo, openInventoryLine); } else { openInventoryLine = insertInventoryLine(cri.getInitInventory(), productId, attrSetInsId, uomId, orderUOMId, locatorId, BigDecimal.ZERO, qty.abs(), BigDecimal.ZERO, orderQty == null ? null : orderQty.abs(), lineNo, closingInventoryLine); insertInventoryLine(cri.getCloseInventory(), productId, attrSetInsId, uomId, orderUOMId, locatorId, qty == null ? null : qty.abs(), BigDecimal.ZERO, orderQty == null ? null : orderQty.abs(), BigDecimal.ZERO, lineNo, openInventoryLine); } if ((i % 100) == 0) { OBDal.getInstance().flush(); OBDal.getInstance().getSession().clear(); // Reload rule after clear session. rule = OBDal.getInstance().get(CostingRule.class, ruleId); } i++; } } finally { stockLines.close(); } // Process closing physical inventories. for (CostingRuleInit cri : rule.getCostingRuleInitList()) { new InventoryCountProcess().processInventory(cri.getCloseInventory(), false); } }