View Javadoc

1   /**
2    * Copyright 2005-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.rice.kim.impl.common.attribute;
17  
18  import org.kuali.rice.core.api.criteria.CriteriaValue;
19  import org.kuali.rice.core.api.criteria.LookupCustomizer;
20  import org.kuali.rice.core.api.criteria.MultiValuedPredicate;
21  import org.kuali.rice.core.api.criteria.Predicate;
22  import org.kuali.rice.core.api.criteria.PropertyPathPredicate;
23  import org.kuali.rice.core.api.criteria.SingleValuedPredicate;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Set;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
32  
33  /**
34   * this is an internal class used by kim service implementations that have generic lookup methods for classes
35   * with "attributes".
36   */
37  public final class AttributeTransform implements LookupCustomizer.Transform<Predicate, Predicate> {
38  
39      private static final LookupCustomizer.Transform<Predicate, Predicate> INSTANCE = new AttributeTransform();
40      private static final String ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE = "attributeDetails.attributeValue";
41      private static final String ATTRIBUTE_DETAILS_ATTRIBUTE_NAME = "attributeDetails.kimAttribute.attributeName";
42      private static final String ATTRIBUTES_REGEX = "^attributes\\[\\w*\\]$";
43      private static final Pattern ATTRIBUTES_PATTERN = Pattern.compile(ATTRIBUTES_REGEX);
44  
45      private AttributeTransform() {
46  
47      }
48  
49      @Override
50      public Predicate apply(final Predicate input) {
51          if (input instanceof PropertyPathPredicate) {
52              String pp = ((PropertyPathPredicate) input).getPropertyPath();
53              if (isAttributesPredicate(pp)) {
54                  final String attributeName = pp.substring(pp.indexOf('[') + 1, pp.indexOf(']'));
55  
56                  final Predicate attrValue;
57                  if (input instanceof SingleValuedPredicate) {
58                      final CriteriaValue<?> value = ((SingleValuedPredicate) input).getValue();
59                      attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, value.getValue());
60                  } else if (input instanceof MultiValuedPredicate) {
61                      final Set<? extends CriteriaValue<?>> values = ((MultiValuedPredicate) input).getValues();
62                      List<Object> l = new ArrayList<Object>();
63                      for (CriteriaValue<?> v : values) {
64                          l.add(v.getValue());
65                      }
66  
67                      attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE, l.toArray());
68                  } else {
69                      attrValue = dynConstruct(input.getClass().getSimpleName(), ATTRIBUTE_DETAILS_ATTRIBUTE_VALUE);
70                  }
71                  return and(equal(ATTRIBUTE_DETAILS_ATTRIBUTE_NAME, attributeName), attrValue);
72              }
73          }
74  
75          return input;
76      }
77  
78      private boolean isAttributesPredicate(String pp) {
79          Matcher matcher = ATTRIBUTES_PATTERN.matcher(pp);
80          return matcher.matches();
81      }
82  
83      public static LookupCustomizer.Transform<Predicate, Predicate> getInstance() {
84          return INSTANCE;
85      }
86  }