001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     George Lefter
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.api;
016
017import java.io.Serializable;
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024/**
025 * This class holds the details for sorting.
026 *
027 * @author <a href='mailto:glefter@nuxeo.com'>George Lefter</a>
028 */
029public class SortInfo implements Serializable {
030
031    private static final long serialVersionUID = -5490026543290755342L;
032
033    private static final Log log = LogFactory.getLog(SortInfo.class);
034
035    public static final String SORT_COLUMN_NAME = "sortColumn";
036
037    public static final String SORT_ASCENDING_NAME = "sortAscending";
038
039    protected String sortColumn;
040
041    protected boolean sortAscending;
042
043    /**
044     * @param sortColumn the column to sort by, in schema:field format
045     * @param sortAscending whether to sort ascending or descending
046     */
047    public SortInfo(String sortColumn, boolean sortAscending) {
048        if (sortColumn == null) {
049            throw new IllegalArgumentException("sortColumn cannot be null");
050        }
051        this.sortColumn = sortColumn;
052        this.sortAscending = sortAscending;
053    }
054
055    public boolean getSortAscending() {
056        return sortAscending;
057    }
058
059    /**
060     * @return the column to sort by, in schema:field format
061     */
062    public String getSortColumn() {
063        return sortColumn;
064    }
065
066    /**
067     * @since 5.4.0
068     */
069    public void setSortColumn(String sortColumn) {
070        this.sortColumn = sortColumn;
071    }
072
073    /**
074     * @since 5.4.0
075     */
076    public void setSortAscending(boolean sortAscending) {
077        this.sortAscending = sortAscending;
078    }
079
080    @Override
081    public boolean equals(Object obj) {
082        if (obj instanceof SortInfo) {
083            SortInfo other = (SortInfo) obj;
084            if (sortColumn != null && sortColumn.equals(other.sortColumn)) {
085                return sortAscending == other.sortAscending;
086            } else if (sortColumn == null && other.sortColumn == null) {
087                return sortAscending == other.sortAscending;
088            }
089            return false;
090        } else {
091            return false;
092        }
093    }
094
095    /**
096     * Returns a map for given sort info, or null if sort info is null.
097     * <p>
098     * The map keys are {@link #SORT_COLUMN_NAME} and {@link #SORT_ASCENDING_NAME}.
099     *
100     * @since 5.4.0
101     */
102    public static Map<String, Serializable> asMap(SortInfo sortInfo) {
103        if (sortInfo == null) {
104            return null;
105        }
106        Map<String, Serializable> res = new HashMap<String, Serializable>();
107        res.put(SORT_COLUMN_NAME, sortInfo.getSortColumn());
108        res.put(SORT_ASCENDING_NAME, Boolean.valueOf(sortInfo.getSortAscending()));
109        return res;
110    }
111
112    /**
113     * Returns a sort info for given map, or null if map is null or does not contain both keys {@link #SORT_COLUMN_NAME}
114     * and {@link #SORT_ASCENDING_NAME}.
115     *
116     * @since 5.4.0
117     */
118    public static SortInfo asSortInfo(Map<String, Serializable> map) {
119        if (map == null) {
120            return null;
121        }
122        if (map.containsKey(SORT_COLUMN_NAME) && map.containsKey(SORT_ASCENDING_NAME)) {
123            return new SortInfo((String) map.get("sortColumn"),
124                    Boolean.parseBoolean(String.valueOf(map.get("sortAscending"))));
125        } else {
126            log.error("Cannot resolve sort info from map: " + map);
127            return null;
128        }
129    }
130
131    @Override
132    public String toString() {
133        return String.format("SortInfo [sortColumn=%s, sortAscending=%s]", sortColumn, Boolean.valueOf(sortAscending));
134    }
135
136}