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.io.IOException; 026import java.io.PrintWriter; 027 028import javax.servlet.ServletRequest; 029import javax.servlet.http.HttpServletResponse; 030import javax.servlet.jsp.PageContext; 031 032import dk.netarkivet.common.exceptions.ArgumentNotValid; 033import dk.netarkivet.common.exceptions.ForwardedToErrorPage; 034import dk.netarkivet.common.utils.I18n; 035import dk.netarkivet.common.webinterface.HTMLUtils; 036import dk.netarkivet.harvester.datamodel.RunningJobsInfoDAO; 037import dk.netarkivet.harvester.harvesting.frontier.FrontierReportCsvExport; 038import dk.netarkivet.harvester.harvesting.frontier.InMemoryFrontierReport; 039import dk.netarkivet.harvester.harvesting.frontier.TopTotalEnqueuesFilter; 040 041/** 042 * UI query to export the frontier report extract as a CSV file. 043 */ 044public class ExportFrontierReportCsvQuery { 045 046 /** 047 * Defines the UI fields and their default values. 048 */ 049 public static enum UI_FIELD { 050 JOB_ID(""); 051 052 private String defaultValue; 053 054 UI_FIELD(String defaultValue) { 055 this.defaultValue = defaultValue; 056 } 057 058 /** 059 * Extracts the field's value from a servlet request. If the request does not define the paraeter's value, it is 060 * set to the default value. 061 * 062 * @param req a servlet request 063 * @return the field's value 064 */ 065 public String getValue(ServletRequest req) { 066 String value = req.getParameter(name()); 067 if (value == null || value.isEmpty()) { 068 return this.defaultValue; 069 } 070 return value; 071 } 072 073 public Object getDefaultValue() { 074 return defaultValue; 075 } 076 077 } 078 079 private final long jobId; 080 081 public ExportFrontierReportCsvQuery(ServletRequest req) { 082 083 String jobIdStr = UI_FIELD.JOB_ID.getValue(req); 084 ArgumentNotValid.checkNotNullOrEmpty(jobIdStr, UI_FIELD.JOB_ID.name()); 085 086 jobId = Long.parseLong(jobIdStr); 087 } 088 089 /** 090 * Performs the export. 091 * 092 * @param context the page context 093 * @param i18n the internationalization package to use. 094 */ 095 public void doExport(PageContext context, I18n i18n) { 096 097 String filterId = new TopTotalEnqueuesFilter().getFilterId(); 098 099 RunningJobsInfoDAO dao = RunningJobsInfoDAO.getInstance(); 100 InMemoryFrontierReport report = dao.getFrontierReport(jobId, filterId); 101 102 HttpServletResponse resp = (HttpServletResponse) context.getResponse(); 103 resp.setHeader("Content-Type", "text/plain"); 104 resp.setHeader("Content-Disposition", "Attachment; filename=" + filterId + "-" + report.getJobName() + ".csv"); 105 106 PrintWriter pw; 107 try { 108 pw = new PrintWriter(resp.getOutputStream()); 109 } catch (IOException e) { 110 HTMLUtils.forwardWithErrorMessage(context, i18n, e, "errorMsg;running.job.details.frontier.exportAsCsv"); 111 throw new ForwardedToErrorPage("Error in frontier report CSV export", e); 112 } 113 114 FrontierReportCsvExport.outputAsCsv(report, pw, ";"); 115 pw.close(); 116 } 117 118}