View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.tklm.leave.workflow.krms;
17  
18  
19  import org.kuali.kpme.core.KPMENamespace;
20  import org.kuali.kpme.core.api.assignment.Assignable;
21  import org.kuali.kpme.core.krms.KPMERulesEngineExecuter;
22  import org.kuali.kpme.core.krms.KpmeKrmsFactBuilderService;
23  import org.kuali.kpme.core.service.HrServiceLocator;
24  import org.kuali.kpme.tklm.common.krms.TklmKrmsConstants;
25  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
26  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
27  import org.kuali.rice.kew.api.exception.WorkflowException;
28  import org.kuali.rice.kew.engine.RouteContext;
29  import org.kuali.rice.krad.document.Document;
30  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
33  import org.kuali.rice.krms.api.engine.Engine;
34  import org.kuali.rice.krms.api.engine.EngineResults;
35  import org.kuali.rice.krms.api.engine.Facts;
36  import org.kuali.rice.krms.api.engine.SelectionCriteria;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  public class LeaveMaintDocRulesEngineExecutor extends KPMERulesEngineExecuter {
42  
43      //assumes document's dataobject being routed implements AssignedToAssignment
44      @Override
45      protected EngineResults performExecute(RouteContext routeContext, Engine engine) {
46          Map<String, String> contextQualifiers = new HashMap<String, String>();
47          contextQualifiers.put("namespaceCode", KPMENamespace.KPME_LM.getNamespaceCode());
48          contextQualifiers.put("name", TklmKrmsConstants.Context.LM_CONTEXT_NAME);
49  
50          // extract facts from routeContext
51          String docContent = routeContext.getDocument().getDocContent();
52  
53          SelectionCriteria selectionCriteria = SelectionCriteria.createCriteria(null, contextQualifiers,
54                  new HashMap<String, String>());
55  
56          //Get timesheet document to create facts
57          String documentId = routeContext.getDocument().getDocumentId();
58          Object dataObject = null;
59          MaintenanceDocument document = null;
60          try {
61              document = (MaintenanceDocument) KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
62              if (document.getNewMaintainableObject().getDataObject() != null
63                      && document.getNewMaintainableObject().getDataObject() instanceof Assignable) {
64                  dataObject = document.getNewMaintainableObject().getDataObject();
65              }
66          } catch (WorkflowException e) {
67              e.printStackTrace();
68          }
69  
70          KpmeKrmsFactBuilderService fbService = HrServiceLocator.getService("lmFactBuilderService");
71          Facts.Builder factsBuilder = Facts.Builder.create();
72          //factsBuilder.addFact()
73          fbService.addFacts(factsBuilder, dataObject, TklmKrmsConstants.Context.LM_CONTEXT_NAME, KPMENamespace.KPME_LM.getNamespaceCode());
74          EngineResults results = engine.execute(selectionCriteria, factsBuilder.build(), null);
75          return results;
76      }
77  }