001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     George Lefter
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031/**
032 * This class holds the details for sorting.
033 *
034 * @author <a href='mailto:glefter@nuxeo.com'>George Lefter</a>
035 */
036public class SortInfo implements Serializable {
037
038    private static final long serialVersionUID = -5490026543290755342L;
039
040    private static final Log log = LogFactory.getLog(SortInfo.class);
041
042    public static final String SORT_COLUMN_NAME = "sortColumn";
043
044    public static final String SORT_ASCENDING_NAME = "sortAscending";
045
046    protected String sortColumn;
047
048    protected boolean sortAscending;
049
050    /**
051     * @param sortColumn the column to sort by, in schema:field format
052     * @param sortAscending whether to sort ascending or descending
053     */
054    public SortInfo(String sortColumn, boolean sortAscending) {
055        if (sortColumn == null) {
056            throw new IllegalArgumentException("sortColumn cannot be null");
057        }
058        this.sortColumn = sortColumn;
059        this.sortAscending = sortAscending;
060    }
061
062    public boolean getSortAscending() {
063        return sortAscending;
064    }
065
066    /**
067     * @return the column to sort by, in schema:field format
068     */
069    public String getSortColumn() {
070        return sortColumn;
071    }
072
073    /**
074     * @since 5.4.0
075     */
076    public void setSortColumn(String sortColumn) {
077        this.sortColumn = sortColumn;
078    }
079
080    /**
081     * @since 5.4.0
082     */
083    public void setSortAscending(boolean sortAscending) {
084        this.sortAscending = sortAscending;
085    }
086
087    @Override
088    public boolean equals(Object obj) {
089        if (obj instanceof SortInfo) {
090            SortInfo other = (SortInfo) obj;
091            if (sortColumn != null && sortColumn.equals(other.sortColumn)) {
092                return sortAscending == other.sortAscending;
093            } else if (sortColumn == null && other.sortColumn == null) {
094                return sortAscending == other.sortAscending;
095            }
096            return false;
097        } else {
098            return false;
099        }
100    }
101
102    /**
103     * Returns a map for given sort info, or null if sort info is null.
104     * <p>
105     * The map keys are {@link #SORT_COLUMN_NAME} and {@link #SORT_ASCENDING_NAME}.
106     *
107     * @since 5.4.0
108     */
109    public static Map<String, Serializable> asMap(SortInfo sortInfo) {
110        if (sortInfo == null) {
111            return null;
112        }
113        Map<String, Serializable> res = new HashMap<>();
114        res.put(SORT_COLUMN_NAME, sortInfo.getSortColumn());
115        res.put(SORT_ASCENDING_NAME, Boolean.valueOf(sortInfo.getSortAscending()));
116        return res;
117    }
118
119    /**
120     * Returns a sort info for given map, or null if map is null or does not contain both keys {@link #SORT_COLUMN_NAME}
121     * and {@link #SORT_ASCENDING_NAME}.
122     *
123     * @since 5.4.0
124     */
125    public static SortInfo asSortInfo(Map<String, Serializable> map) {
126        if (map == null) {
127            return null;
128        }
129        if (map.containsKey(SORT_COLUMN_NAME) && map.containsKey(SORT_ASCENDING_NAME)) {
130            return new SortInfo((String) map.get("sortColumn"),
131                    Boolean.parseBoolean(String.valueOf(map.get("sortAscending"))));
132        } else {
133            log.error("Cannot resolve sort info from map: " + map);
134            return null;
135        }
136    }
137
138    @Override
139    public String toString() {
140        return String.format("SortInfo [sortColumn=%s, sortAscending=%s]", sortColumn, Boolean.valueOf(sortAscending));
141    }
142
143}