#!/usr/bin/perl #SOAP SERVER use DBI; use strict; use warnings; use SOAP::Lite; use SOAP::Transport::HTTP; my $daemon = SOAP::Transport::HTTP::Daemon->new(LocalAddr => 'localhost', => LocalPort => 9005,Reuse=>1); $daemon->dispatch_to('Todo'); print "Contact to SOAP server at ", $daemon->url, "\n"; $daemon->handle(); package Todo; my $usrname = 'student'; my $passwrd = '1qaz2wsx'; #geToDoList Method sub getTodoList{ print "getToDoList method invoked by client \n"; my ($method,$acronym) = @_; my $dbh = DBI->connect('DBI:mysql:organize:localhost:9005',$usrname,$passwrd) or die "Failed to connect to database: " . DBI->errstr; my $sth = $dbh->prepare('SELECT * FROM todo WHERE user = ?') or die "Couldn't prepare statement: " . $dbh->errstr; my @toDoDataArray; $sth->execute($acronym) or die "Couldn't execute statement: " . $sth->errstr; my @my_array; while (@toDoDataArray = $sth->fetchrow_array()) { my $response = SOAP::Data->name( "getTodoListResponse" => \SOAP::Data->value( SOAP::Data->name( "getTodoListResult" => \SOAP::Data->value( SOAP::Data->name( "TodoData"=>\SOAP::Data->value( SOAP::Data->name('user' => $toDoDataArray[4]), SOAP::Data->name('date' => $toDoDataArray[2]), SOAP::Data->name('note' => $toDoDataArray[1]), SOAP::Data->name('prio' => $toDoDataArray[3]), SOAP::Data->name('id' => $toDoDataArray[0]) ) ) ) ) ) ); push (@my_array,$response); } $dbh->disconnect(); return @my_array; } #getTodoOneDay Method sub getTodoOneDay{ print "getTodoOneDay method invoked by client \n"; my ($method,$acronym,$date) = @_; my $dbh = DBI->connect('DBI:mysql:organize:localhost:9005',$usrname,$passwrd) or die "Failed to connect to database: " . DBI->errstr; my $sth = $dbh->prepare('SELECT * FROM todo WHERE user = ? AND date = ?') or die "Failed to prepare statement: " . $dbh->errstr; my @toDoDataArray; $sth->execute($acronym,$date) or die "Failed to execute statement: " . $sth->errstr; my @my_array; while (@toDoDataArray = $sth->fetchrow_array()) { my $response = SOAP::Data->name( "getTodoListResponse" => \SOAP::Data->value( SOAP::Data->name( "getTodoListResult" => \SOAP::Data->value( SOAP::Data->name( "TodoData"=>\SOAP::Data->value( SOAP::Data->name('user' => $toDoDataArray[4]), SOAP::Data->name('date' => $toDoDataArray[2]), SOAP::Data->name('note' => $toDoDataArray[1]), SOAP::Data->name('prio' => $toDoDataArray[3]), SOAP::Data->name('id' => $toDoDataArray[0]) ) ) ) ) ) ); push (@my_array,$response); } $dbh->disconnect(); return @my_array; } #createToDo Method sub createTodo{ print "createToDo method invoked by client \n"; my ($method,$acronym,$date,$note,$priority,$id) = @_; my $dbh = DBI->connect('DBI:mysql:organize:localhost:9005','root','jimma1503') or die "Failed connect to database: " . DBI->errstr; my $query="INSERT INTO todo (id,note,date,prio,user) VALUES ('$id','$note','$date','$priority','$acronym')"; my $sth = $dbh->prepare($query) || die "prepare: $query: $DBI::errstr"; $sth->execute || die "execute: $query: $DBI::errstr"; $dbh->disconnect(); return "INSERTED"; } #deletetodo Method sub deleteTodo { print "deleteToDo method invoked by client \n"; my($method,$acronym,$id)=@_; my $dbh = DBI->connect('DBI:mysql:organize:localhost:9005','root','jimma1503') or die "Failed to connect to database: " . DBI->errstr; my $sth = $dbh->prepare("DELETE FROM todo WHERE user = ? AND id=?"); $sth->execute($acronym,$id ) or die $DBI::errstr; $sth->finish(); $dbh->disconnect(); return "DELETED"; } #updateTodo Method sub updateTodo { print "updateTodo method invoked by client \n"; my($method,$acronym,$time,$note,$priority,$id)=@_; my $dbh = DBI->connect('DBI:mysql:organize:localhost:9002','$usrname','$password') or die "Failed to connect to database: " . DBI->errstr; my $sth = $dbh->prepare( 'UPDATE todo SET user=?, date=?, note=?, prio=? WHERE id=?' ); $sth->execute($acronym,$time,$note,$priority,$id) or die ( "Error on UPDATE: $DBI::errstr\n" ); $sth->finish(); $dbh->disconnect(); return "UPDATED"; }