-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathActivity.pm
194 lines (172 loc) · 5.13 KB
/
Activity.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
=head1 NAME
Tuba::Activity : Controller class for books
=cut
package Tuba::Activity;
use Mojo::Base qw/Tuba::Controller/;
use Tuba::DB::Objects qw/-nicknames/;
use JSON::XS;
sub list {
my $c = shift;
$c->SUPER::list(@_);
}
sub show {
my $c = shift;
$c->stash('object', $c->_this_object);
$c->SUPER::show(@_);
}
sub create {
my $c = shift;
my $spatial_extent = $c->_spatial_extent;
my $error = _check_valid_geojson($spatial_extent) if $spatial_extent;
return $c->create_error($error) if $error;
$c->SUPER::create(@_);
}
sub update {
my $c = shift;
my $spatial_extent = $c->_spatial_extent;
my $error = _check_valid_geojson($spatial_extent) if $spatial_extent;
return $c->update_error($error) if $error;
$c->SUPER::update(@_);
}
sub make_tree_for_show {
my $c = shift;
my $obj = shift;
my $tree = $obj->as_tree(c => $c,
( $c->param('brief') ? (bonsai => 1) : ())
);
$tree->{methodologies} = [
map $_->as_tree(c => $c), $obj->methodologies
];
$tree->{publication_maps} = [
map $_->as_tree, $obj->publication_maps
];
return $tree;
}
sub normalize_form_parameter {
my $c = shift;
my %args = @_;
my ($column, $value) = @args{qw/column value/};
my $obj;
for ($column) {
/^methodology_publication_id$/ and $obj = $c->str_to_obj($value);
/^output_publication_id$/ and $obj = $c->str_to_obj($value);
}
if ($obj) {
my $pub = $obj->get_publication(autocreate => 1);
$pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $pub->id;
return $pub->id;
}
return $value;
}
sub _default_order {
my $c = shift;
return ( qw/
identifier
methodology
visualization_methodology
methodology_citation
methodology_contact
activity_duration
source_access_date
source_modifications
modified_source_location
interim_artifacts
output_artifacts
computing_environment
software
visualization_software
start_time
end_time
dataset_variables
spatial_extent
data_usage
notes
methodology_publication_id
methodology_contributor_id/
);
}
sub update_rel_form {
my $c = shift;
$c->stash(relationships => [qw/methodologies/]);
return $c->SUPER::update_rel_form(@_);
}
sub _default_rel_controls {
my $c = shift;
return ($c->SUPER::_default_controls(@_),
methodologies => sub {
my $c = shift;
{ template => 'publications', params => { method => 'methodologies' } }
},
);
}
sub update_rel {
my $c = shift;
my $activity = $c->_this_object;
$c->stash(tab => 'update_rel_for');
# TODO handle JSON
if (my $new = $c->param('new_publication')) {
my $obj = $c->str_to_obj($new);
my $pub = $obj->get_publication(autocreate => 1);
$pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note) unless $pub->id;
my $methodology = Methodology->new(
activity_identifier => $activity->identifier,
publication_id => $pub->id
);
$methodology->save(audit_user => $c->audit_user, audit_note => $c->audit_note)
or return $c->update_error($methodology->error);
}
for my $id ($c->param('delete_publication')) {
next unless $id;
Tuba::DB::Object::Methodology::Manager->delete_objects(
where => [{activity_identifier => $activity->identifier, publication_id => $id}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}
$c->respond_to(
json => sub {
shift->render(json => { status => 'ok' })
},
html => sub {
return shift->redirect_without_error('update_rel_form');
},
);
}
sub _spatial_extent {
my $c = shift;
return $c->param('spatial_extent') if $c->param('spatial_extent');
my $json = $c->req->json;
return unless $json;
return $json->{spatial_extent};
}
sub _check_valid_geojson {
my $spatial_param = shift;
my $valid_geojson_types = {
Point => 1,
MultiPoint => 1,
LineString => 1,
MultiLineString => 1,
Polygon => 1,
MultiPolygon => 1,
GeometryCollection => 1,
Feature => 1,
FeatureCollection => 1,
};
# parse valid JSON
my $error;
my $geojson = eval { JSON::XS->new->decode($spatial_param); };
if ($@ || (ref $geojson ne 'HASH')) {
$error = "Invalid geoJSON: could not parse json :".($@ || ref $geojson);
}
# check the field type exists
if ( !$error && $geojson->{'type'} ) {
# check the type value is valid
unless ( $valid_geojson_types->{ $geojson->{'type'} } ) {
$error = "Invalid geoJSON: Bad type '$geojson->{type}' not one of: " . join(', ', keys %$valid_geojson_types);
}
}
else {
$error = "Invalid geoJSON: missing type field" unless $error;
}
return $error;
}
1;