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.faces.context.FacesContext;
024    import javax.faces.el.ValueBinding;
025    import java.util.Comparator;
026    import java.util.Map;
027    
028    /*
029     * Created: Mon Apr 15 15:56:44 2002
030     */
031    @SuppressWarnings("deprecation")
032    public class ValueBindingComparator extends ComparatorBase {
033    
034      private static final Logger LOG = LoggerFactory.getLogger(ValueBindingComparator.class);
035    
036      private FacesContext facesContext;
037    
038      private String var;
039    
040      private ValueBinding valueBinding;
041    
042      public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding) {
043        this.facesContext = facesContext;
044        this.var = var;
045        this.valueBinding = valueBinding;
046      }
047    
048      public ValueBindingComparator(FacesContext facesContext, String var, ValueBinding valueBinding, boolean reverse) {
049        super(reverse);
050        this.facesContext = facesContext;
051        this.var = var;
052        this.valueBinding = valueBinding;
053      }
054    
055      public ValueBindingComparator(FacesContext facesContext, String var,
056          ValueBinding valueBinding, Comparator comparator) {
057        super(comparator);
058        this.facesContext = facesContext;
059        this.var = var;
060        this.valueBinding = valueBinding;
061      }
062    
063      public ValueBindingComparator(FacesContext facesContext, String var,
064          ValueBinding valueBinding, boolean reverse, Comparator comparator) {
065        super(reverse, comparator);
066        this.facesContext = facesContext;
067        this.var = var;
068        this.valueBinding = valueBinding;
069      }
070    
071      public boolean equals(Object o) {
072        if (this == o) {
073          return true;
074        }
075        if (o == null || getClass() != o.getClass()) {
076          return false;
077        }
078    
079        final ValueBindingComparator that = (ValueBindingComparator) o;
080    
081        if (!super.equals(o)) {
082          return false;
083        }
084        if (facesContext != null ? !facesContext.equals(that.facesContext) : that.facesContext != null) {
085          return false;
086        }
087        if (valueBinding != null ? !valueBinding.equals(that.valueBinding) : that.valueBinding != null) {
088          return false;
089        }
090        if (var != null ? !var.equals(that.var) : that.var != null) {
091          return false;
092        }
093    
094        return true;
095      }
096    
097      public int hashCode() {
098        int result;
099        result = (facesContext != null ? facesContext.hashCode() : 0);
100        result = 29 * result + (var != null ? var.hashCode() : 0);
101        result = 29 * result + (valueBinding != null ? valueBinding.hashCode() : 0);
102        result = 29 * result + super.hashCode();
103        return result;
104      }
105    
106      // implementation of java.util.Comparator interface
107    
108      /**
109       * @param param1 <description>
110       * @param param2 <description>
111       * @return <description>
112       */
113      public int compare(Object param1, Object param2) {
114        Object obj1;
115        Object obj2;
116        try {
117          final Map requestMap = facesContext.getExternalContext().getRequestMap();
118          requestMap.put(var, param1);
119          obj1 = valueBinding.getValue(facesContext);
120          requestMap.put(var, param2);
121          obj2 = valueBinding.getValue(facesContext);
122    
123        } catch (Exception e) {
124          LOG.error(e.getMessage(), e);
125          return 0;
126        }
127        return super.internalCompare(obj1, obj2);
128      }
129    
130    }