erp5storage.taskmanagerview.js 10.5 KB
Newer Older
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
/*
 * Copyright 2013, Nexedi SA
 * Released under the LGPL license.
 * http://www.gnu.org/licenses/lgpl.html
 */

/*jslint indent: 2, maxlen: 80, nomen: true */
/*global jIO, UriTemplate, FormData, RSVP, URI, DOMParser, Blob, console,
  ProgressEvent, define, ERP5Storage */

(function (dependencies, module) {
  "use strict";
  if (typeof define === 'function' && define.amd) {
    return define(dependencies, module);
  }
  module(RSVP, jIO, URI, UriTemplate, ERP5Storage);
}([
  "rsvp",
  "jio",
  "uri",
  "uritemplate",
  "erp5storage"
], function (RSVP, jIO, URI, UriTemplate, ERP5Storage) {
  "use strict";

  var hasOwnProperty = Function.prototype.call.bind(
    Object.prototype.hasOwnProperty
  ), constant = {};


  constant.task_state_to_action = {
    // Auto Planned : ?
    "Cancelled": "cancel",
    "Confirmed": "confirm",
    // Draft : ?
    "Deleted": "delete",
    "Ordered": "order",
    "Planned": "plan"
  };

  constant.allDocsState = {"data": {
    "total_rows": 7,
    "rows": [{
      "id": "taskmanager:state_module/1",
      "doc": {
        "type": "State",
        "title": "Auto Planned"
        //"state": "Auto Planned"
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/2",
      "doc": {
        "type": "State",
        "title": "Cancelled",
        //"state": "Cancelled",
        "action": constant.task_state_to_action.Cancelled
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/3",
      "doc": {
        "type": "State",
        "title": "Confirmed",
        //"state": "Confirmed",
        "action": constant.task_state_to_action.Confirmed
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/4",
      "doc": {
        "type": "State",
        "title": "Deleted",
        //"state": "Deleted",
        "action": constant.task_state_to_action.Deleted
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/5",
      "doc": {
        "type": "State",
        "title": "Draft"
        //"state": "Draft"
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/6",
      "doc": {
        "type": "State",
        "title": "Ordered",
        //"state": "Ordered",
        "action": constant.task_state_to_action.Ordered
      },
      "values": {}
    }, {
      "id": "taskmanager:state_module/7",
      "doc": {
        "type": "State",
        "title": "Planned",
        //"state": "Planned",
        "action": constant.task_state_to_action.Planned
      },
      "values": {}
    }]
  }};

  constant.mapping_jio_to_erp5 = {};
  constant.mapping_erp5_to_jio = {};

  // XXX docstring
  function addMetadataMapping(jio_type, erp5_type) {
    if (typeof jio_type !== "string" || typeof erp5_type !== "string" ||
        !jio_type || !erp5_type) {
      throw new TypeError("addMetadataMapping(): The two arguments " +
                          "must be non empty strings");
    }
    if (constant.mapping_jio_to_erp5[jio_type]) {
      throw new TypeError("A mapping already exists for jIO metadata '" +
                          jio_type + "'");
    }
    if (constant.mapping_erp5_to_jio[erp5_type]) {
      throw new TypeError("A mapping already exists for ERP5 metadata '" +
                          erp5_type + "'");
    }
    constant.mapping_jio_to_erp5[jio_type] = erp5_type;
    constant.mapping_erp5_to_jio[erp5_type] = jio_type;
  }
  addMetadataMapping("type", "portal_type");
  addMetadataMapping("state", "translated_simulation_state_title");
  addMetadataMapping("project", "source_project_title");
  addMetadataMapping("start", "start_date");
  addMetadataMapping("stop", "stop_date");

  // addMetadataMapping("location", "destination_title");
  // addMetadataMapping("source", "source_title");
  // addMetadataMapping("requester", "destination_decision_title");
  // addMetadataMapping("contributor", "contributor_list");
  // addMetadataMapping("category", "category_list");

  // XXX docstring
  function toERP5Metadata(jio_type) {
    /*jslint forin: true */
    if (typeof jio_type === "string") {
      return constant.mapping_jio_to_erp5[jio_type] || jio_type;
    }
    var result = {}, key;
    if (typeof jio_type === "object" && jio_type) {
      for (key in jio_type) {
        if (hasOwnProperty(jio_type, key)) {
          result[toERP5Metadata(key)] = jio_type[key];
        }
      }
    }
    return result;
  }

  // XXX docstring
  function toJIOMetadata(erp5_type) {
    /*jslint forin: true */
    if (typeof erp5_type === "string") {
      return constant.mapping_erp5_to_jio[erp5_type] || erp5_type;
    }
    var result = {}, key;
    if (typeof erp5_type === "object" && erp5_type) {
      for (key in erp5_type) {
        if (hasOwnProperty(erp5_type, key)) {
          result[toJIOMetadata(key)] = erp5_type[key];
        }
      }
    }
    return result;
  }

  function getHatoas(param) {
    return ERP5Storage.getSiteDocument(this._url).
      then(function (site_hal) {
        // XXX need to get modified metadata
        return jIO.util.ajax({
          "type": "GET",
          "url": UriTemplate.parse(site_hal._links.traverse.href)
                            .expand({
              relative_url: param._id,
              view: "view"
            }),
          "xhrFields": {
            withCredentials: true
          }
        });
      });
  }

  ERP5Storage.onView.taskmanager = {};
  ERP5Storage.onView.taskmanager.get = function (param, options) {
194 195 196 197
    return ERP5Storage.onView["default"].get.call(this, param, options).
      then(function (answer) {
        answer.data = toJIOMetadata(answer.data);
        return answer;
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
      });
  };

  ERP5Storage.onView.taskmanager.put = function (metadata, options) {
    return getHatoas.call(this, metadata, options).
      then(function (event) {
        var result = JSON.parse(event.target.responseText),
          put_action = result._embedded._view._actions.put,
          renderer_form = result._embedded._view,
          data = new FormData(),
          action,
          action_url;
        data.append(renderer_form.form_id.key,
                    renderer_form.form_id['default']);
        metadata = toERP5Metadata(metadata);
        if (metadata.translated_simulation_state_title !==
            result.translated_simulation_state_title) {
          action = constant.task_state_to_action[
            metadata.translated_simulation_state_title
          ];
          if (!action) {
            throw new Error(
              "State \"" +
                metadata.translated_simulation_state_title +
                "\" is not available."
            );
          }
          if (result._links && Array.isArray(result._links.action_workflow)) {
            result._links.action_workflow.some(function (workflow) {
              if (workflow.name === action + "_action") {
                action_url = workflow.href;
                return true;
              }
              return false;
            });
          }
          if (!action_url) {
            throw new Error("Can not change state.");
          }
          // XXX ERP5 side need to implement this feature
          // -> do on action_url, then return vars;
          console.warn("NotImplemented");
          console.log("Do on:", action_url);
          return {
            "put_action": put_action,
            "renderer_form": renderer_form,
            "data": data
          };
        }
        return {
          "put_action": put_action,
          "renderer_form": renderer_form,
          "data": data
        };
      }).then(function (vars) {
        /*jslint forin: true */
        var key;
        for (key in metadata) {
          if (hasOwnProperty(metadata, key)) {
            if (key !== "_id") {
              // Hardcoded my_ ERP5 behaviour
              if (hasOwnProperty(vars.renderer_form, "my_" + key)) {
                vars.data.append(
                  vars.renderer_form["my_" + key].key,
                  metadata[key]
                );
264 265 266
              } // else {
              //   throw new Error("Can not save property " + key);
              // }
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
            }
          }
        }
        return jIO.util.ajax({
          "type": vars.put_action.method,
          "url": vars.put_action.href,
          "data": vars.data,
          "xhrFields": {
            withCredentials: true
          }
        });
      });
  };

  ERP5Storage.onView.taskmanager.allDocs = function (param, options) {
    var that = this;
    /*jslint unparam: true */
    function changeQueryKeysToERP5Metadata() {
      if (Array.isArray(options.select_list)) {
        options.select_list = options.select_list.map(toERP5Metadata);
      }
      try {
        options.query = jIO.QueryFactory.create(options.query);
        options.query.onParseSimpleQuery = function (object) {
          object.parsed.key = toERP5Metadata(object.parsed.key);
        };
        return options.query.parse().then(function (query) {
          options.query = jIO.QueryFactory.create(query).toString();
        });
      } catch (e) {
        delete options.query;
        return RSVP.resolve();
      }
    }

    function requestERP5(site_hal) {
      return jIO.util.ajax({
        "type": "GET",
        "url": UriTemplate.parse(site_hal._links.raw_search.href)
          .expand({
            query: options.query,
            // XXX Force erp5 to return embedded document
            select_list: options.select_list || [
              "portal_type",
              "title",
              "reference",
              "translated_simulation_state_title",
              "description"
            ],
            limit: options.limit
          }),
        "xhrFields": {
          withCredentials: true
        }
      });
    }

    function formatAnswer(event) {
      var catalog_json = JSON.parse(event.target.responseText),
        data = catalog_json._embedded.contents,
        count = data.length,
        i,
        uri,
        item,
        result = [];
      for (i = 0; i < count; i += 1) {
        item = data[i];
        uri = new URI(item._links.self.href);
        delete item._links;
        item = toJIOMetadata(item);
        result.push({
          id: uri.segment(2),
          doc: item,
          value: item
        });
      }
      return {"data": {"rows": result, "total_rows": result.length}};
    }

    function continueAllDocs() {
      // Hard code for states
      if (options.query === "portal_type: \"State\"") {
        return constant.allDocsState;
      }

      return ERP5Storage.getSiteDocument(that._url).
        then(requestERP5).
        then(formatAnswer);
    }

    return changeQueryKeysToERP5Metadata().then(continueAllDocs);

  };

}));