001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2018 The Royal Danish Library, 
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.webinterface;
024
025import java.util.HashMap;
026
027import dk.netarkivet.common.exceptions.ArgumentNotValid;
028import dk.netarkivet.common.utils.TableSort;
029
030/**
031 * Class used to manage the sort of tables in the harvest status running screen.
032 * Used only by History/Harveststatus-running.jsp
033 */
034public class HarvestStatusRunningTablesSort {
035    /** list of the column id. */
036    public enum ColumnId {
037        NONE, ID, HOST, PROGRESS, ELAPSED, QFILES, TOTALQ, ACTIVEQ, EXHAUSTEDQ, RETIREDQ
038    }
039
040    ;
041
042    /** map containing the sort data of each table. */
043    private HashMap<String, TableSort> sortData;
044
045    /** Constructor. */
046    public HarvestStatusRunningTablesSort() {
047        sortData = new HashMap<String, TableSort>();
048    }
049
050    /**
051     * return the ColumnId corresponding to the hash code.
052     *
053     * @param columnIdInt the hash code
054     * @return the ColumnId
055     */
056    public final ColumnId getColumnIdByHash(final int columnIdInt) {
057        if (HarvestStatusRunningTablesSort.ColumnId.ID.hashCode() == columnIdInt) {
058            return HarvestStatusRunningTablesSort.ColumnId.ID;
059        } else if (HarvestStatusRunningTablesSort.ColumnId.HOST.hashCode() == columnIdInt) {
060            return HarvestStatusRunningTablesSort.ColumnId.HOST;
061        } else if (HarvestStatusRunningTablesSort.ColumnId.PROGRESS.hashCode() == columnIdInt) {
062            return HarvestStatusRunningTablesSort.ColumnId.PROGRESS;
063        } else if (HarvestStatusRunningTablesSort.ColumnId.ELAPSED.hashCode() == columnIdInt) {
064            return HarvestStatusRunningTablesSort.ColumnId.ELAPSED;
065        } else if (HarvestStatusRunningTablesSort.ColumnId.QFILES.hashCode() == columnIdInt) {
066            return HarvestStatusRunningTablesSort.ColumnId.QFILES;
067        } else if (HarvestStatusRunningTablesSort.ColumnId.TOTALQ.hashCode() == columnIdInt) {
068            return HarvestStatusRunningTablesSort.ColumnId.TOTALQ;
069        } else if (HarvestStatusRunningTablesSort.ColumnId.ACTIVEQ.hashCode() == columnIdInt) {
070            return HarvestStatusRunningTablesSort.ColumnId.ACTIVEQ;
071        } else if (HarvestStatusRunningTablesSort.ColumnId.EXHAUSTEDQ.hashCode() == columnIdInt) {
072            return HarvestStatusRunningTablesSort.ColumnId.EXHAUSTEDQ;
073        } else if (HarvestStatusRunningTablesSort.ColumnId.RETIREDQ.hashCode() == columnIdInt) {
074            return HarvestStatusRunningTablesSort.ColumnId.RETIREDQ;
075        }
076
077        return HarvestStatusRunningTablesSort.ColumnId.NONE;
078    }
079
080    /**
081     * return the ColumnId of the sorted table.
082     *
083     * @param harvestName the harvest name
084     * @return the ColumnId
085     */
086    public final ColumnId getSortedColumnIdentByHarvestName(final String harvestName) {
087        ArgumentNotValid.checkNotNull(harvestName, "harvest name can't be null");
088        TableSort tbs = getTableSort(harvestName);
089        int columnIdInt = tbs.getColumnIdent();
090
091        return getColumnIdByHash(columnIdInt);
092    }
093
094    /**
095     * return the SortOrder of the sorted table.
096     *
097     * @param harvestName the harvest name
098     * @return the SortOrder
099     */
100    public final TableSort.SortOrder getSortOrderByHarvestName(final String harvestName) {
101        ArgumentNotValid.checkNotNull(harvestName, "harvest name can't be null");
102        TableSort.SortOrder order = TableSort.SortOrder.NONE;
103        TableSort tbs = getTableSort(harvestName);
104        order = tbs.getOrder();
105
106        return order;
107    }
108
109    /**
110     * effect of a click on a column.
111     *
112     * @param harvestName the harvest name
113     * @param column ColumnId of the clicked column
114     */
115    public final void sort(final String harvestName, final ColumnId column) {
116        TableSort tbs = getTableSort(harvestName);
117        TableSort.SortOrder order = tbs.getOrder();
118
119        // another column
120        if (tbs.getColumnIdent() != column.hashCode()) {
121            order = TableSort.SortOrder.NONE;
122            tbs.setColumnIdent(column.hashCode());
123        }
124
125        // change order
126        if (order == TableSort.SortOrder.NONE) {
127            order = TableSort.SortOrder.INCR;
128        } else if (order == TableSort.SortOrder.INCR) {
129            order = TableSort.SortOrder.DESC;
130        } else {
131            order = TableSort.SortOrder.NONE;
132        }
133        tbs.setOrder(order);
134    }
135
136    /**
137     * effect of a click on a column.
138     *
139     * @param harvestName the harvest name
140     * @param column hashcode of the ColumnId of the clicked column
141     */
142    public final void sortByHarvestName(final String harvestName, final int column) {
143
144        ColumnId columnId = HarvestStatusRunningTablesSort.ColumnId.NONE;
145        columnId = getColumnIdByHash(column);
146        sort(harvestName, columnId);
147    }
148
149    /**
150     * return the TableSort object describing the sort.
151     *
152     * @param harvestName the harvest name
153     * @return the TableSort
154     */
155    private TableSort getTableSort(final String harvestName) {
156        TableSort tbs = sortData.get(harvestName);
157
158        if (tbs == null) {
159            tbs = new TableSort(HarvestStatusRunningTablesSort.ColumnId.NONE.hashCode(), TableSort.SortOrder.NONE);
160            sortData.put(harvestName, tbs);
161        }
162
163        return tbs;
164    }
165}