001    package org.apache.myfaces.tobago.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    
023    import javax.el.ValueExpression;
024    import javax.faces.context.FacesContext;
025    import java.util.Comparator;
026    import java.util.Map;
027    
028    public class ValueExpressionComparator extends ComparatorBase {
029    
030      private static final Logger LOG = LoggerFactory.getLogger(ValueExpressionComparator.class);
031    
032      private FacesContext facesContext;
033    
034      private String var;
035    
036      private ValueExpression valueExpression;
037    
038      public ValueExpressionComparator(FacesContext facesContext, String var, ValueExpression valueExpression) {
039        this.facesContext = facesContext;
040        this.var = var;
041        this.valueExpression = valueExpression;
042      }
043    
044      public ValueExpressionComparator(FacesContext facesContext, String var, ValueExpression valueExpression,
045          boolean reverse) {
046        super(reverse);
047        this.facesContext = facesContext;
048        this.var = var;
049        this.valueExpression = valueExpression;
050      }
051    
052      public ValueExpressionComparator(FacesContext facesContext, String var,
053          ValueExpression valueExpression, Comparator comparator) {
054        super(comparator);
055        this.facesContext = facesContext;
056        this.var = var;
057        this.valueExpression = valueExpression;
058      }
059    
060      public ValueExpressionComparator(FacesContext facesContext, String var,
061          ValueExpression valueExpression, boolean reverse, Comparator comparator) {
062        super(reverse, comparator);
063        this.facesContext = facesContext;
064        this.var = var;
065        this.valueExpression = valueExpression;
066      }
067    
068      public boolean equals(Object o) {
069        if (this == o) {
070          return true;
071        }
072        if (o == null || getClass() != o.getClass()) {
073          return false;
074        }
075    
076        final ValueExpressionComparator that = (ValueExpressionComparator) o;
077    
078        if (!super.equals(o)) {
079          return false;
080        }
081        if (facesContext != null ? !facesContext.equals(that.facesContext) : that.facesContext != null) {
082          return false;
083        }
084        if (valueExpression != null ? !valueExpression.equals(that.valueExpression) : that.valueExpression != null) {
085          return false;
086        }
087        if (var != null ? !var.equals(that.var) : that.var != null) {
088          return false;
089        }
090    
091        return true;
092      }
093    
094      public int hashCode() {
095        int result;
096        result = (facesContext != null ? facesContext.hashCode() : 0);
097        result = 29 * result + (var != null ? var.hashCode() : 0);
098        result = 29 * result + (valueExpression != null ? valueExpression.hashCode() : 0);
099        result = 29 * result + super.hashCode();
100        return result;
101      }
102    
103      // implementation of java.util.Comparator interface
104    
105      /**
106       * @param param1 <description>
107       * @param param2 <description>
108       * @return <description>
109       */
110      public int compare(Object param1, Object param2) {
111        Object obj1;
112        Object obj2;
113        try {
114          final Map requestMap = facesContext.getExternalContext().getRequestMap();
115          requestMap.put(var, param1);
116          obj1 = valueExpression.getValue(facesContext.getELContext());
117          requestMap.put(var, param2);
118          obj2 = valueExpression.getValue(facesContext.getELContext());
119    
120        } catch (Exception e) {
121          LOG.error(e.getMessage(), e);
122          return 0;
123        }
124        return super.internalCompare(obj1, obj2);
125      }
126    }