This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restQueryReport.js
322 lines (302 loc) · 11.5 KB
/
restQueryReport.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
Created by Terry Brady, Georgetown University Library
This code is an optional add-on for DSpace.
https://github.com/DSpace-Labs/DSpace-REST-Reports
*/
var QueryReport = function() {
Report.call(this);
//If sortable.js is included, uncomment the following
//this.hasSorttable = function(){return true;}
this.getDefaultParameters = function(){
return {
"collSel[]" : [],
"query_field[]" : [],
"query_op[]" : [],
"query_val[]" : [],
"show_fields[]" : [],
"filters" : "",
"limit" : this.ITEM_LIMIT,
"offset" : 0,
};
}
this.getCurrentParameters = function(){
var params = {
"query_field[]" : [],
"query_op[]" : [],
"query_val[]" : [],
"collSel[]" : ($("#collSel").val() == null) ? [""] : $("#collSel").val(),
limit : this.myReportParameters.getLimit(),
offset : this.myReportParameters.getOffset(),
"expand" : "parentCollection,metadata",
filters : this.myFilters.getFilterList(),
"show_fields[]" : this.myMetadataFields.getShowFields(),
};
$("select.query-tool,input.query-tool").each(function() {
var paramArr = params[$(this).attr("name")];
paramArr[paramArr.length] = $(this).val();
});
return params;
}
var self = this;
this.init = function() {
this.baseInit();
var communitySelector = new CommunitySelector(this, $("#collSelector"), this.myReportParameters.params["collSel[]"]);
}
this.initMetadataFields = function() {
this.myMetadataFields = new QueryableMetadataFields(self);
this.myMetadataFields.load();
}
this.myAuth.callback = function(data) {
$(".query-button").click(function(){self.runQuery();})
}
this.runQuery = function() {
this.spinner.spin($("body")[0]);
$("button").attr("disabled", true);
$.ajax({
url: "/rest/filtered-items",
data: this.getCurrentParameters(),
dataType: "json",
headers: self.myAuth.getHeaders(),
success: function(data){
data.metadata = $("#show-fields select").val();
self.drawItemFilterTable(data);
self.spinner.stop();
$("button").attr("disabled", false);
},
error: function(xhr, status, errorThrown) {
alert("Error in /rest/filtered-items "+ status+ " " + errorThrown);
},
complete: function(xhr, status, errorThrown) {
self.spinner.stop();
$("button").attr("disabled", false);
}
});
}
this.drawItemFilterTable = function(data) {
$("#itemtable").replaceWith($('<table id="itemtable" class="sortable"></table>'));
var itbl = $("#itemtable");
var tr = self.myHtmlUtil.addTr(itbl).addClass("header");
self.myHtmlUtil.addTh(tr, "Num").addClass("num").addClass("sorttable_numeric");
self.myHtmlUtil.addTh(tr, "id");
self.myHtmlUtil.addTh(tr, "collection");
self.myHtmlUtil.addTh(tr, "Item Handle");
self.myHtmlUtil.addTh(tr, "dc.title");
var mdCols = [];
if (data.metadata) {
$.each(data.metadata, function(index, field) {
if (field != "") {
self.myHtmlUtil.addTh(tr,field).addClass("returnFields");
mdCols[mdCols.length] = field;
}
});
}
$.each(data.items, function(index, item){
var tr = self.myHtmlUtil.addTr(itbl);
tr.addClass(index % 2 == 0 ? "odd data" : "even data");
self.myHtmlUtil.addTd(tr, self.myReportParameters.getOffset()+index+1).addClass("num");
self.myHtmlUtil.addTd(tr, self.getId(item));
if (item.parentCollection == null) {
self.myHtmlUtil.addTd(tr, "--");
} else {
self.myHtmlUtil.addTdAnchor(tr, item.parentCollection.name, "/handle/" + item.parentCollection.handle);
}
self.myHtmlUtil.addTdAnchor(tr, item.handle, "/handle/" + item.handle);
self.myHtmlUtil.addTd(tr, item.name);
for(var i=0; i<mdCols.length; i++) {
var key = mdCols[i];
var td = self.myHtmlUtil.addTd(tr, "");
$.each(item.metadata, function(colindex, metadata) {
if (metadata.key == key) {
if (metadata.value != null) {
var div = $("<div>"+metadata.value+"</div>");
td.append(div);
}
}
});
}
});
this.displayItems(data["query-annotation"],
this.myReportParameters.getOffset(),
this.myReportParameters.getLimit(),
function(){
self.myReportParameters.updateOffset(false);
self.runQuery();
},
function(){
self.myReportParameters.updateOffset(true);
self.runQuery();
}
);
if (this.hasSorttable()) {
sorttable.makeSortable(itbl[0]);
}
$("#metadatadiv").accordion("option", "active", $("#metadatadiv > h3").length - 1);
}
//Ignore the first column containing a row number and the item handle, get handle for the collection
this.exportCol = function(colnum, col) {
var data = "";
if (colnum == 0) return "";
if (colnum == 3) return "";
data += (colnum == 1) ? "" : ",";
if (colnum == 2) {
var anchor = $(col).find("a");
var href = anchor.is("a") ? anchor.attr("href").replace(/\/handle\//,"") : $(col).text();
data += "\"" + href + "\"";
} else {
data += self.exportCell(col); }
return data;
}
}
QueryReport.prototype = Object.create(Report.prototype);
$(document).ready(function(){
var myReport=new QueryReport();
myReport.init();
});
var QueryableMetadataFields = function(report) {
MetadataFields.call(this, report);
var self = this;
this.initFields = function(data, report) {
self.metadataSchemas = data;
var params = report.myReportParameters.params;
var fields = params["query_field[]"];
var ops = params["query_op[]"];
var vals = params["query_val[]"];
if (fields && ops && vals) {
if (fields.length == 0) {
self.drawFilterQuery("*","exists","");
} else {
for(var i=0; i<fields.length; i++) {
var op = ops.length > i ? ops[i] : "";
var val = vals.length > i ? vals[i] : "";
self.drawFilterQuery(fields[i],op,val);
}
}
}
self.drawShowFields(params["show_fields[]"]);
self.initQueries();
report.spinner.stop();
$(".query-button").attr("disabled", false);
}
this.initQueries = function() {
$("#predefselect")
.append($("<option value='new'>New Query</option>"))
.append($("<option value='q1'>Has No Title</option>"))
.append($("<option value='q2'>Has No dc.identifier.uri</option>"))
.append($("<option value='q3'>Has compound subject</option>"))
.append($("<option value='q4'>Has compound dc.contributor.author</option>"))
.append($("<option value='q5'>Has compound dc.creator</option>"))
.append($("<option value='q6'>Has URL in dc.description</option>"))
.append($("<option value='q7'>Has full text in dc.description.provenance</option>"))
.append($("<option value='q8'>Has non-full text in dc.description.provenance</option>"))
.append($("<option value='q9'>Has empty metadata</option>"))
.append($("<option value='q10'>Has unbreaking metadata in description</option>"))
.append($("<option value='q11'>Has long metadata</option>"))
.append($("<option value='q12'>Has XML entity in metadata</option>"))
.append($("<option value='q13'>Has non-ascii character in metadata</option>"))
.on("change",function(){
$("div.metadata").remove();
var val = $("#predefselect").val();
if (val == 'new') {
self.drawFilterQuery("","","");
} else if (val == 'q1') {
self.drawFilterQuery("dc.title","doesnt_exist","");
} else if (val == 'q2') {
self.drawFilterQuery("dc.identifier.uri","doesnt_exist","");
} else if (val == 'q3') {
self.drawFilterQuery("dc.subject.*","like","%;%");
} else if (val == 'q4') {
self.drawFilterQuery("dc.contributor.author","like","% and %");
} else if (val == 'q5') {
self.drawFilterQuery("dc.creator","like","% and %");
} else if (val == 'q6') {
self.drawFilterQuery("dc.description","matches","^.*(http://|https://|mailto:).*$");
} else if (val == 'q7') {
self.drawFilterQuery("dc.description.provenance","matches","^.*No\\. of bitstreams(.|\\r|\\n|\\r\\n)*\\.(PDF|pdf|DOC|doc|PPT|ppt|DOCX|docx|PPTX|pptx).*$");
} else if (val == 'q8') {
self.drawFilterQuery("dc.description.provenance","doesnt_match","^.*No\\. of bitstreams(.|\\r|\\n|\\r\\n)*\\.(PDF|pdf|DOC|doc|PPT|ppt|DOCX|docx|PPTX|pptx).*$");
} else if (val == 'q9') {
self.drawFilterQuery("*","matches","^\\s*$");
} else if (val == 'q10') {
self.drawFilterQuery("dc.description.*","matches","^.*[^\\s]{50,}.*$");
} else if (val == 'q11') {
self.drawFilterQuery("*","matches","^((.|\r|\n){100,100}){50,}(.|\r|\n)*$"); //the {100,100}{50,} accommodates postgres limits
} else if (val == 'q12') {
self.drawFilterQuery("*","matches","^.*&#.*$");
} else if (val == 'q13') {
self.drawFilterQuery("*","matches","^.*[^[:ascii:]].*$");
}
});
}
this.drawFilterQuery = function(pField, pOp, pVal) {
var div = $("<div class='metadata'/>").appendTo("#queries");
var sel = $("<select class='query-tool' name='query_field[]'/>");
var opt = $("<option/>");
sel.append(opt);
opt = $("<option value='*'>Any Field</option>");
sel.append(opt);
$.each(self.metadataSchemas, function(index, schema){
$.each(schema.fields, function(findex, field) {
var name = field.name;
var parts = name.match(/^([^\.]+)\.([^\.]+)\.([^\.]+)$/);
if (parts == null) {
var wildname = name + ".*";
var opt = $("<option/>");
opt.attr("value",wildname).text(wildname);
sel.append(opt);
}
var opt = $("<option/>");
opt.attr("value",name).text(name);
sel.append(opt);
});
});
sel.val(pField);
div.append(sel);
var opsel = $("<select class='query-tool' name='query_op[]'/>");
$("<option>exists</option>").val("exists").appendTo(opsel);
$("<option>does not exist</option>").val("doesnt_exist").appendTo(opsel);
$("<option selected>equals</option>").val("equals").appendTo(opsel);
$("<option>does not equal</option>").val("not_equals").appendTo(opsel);
$("<option>like</option>").val("like").appendTo(opsel);
$("<option>not like</option>").val("not_like").appendTo(opsel);
$("<option>contains</option>").val("contains").appendTo(opsel);
$("<option>does not contain</option>").val("doesnt_contain").appendTo(opsel);
$("<option>matches</option>").val("matches").appendTo(opsel);
$("<option>does not match</option>").val("doesnt_match").appendTo(opsel);
opsel.val(pOp);
opsel.change(function(){
self.valField($(this));
});
div.append(opsel);
var input = $("<input class='query-tool' name='query_val[]'/>");
div.append(input);
input.val(pVal);
self.valField(opsel);
$("<button class='field_plus'>+</button>").appendTo(div).click(function(){
self.drawFilterQuery();
self.queryButtons();
});
$("<button class='field_minus'>-</button>").appendTo(div).click(function(){
$(this).parent("div.metadata").remove();
self.queryButtons();
});
self.queryButtons();
}
this.valField = function(valop) {
var val = valop.val();
var disableval = (val == "exists" || val == "not_exists");
var valinput = valop.parent("div.metadata").find("input[name='query_val[]']");
valinput.attr("readonly",disableval);
if (disableval) {
valinput.val("");
}
}
this.queryButtons = function() {
$("button.field_plus").attr("disabled",true);
$("button.field_plus:last").attr("disabled",false);
$("button.field_minus").attr("disabled",false);
if ($("button.field_minus").length == 1) {
$("button.field_minus").attr("disabled",true);
}
}
}
QueryableMetadataFields.prototype = Object.create(MetadataFields.prototype);