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.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       * This method gets all terms from the <code>factsObject</code>and add it to the KRMS FactsBuilder
61       * @param factsBuilder
62       * @param factsObject
63       * @param contextId
64       * @param factTermNS
65       */
66      public void addObjectMembersAsFacts(Facts.Builder factsBuilder, Object factsObject,String contextId, String factTermNS) {
67          TermRepositoryService termRepositoryService = HrServiceLocator.getService("termRepositoryService");
68          //TermSpecificationDefinition termSpec = termRepositoryService.getTermSpecificationByNameAndNamespace("KPME_Term_SPEC", "KPME_NMSPC");
69          //List<TermSpecificationDefinition> termSpecs = Collections.singletonList(termSpec);
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 }