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.http.HttpServletRequest;
027import javax.servlet.jsp.PageContext;
028
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
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.HarvestChannel;
037import dk.netarkivet.harvester.datamodel.HarvestChannelDAO;
038import dk.netarkivet.harvester.datamodel.HarvestDefinitionDAO;
039
040/**
041 * Abstract class representing web UI action for harvest channels.
042 */
043public abstract class HarvestChannelAction {
044
045    public static enum ActionType {
046        createHarvestChannel, mapHarvestToChannel
047    }
048
049    public final static String ACTION = "channelAction";
050    public final static String CHANNEL_NAME = "channelName";
051    public final static String HARVEST_ID = "harvestId";
052    public final static String CHANNEL_ID = "channelId";
053    public final static String SNAPSHOT = "channelIsSnapshot";
054    public final static String COMMENTS = "channelComments";
055
056    //private static final Log log = LogFactory.getLog(HarvestChannelAction.class);
057    protected static final Logger log = LoggerFactory.getLogger(HarvestChannelAction.class);
058    /**
059     * This method processes the request to determine which action it corresponds to and passes the request along
060     * accordingly. Available actions are:
061     * <ul>
062     * <li>create harvest channel</li>
063     * <li>map harvest definition to channel</li>
064     * </ul>
065     *
066     * @param context the original servlet context of the request.
067     * @param i18n the internationalisation to be used.
068     * @throws ForwardedToErrorPage if an exception is thrown while carrying out the action.
069     */
070    public static void processRequest(PageContext context, I18n i18n) throws ForwardedToErrorPage {
071        ArgumentNotValid.checkNotNull(context, "PageContext context");
072        ArgumentNotValid.checkNotNull(i18n, "I18n i18n");
073        log.debug("Processing request");
074        HttpServletRequest request = (HttpServletRequest) context.getRequest();
075        try {
076            String action = request.getParameter(ACTION);
077            if (action == null || action.isEmpty()) {
078                return;
079            }
080            switch (ActionType.valueOf(action)) {
081            case createHarvestChannel:
082                String name = request.getParameter(CHANNEL_NAME);
083                HTMLUtils.forwardOnEmptyParameter(context, CHANNEL_NAME);
084                HarvestChannelDAO dao = HarvestChannelDAO.getInstance();
085                dao.create(new HarvestChannel(name, false, false, request.getParameter(COMMENTS)));
086                break;
087            case mapHarvestToChannel:
088                long harvestId = Long.parseLong(request.getParameter(HARVEST_ID));
089                long channelId = Long.parseLong(request.getParameter(CHANNEL_ID));
090                HarvestChannelDAO hcDao = HarvestChannelDAO.getInstance();
091                HarvestDefinitionDAO hdDao = HarvestDefinitionDAO.getInstance();
092                hdDao.mapToHarvestChannel(harvestId, hcDao.getById(channelId));
093                break;
094            default:
095            }
096        } catch (Throwable e) {
097            HTMLUtils.forwardWithErrorMessage(context, i18n, e, "errormsg;harvest.channel.create.error");
098            throw new ForwardedToErrorPage("Error in Harvest Channels", e);
099        }
100    }
101
102}