001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University 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.List;
026
027import dk.netarkivet.common.CommonSettings;
028import dk.netarkivet.common.exceptions.ArgumentNotValid;
029import dk.netarkivet.common.utils.Settings;
030import dk.netarkivet.harvester.datamodel.DomainDAO;
031import dk.netarkivet.harvester.datamodel.DomainHarvestInfo;
032
033/**
034 * Used to manage the model used in the domain harvest history page. See Harveststatus-perdomain.jsp.
035 */
036public class HarvestHistoryTableHelper {
037    public static final String HARVEST_NAME_FIELD = "hdname";
038    public static final String HARVEST_NUMBER_FIELD = "harvest_num";
039    public static final String JOB_ID_FIELD = "job_id";
040    public static final String CONFIGURATION_NAME_FIELD = "configname";
041    public static final String START_TIME_FIELD = "startdate";
042    public static final String STOP_TIME_FIELD = "enddate";
043    public static final String BYTES_HARVESTED_FIELD = "bytecount";
044    public static final String DOCUMENTS_HARVESTED_FIELD = "objectcount";
045    public static final String STOPPED_DUE_TO_FIELD = "stopreason";
046
047    private static final String INC_SORT_ARROW = "&uarr;";
048    private static final String DEC_SORT_ARROW = "&darr;";
049    private static final String NO_SORT_ARROW = "";
050
051    private final String sortField;
052    private final String sortOrder;
053
054    private final String domainName;
055    private final int pageIndex;
056    private final long definedPageSize;
057    private final long currentPageSize;
058    private long startIndex;
059
060    private long endIndex;
061    private List<DomainHarvestInfo> harvestInfoList;
062
063    public HarvestHistoryTableHelper(String domainNameParameter, String sortFieldParameter, String sortOrderParameter,
064            String pageIndexParameter) {
065        ArgumentNotValid.checkNotNull(domainNameParameter, "domainName");
066        domainName = domainNameParameter;
067        if (sortFieldParameter != null) {
068            sortField = sortFieldParameter;
069        } else {
070            sortField = HarvestHistoryTableHelper.START_TIME_FIELD;
071        }
072        if (sortOrderParameter != null) {
073            sortOrder = sortOrderParameter;
074        } else {
075            sortOrder = Constants.SORT_ORDER_ASC;
076        }
077        if (pageIndexParameter != null) {
078            pageIndex = Integer.parseInt(pageIndexParameter);
079        } else {
080            pageIndex = 1;
081        }
082        harvestInfoList = DomainDAO.getInstance().listDomainHarvestInfo(domainName, sortField,
083                sortOrder.equals(Constants.SORT_ORDER_ASC) ? true : false);
084
085        definedPageSize = Settings.getLong(CommonSettings.HARVEST_STATUS_DFT_PAGE_SIZE);
086        currentPageSize = (definedPageSize == 0 ? harvestInfoList.size() : definedPageSize);
087        if (harvestInfoList.size() > 0) {
088            startIndex = ((pageIndex - 1) * currentPageSize);
089            endIndex = Math.min(startIndex + currentPageSize, getNumberOfResults());
090        } else {
091            // Dont's show "Search results: 0, displaying results 1 to 0"
092            // but "Search results: 0, displaying results 0 to 0"
093            startIndex = -1;
094            endIndex = 0;
095        }
096    }
097
098    /**
099     * @return Return the list of DomainHarvestInfos for the current page.
100     */
101    public List<DomainHarvestInfo> listCurrentPageHarvestHistory() {
102        return harvestInfoList.subList((int) startIndex, (int) endIndex);
103    }
104
105    /**
106     * @return the index of the first result on the current page. The result is the full list of
107     * <code>DomainHarvestInfo</code> objects for this domain for the selected sorting.
108     */
109    public long getStartIndex() {
110        return startIndex;
111    }
112
113    /**
114     * @return the index of the last result on the current page. The result is the full list of
115     * <code>DomainHarvestInfo</code> objects for this domain for the selected sorting.
116     */
117    public long getEndIndex() {
118        return endIndex;
119    }
120
121    /**
122     * @return The index of the current page.
123     */
124    public int getPageIndex() {
125        return pageIndex;
126    }
127
128    /**
129     * @return The total number of <code>DomainHarvestInfo</code> objects in the db for this domain.
130     */
131    public long getNumberOfResults() {
132        return harvestInfoList.size();
133    }
134
135    /**
136     * @return <code>true</code> if the next page is available, else <code>false</code>
137     */
138    public boolean isNextPageAvailable() {
139        return HarvestStatus.isNextLinkActive(currentPageSize, getNumberOfResults(), endIndex);
140    }
141
142    /**
143     * @return <code>true</code> if the previous page is available, else <code>false</code>
144     */
145    public boolean isPreviousPageAvailable() {
146        return HarvestStatus.isPreviousLinkActive(currentPageSize, getNumberOfResults(), startIndex);
147    }
148
149    /**
150     * @return A string representing the parameters for the javascripting next/previous link functionality.
151     */
152    public String generateParameterStringForPaging() {
153        return "'" + Constants.DOMAIN_SEARCH_PARAM + "'," + "'" + domainName + "'," + "'" + Constants.SORT_FIELD_PARAM
154                + "'," + "'" + sortField + "'," + "'" + Constants.SORT_ORDER_PARAM + "'," + "'" + sortOrder + "'";
155    }
156
157    /**
158     * Calculates the sort order arrow for the headers of a sortable table
159     *
160     * @param sortField The sort field to find a arrow for.
161     * @return The relevant arrow for the indicated field. Will be the reverse if the sorting is already on this field
162     * else an empty string will be returned
163     */
164    public String getOrderArrow(String sortField) {
165        ArgumentNotValid.checkNotNull(sortField, "sortField");
166        if (sortField.equals(this.sortField)) {
167            return sortOrder.equals(Constants.SORT_ORDER_ASC) ? INC_SORT_ARROW : DEC_SORT_ARROW;
168        }
169        return NO_SORT_ARROW;
170    }
171
172    /**
173     * Calculates the reverse sort order for this file. If the field isn't used for ordering, Constants.SORT_ORDER_ASC
174     * is returned.
175     *
176     * @param sortField The sort field to find a new order for.
177     * @return The relevant asc/desc string.
178     */
179    public String getOrderAfterClick(String sortField) {
180        ArgumentNotValid.checkNotNull(sortField, "sortField");
181        if (sortField.equals(this.sortField)) {
182            return sortOrder.equals(Constants.SORT_ORDER_ASC) ? Constants.SORT_ORDER_DESC : Constants.SORT_ORDER_ASC;
183        }
184        return Constants.SORT_ORDER_ASC;
185    }
186}