#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

# This file is a part of VHFFS4 Hosting Platform
# Please respect the licence of this file and the whole software

use strict;
use utf8;

package Vhffs::Services::Mercurial;

use base qw(Vhffs::Object);
use Vhffs::Group;
use DBI;

sub check_name($) {
    my $name = shift;
    return ($name =~ /^[a-z0-9]+\/[a-z0-9]{3,64}$/);
}

sub delete
{
    my $self = shift;
    my $query;
    my $request;

    $query = "DELETE FROM vhffs_mercurial WHERE object_id='".$self->{'object_id'}."'";
    $request = $self->{'db'}->prepare($query);
    $request->execute or return -1;

    return -1 if( $self->SUPER::delete < 0 );
}

sub create
{
   my ($main, $rname, $description, $user, $group) = @_;

    return undef unless(defined($user) && defined($group));
    return undef unless(check_name($rname));

    my $mercurial;

    my $dbh = $main->get_db_object();
    local $dbh->{RaiseError} = 1;
    local $dbh->{PrintError} = 0;
    $dbh->begin_work;

    eval {
        my $parent = Vhffs::Object::create($main, $user->get_uid, $group->get_gid, $description, undef, Vhffs::Constants::TYPE_MERCURIAL);

        die('Unable to create parent object') unless(defined $parent);

        my $sql = 'INSERT INTO vhffs_mercurial(reponame, public, ml_name, object_id) VALUES(?, 1, \'\', ?)';
        my $sth = $dbh->prepare($sql);
        $sth->execute($rname, $parent->get_oid) or return undef;

        $dbh->commit;
        $mercurial = get_by_reponame($main, $rname);
    };

    if($@) {
        warn "Unable to create mercurial repository $rname: $@\n";
        $dbh->rollback;
    }

    return $mercurial;
}

sub get_by_reponame($$) {
    my ($main, $reponame) = @_;
    my @params;

    my $sql = 'SELECT s.mercurial_id, s.reponame, o.owner_uid, o.owner_gid, s.public, s.ml_name, o.object_id, o.date_creation, o.description, o.state FROM vhffs_mercurial s INNER JOIN vhffs_object o ON o.object_id = s.object_id WHERE s.reponame = ?';

    my $dbh = $main->get_db_object();

    return undef unless(@params = $dbh->selectrow_array($sql, undef, $reponame));

    return _new Vhffs::Services::Mercurial($main, @params);

}

=head2 fill_object

See C<Vhffs::Object::fill_object>.

=cut

sub fill_object {
    my ($class, $obj) = @_;
    my $sql = q{SELECT mercurial_id, reponame, public, ml_name FROM vhffs_mercurial
        WHERE object_id = ?};
    return $class->SUPER::_fill_object($obj, $sql);
}

sub _new
{
    my ($class, $main, $mercurial_id, $reponame, $owner_uid, $owner_gid, $public, $ml_name, $oid, $date_creation, $description, $state) = @_;

    my $self = $class->SUPER::_new($main, $oid, $owner_uid, $owner_gid, $date_creation, $description, '', $state, Vhffs::Constants::TYPE_MERCURIAL);
    return undef unless(defined $self);

    $self->{mercurial_id} = $mercurial_id;
    $self->{reponame} = $reponame;
    $self->{public} = $public;
    $self->{ml_name} = $ml_name;

    return $self;
}

sub commit
{
    my $self = shift;

    my $dbh = $self->get_db_object;
    my $sql = 'UPDATE vhffs_mercurial SET public = ?, ml_name = ? WHERE mercurial_id = ?';
    $dbh->do($sql, undef, $self->{public}, $self->{ml_name}, $self->{mercurial_id});

    $self->SUPER::commit;
}

sub set_public
{
    my $self = shift;
    $self->{'public'} = 1;
}

sub set_private
{
    my $self = shift;
    $self->{'public'} = 0;
}

sub set_ml_name {
    my ($self, $ml_name) = @_;
    return -1 unless(Vhffs::Functions::valid_mail($ml_name) || $ml_name =~ /^\s*$/);
    $self->{ml_name} = $ml_name;
}

sub is_public
{
    my $self = shift;
    
    return -1 if( ! defined $self->{'public'} );

    if( $self->{'public'} == 1 ) {
        return 1;
    } else {
        return 0;
    }
}

sub get_ml_name {
    my $self = shift;
    return $self->{ml_name};
}

sub get_reponame
{
    my $self = shift;
    return $self->{'reponame'};
}

sub get_label
{
    my $self = shift;
    return $self->{reponame};
}

sub getall
{
    my ($vhffs, $state, $name, $group) = @_;

    my $mercurial = [];
    my @params;

    my $sql = 'SELECT s.mercurial_id, s.reponame, o.owner_uid, o.owner_gid, s.public, s.ml_name, o.object_id, o.date_creation, o.description, o.state FROM vhffs_mercurial s, vhffs_object o WHERE o.object_id = s.object_id';
    

    if(defined $state) {
        $sql .= ' AND o.state = ?';
        push(@params, $state);
    }
    if(defined $name) {
        $sql .= ' AND s.name = ?';
        push(@params, $name);
    }
    if(defined($group)) {
        $sql .= ' AND o.owner_gid = ?';
        push(@params, $group->get_gid);
    }
    $sql .= ' ORDER BY s.reponame';

    my $dbh = $vhffs->get_db_object();

    my $sth = $dbh->prepare($sql);
    $sth->execute(@params) or die($sql) ; #return undef;

    while(my $s = $sth->fetchrow_arrayref()) {
        push(@$mercurial, _new Vhffs::Services::Mercurial($vhffs, @$s));
    }
    return $mercurial;
}

sub getall_by_group
{
    my ($vhffs, $group) = @_;

    my $mercurial = [];
    my @params;

    return undef unless( defined $group );

    my $sql = 'SELECT s.mercurial_id, s.reponame, o.owner_uid, o.owner_gid, s.public, o.object_id, o.date_creation, o.description, o.state FROM vhffs_mercurial s INNER
 JOIN vhffs_object o ON o.object_id = s.object_id WHERE o.owner_gid = ? ORDER BY s.reponame';

    my $dbh = $vhffs->get_db_object();

    my $sth = $dbh->prepare($sql);
    $sth->execute($group->get_gid) or return undef;

    while(my $s = $sth->fetchrow_arrayref()) {
        push(@$mercurial, _new Vhffs::Services::Mercurial($vhffs, @$s));
    }
    return $mercurial;
}

sub get_dir
{
    my $self = shift;
    return undef if( ! defined $self );
    
    return( $self->{'main'}->get_config->get_datadir . "/mercurial/mercurialroot/" . $self->get_reponame );
}

1;
