]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/URLVerify/CreateSession.js
c17681caf9c0607954f58bdaefe90470e1b24468
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / URLVerify / CreateSession.js
1 if (!dojo._hasResource["openils.URLVerify.CreateSession"]) {
2     dojo.require("dojo.data.ItemFileWriteStore");
3     dojo.require("dojox.jsonPath");
4     dojo.require("fieldmapper.OrgUtils");
5     dojo.require("openils.Util");
6     dojo.require("openils.CGI");
7     dojo.require("openils.PermaCrud");
8     dojo.require("openils.widget.FilteringTreeSelect");
9     dojo.require("openils.URLVerify.Verify");
10
11     dojo.requireLocalization("openils.URLVerify", "URLVerify");
12
13     dojo._hasResource["openils.URLVerify.CreateSession"] = true;
14     dojo.provide("openils.URLVerify.CreateSession");
15
16     dojo.declare("openils.URLVerify.CreateSession", null, {});
17
18     /* Take care that we add nothing to the global namespace. */
19
20 (function() {
21     var module = openils.URLVerify.CreateSession;
22     var localeStrings =
23         dojo.i18n.getLocalization("openils.URLVerify", "URLVerify");
24     var uvus_progress = 0;
25
26     /* Take search text box input, selected saved search ids, and selected
27      * scope shortname to produce one search string. */
28     module._prepare_search = function(basic, saved, scope) {
29         if (saved.length) {
30             basic += " " + dojo.map(
31                 saved, function(s) { return "saved_query(" + s + ")"; }
32             ).join(" ");
33         }
34
35         if (scope && !basic.match(/site\(.+\)/))
36             basic += " site(" + scope + ")";
37
38         return basic;
39     };
40
41     /* Reacting to the interface's "Begin" button, this function triggers the
42      * first of three server-side processes necessary to create a session:
43      *
44      * 1) create the session itself (API call), */
45     module.begin = function() {
46         var name = uv_session_name.attr("value");
47
48         var scope;
49         try {
50             scope = module.org_selector.store.getValue(
51                 module.org_selector.item,
52                 "shortname"
53             );
54         } catch (E) {
55             /* probably nothing valid is selected; move on */
56             void(0);
57         }
58
59         var search = module._prepare_search(
60             uv_search.attr("value"),
61             dojo.filter(
62                 dojo.byId("saved-searches").options,
63                 function(o) { return o.selected; }
64             ).map(
65                 function(o) { return o.value; }
66             ),
67             scope
68         );
69
70         if (!module.tag_and_subfields.any()) {
71             alert(localeStrings.NEED_UVUS);
72             return;
73         }
74
75         module.progress_dialog.attr("title", localeStrings.CREATING);
76         module.progress_dialog.show(true);
77         fieldmapper.standardRequest(
78             ["open-ils.url_verify", "open-ils.url_verify.session.create"], {
79                 "params": [openils.User.authtoken, name, search],
80                 "async": true,
81                 "onresponse": function(r) {
82                     if (r = openils.Util.readResponse(r)) {
83                         /* I think we're modal enough to get away with this. */
84                         module.session_id = r;
85                         module.save_tags();
86                     }
87                 }
88             }
89         );
90     };
91
92     /* 2a) save the tag/subfield sets for URL extraction, */
93     module.save_tags = function() {
94         module.progress_dialog.attr("title", localeStrings.SAVING_TAGS);
95         module.progress_dialog.show(); /* sic */
96
97         uvus_progress = 0;
98
99         /* Note we're not using openils.PermaCrud, which is inadequate
100          * when you need one big transaction. Thanks for figuring it
101          * out Bill. */
102         var pcrud_raw = new OpenSRF.ClientSession("open-ils.pcrud");
103
104         pcrud_raw.connect();
105
106         pcrud_raw.request({
107             "method": "open-ils.pcrud.transaction.begin",
108             "params": [openils.User.authtoken],
109             "oncomplete": function(r) {
110                 module._create_uvus_one_at_a_time(
111                     pcrud_raw,
112                     module.tag_and_subfields.generate_uvus(module.session_id)
113                 );
114             }
115         }).send();
116     };
117
118     /* 2b */
119     module._create_uvus_one_at_a_time = function(pcrud_raw, uvus_list) {
120         pcrud_raw.request({
121             "method": "open-ils.pcrud.create.uvus",
122             "params": [openils.User.authtoken, uvus_list[0]],
123             "oncomplete": function(r) {
124                 var new_uvus = openils.Util.readResponse(r);
125                 module.progress_dialog.update(
126                     {"maximum": uvus_list.length, "progress": ++uvus_progress}
127                 );
128
129                 uvus_list.shift();  /* /now/ actually shorten working list */
130
131                 if (uvus_list.length < 1) {
132                     pcrud_raw.request({
133                         "method": "open-ils.pcrud.transaction.commit",
134                         "params": [openils.User.authtoken],
135                         "oncomplete": function(r) {
136                             pcrud_raw.disconnect();
137                             module.perform_search();
138                         }
139                     }).send();
140
141                 } else {
142                     module._create_uvus_one_at_a_time(
143                         pcrud_raw, uvus_list
144                     );
145                 }
146              }
147         }).send();
148     };
149
150     /* 3) search and populate the container (API call). */
151     module.perform_search = function() {
152         var search_result_count = 0;
153
154         module.progress_dialog.attr("title", localeStrings.PERFORMING_SEARCH);
155         module.progress_dialog.show(true);
156
157         fieldmapper.standardRequest(
158             ["open-ils.url_verify",
159                 "open-ils.url_verify.session.search_and_extract"], {
160                 "params": [openils.User.authtoken, module.session_id],
161                 "async": true,
162                 "onresponse": function(r) {
163                     r = openils.Util.readResponse(r);
164                     if (!search_result_count) {
165                         search_result_count = Number(r);
166
167                         module.progress_dialog.show(); /* sic */
168                         module.progress_dialog.attr(
169                             "title", localeStrings.EXTRACTING_URLS
170                         );
171                         module.progress_dialog.update(
172                             {"maximum": search_result_count, "progress": 0}
173                         );
174                     } else {
175                         module.progress_dialog.update({"progress": r.length})
176                     }
177                 },
178                 "oncomplete": function() {
179                     module.progress_dialog.attr(
180                         "title", localeStrings.REDIRECTING
181                     );
182                     module.progress_dialog.show(true);
183
184                     if (no_url_selection.checked) {
185                         /* verify URLs and ultimately redirect to review page */
186                         openils.URLVerify.Verify.go(
187                             module.session_id, null, module.progress_dialog
188                         );
189                     } else {
190                         /* go to the URL selection page, allowing users to
191                          * selectively verify URLs */
192                         location.href = oilsBasePath +
193                             "/url_verify/select_urls?session_id=" +
194                             module.session_id;
195                     }
196                 }
197             }
198         );
199     };
200
201     /* At least in Dojo 1.3.3 (I know, I know), dijit.form.MultiSelect does
202      * not behave like FilteringSelect, like you might expect, or work from a
203      * data store.  So we'll use a native <select> control, which will have
204      * fewer moving parts that can go haywire anyway.
205      */
206     module._populate_saved_searches = function(node) {
207         var list = module.pcrud.retrieveAll(
208             "asq", {"order_by": {"asq": "label"}}
209         );
210
211         dojo.forEach(
212             list, function(o) {
213                 dojo.create(
214                     "option", {
215                         "innerHTML": o.label(),
216                         "value": o.id(),
217                         "title": o.query_text()
218                     }, node, "last"
219                 );
220             }
221         );
222     };
223
224     /* set up an all-org-units-in-the-tree selector */
225     module._prepare_org_selector = function(node) {
226         var widget = new openils.widget.FilteringTreeSelect(null, node);
227         widget.searchAttr = "name";
228         widget.labelAttr = "name";
229         widget.tree = fieldmapper.aou.globalOrgTree;
230         widget.parentField = 'parent_ou';
231         widget.startup();
232         widget.attr("value", openils.User.user.ws_ou());
233
234         module.org_selector = widget;
235     };
236
237     /* Can only be called by setup() */
238     module._clone = function(id) {
239         module.progress_dialog.attr("title", localeStrings.CLONING);
240         var old_session = module.pcrud.retrieve(
241             "uvs", id, {"flesh": 1, "flesh_fields": {"uvs": ["selectors"]}}
242         );
243
244         /* Set name to "Copy of [old name]" */
245         uv_session_name.attr(
246             "value", dojo.string.substitute(
247                 localeStrings.CLONE_SESSION_NAME, [old_session.name()]
248             )
249         );
250
251         /* Set search field. */
252         uv_search.attr("value", old_session.search());
253
254         /* Explain to user why we don't affect the saved searches picker. */
255         if (old_session.search().match(/saved_query/))
256             openils.Util.show("clone-saved-search-warning");
257
258         /* Add related xpaths (URL selectors) to TagAndSubfieldsMgr. */
259         module.tag_and_subfields.add_xpaths(old_session.selectors());
260     };
261
262     module.setup = function(saved_search_id, org_selector_id, progress_dialog) {
263         module.pcrud = new openils.PermaCrud(); /* only used for setup */
264
265         module.progress_dialog = progress_dialog;
266
267         module.progress_dialog.attr("title", localeStrings.INTERFACE_SETUP);
268         module.progress_dialog.show(true);
269
270         module._populate_saved_searches(dojo.byId(saved_search_id));
271         module._prepare_org_selector(dojo.byId(org_selector_id));
272
273         var cgi = new openils.CGI();
274         if (cgi.param("clone"))
275             module._clone(cgi.param("clone"));
276
277         module.pcrud.disconnect();
278
279         module.progress_dialog.hide();
280     };
281
282     /* This is the thing that lets you add/remove rows of tab/subfield pairs */
283     function TagAndSubfieldsMgr(container_id) {
284         var self = this;
285
286         this.container_id = container_id;
287         this.counter = 0;
288
289         this.read_new = function() {
290             var controls = dojo.query(".tag-and-subfield-add-another input");
291
292             return {
293                 "tag": controls[0].value,
294                 "subfields": openils.Util.uniqueElements(
295                     controls[1].value.replace(/[^0-9a-z]/g, "").split("")
296                 ).sort().join("")
297             };
298         };
299
300         this.add = function() {
301             var newdata = this.read_new();
302             var newid = "t-and-s-row-" + String(this.counter++);
303             var div = dojo.create(
304                 "div", {
305                     "id": newid,
306                     "innerHTML": "<span class='t-and-s-tag'>" +
307                         newdata.tag +
308                         "</span> \u2021<span class='t-and-s-subfields'>" +
309                         newdata.subfields + "</span> "
310                 }, this.container_id, "last"
311             );
312             dojo.create(
313                 "a", {
314                     "href": "javascript:void(0);",
315                     "onclick": function() {
316                         var me = dojo.byId(newid);
317                         me.parentNode.removeChild(me);
318                     },
319                     "innerHTML": "[X]" /* XXX i18n */
320                 }, div, "last"
321             );
322         };
323
324         this.add_xpaths = function(xpaths) {
325             if (!dojo.isArray(xpaths)) {
326                 console.info("No xpaths to add");
327                 return;
328             }
329
330             dojo.forEach(
331                 xpaths, dojo.hitch(this, function(xpath) {
332                     var newid = "t-and-s-row-" + String(this.counter++);
333                     var div = dojo.create(
334                         "div", {
335                             "id": newid,
336                             "innerHTML": localeStrings.XPATH +
337                                 " <span class='t-and-s-xpath'>" +
338                                 xpath.xpath() + "</span>"
339                         }, this.container_id, "last"
340                     );
341                     dojo.create(
342                         "a", {
343                             "href": "javascript:void(0);",
344                             "onclick": function() {
345                                 var me = dojo.byId(newid);
346                                 me.parentNode.removeChild(me);
347                             },
348                             "innerHTML": "[X]" /* XXX i18n */
349                         }, div, "last"
350                     );
351                 })
352             );
353         };
354
355         /* return a boolean indicating whether or not we have any rows */
356         this.any = function() {
357             return Boolean(
358                 dojo.query(
359                     '[id^="t-and-s-row-"]', dojo.byId(this.container_id)
360                 ).length
361             );
362         };
363
364         /* Return one uvus object for each unique tag we have a row for,
365          * and use the given session_id for the uvus.session field. */
366         this.generate_uvus = function(session_id) {
367             var uniquely_grouped = {};
368             dojo.query(
369                 '[id^="t-and-s-row-"]', dojo.byId(this.container_id)
370             ).forEach(
371                 function(row) {
372                     var holds_xpath = dojo.query(".t-and-s-xpath", row);
373                     if (holds_xpath.length) {
374                         uniquely_grouped.xpath = uniquely_grouped.xpath || [];
375                         uniquely_grouped.xpath.push(holds_xpath[0].innerHTML);
376                     } else {
377                         var tag = dojo.query(".t-and-s-tag", row)[0].innerHTML;
378                         var subfield =
379                             dojo.query(".t-and-s-subfields", row)[0].innerHTML;
380
381                         var existing;
382                         if ((existing = uniquely_grouped[tag])) { // assignment
383                             existing = openils.Util.uniqueElements(
384                                 (existing + subfield).split("")
385                             ).sort().join("");
386                         } else {
387                             uniquely_grouped[tag] = subfield;
388                         }
389                     }
390                 }
391             );
392
393             var uvus_list = [];
394             /* Handle things that are already in XPath form first (these
395              * come from cloning link checker sessions. */
396             if (uniquely_grouped.xpath) {
397                 dojo.forEach(
398                     uniquely_grouped.xpath,
399                     function(xpath) {
400                         var obj = new uvus();
401
402                         obj.session(session_id);
403                         obj.xpath(xpath);
404                         uvus_list.push(obj);
405                     }
406                 );
407                 delete uniquely_grouped.xpath;
408             }
409
410             /* Now handle anything entered by hand. */
411             for (var tag in uniquely_grouped) {
412                 var obj = new uvus();
413
414                 obj.session(session_id);
415
416                 /* XXX TODO Handle control fields (No subfields. but would
417                  * control fields ever contain URLs? Don't know.) */
418                 obj.xpath(
419                     "//*[@tag='" + tag + "']/*[" +
420                     uniquely_grouped[tag].split("").map(
421                         function(c) { return "@code='" + c + "'"; }
422                     ).join(" or ") +
423                     "]"
424                 );
425
426                 uvus_list.push(obj);
427             }
428
429             return uvus_list;
430         };
431
432     }
433
434     module.tag_and_subfields =
435         new TagAndSubfieldsMgr("uv-tags-and-subfields");
436
437 }());
438
439 }