blob: 5f90829be1bfc8a35a5bba0e9cfb3bc9971ec51b [file] [log] [blame]
Akron9642cf32017-10-30 12:42:14 +01001package Krawfish::Koral::Report;
Akronee06a132017-12-08 16:59:27 +01002use strict;
3use warnings;
Akron290f59f2017-08-17 21:55:07 +02004use Role::Tiny;
Akronee06a132017-12-08 16:59:27 +01005use Krawfish::Log;
6
Akron71fc0ec2017-11-02 17:34:21 +01007requires qw/error
8 warning
9 message
10 has_error
11 has_warning
12 has_message/;
Akronddf077a2016-11-05 15:00:00 +010013
Akron7aed51c2017-10-31 16:23:49 +010014# Report on errors, warnings an anything else
Akron4a46e6e2017-08-16 17:49:16 +020015
Akronfa425112017-12-14 22:20:28 +020016use constant {
17 CONTEXT => 'http://korap.ids-mannheim.de/ns/koral/0.6/context.jsonld',
18 DEBUG => 0
19};
Akron6ce51082017-07-26 17:31:41 +020020
Akrona588d072017-10-13 14:45:34 +020021
Akronddf077a2016-11-05 15:00:00 +010022# Add error
23sub error {
Akron6ce51082017-07-26 17:31:41 +020024 my $self = shift;
25 print_log('info', 'Error: ' . join(' ', @_)) if DEBUG;
26 return $self->_info('error', @_);
Akronddf077a2016-11-05 15:00:00 +010027};
28
Akrona588d072017-10-13 14:45:34 +020029
30# Add warning
Akron8aee4a62016-11-14 21:33:12 +010031sub warning {
Akron6ce51082017-07-26 17:31:41 +020032 my $self = shift;
33 print_log('info', 'Warning: ' . join(' ', @_)) if DEBUG;
34 return $self->_info('warning', @_);
Akron8aee4a62016-11-14 21:33:12 +010035};
36
Akrona588d072017-10-13 14:45:34 +020037
38# Add message
Akron8aee4a62016-11-14 21:33:12 +010039sub message {
Akron6ce51082017-07-26 17:31:41 +020040 my $self = shift;
41 print_log('info', 'Message: ' . join(' ', @_)) if DEBUG;
42 return $self->_info('message', @_);
Akron8aee4a62016-11-14 21:33:12 +010043};
Akron6621e112016-11-05 17:21:39 +010044
Akrona588d072017-10-13 14:45:34 +020045
Akron6621e112016-11-05 17:21:39 +010046# Is there an error?
Akronddf077a2016-11-05 15:00:00 +010047sub has_error {
48 return 1 if $_[0]->{error};
49 return;
50};
51
Akron6621e112016-11-05 17:21:39 +010052
Akron8aee4a62016-11-14 21:33:12 +010053# Is there a warning?
54sub has_warning {
55 return 1 if $_[0]->{warning};
56 return;
57};
58
59
60# Is there a warning?
61sub has_message {
62 return 1 if $_[0]->{message};
63 return;
64};
65
66
Akron6621e112016-11-05 17:21:39 +010067# Copy information from another object
Akrona588d072017-10-13 14:45:34 +020068# Function
Akron6621e112016-11-05 17:21:39 +010069sub copy_info_from {
70 my ($self, $obj) = @_;
Akron8aee4a62016-11-14 21:33:12 +010071
72 # Copy from types
73 foreach my $type (qw/error warning message/) {
74 if ($obj->{$type}) {
75 push @{$self->{$type} //= []}, @{$obj->{$type}};
76 };
Akron6621e112016-11-05 17:21:39 +010077 };
Akronfa425112017-12-14 22:20:28 +020078
79 $self;
Akron6621e112016-11-05 17:21:39 +010080};
81
Akron8aee4a62016-11-14 21:33:12 +010082
Akrona84ef2d2017-08-07 14:45:46 +020083# Copy information from another object
Akronfa425112017-12-14 22:20:28 +020084sub move_info_from {
Akrona84ef2d2017-08-07 14:45:46 +020085 my ($self, $obj) = @_;
86
87 # Copy from types
88 foreach my $type (qw/error warning message/) {
89 if ($obj->{$type}) {
90 push @{$self->{$type} //= []}, @{$obj->{$type}};
91 delete $obj->{$type};
92 };
93 };
Akronfa425112017-12-14 22:20:28 +020094
95 $self;
Akrona84ef2d2017-08-07 14:45:46 +020096};
97
98
Akrona588d072017-10-13 14:45:34 +020099# Merge infos with a new object
Akron02c0ff22016-11-24 17:58:34 +0100100sub merge_info {
101 my ($self, $target) = @_;
102 copy_info_from($target, $self);
103};
104
105
Akrona588d072017-10-13 14:45:34 +0200106# Information
Akron8aee4a62016-11-14 21:33:12 +0100107sub _info {
108 my $self = shift;
109 my ($type, $code, $msg, @param) = @_;
110 unless (defined $code) {
111 return $self->{$type};
112 };
113 push(@{$self->{$type} //= []}, [$code, $msg, @param]);
114 return $self;
115};
Akronddf077a2016-11-05 15:00:00 +0100116
Akrona588d072017-10-13 14:45:34 +0200117
Akron8b3d9ff2017-12-13 15:01:22 +0100118sub to_koral_report {
119 my ($self, $type) = @_;
120 return $self->_info($type);
121};
122
123
124# Wrap the fragment in context
125sub to_koral_query {
126 my $self = shift;
127 my $koral = $self->to_koral_fragment;
128 $koral->{'@context'} = CONTEXT;
129
Akronfa425112017-12-14 22:20:28 +0200130 # Add potential warnings
Akron8b3d9ff2017-12-13 15:01:22 +0100131 if ($self->has_warning) {
132 $koral->{warnings} = $self->to_koral_report('warning')
133 };
134
Akronfa425112017-12-14 22:20:28 +0200135 # Add potential errors
Akron8b3d9ff2017-12-13 15:01:22 +0100136 if ($self->has_error) {
137 $koral->{errors} = $self->to_koral_report('error')
138 };
139
Akronfa425112017-12-14 22:20:28 +0200140 # Add potential messages
Akron8b3d9ff2017-12-13 15:01:22 +0100141 if ($self->has_message) {
142 $koral->{messages} = $self->to_koral_report('message')
143 };
144
145 return $koral;
146};
147
148
149
Akronddf077a2016-11-05 15:00:00 +01001501;