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 */ 023 024package dk.netarkivet.harvester.webinterface; 025 026import javax.servlet.ServletRequest; 027import javax.servlet.jsp.PageContext; 028 029import dk.netarkivet.common.exceptions.ArgumentNotValid; 030import dk.netarkivet.common.exceptions.ForwardedToErrorPage; 031import dk.netarkivet.common.utils.I18n; 032import dk.netarkivet.common.webinterface.HTMLUtils; 033import dk.netarkivet.harvester.datamodel.Domain; 034import dk.netarkivet.harvester.datamodel.DomainDAO; 035import dk.netarkivet.harvester.datamodel.SeedList; 036 037/** 038 * Contains utility methods for updating seedlists from the GUI. 039 */ 040 041public class DomainSeedsDefinition { 042 /** 043 * Private constructor. No sense in initialising this class. 044 */ 045 private DomainSeedsDefinition() { 046 047 } 048 049 /** 050 * Utility class gathering together data relating to the editing of a seed list. 051 */ 052 public static class UrlInfo { 053 private String urlListName; 054 private String seedList; 055 056 public UrlInfo(String urlListName, String seedList) { 057 this.urlListName = urlListName; 058 this.seedList = seedList; 059 } 060 061 public String getUrlListName() { 062 return urlListName; 063 } 064 065 public String getSeedList() { 066 return seedList; 067 } 068 } 069 070 /** 071 * Extracts information from a servlet request to update seedlists in a domain 072 * <p> 073 * editUrlList: if not null, we are editing, not updating so return 074 * <p> 075 * (urlListName, seedlist) The name of a seedlist and the actual seedlist for a seedlist to be updated. If 076 * urlListName is present and non-empty, seedlist must also be non-empty. 077 * 078 * @param context 079 * @param i18n 080 */ 081 public static void processRequest(PageContext context, I18n i18n) { 082 ArgumentNotValid.checkNotNull(context, "PageContext context"); 083 ArgumentNotValid.checkNotNull(i18n, "I18n i18n"); 084 085 ServletRequest request = context.getRequest(); 086 if (request.getParameter(Constants.UPDATE_PARAM) == null) { 087 return; 088 } 089 090 HTMLUtils.forwardOnEmptyParameter(context, Constants.DOMAIN_PARAM, Constants.URLLIST_NAME_PARAM, 091 Constants.SEED_LIST_PARAMETER); 092 093 String name = request.getParameter(Constants.DOMAIN_PARAM).trim(); 094 String urlListName = request.getParameter(Constants.URLLIST_NAME_PARAM).trim(); 095 String seedList = request.getParameter(Constants.SEED_LIST_PARAMETER).trim(); 096 097 // check the edition number before updating 098 long edition = HTMLUtils.parseOptionalLong(context, Constants.EDITION_PARAM, -1L); 099 100 if (!DomainDAO.getInstance().exists(name)) { 101 HTMLUtils.forwardWithErrorMessage(context, i18n, "errormsg;unknown.domain.0", name); 102 throw new ForwardedToErrorPage("Domain '" + name + "' does not exist"); 103 } 104 105 Domain domain = DomainDAO.getInstance().read(name); 106 107 if (domain.getEdition() != edition) { 108 HTMLUtils.forwardWithRawErrorMessage( 109 context, 110 i18n, 111 "errormsg;domain.definition.changed.0.retry.1", 112 "<br/><a href=\"Definitions-edit-domain.jsp?" + Constants.DOMAIN_PARAM + "=" 113 + HTMLUtils.escapeHtmlValues(HTMLUtils.encode(name)) + "\">", "</a>"); 114 throw new ForwardedToErrorPage("Domain '" + name + "' has changed"); 115 } 116 117 UrlInfo urlInfo = new UrlInfo(urlListName, seedList); 118 String comments = request.getParameter(Constants.COMMENTS_PARAM); 119 updateDomain(domain, urlInfo, comments); 120 } 121 122 /** 123 * Update a domain from given (checked) seedlist data. 124 * 125 * @param domain The domain to update. 126 * @param urlInfo The seedlist to update 127 * @param comments Any comments for this seedlist. 128 */ 129 private static void updateDomain(Domain domain, UrlInfo urlInfo, String comments) { 130 // Update/create seedlist 131 String seedlistName = urlInfo.getUrlListName(); 132 SeedList sl = new SeedList(seedlistName, urlInfo.getSeedList()); 133 if (comments != null) { 134 sl.setComments(comments); 135 } 136 if (domain.hasSeedList(seedlistName)) { 137 domain.updateSeedList(sl); 138 } else { 139 domain.addSeedList(sl); 140 } 141 DomainDAO.getInstance().update(domain); 142 } 143}