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");
11 dojo.requireLocalization("openils.URLVerify", "URLVerify");
13 dojo._hasResource["openils.URLVerify.CreateSession"] = true;
14 dojo.provide("openils.URLVerify.CreateSession");
16 dojo.declare("openils.URLVerify.CreateSession", null, {});
18 /* Take care that we add nothing to the global namespace. */
21 var module = openils.URLVerify.CreateSession;
23 dojo.i18n.getLocalization("openils.URLVerify", "URLVerify");
24 var uvus_progress = 0;
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) {
30 basic += " " + dojo.map(
31 saved, function(s) { return "saved_query(" + s + ")"; }
35 if (scope && !basic.match(/site\(.+\)/))
36 basic += " site(" + scope + ")";
41 /* Reacting to the interface's "Begin" button, this function triggers the
42 * first of three server-side processes necessary to create a session:
44 * 1) create the session itself (API call), */
45 module.begin = function() {
46 var name = uv_session_name.attr("value");
50 scope = module.org_selector.store.getValue(
51 module.org_selector.item,
55 /* probably nothing valid is selected; move on */
59 var search = module._prepare_search(
60 uv_search.attr("value"),
62 dojo.byId("saved-searches").options,
63 function(o) { return o.selected; }
65 function(o) { return o.value; }
70 if (!module.tag_and_subfields.any()) {
71 alert(localeStrings.NEED_UVUS);
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],
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;
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 */
99 /* Note we're not using openils.PermaCrud, which is inadequate
100 * when you need one big transaction. Thanks for figuring it
102 var pcrud_raw = new OpenSRF.ClientSession("open-ils.pcrud");
107 "method": "open-ils.pcrud.transaction.begin",
108 "params": [openils.User.authtoken],
109 "oncomplete": function(r) {
110 module._create_uvus_one_at_a_time(
112 module.tag_and_subfields.generate_uvus(module.session_id)
119 module._create_uvus_one_at_a_time = function(pcrud_raw, uvus_list) {
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}
129 uvus_list.shift(); /* /now/ actually shorten working list */
131 if (uvus_list.length < 1) {
133 "method": "open-ils.pcrud.transaction.commit",
134 "params": [openils.User.authtoken],
135 "oncomplete": function(r) {
136 pcrud_raw.disconnect();
137 module.perform_search();
142 module._create_uvus_one_at_a_time(
150 /* 3) search and populate the container (API call). */
151 module.perform_search = function() {
152 var search_result_count = 0;
154 module.progress_dialog.attr("title", localeStrings.PERFORMING_SEARCH);
155 module.progress_dialog.show(true);
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],
162 "onresponse": function(r) {
163 r = openils.Util.readResponse(r);
164 if (!search_result_count) {
165 search_result_count = Number(r);
167 module.progress_dialog.show(); /* sic */
168 module.progress_dialog.attr(
169 "title", localeStrings.EXTRACTING_URLS
171 module.progress_dialog.update(
172 {"maximum": search_result_count, "progress": 0}
175 module.progress_dialog.update({"progress": r.length})
178 "oncomplete": function() {
179 module.progress_dialog.attr(
180 "title", localeStrings.REDIRECTING
182 module.progress_dialog.show(true);
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
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=" +
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.
206 module._populate_saved_searches = function(node) {
207 var list = module.pcrud.retrieveAll(
208 "asq", {"order_by": {"asq": "label"}}
215 "innerHTML": o.label(),
217 "title": o.query_text()
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';
232 widget.attr("value", openils.User.user.ws_ou());
234 module.org_selector = widget;
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"]}}
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()]
251 /* Set search field. */
252 uv_search.attr("value", old_session.search());
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");
258 /* Add related xpaths (URL selectors) to TagAndSubfieldsMgr. */
259 module.tag_and_subfields.add_xpaths(old_session.selectors());
262 module.setup = function(saved_search_id, org_selector_id, progress_dialog) {
263 module.pcrud = new openils.PermaCrud(); /* only used for setup */
265 module.progress_dialog = progress_dialog;
267 module.progress_dialog.attr("title", localeStrings.INTERFACE_SETUP);
268 module.progress_dialog.show(true);
270 module._populate_saved_searches(dojo.byId(saved_search_id));
271 module._prepare_org_selector(dojo.byId(org_selector_id));
273 var cgi = new openils.CGI();
274 if (cgi.param("clone"))
275 module._clone(cgi.param("clone"));
277 module.pcrud.disconnect();
279 module.progress_dialog.hide();
282 /* This is the thing that lets you add/remove rows of tab/subfield pairs */
283 function TagAndSubfieldsMgr(container_id) {
286 this.container_id = container_id;
289 this.read_new = function() {
290 var controls = dojo.query(".tag-and-subfield-add-another input");
293 "tag": controls[0].value,
294 "subfields": openils.Util.uniqueElements(
295 controls[1].value.replace(/[^0-9a-z]/g, "").split("")
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(
306 "innerHTML": "<span class='t-and-s-tag'>" +
308 "</span> \u2021<span class='t-and-s-subfields'>" +
309 newdata.subfields + "</span> "
310 }, this.container_id, "last"
314 "href": "javascript:void(0);",
315 "onclick": function() {
316 var me = dojo.byId(newid);
317 me.parentNode.removeChild(me);
319 "innerHTML": "[X]" /* XXX i18n */
324 this.add_xpaths = function(xpaths) {
325 if (!dojo.isArray(xpaths)) {
326 console.info("No xpaths to add");
331 xpaths, dojo.hitch(this, function(xpath) {
332 var newid = "t-and-s-row-" + String(this.counter++);
333 var div = dojo.create(
336 "innerHTML": localeStrings.XPATH +
337 " <span class='t-and-s-xpath'>" +
338 xpath.xpath() + "</span>"
339 }, this.container_id, "last"
343 "href": "javascript:void(0);",
344 "onclick": function() {
345 var me = dojo.byId(newid);
346 me.parentNode.removeChild(me);
348 "innerHTML": "[X]" /* XXX i18n */
355 /* return a boolean indicating whether or not we have any rows */
356 this.any = function() {
359 '[id^="t-and-s-row-"]', dojo.byId(this.container_id)
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 = {};
369 '[id^="t-and-s-row-"]', dojo.byId(this.container_id)
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);
377 var tag = dojo.query(".t-and-s-tag", row)[0].innerHTML;
379 dojo.query(".t-and-s-subfields", row)[0].innerHTML;
382 if ((existing = uniquely_grouped[tag])) { // assignment
383 existing = openils.Util.uniqueElements(
384 (existing + subfield).split("")
387 uniquely_grouped[tag] = subfield;
394 /* Handle things that are already in XPath form first (these
395 * come from cloning link checker sessions. */
396 if (uniquely_grouped.xpath) {
398 uniquely_grouped.xpath,
400 var obj = new uvus();
402 obj.session(session_id);
407 delete uniquely_grouped.xpath;
410 /* Now handle anything entered by hand. */
411 for (var tag in uniquely_grouped) {
412 var obj = new uvus();
414 obj.session(session_id);
416 /* XXX TODO Handle control fields (No subfields. but would
417 * control fields ever contain URLs? Don't know.) */
419 "//*[@tag='" + tag + "']/*[" +
420 uniquely_grouped[tag].split("").map(
421 function(c) { return "@code='" + c + "'"; }
434 module.tag_and_subfields =
435 new TagAndSubfieldsMgr("uv-tags-and-subfields");