dojo.require("fieldmapper.OrgUtils");
dojo.require("openils.PermaCrud");
dojo.require("openils.widget.FilteringTreeSelect");
+dojo.requireLocalization("openils.url_verify", "url_verify");
+/* From here on, we add nothing to the global namespace but create_session */
var create_session = {};
(function() {
+ var localeStrings =
+ dojo.i18n.getLocalization("openils.url_verify", "url_verify");
+ var uvus_progress = 0;
+
+ /* Take search text box input, selected saved search ids, and selected
+ * scope shortname to produce one search string. */
+ create_session._prepare_search = function(basic, saved, scope) {
+ if (saved.length) {
+ basic += " " + dojo.map(
+ saved, function(s) { return "saved_query(" + s + ")"; }
+ ).join(" ");
+ }
+
+ if (scope && !basic.match(/site\(.+\)/))
+ basic += " site(" + scope + ")";
+
+ return basic;
+ };
+
+ /* Reacting to the interface's "Begin" button, this function triggers the
+ * first of three server-side processes necessary to create a session:
+ * 1) create the session itself (API call), */
create_session.begin = function() {
- alert("XXX TODO implement");
+ var name = uv_session_name.attr("value");
+
+ var scope;
+ try {
+ scope = create_session.org_selector.store.getValue(
+ create_session.org_selector.item,
+ "shortname"
+ );
+ } catch (E) {
+ /* probably nothing valid is selected; move on */
+ void(0);
+ }
+
+ var search = create_session._prepare_search(
+ uv_search.attr("value"),
+ dojo.filter(
+ dojo.byId("saved-searches").options,
+ function(o) { return o.selected; }
+ ).map(
+ function(o) { return o.value; }
+ ),
+ scope
+ );
+
+ progress_dialog.attr("title", localeStrings.CREATING);
+ progress_dialog.show(true);
+ fieldmapper.standardRequest(
+ ["open-ils.url_verify", "open-ils.url_verify.create_session"], {
+ "params": [openils.User.authtoken, name, search],
+ "async": true,
+ "onresponse": function(r) {
+ if (r = openils.Util.readResponse(r)) {
+ create_session.session_id = r; /* we're modal enough to get away with this */
+ create_session.save_tags();
+ }
+ }
+ }
+ );
+ };
+
+ /* 2) save the tag/subfield sets for URL extraction, */
+ create_session.save_tags = function() {
+ progress_dialog.attr("title", localeStrings.SAVING_TAGS);
+ progress_dialog.show(); /* sic */
+
+ uvus_progress = 0;
+
+ /* Note we're not using openils.PermaCrud, which is inadequate
+ * when you want transactions. Thanks for figuring it out, Bill. */
+ var pcrud_raw = new OpenSRF.ClientSession("open-ils.pcrud");
+
+ pcrud_raw.connect();
+
+ pcrud_raw.request({
+ "method": "open-ils.pcrud.transaction.begin",
+ "params": [openils.User.authtoken],
+ "oncomplete": function(r) {
+ create_session._create_uvus_one_at_a_time(
+ pcrud_raw,
+ create_session.tag_and_subfields.generate_uvus(
+ create_session.session_id
+ )
+ );
+ }
+ }).send();
+ };
+
+ /* 2b */
+ create_session._create_uvus_one_at_a_time = function(pcrud_raw, uvus_list) {
+ pcrud_raw.request({
+ "method": "open-ils.pcrud.create.uvus",
+ "params": [openils.User.authtoken, uvus_list[0]],
+ "oncomplete": function(r) {
+ var new_uvus = openils.Util.readResponse(r);
+ progress_dialog.update(
+ {"maximum": uvus_list.length, "progress": ++uvus_progress}
+ );
+
+ uvus_list.shift(); /* /now/ actually shorten the list */
+
+ if (uvus_list.length < 1) {
+ pcrud_raw.request({
+ "method": "open-ils.pcrud.transaction.commit",
+ "params": [openils.User.authtoken],
+ "oncomplete": function(r) {
+ pcrud_raw.disconnect();
+ create_session.perform_search();
+ }
+ }).send();
+
+ } else {
+ create_session._create_uvus_one_at_a_time(
+ pcrud_raw, uvus_list
+ );
+ }
+ }
+ }).send();
+ };
+
+ /* 3) search and populate the container (API call). */
+ create_session.perform_search = function() {
+ progress_dialog.attr("title", localeStrings.PERFORMING_SEARCH);
+ progress_dialog.show(true);
+
+ fieldmapper.standardRequest(
+ ["open-ils.url_verify", "open-ils.url_verify.session_perform_search"], {
+ "params": [openils.User.authtoken, create_session.session_id],
+ "async": true,
+ "onresponse": function(r) {
+ /* XXX atm, this responds with the number of created results */
+ r = openils.Util.readResponse(r);
+ progress_dialog.hide();
+ progress_dialog.attr("title", "");
+
+ console.warn("XXX TODO redirect to next interface");
+ }
+ }
+ );
};
/* At least in Dojo 1.3.3 (I know, I know), dijit.form.MultiSelect does
widget.parentField = 'parent_ou';
widget.startup();
widget.attr("value", openils.User.user.ws_ou());
+
+ create_session.org_selector = widget;
};
/* This is the thing that lets you add/remove rows of tab/subfield pairs */
this.counter++;
};
- this.get_all = function() {
+ this.generate_uvus = function(session_id) {
+ var uniquely_grouped = {};
+ dojo.query(
+ '[id^="t-and-s-row-"]', dojo.byId(this.container_id)
+ ).forEach(
+ function(row) {
+ var tag = dojo.query(".t-and-s-tag", row)[0].innerHTML;
+ var subfield = dojo.query(".t-and-s-subfields", row)[0].innerHTML;
+
+ var existing;
+ if ((existing = uniquely_grouped[tag])) { /* sic, assignment */
+ existing = openils.Util.uniqueElements(
+ (existing + subfield).split("")
+ ).sort().join("");
+ } else {
+ uniquely_grouped[tag] = subfield;
+ }
+ }
+ );
+
+ var uvus_list = [];
+ for (var tag in uniquely_grouped) {
+ var obj = new uvus();
+
+ obj.session(session_id);
+ obj.xpath(
+ "//marc:datafield[@tag='" + tag + "']/marc:subfield[" +
+ uniquely_grouped[tag].split("").map(
+ function(c) {
+ return "@code='" + c + "'";
+ }
+ ).join(" or ") +
+ "]"
+ );
+
+ uvus_list.push(obj);
+ }
+
+ return uvus_list;
};
}