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.util.Iterator; 027 028import javax.servlet.ServletException; 029import javax.servlet.http.HttpServlet; 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032 033import com.hp.gagawa.java.elements.A; 034import com.hp.gagawa.java.elements.Form; 035import com.hp.gagawa.java.elements.Input; 036import com.hp.gagawa.java.elements.Option; 037import com.hp.gagawa.java.elements.Select; 038 039import dk.netarkivet.common.utils.I18n; 040import dk.netarkivet.harvester.datamodel.HarvestChannel; 041import dk.netarkivet.harvester.datamodel.HarvestChannelDAO; 042import dk.netarkivet.harvester.webinterface.HarvestChannelAction.ActionType; 043 044/** 045 * This class process an Ajax call from the UI to generate a form that allows to map a harvest to a channel. 046 */ 047@SuppressWarnings({"serial"}) 048public class HarvestChannelMappingServlet extends HttpServlet { 049 050 private static final I18n I18N = new I18n(dk.netarkivet.harvester.Constants.TRANSLATIONS_BUNDLE); 051 052 /** 053 * Enumarate HTTP request parameter names. 054 */ 055 public static enum Param { 056 /** 057 * harvest id. 058 */ 059 harvestId, 060 /** 061 * Harvest type. 062 */ 063 snapshot, 064 /** 065 * Current harvest channel name. 066 */ 067 currentChannelId 068 } 069 070 @Override 071 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 072 073 long harvestId = Long.parseLong(req.getParameter(Param.harvestId.name())); 074 boolean snapshot = Boolean.parseBoolean(req.getParameter(Param.snapshot.name())); 075 076 Form selectChannelForm = new Form("./HarvestChannel-edit-harvest-mappings.jsp"); 077 selectChannelForm.setMethod("post"); 078 079 Input hiddenAction = new Input(); 080 hiddenAction.setType("hidden"); 081 hiddenAction.setId(HarvestChannelAction.ACTION); 082 hiddenAction.setName(HarvestChannelAction.ACTION); 083 hiddenAction.setValue(ActionType.mapHarvestToChannel.name()); 084 selectChannelForm.appendChild(hiddenAction); 085 086 Input hiddenHarvestId = new Input(); 087 hiddenHarvestId.setType("hidden"); 088 hiddenHarvestId.setId(HarvestChannelAction.HARVEST_ID); 089 hiddenHarvestId.setName(HarvestChannelAction.HARVEST_ID); 090 hiddenHarvestId.setValue(Long.toString(harvestId)); 091 selectChannelForm.appendChild(hiddenHarvestId); 092 093 Select selectChannel = new Select(); 094 selectChannel.setId(HarvestChannelAction.CHANNEL_ID); 095 selectChannel.setName(HarvestChannelAction.CHANNEL_ID); 096 097 HarvestChannelDAO dao = HarvestChannelDAO.getInstance(); 098 099 HarvestChannel mappedChan = dao.getChannelForHarvestDefinition(harvestId); 100 if (mappedChan == null) { 101 mappedChan = dao.getDefaultChannel(snapshot); 102 } 103 long mappedChanId = mappedChan.getId(); 104 105 Iterator<HarvestChannel> chans = dao.getAll(snapshot); 106 while (chans.hasNext()) { 107 HarvestChannel chan = chans.next(); 108 Option opt = new Option(); 109 long id = chan.getId(); 110 opt.setValue(Long.toString(id)); 111 opt.appendText(chan.getName()); 112 if (id == mappedChanId) { 113 opt.setSelected("selected"); 114 } 115 selectChannel.appendChild(opt); 116 } 117 selectChannelForm.appendChild(selectChannel); 118 119 Input submit = new Input(); 120 submit.setType("submit"); 121 submit.setValue(I18N.getString(resp.getLocale(), "edit.harvest.mappings.dialog.submit")); 122 selectChannelForm.appendChild(submit); 123 124 A cancelLink = new A(); 125 cancelLink.setHref("#"); 126 cancelLink.appendText(I18N.getString(resp.getLocale(), "edit.harvest.mappings.dialog.cancel")); 127 cancelLink.setAttribute("onClick", "onClickCancelEditChannel()"); 128 selectChannelForm.appendChild(cancelLink); 129 130 resp.setContentType("text/html"); 131 resp.setHeader("Cache-Control", "no-cache"); 132 resp.getWriter().write(selectChannelForm.write()); 133 } 134 135}