Coverage Report - org.kuali.rice.kew.rule.GenericWorkflowAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericWorkflowAttribute
0%
0/40
0%
0/16
2.3
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.rule;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import javax.xml.xpath.XPathExpressionException;
 25  
 
 26  
 import org.apache.commons.lang.ObjectUtils;
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.kew.routeheader.DocumentContent;
 29  
 
 30  
 
 31  
 /**
 32  
  * Generic base class that implements common functionality to simplify implementing
 33  
  * a WorkflowAttribute.  This includes simplified template methods, as well as a generic
 34  
  * attribute content model.
 35  
  * 
 36  
  * <p>Control flow (for isMatch):</p>
 37  
  * 
 38  
  * <ol>
 39  
  *   <li>{@link #isMatch(DocumentContent, List)}
 40  
  *     <ol>
 41  
  *       <li>{@link #isMatch(List, List)}
 42  
  *         <ol>
 43  
  *           <li>{@link #isMatch(Map, List)}</li>
 44  
  *         </ol>
 45  
  *       </li>
 46  
  *     </ol>
 47  
  *   </li>
 48  
  * </ol>
 49  
  * 
 50  
  * The default matching algorithm will match:
 51  
  * <blockquote><i>if any single attribute's properties are a match for all rule extension values</i></blockquote>
 52  
  * This implementation does not (yet!) implement a generic internal map of properties, so it is up to subclasses
 53  
  * to expose specific named getters/setters to set data on an attribute of this ancestry.
 54  
  * 
 55  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 56  
  */
 57  
 public abstract class GenericWorkflowAttribute extends AbstractWorkflowAttribute {
 58  0
     protected final Logger log = Logger.getLogger(getClass());
 59  
     protected final String attributeName;
 60  
     protected final GenericAttributeContent content;
 61  
     
 62  
     public GenericWorkflowAttribute() {
 63  0
         this(null); // can't do getClass().getName() so we'll have to pass null...shame
 64  0
     }
 65  
 
 66  0
     public GenericWorkflowAttribute(String uniqueName) {
 67  0
         if (uniqueName != null) {
 68  0
             this.attributeName = uniqueName;
 69  
         } else {
 70  0
             this.attributeName = getClass().getName();
 71  
         }
 72  0
         content = new GenericAttributeContent(attributeName);
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Template method for subclasses to override to expose attribute state
 77  
      * @return map exposing attribute state
 78  
      */
 79  
     public abstract Map<String, String> getProperties();
 80  
 
 81  
     /**
 82  
      * Simply defers to GenericAttributeContent to generate suitable XML content in a standard fashion
 83  
      */
 84  
     public String getDocContent() {
 85  0
         String dc = content.generateContent(getProperties());
 86  
         //log.info("Generating doc content: " + dc, new Exception("Dummy exception"));
 87  0
         return dc;
 88  
     }
 89  
 
 90  
     public boolean isMatch(DocumentContent docContent, List<RuleExtension> ruleExtensions) {
 91  0
         log.info("isMatch: " + docContent + " " + ruleExtensions);
 92  
         try {
 93  
             // could be multiple attributes on the incoming doc content!
 94  0
             List<Map<String, String>> propertiesList = content.parseContent(docContent.getAttributeContent());
 95  
             
 96  0
             return isMatch(propertiesList, ruleExtensions);
 97  0
         } catch (XPathExpressionException xpee) {
 98  0
             String message = "Error parsing attribute '" + attributeName + "' content: " + docContent.getDocContent();
 99  0
             log.error(message, xpee);
 100  0
             throw new RuntimeException(xpee);
 101  
         }
 102  
     }
 103  
 
 104  
     /**
 105  
      * Returns true if any single incoming attribute's properties are a match for all rule extension values
 106  
      * @param propertiesList the list of incoming attributes' properties
 107  
      * @param ruleExtensions the rule extensions
 108  
      * @return true if any single attribute's properties are a match for all rule extension values
 109  
      */
 110  
     protected boolean isMatch(List<Map<String, String>> propertiesList, List<RuleExtension> ruleExtensions) {
 111  0
         for (Map<String, String> properties: propertiesList) {
 112  0
             return isMatch(properties, ruleExtensions);
 113  
         }
 114  0
         return false;
 115  
     }
 116  
 
 117  
     /**
 118  
      * Returns true if all key/value pairs defined by the specified rule extensions are present in the incoming attribute's
 119  
      * properties
 120  
      * @param properties incoming attribute's properties
 121  
      * @param ruleExtensions list of rule extensions
 122  
      * @return true if all key/value pairs defined by the specified rule extensions are present in the incoming attribute's
 123  
      */
 124  
     protected boolean isMatch(Map<String, String> properties, List<RuleExtension> ruleExtensions) {
 125  0
         for (RuleExtension ruleExtension: ruleExtensions) {
 126  0
             for (RuleExtensionValue ruleExtensionValue: ruleExtension.getExtensionValues()) {
 127  0
                 if (!ObjectUtils.equals(ruleExtensionValue.getValue(), properties.get(ruleExtensionValue.getKey()))) {
 128  0
                     return false;
 129  
                 }
 130  
             }
 131  
         }
 132  0
         return true;
 133  
     }
 134  
 
 135  
     /**
 136  
      * These guys should probably be implemented to set the parameters on an internal member property map this attribute
 137  
      * should use to contain all properties set on it, like StandardGenericXmlAttribute.
 138  
      * @see #getProperties()
 139  
      * TODO: implement me!
 140  
      */
 141  
     public List validateRoutingData(Map paramMap) {
 142  0
         return Collections.EMPTY_LIST;
 143  
     }
 144  
     public List validateRuleData(Map paramMap) {
 145  0
         return Collections.EMPTY_LIST;
 146  
     }
 147  
 
 148  
     //public List validateClientRouting....
 149  
 
 150  
     /**
 151  
      * I think the job of this method is to marshal the current state of the attribute into a representative list of rule extension
 152  
      * values.  On that assumption, this method should simply create a list of RuleExtensionValues based on the the property map
 153  
      * this attribute uses to hold property values.
 154  
      * 
 155  
      * TODO: this is not fully implemented! e.g. generic property map like StandardGenericXmlAttribute
 156  
      */
 157  
     public List<RuleExtensionValue> getRuleExtensionValues() {
 158  0
         log.info("getRuleExtensionValues");
 159  0
         List<RuleExtensionValue> exts = new ArrayList<RuleExtensionValue>();
 160  0
         Map<String, String> props = getProperties();
 161  0
         if (props != null) {
 162  0
             for (Map.Entry<String, String> entry: props.entrySet()) {
 163  0
                 if (entry.getValue() != null) {
 164  0
                     RuleExtensionValue ruleVal = new RuleExtensionValue();
 165  0
                     ruleVal.setKey(entry.getKey());
 166  0
                     ruleVal.setValue(entry.getValue());
 167  0
                     exts.add(ruleVal);
 168  0
                 }
 169  
             }
 170  
         }
 171  0
         return exts;
 172  
     }
 173  
 }