Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
    <service name="setLastInventoryCount" engine="groovy"
                location="component://product/script/org/ofbiz/product/inventory/InventoryServices.groovy" invoke="setLastInventoryCount">
        <description>Service which run as EECA (on InventoryItemDetail entity) and updates lastInventoryCount for products available in facility in ProductFacility entity</description>
        <attribute name="inventoryItemId" mode="IN" type="String" optional="false"/>
    </service>
...
def setLastInventoryCount() {
    inventoryItem = findOneselect().from('InventoryItem', ).where([inventoryItemId:parameters.inventoryItemId]).queryOne()
    if (!inventoryItem) {
        logWarning("The InventoryItem with inventoryItemId=${parameters.inventoryItemId} doesn't exist.")
        return failure("Inventory item with id ${parameters.inventoryItemId} was not found.")
    }
    List productFacilities = findListselect().from('ProductFacility', ).where([productId:inventoryItem.productId, facilityId:inventoryItem.facilityId]).queryList()
    productFacilities.each {
        countResult = runService('getInventoryAvailableByFacility', [productId:it.productId, facilityId: it.facilityId])
        result = runService('updateProductFacility', [productId:it.productId, facilityId:it.facilityId, lastInventoryCount:countResult.availableToPromiseTotal])
    }
    return success("Updated inventory count for product ${inventoryItem.productId}.")
}

...

Code Block
def updateProductCategoryMember() {
    if (!parameters.thruDate) {
        parameters.thruDate = UtilDateTime.nowTimestamp()
    }
    try {
        productCategoryMember = EntityUtil.getFirstselect(EntityUtil).filterByDatefrom(findList('ProductCategoryMember', ).where([productCategoryId: parameters.productCategoryId, productId: parameters.productId]).filterByDate().queryFirst()
        if (productCategoryMember) {
            productCategoryMember.setString('thruDate', parameters.thruDate)
            productCategoryMember.store()
        }
    } catch(Exception e) {
        return error("The following error occurred setting thruDate on category ${parameters.productCategoryId} for product ${parameters.productId}: ${e.getMessage()}")
    }
    return success()
}

...

  • calling services:

    Code Block
    Map runService(String serviceName, Map inputMap)
    Code Block
    Map run(Map args)
    Info

    The "run" method is implemented using named parameters and supports service calls with the following style:

    Code Block
    result = run service: 'createProduct', with: [productId: 'WG-1111']
  • retrieving data

    Code Block
    GenericValue findOne(String entityName, Map inputMap)
    Code BlockList findList(String entityName, Map inputMap)

    Code Block
    EntityQuery select(...)
    Code Block
    EntityQuery from(...)
    Info

    The "select" and "from" methods can be used in the following ways:

    Code Block
    record = select().from('Product').where(productId, 'WG-1111').queryOne() // selects all the columns of the record
    record = from('Product').where(productId, 'WG-1111').queryOne() // same as above
    record = select('internalName').from('Product').where(productId, 'WG-1111').queryOne() // selects one column of the record

    For more details refer to the Javadocs of EntityQuery.

  • modifying data:

    Code Block
    GenericValue makeValue(String entityName)

    and then call the methods on the GenericValue object (remove/store etc...)

  • logging (they all accept a GString i.e. $notation):

    Code Block
    logInfo(String message)
    Code Block
    logWarning(String message)
    Code Block
    logError(String message)
  • returning from the service or event (the methods simply return a Map for services or a string for events but you still have to use the "return" keyword to return the map back; when used by events the error method adds also the error message, if specified, to the request object):

    Code Block
    def success(String message)
    Code Block
    Map failure(String message)
    Code Block
    def error(String message)

...