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