1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.krms;
17
18
19 import org.apache.commons.beanutils.PropertyUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.kpme.core.service.HrServiceLocator;
22 import org.kuali.rice.core.api.exception.RiceRuntimeException;
23 import org.kuali.rice.core.api.util.xml.XmlHelper;
24 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
25 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
26 import org.kuali.rice.krms.api.engine.Facts;
27 import org.kuali.rice.krms.api.repository.category.CategoryDefinition;
28 import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
29 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
30 import org.w3c.dom.Document;
31
32 import javax.xml.xpath.XPath;
33 import javax.xml.xpath.XPathConstants;
34 import java.beans.PropertyDescriptor;
35 import java.io.ByteArrayInputStream;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.Collections;
39 import java.util.List;
40
41 public abstract class KpmeKrmsFactBuilderServiceHelper implements KpmeKrmsFactBuilderService {
42 protected final Logger LOG = Logger.getLogger(KpmeKrmsFactBuilderServiceHelper.class);
43
44 protected String getElementValue(String docContent, String xpathExpression) {
45 try {
46 Document document = XmlHelper.trimXml(new ByteArrayInputStream(docContent.getBytes()));
47
48 XPath xpath = XPathHelper.newXPath();
49 String value = (String) xpath.evaluate(xpathExpression, document, XPathConstants.STRING);
50
51 return value;
52
53 } catch (Exception e) {
54 throw new RiceRuntimeException();
55 }
56 }
57
58
59
60
61
62
63
64
65
66 public void addObjectMembersAsFacts(Facts.Builder factsBuilder, Object factsObject,String contextId, String factTermNS) {
67 TermRepositoryService termRepositoryService = HrServiceLocator.getService("termRepositoryService");
68
69
70 List<TermSpecificationDefinition> termSpecs=(List<TermSpecificationDefinition>) termRepositoryService.findAllTermSpecificationsByContextId(contextId);
71 for (TermSpecificationDefinition termSpecificationDefinition : termSpecs) {
72
73 if(isPropertyType(termSpecificationDefinition)){
74 String termNS = termSpecificationDefinition.getNamespace();
75 if(termNS.equals(factTermNS)){
76 String factKey = termSpecificationDefinition.getName();
77 if(factsObject!=null){
78 Class factsClazz = factsObject.getClass();
79 PropertyDescriptor propDescriptor = null;
80 try {
81 propDescriptor = PropertyUtils.getPropertyDescriptor(factsObject, factKey);
82 if(propDescriptor!=null){
83 Object propertyValue = null;
84 Method readMethod = propDescriptor.getReadMethod();
85 if(readMethod!=null){
86 propertyValue = propDescriptor.getReadMethod().invoke(factsObject);
87 }
88 factsBuilder.addFact(factKey, propertyValue);
89 }
90 }catch (IllegalArgumentException e) {
91 LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
92 }catch (IllegalAccessException e) {
93 LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
94 }catch (InvocationTargetException e) {
95 LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
96 }catch (NoSuchMethodException e) {
97 LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
98 }
99 }else{
100 factsBuilder.addFact(factKey, null);
101 }
102 }
103 }
104 }
105 }
106
107 private boolean isPropertyType(TermSpecificationDefinition termSpecificationDefinition) {
108 List<CategoryDefinition> catgories = termSpecificationDefinition.getCategories();
109 for (CategoryDefinition categoryDefinition : catgories) {
110 if(categoryDefinition.getName().equals("Property"))
111 return true;
112 }
113 return false;
114 }
115 }