package org.eclipse.packagedrone.repo.web.utils;

import static com.google.common.net.UrlEscapers.urlPathSegmentEscaper;

import java.util.Optional;

import org.eclipse.packagedrone.repo.channel.ChannelArtifactInformation;
import org.eclipse.packagedrone.repo.channel.ChannelId;
import org.eclipse.packagedrone.repo.channel.ChannelNotFoundException;
import org.eclipse.packagedrone.repo.channel.ChannelService;
import org.eclipse.packagedrone.repo.channel.ReadableChannel;
import org.eclipse.packagedrone.repo.channel.ChannelService.By;
import org.eclipse.packagedrone.repo.channel.ChannelService.ChannelOperation;
import org.eclipse.packagedrone.web.ModelAndView;
import org.eclipse.packagedrone.web.common.CommonController;

public final class Channels
{
    private Channels ()
    {
    }

    @FunctionalInterface
    public interface ArtifactOperation<T extends ReadableChannel>
    {
        public ModelAndView process ( T target, ChannelArtifactInformation artifact ) throws Exception;
    }

    public static <T> ModelAndView withChannel ( final ChannelService service, final String channelId, final Class<T> clazz, final ChannelOperation<ModelAndView, T> operation )
    {
        try
        {
            return service.accessCall ( By.id ( channelId ), clazz, operation );
        }
        catch ( final ChannelNotFoundException e )
        {
            return CommonController.createNotFound ( "channel", channelId );
        }
    }

    public static <T extends ReadableChannel> ModelAndView withArtifact ( final ChannelService service, final String channelId, final String artifactId, final Class<T> clazz, final ArtifactOperation<T> operation )
    {
        return Channels.withChannel ( service, channelId, clazz, channel -> {

            final Optional<ChannelArtifactInformation> artifact = channel.getArtifact ( artifactId );
            if ( !artifact.isPresent () )
            {
                return CommonController.createNotFound ( "artifact", artifactId );
            }

            return operation.process ( channel, artifact.get () );
        } );
    }

    public static ModelAndView redirectViewArtifact ( final ChannelArtifactInformation artifact )
    {
        return redirectViewArtifact ( artifact.getChannelId ().getId (), artifact.getId () );
    }

    public static ModelAndView redirectViewArtifact ( final String channelId, final String artifactId )
    {
        return new ModelAndView ( "redirect:/channel/" + urlPathSegmentEscaper ().escape ( channelId ) + "/artifacts/" + urlPathSegmentEscaper ().escape ( artifactId ) + "/view" );
    }

    public static ModelAndView redirectViewChannel ( final ChannelId channel )
    {
        return redirectViewChannel ( channel.getId () );
    }

    public static ModelAndView redirectViewChannel ( final String channelId )
    {
        return new ModelAndView ( "redirect:" + channelTarget ( channelId ) );
    }

    public static String channelTarget ( final String channelId )
    {
        return "/channel/" + urlPathSegmentEscaper ().escape ( channelId ) + "/view";
    }
}

@@ -1,12 +1,538 @@
+/*******************************************************************************%0A * Copyright (c) 2015 IBH SYSTEMS GmbH.%0A * All rights reserved. This program and the accompanying materials%0A * are made available under the terms of the Eclipse Public License v1.0%0A * which accompanies this distribution, and is available at%0A * http://www.eclipse.org/legal/epl-v10.html%0A *%0A * Contributors:%0A *     IBH SYSTEMS GmbH - initial API and implementation%0A *******************************************************************************/%0A
 package org.
@@ -934,70 +934,8 @@
 ce;%0A
-import org.eclipse.packagedrone.repo.channel.ReadableChannel;%0A
 impo
@@ -994,16 +994,16 @@
 ice.By;%0A
-
 import o
@@ -1068,24 +1068,86 @@
 lOperation;%0A
+import org.eclipse.packagedrone.repo.channel.ReadableChannel;%0A
 import org.e

add missing license header