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;
026import java.util.Set;
027import java.util.TreeSet;
028
029import javax.servlet.ServletRequest;
030
031import dk.netarkivet.common.exceptions.UnknownID;
032import dk.netarkivet.harvester.datamodel.DomainDAO;
033import dk.netarkivet.harvester.datamodel.Job;
034import dk.netarkivet.harvester.datamodel.JobDAO;
035import dk.netarkivet.harvester.datamodel.JobStatus;
036import dk.netarkivet.harvester.datamodel.JobStatusInfo;
037
038/**
039 * Represents a query for job IDs that would be set to harvest a given domain.
040 */
041public class FindRunningJobQuery {
042
043    /**
044     * Defines the UI fields and their default values.
045     */
046    public static enum UI_FIELD {
047        DOMAIN_NAME("");
048
049        private String defaultValue;
050
051        UI_FIELD(String defaultValue) {
052            this.defaultValue = defaultValue;
053        }
054
055        /**
056         * Extracts the field's value from a servlet request. If the request does not define the paraeter's value, it is
057         * set to the default value.
058         *
059         * @param req a servlet request
060         * @return the field's value
061         */
062        public String getValue(ServletRequest req) {
063            String value = req.getParameter(name());
064            if (value == null || value.isEmpty()) {
065                return this.defaultValue;
066            }
067            return value;
068        }
069
070        public Object getDefaultValue() {
071            return defaultValue;
072        }
073
074    }
075
076    /**
077     * The domain name being searched for.
078     */
079    private String domainName;
080
081    /**
082     * The currently running job harvesting the given domain.
083     */
084    private Set<Long> runningJobIds = new TreeSet<Long>();
085
086    /**
087     * Builds a request to find a running job. UI fileds values will be extracted from the given {@link ServletRequest}.
088     *
089     * @param req the {@link ServletRequest} to parse.
090     */
091    public FindRunningJobQuery(ServletRequest req) {
092        domainName = UI_FIELD.DOMAIN_NAME.getValue(req);
093
094        if (domainName == null || domainName.isEmpty()) {
095            return;
096        }
097
098        if (!DomainDAO.getInstance().exists(domainName)) {
099            throw new UnknownID("Domain " + domainName + " is not registered!");
100        }
101
102        List<JobStatusInfo> startedJobs = JobDAO.getInstance().getStatusInfo(JobStatus.STARTED);
103        for (JobStatusInfo jsi : startedJobs) {
104            long jobId = jsi.getJobID();
105            Job job = JobDAO.getInstance().read(jobId);
106            Set<String> domains = job.getDomainConfigurationMap().keySet();
107            if (domains.contains(domainName)) {
108                runningJobIds.add(jobId);
109            }
110        }
111    }
112
113    /**
114     * @return the domain name to search for.
115     */
116    public String getDomainName() {
117        return domainName;
118    }
119
120    /**
121     * @return the IDs of the currently running jobs whose configurations include the given domain.
122     */
123    public Long[] getRunningJobIds() {
124        return (Long[]) runningJobIds.toArray(new Long[runningJobIds.size()]);
125    }
126
127}