Chapter 45. Rules in case management Cases are data-driven, rather than following a sequential flow. The steps required to resolve a case rely on data, which is provided by people involved in the case, or the system can be configured to trigger further actions based on the data available. In the latter case, you can use business rules to decide what further actions are required for the case to continue or reach a resolution. Data can be inserted into the case file at any point during the case. The decision engine constantly monitors case file data, meaning that rules react to data that is contained in the case file. Using rules to monitor and respond to changes in the case file data provides a level of automation that drives cases forward. 45.1. Using rules to drive cases Refer to the case management IT_Orders sample project in Business Central. Suppose that the particular hardware specification provided by the supplier is incorrect or invalid. The supplier needs to provide a new, valid order so that the case can continue. Rather than wait for the manager to reject the invalid specification and create a new request for the supplier, you can create a business rule that will react immediately when the case data indicates that the provided specification is invalid. It can then create a new hardware specification request for the supplier. The following procedure demonstrates how to create and use a business rule to execute this scenario. Prerequisites The IT_Orders sample project is open in Business Central, but it is not deployed to KIE Server. The ServiceRegistry is part of the jbpm-services-api module, and must be available on the class path. Note If building the project outside of Business Central, the following dependencies must be added to the project: org.jbpm:jbpm-services-api org.jbpm:jbpm-case-mgmt-api Procedure Create the following business rule file called validate-document.drl : package defaultPackage; import java.util.Map; import java.util.HashMap; import org.jbpm.casemgmt.api.CaseService; import org.jbpm.casemgmt.api.model.instance.CaseFileInstance; import org.jbpm.document.Document; import org.jbpm.services.api.service.ServiceRegistry; rule "Invalid document name - reupload" when USDcaseData : CaseFileInstance() Document(name == "invalid.pdf") from USDcaseData.getData("hwSpec") then System.out.println("Hardware specification is invalid"); USDcaseData.remove("hwSpec"); update(USDcaseData); CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE); caseService.triggerAdHocFragment(USDcaseData.getCaseId(), "Prepare hardware spec", null); end This business rule detects when a file named invalid.pdf is uploaded to the case file. It then removes the invalid.pdf document and creates a new instance of the Prepare hardware spec user task. Click Deploy to build the IT_Orders project and deploy it to a KIE Server. Note You can also select the Build & Install option to build the project and publish the KJAR file to the configured Maven repository without deploying to a KIE Server. In a development environment, you can click Deploy to deploy the built KJAR file to a KIE Server without stopping any running instances (if applicable), or click Redeploy to deploy the built KJAR file and replace all instances. The time you deploy or redeploy the built KJAR, the deployment unit (KIE container) is automatically updated in the same target KIE Server. In a production environment, the Redeploy option is disabled and you can click Deploy only to deploy the built KJAR file to a new deployment unit (KIE container) on a KIE Server. To configure the KIE Server environment mode, set the org.kie.server.mode system property to org.kie.server.mode=development or org.kie.server.mode=production . To configure the deployment behavior for a corresponding project in Business Central, go to project Settings General Settings Version and toggle the Development Mode option. By default, KIE Server and all new projects in Business Central are in development mode. You cannot deploy a project with Development Mode turned on or with a manually added SNAPSHOT version suffix to a KIE Server that is in production mode. Create a file called invalid.pdf and save it locally. Create a file called valid-spec.pdf and save it locally. In Business Central, go to Menu Projects IT_Orders to open the IT_Orders project. Click Import Asset in the upper-right corner of the page. Upload the validate-document.drl file to the default package ( src/main/resources ) and click Ok . The validate-document.drl rule is shown in the rule editor. Click Save or close to exit the rule editor. Open the Showcase application by either clicking the Apps launcher (if it is installed), or go to http://localhost:8080/rhpam-case-mgmt-showcase/jbpm-cm.html . Click Start Case for the IT_Orders project. In this example, Aimee is the case owner , Katy is the manager , and the supplier group is supplier . Log out of Business Central, and log back in as a user that belongs to the supplier group. Go to Menu Track Task Inbox . Open the Prepare hardware spec task and click Claim . This assigns the task to the logged in user. Click Start and click to locate the invalid.pdf hardware specification file. Click to upload the file. Click Complete . The value in the Task Inbox for the Prepare hardware spec is Ready . In Showcase, click Refresh in the upper-right corner. Notice that a Prepare hardware task message appears in the Completed column and another appears in the In Progress column. This is because the first Prepare hardware spec task has been completed with the specification file invalid.pdf . As a result, the business rule causes the task and file to be discarded, and a new user task created. In the Business Central Task Inbox , repeat the steps to upload the valid-spec.pdf file instead of invalid.pdf .

Commands:
package defaultPackage; import java.util.Map; import java.util.HashMap; import org.jbpm.casemgmt.api.CaseService; import org.jbpm.casemgmt.api.model.instance.CaseFileInstance; import org.jbpm.document.Document; import org.jbpm.services.api.service.ServiceRegistry; rule "Invalid document name - reupload" when USDcaseData : CaseFileInstance() Document(name == "invalid.pdf") from USDcaseData.getData("hwSpec") then System.out.println("Hardware specification is invalid"); USDcaseData.remove("hwSpec"); update(USDcaseData); CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE); caseService.triggerAdHocFragment(USDcaseData.getCaseId(), "Prepare hardware spec", null); end