our
$VERSION
=
'2.0.10'
;
has
parser
=> (
is
=>
'rw'
,
builder
=>
sub
{
return
XML::LibXML->new(
load_ext_dtd
=> 0,
expand_entities
=> 0 );
}
);
sub
newNotification {
my
(
$self
,
$xml
,
$defaultCond
) =
@_
;
$defaultCond
||=
''
;
eval
{
$xml
=
$self
->parser->parse_string(
$xml
) };
if
(
my
$err
= $@ ) {
eval
{
$self
->logger->error(
"Unable to read XML file : $err"
) };
return
0;
}
my
@notifs
;
my
(
$version
,
$encoding
) = (
$xml
->version(),
$xml
->encoding() );
foreach
my
$notif
(
$xml
->documentElement->getElementsByTagName(
'notification'
) )
{
my
@data
= ();
$notif
->{reference} =~ s/_/-/g;
foreach
(
qw(date uid reference)
) {
my
$tmp
;
unless
(
$tmp
=
$notif
->getAttribute(
$_
) ) {
$self
->logger->error(
"Attribute $_ is missing"
);
return
0;
}
if
(
$self
->get(
$notif
->{uid},
$notif
->{reference} ) ) {
my
$err
=
"A notification already exists with reference "
.
$notif
->{reference};
$self
->logger->error(
"$err"
);
return
0;
}
$tmp
=~ s/^(\d{4}-\d{2}-\d{2}).*$/$1/;
push
@data
,
$tmp
;
}
foreach
(
qw(condition)
) {
my
$tmp
;
if
(
$tmp
=
$notif
->getAttribute(
$_
) ) {
push
@data
,
$tmp
;
}
else
{
$self
->logger->info(
"Set defaultCondition ($defaultCond) for notification "
.
$notif
->{reference} );
push
@data
,
$defaultCond
;
}
}
my
$result
= XML::LibXML::Document->new(
$version
,
$encoding
);
my
$root
= XML::LibXML::Element->new(
'root'
);
$root
->appendChild(
$notif
);
$result
->setDocumentElement(
$root
);
$result
=
$result
->serialize;
utf8::encode(
$result
);
push
@notifs
, [
@data
,
$result
];
}
my
$count
;
foreach
(
@notifs
) {
$count
++;
my
(
$r
,
$err
) =
$self
->newNotif(
@$_
);
die
"$err"
unless
(
$r
);
}
return
$count
;
}
sub
deleteNotification {
my
(
$self
,
$uid
,
$myref
) =
@_
;
my
@data
;
unless
(
$uid
and
$myref
) {
$self
->userLogger->error(
"SOAP service deleteNotification called without all parameters"
);
return
0;
}
$self
->logger->debug(
"SOAP service deleteNotification called for uid $uid and reference $myref"
);
my
$user
=
$self
->get(
$uid
);
return
0
unless
(
$user
);
my
$count
= 0;
foreach
my
$ref
(
keys
%$user
) {
my
$xml
=
$self
->parser->parse_string(
$user
->{
$ref
} );
foreach
my
$notif
(
$xml
->documentElement->getElementsByTagName(
'notification'
) )
{
if
(
$notif
->getAttribute(
'reference'
) eq
$myref
) {
push
@data
,
$ref
;
}
foreach
(
@data
) {
if
(
$self
->purge(
$_
, 1 ) ) {
$self
->logger->debug(
"Notification $_ was removed."
);
$count
++;
}
}
}
}
return
$count
;
}
1;