-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPrioritySpecimenEvent.sql
153 lines (129 loc) · 3.68 KB
/
getPrioritySpecimenEvent.sql
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
/*
Many Arctos specimens have multiple Specimen-Events for various reasons
Arctos also contains tabular views which can display data from only one event
This function finds a "priority" event, which is hopefully a less-than-random event based on criteria laid out by the collections.
This function is also used to select an event for GGBN tissue mapping when there is no explicit link between parts and events
*/
/*
NEW AND HOT!!
- old-n-busted below, in case
rank various stuff, sum up the ranks, take the winner
* specimen_event_type
* heavily favor "manufacture" - this should always win when present
* slightly favor 'collection'
* coordiantes
* slight favor for having coordinates
* verificationstatus
* this should basically control the winner when there's no 'manufacture' event type
*/
CREATE OR REPLACE FUNCTION getPrioritySpecimenEvent(coid IN number)
RETURN number
AS
sid number;
BEGIN
select
specimen_event_id
into
sid
from (
select
specimen_event_id,
--specimen_event_type,
--verificationstatus,
-- sum up the points columns
lpt + sept + vspt ttlpts
from (
select
specimen_event.specimen_event_id,
--specimen_event.specimen_event_type,
--specimen_event.verificationstatus,
decode(locality.dec_lat,null,0,1) lpt,
decode(specimen_event.specimen_event_type,
'manufacture',10,
'collection',1,
0
) sept,
decode(specimen_event.verificationstatus,
'verified and locked',5,
'accepted',4,
'unverified',0,
'unaccepted',-1,
0) vspt
from
specimen_event,
collecting_event,
locality
where
specimen_event.collecting_event_id=collecting_event.collecting_event_id and
collecting_event.locality_id=locality.locality_id and
--specimen_event.verificationstatus != 'unaccepted' and
not exists (select locality_id from geology_attributes where geology_attributes.locality_id=locality.locality_id and GEOLOGY_ATTRIBUTE='access' and GEO_ATT_VALUE='private') and
specimen_event.collection_object_id=coid
)
order by
ttlpts desc
) where rownum=1;
return sid;
end;
/
CREATE or replace PUBLIC SYNONYM getPrioritySpecimenEvent FOR getPrioritySpecimenEvent;
GRANT EXECUTE ON getPrioritySpecimenEvent TO PUBLIC;
select getPrioritySpecimenEvent2(23817647) from dual;
/*
old-n-busted follows
*/
/*
Finds "best" specimen event, where "best" is ordered by
event type is place of manufacture
has coordinates
grab something random
*/
CREATE OR REPLACE FUNCTION getPrioritySpecimenEvent(coid IN number)
RETURN number
AS
reid number;
p number := 900;
tp number;
BEGIN
for r in (
select
specimen_event.specimen_event_id,
specimen_event.specimen_event_type,
decode(locality.dec_lat,null,0,1) hascoords
from
specimen_event,
collecting_event,
locality
where
specimen_event.collecting_event_id=collecting_event.collecting_event_id and
collecting_event.locality_id=locality.locality_id and
--specimen_event.verificationstatus != 'unaccepted' and
specimen_event.collection_object_id=coid
order by began_date ASC
) loop
if r.specimen_event_type = 'manufacture' then
tp:=1;
if tp < p then
p:=tp;
reid:= r.specimen_event_id;
end if;
elsif r.hascoords=1 then
tp:=2;
if tp < p then
p:=tp;
reid:= r.specimen_event_id;
end if;
else
tp:=400;
if tp < p then
p:=tp;
reid:= r.specimen_event_id;
end if;
end if;
end loop;
RETURN reid;
end;
/
sho err;
CREATE or replace PUBLIC SYNONYM getPrioritySpecimenEvent FOR getPrioritySpecimenEvent;
GRANT EXECUTE ON getPrioritySpecimenEvent TO PUBLIC;