1 package org.kuali.student.r2.common.criteria.transform; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 import org.kuali.rice.core.api.criteria.AndPredicate; 7 import org.kuali.rice.core.api.criteria.OrPredicate; 8 9 import org.kuali.rice.core.api.criteria.Predicate; 10 import org.kuali.rice.core.api.criteria.PredicateFactory; 11 import org.kuali.rice.core.api.criteria.PropertyPathPredicate; 12 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; 13 14 public class RenameTransform extends BaseTransform { 15 16 private Map<String, String> renameAttributes; 17 18 public RenameTransform () { 19 } 20 21 public RenameTransform(Map<String, String> renameAttributes) { 22 this (); 23 this.renameAttributes = renameAttributes; 24 } 25 26 27 @Override 28 public Predicate apply(final Predicate input, Criteria criteria) { 29 if (input instanceof AndPredicate) { 30 AndPredicate and = (AndPredicate) input; 31 List<Predicate> andClauses = new ArrayList<Predicate> (); 32 for (Predicate p: and.getPredicates()) { 33 Predicate renamedPredicate = this.apply(p, criteria); 34 andClauses.add(renamedPredicate); 35 } 36 return PredicateFactory.and(andClauses.toArray(new Predicate[andClauses.size()])); 37 } 38 if (input instanceof OrPredicate) { 39 OrPredicate and = (OrPredicate) input; 40 List<Predicate> orClauses = new ArrayList<Predicate> (); 41 for (Predicate p: and.getPredicates()) { 42 Predicate renamedPredicate = this.apply(p, criteria); 43 orClauses.add(renamedPredicate); 44 } 45 return PredicateFactory.or(orClauses.toArray(new Predicate[orClauses.size()])); 46 } 47 if (input instanceof PropertyPathPredicate) { 48 String pp = ((PropertyPathPredicate) input).getPropertyPath(); 49 String newName = this.getRenameProperty(pp); 50 if (newName != null) { 51 return this.createPredicate(input, newName); 52 } 53 } 54 55 return input; 56 } 57 58 public Map<String, String> getRenameAttributes() { 59 return renameAttributes; 60 } 61 62 public void setRenameAttributes(Map<String, String> renameAttributes) { 63 this.renameAttributes = renameAttributes; 64 } 65 66 private String getRenameProperty(String key) { 67 return renameAttributes.get(key); 68 } 69 }