use strict;
use diagnostics;
return 1;

=head1 Name

OSSTag - A Social Tagging Engine

Copyright (c) 2006 Open Source Development Labs, Inc.

=cut

package osstag_dbmapper;
use osstag_dbconnection;
use osstag_obj_phymap;
use osstag_obj_semmap;
use osstag_obj_assoziations;

sub new($)
{
	my $class = shift;
	my $self = {};
	bless $self, $class;

	my ($dbh) = @_;
	$self->{dbc} = new osstag_dbconnection($dbh);

	# pre-cache aspects
	my @result = $self->{dbc}->getAspects();
	foreach my $row (@result)
	{	my $aid = ${$row}[0];
		my $aspect = ${$row}[1];
		$self->{aspects_by_id}->{$aid} = $aspect;
		$self->{aspects_by_aspect}->{$aspect} = $aid;
		push @{$self->{aspects}}, $aspect;
	}

	return $self;
}

=head1 Functions

=head2 createPhymapByPhyPointer

=cut

sub createPhymapForPhy_pointer($)
{
	my $self = shift;
	my ($project, $phy_pointer) = @_;

	my @result = $self->{dbc}->selectPhymapByPhypointer($project, $phy_pointer);

	if( @result != 1)
	{
		return -1;
	}
	# assumes that always only *one* phymap per phypointer exists

	my $phymap = $self->createPhymapFromDatabaseRow($result[0]);

	$self->createSemmapForPhymap($phymap);

	return $phymap;
}

sub createPhymapsForProject($)
{
	my $self = shift;
	my ($project) = @_;

	my @result = $self->{dbc}->selectPhymapsByProject($project);

	my @phymaps;
	foreach my $phymaprow (@result)
	{
		my $phymap = $self->createPhymapFromDatabaseRow($phymaprow);
		$self->createSemmapForPhymap($phymap);
		push @phymaps, $phymap;
	}

	return @phymaps;
}



sub createSemmapForPhymap($)
{
	my $self = shift;
	my ($phymap) = @_;

	my @result = $self->{dbc}->selectSemmapByPhymapFk($phymap->{id});
	foreach my $row (@result)
	{
		my $semmap = $self->createSemmapFromDatabaseRow($row, $phymap);
		$self->createAssoziationsForSemmap($semmap);
	}
}

sub createAssoziationsForSemmap($)
{
	my $self = shift;
	my ($semmap) = @_;

	my @result = $self->{dbc}->selectAssoziationsBySemmapFk($semmap->{id});
	foreach my $row (@result)
	{
		$self->createAssoziationsFromDatabaseRow($row, $semmap);
	}
}

sub createPhymapFromDatabaseRow($)
{
	my $self = shift;
	my ($row) = @_;
	return new osstag_obj_phymap(${$row}[0], ${$row}[1], ${$row}[2]);
}

sub createSemmapFromDatabaseRow($$)
{
	my $self = shift;
	my ($row, $phymap) = @_;
	return new osstag_obj_semmap(${$row}[0], $phymap, ${$row}[2], ${$row}[3]);
}

sub createAssoziationsFromDatabaseRow($$)
{
	my $self = shift;
	my ($row, $semmap) = @_;
	return new osstag_obj_assoziations(${$row}[0], $semmap, ${$row}[2], ${$row}[3]);
}

sub saveNewPhymap($)
{
	my $self = shift;
	my ($phymap) = @_;
	$phymap->{id} = $self->{dbc}->insertPhymap($phymap->{phy_pointer}, $phymap->{project}); 

	foreach my $semmap (@{$phymap->{semmap}})
	{
		$self->saveNewSemmap($semmap);
	}
}

sub saveNewSemmap($)
{
	my $self = shift;
	my ($semmap) = @_;
	$semmap->{id} = $self->{dbc}->insertSemmap($semmap->{phymap}->{id}, $semmap->{sem_desc}, $self->{aspects_by_aspect}->{$semmap->{sem_aspect}}); 
	foreach my $assoz (@{$semmap->{assoziations}})
	{
		$self->saveNewAssoziation($assoz);
	}
}

sub saveNewAssoziation($)
{
	my $self = shift;
	my ($assoz) = @_;
	$assoz->{id} = $self->{dbc}->insertAsssoziation($assoz->{semmap}->{id}, $assoz->{original_tag}, $assoz->{normalized_tag}); 
}
=head1 License

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License 
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

=head1 Version

Version: 0.2

Date:    07-JUN-2006

=head1 Author

Author:  Jan Kechel (jan@kechel.de)

=cut 

