View Javadoc

1   /**
2    * Copyright 2005-2013 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.rice.krad.uif.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  
23  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
24  import org.kuali.rice.krad.service.impl.RemoteModuleServiceBase;
25  
26  /**
27   * Mock module service for testing UIF components without back-end interaction.
28   * 
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class MockModuleService extends RemoteModuleServiceBase {
32  
33      private final List<? extends ExternalizableBusinessObject> instances;
34      private final List<Class<?>> lookupable;
35      private final List<Class<?>> inquirable;
36  
37      /**
38       * Create a mock module service for a given set of instances and key properties.
39       * 
40       * @param instances List of mock instances to make available.
41       * @param lookupable List of classes to treat as lookupable.
42       * @param inquirable List of classes to treat as inquirable.
43       */
44      public MockModuleService(List<? extends ExternalizableBusinessObject> instances, List<Class<?>> lookupable,
45              List<Class<?>> inquirable) {
46          this.instances = instances;
47          this.lookupable = lookupable;
48          this.inquirable = inquirable;
49      }
50  
51      /**
52       * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObject(java.lang.Class,
53       *      java.util.Map)
54       */
55      @Override
56      public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass,
57              Map<String, Object> fieldValues) {
58          for (ExternalizableBusinessObject instance : instances) {
59              boolean match = true;
60  
61              fieldLoop: for (Entry<String, Object> fieldValue : fieldValues.entrySet()) {
62                  Object matchValue = ObjectPropertyUtils.getPropertyValue(instance, fieldValue.getKey());
63                  if (matchValue == null ? fieldValue.getValue() != null : !matchValue.equals(fieldValue.getValue())) {
64                      match = false;
65                      break fieldLoop;
66                  }
67              }
68  
69              if (match) {
70                  return businessObjectClass.cast(instance);
71              }
72          }
73          return null;
74      }
75  
76      /**
77       * @see org.kuali.rice.krad.service.ModuleService#getExternalizableBusinessObjectsList(java.lang.Class,
78       *      java.util.Map)
79       */
80      @Override
81      public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(
82              Class<T> businessObjectClass, Map<String, Object> fieldValues) {
83          List<T> rv = new ArrayList<T>();
84          for (ExternalizableBusinessObject instance : instances) {
85              boolean match = true;
86  
87              fieldLoop: for (Entry<String, Object> fieldValue : fieldValues.entrySet()) {
88                  Object matchValue = ObjectPropertyUtils.getPropertyValue(instance, fieldValue.getKey());
89                  if (matchValue == null ? fieldValue.getValue() != null : !matchValue.equals(fieldValue.getValue())) {
90                      match = false;
91                      break fieldLoop;
92                  }
93              }
94  
95              if (match) {
96                  rv.add(businessObjectClass.cast(instance));
97              }
98          }
99          return rv;
100     }
101 
102     /**
103      * @see org.kuali.rice.krad.service.ModuleService#isExternalizableBusinessObjectLookupable(java.lang.Class)
104      */
105     @Override
106     public boolean isExternalizableBusinessObjectLookupable(@SuppressWarnings("rawtypes") Class boClass) {
107         return lookupable.contains(boClass);
108     }
109 
110     /**
111      * @see org.kuali.rice.krad.service.ModuleService#isExternalizableBusinessObjectInquirable(java.lang.Class)
112      */
113     @Override
114     public boolean isExternalizableBusinessObjectInquirable(@SuppressWarnings("rawtypes") Class boClass) {
115         return inquirable.contains(boClass);
116     }
117 
118     boolean isResponsible(Class<?> boClass) {
119         if (isExternalizableBusinessObjectInquirable(boClass) || isExternalizableBusinessObjectLookupable(boClass)) {
120             return true;
121         }
122         
123         for (ExternalizableBusinessObject instance : instances) {
124             if (boClass.isInstance(instance)) {
125                 return true;
126             }
127         }
128         
129         return false;
130     }
131 
132 }