#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::Matcher::Command::Dispatch;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::Assert;
use Common::Debug;

use lib '/app/tools/raptor/lib';
use Raptor::Tracker::Command::Main;

use Raptor::Command;
use base 'Raptor::Command';

sub cmd  { return "dispatch"; }
sub area { return "matcher"; }

use constant kTemplate => '/app/tools/raptor/templates/reports.xsl';

sub execute {
    my ($self) = @_;

    # This command is essentially a 'shim'.
    # The 'old' raptor matcher code was not broken up into logical
    # commands.  I am going to re-organize the code so that each
    # seperate function has a seperate command.
    # However, the templates don't know anything about these commands.
    # So this Dispatch command will catch all incoming requests that don't
    # have a 'c=xxx' parameter (basically, anything coming from an unconverted
    # template), and redirect to the correct Command.

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $paramsString = $self->getParamsAsString();

    debug("in dispatch, paramsString = $paramsString");

    if ( $self->getParam('file') ) {
        $paramsString .= '&c=file';
    } elsif ( $self->getParam('sale') ) {
        if ( 'unmatch' eq $self->getParam('cmd') ) {
            $paramsString .= '&c=unmatch';
        } elsif ( $self->getParam('product') ) {
            $paramsString .= '&c=match';
        } else {
            $paramsString .= '&c=sale';
        }
    } else {

        # Redirect to the main tracker page
        #
        debug("... redirecting back to tracker main page, no valid args found");
        my $command = Raptor::Tracker::Command::Main->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    debug("... about to redirect to /app/matcher?$paramsString");
    $response = RSApache::Response::Redirect->new("/app/matcher?$paramsString");
    return $response;
}

1;
