test/integration/allocine_dossier_independant/js/libs/tracemanager.js
changeset 901 12b2cd7e9159
parent 899 445631dffdf8
equal deleted inserted replaced
899:445631dffdf8 901:12b2cd7e9159
     1 /*
     1 /*
     2  * Modelled Trace API
     2  * Modelled Trace API
       
     3  *
       
     4  * This file is part of ktbs4js.
       
     5  *
       
     6  * ktbs4js is free software: you can redistribute it and/or modify it
       
     7  * under the terms of the GNU Lesser General Public License as
       
     8  * published by the Free Software Foundation, either version 3 of the
       
     9  * License, or (at your option) any later version.
       
    10  *
       
    11  * ktbs4js is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with ktbs4js.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
     3  */
    19  */
     4 IriSP.TraceManager = function($) {
    20 /* FIXME: properly use require.js feature. This will do for debugging in the meantime */
       
    21 window.tracemanager = (function($) {
     5      // If there are more than MAX_FAILURE_COUNT synchronisation
    22      // If there are more than MAX_FAILURE_COUNT synchronisation
     6      // failures, then disable synchronisation
    23      // failures, then disable synchronisation
     7      MAX_FAILURE_COUNT = 20;
    24      MAX_FAILURE_COUNT = 20;
     8 
    25 
     9      // If there are more than MAX_BUFFER_SIZE obsels in the buffer,
    26      // If there are more than MAX_BUFFER_SIZE obsels in the buffer,
    10      // then "compress" them as a single "ktbsFullBuffer"
    27      // then "compress" them as a single "ktbsFullBuffer"
    11      MAX_BUFFER_SIZE = 500;
    28      MAX_BUFFER_SIZE = 500;
    12 
    29 
    13      var BufferedService_prototype = {
    30      var BufferedService_prototype = {
    14          /*
    31          /*
    15 * Buffered service for traces
    32           *  Buffered service for traces
    16 */
    33           */
    17          // url: "",
    34          // url: "",
    18          // buffer: [],
    35          // buffer: [],
    19          // isReady: false,
    36          // isReady: false,
    20          // timer: null,
    37          // timer: null,
    21          // failureCount: 0,
    38          // failureCount: 0,
    70                  }
    87                  }
    71              }
    88              }
    72          },
    89          },
    73 
    90 
    74          /* Sync mode: delayed, sync (immediate sync), none (no
    91          /* Sync mode: delayed, sync (immediate sync), none (no
    75 * synchronisation with server, the trace has to be explicitly saved
    92           * synchronisation with server, the trace has to be explicitly saved
    76 * if needed */
    93           * if needed */
    77          set_sync_mode: function(mode, default_subject) {
    94          set_sync_mode: function(mode, default_subject) {
    78              this.sync_mode = mode;
    95              this.sync_mode = mode;
    79              if (! this.isReady && mode !== "none")
    96              if (! this.isReady && mode !== "none")
    80                  this.init(default_subject);
    97                  this.init(default_subject);
    81              if (mode == 'delayed') {
    98              if (mode == 'delayed') {
   116                  this.timer = null;
   133                  this.timer = null;
   117              }
   134              }
   118          },
   135          },
   119 
   136 
   120          /*
   137          /*
   121 * Initialize the sync service
   138           * Initialize the sync service
   122 */
   139           */
   123          init: function(default_subject) {
   140          init: function(default_subject) {
   124              var self = this;
   141              var self = this;
   125              if (this.isReady)
   142              if (this.isReady)
   126                  /* Already initialized */
   143                  /* Already initialized */
   127                  return;
   144                  return;
   168      };
   185      };
   169      BufferedService.prototype = BufferedService_prototype;
   186      BufferedService.prototype = BufferedService_prototype;
   170 
   187 
   171      var Trace_prototype = {
   188      var Trace_prototype = {
   172          /* FIXME: We could/should use a sorted list such as
   189          /* FIXME: We could/should use a sorted list such as
   173 http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
   190           http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
   174 to speed up queries based on time */
   191           to speed up queries based on time */
   175          obsels: [],
   192          obsels: [],
   176          /* Trace URI */
   193          /* Trace URI */
   177          uri: "",
   194          uri: "",
   178          default_subject: "",
   195          default_subject: "",
   179          /* baseuri is used as the base URI to resolve relative
   196          /* baseuri is used as the base URI to resolve relative
   180 * attribute-type names in obsels. Strictly speaking, this
   197           * attribute-type names in obsels. Strictly speaking, this
   181 * should rather be expressed as a reference to model, or
   198           * should rather be expressed as a reference to model, or
   182 * more generically, as a qname/URI dict */
   199           * more generically, as a qname/URI dict */
   183          baseuri: "",
   200          baseuri: "",
   184          /* Mapping of obsel type or property name to a compact
   201          /* Mapping of obsel type or property name to a compact
   185 * representation (shorthands).
   202           * representation (shorthands).
   186 */
   203           */
   187          shorthands: null,
   204          shorthands: null,
   188          syncservice: null,
   205          syncservice: null,
   189 
   206 
   190          /* Define the trace URI */
   207          /* Define the trace URI */
   191          set_uri: function(uri) {
   208          set_uri: function(uri) {
   192              this.uri = uri;
   209              this.uri = uri;
   193          },
   210          },
   194 
   211 
   195          /* Sync mode: delayed, sync (immediate sync), none (no
   212          /* Sync mode: delayed, sync (immediate sync), none (no
   196 * synchronisation with server, the trace has to be explicitly saved
   213           * synchronisation with server, the trace has to be explicitly saved
   197 * if needed */
   214           * if needed */
   198          set_sync_mode: function(mode) {
   215          set_sync_mode: function(mode) {
   199              if (this.syncservice !== null) {
   216              if (this.syncservice !== null) {
   200                  this.syncservice.set_sync_mode(mode, this.default_subject);
   217                  this.syncservice.set_sync_mode(mode, this.default_subject);
   201              }
   218              }
   202          },
   219          },
   203 
   220 
   204          /*
   221          /*
   205 * Return a list of the obsels of this trace matching the parameters
   222           * Return a list of the obsels of this trace matching the parameters
   206 */
   223           */
   207          list_obsels: function(_begin, _end, _reverse) {
   224          list_obsels: function(_begin, _end, _reverse) {
   208              var res;
   225              var res;
   209              if (typeof _begin !== 'undefined' || typeof _end !== 'undefined') {
   226              if (typeof _begin !== 'undefined' || typeof _end !== 'undefined') {
   210                  /*
   227                  /*
   211 * Not optimized yet.
   228                   * Not optimized yet.
   212 */
   229                   */
   213                  res = [];
   230                  res = [];
   214                  var l = this.obsels.length;
   231                  var l = this.obsels.length;
   215                  for (var i = 0; i < l; i++) {
   232                  for (var i = 0; i < l; i++) {
   216                      var o = this.obsels[i];
   233                      var o = this.obsels[i];
   217                      if ((typeof _begin !== 'undefined' && o.begin > _begin) && (typeof _end !== 'undefined' && o.end < _end)) {
   234                      if ((typeof _begin !== 'undefined' && o.begin > _begin) && (typeof _end !== 'undefined' && o.end < _end)) {
   235              return res;
   252              return res;
   236 
   253 
   237          },
   254          },
   238 
   255 
   239          /*
   256          /*
   240 * Return the obsel of this trace identified by the URI, or undefined
   257           * Return the obsel of this trace identified by the URI, or undefined
   241 */
   258           */
   242          get_obsel: function(id) {
   259          get_obsel: function(id) {
   243              for (var i = 0; i < this.obsels.length; i++) {
   260              for (var i = 0; i < this.obsels.length; i++) {
   244                  /* FIXME: should check against variations of id/uri, take this.baseuri into account */
   261                  /* FIXME: should check against variations of id/uri, take this.baseuri into account */
   245                  if (this.obsels[i].uri === id) {
   262                  if (this.obsels[i].uri === id) {
   246                      return this.obsels[i];
   263                      return this.obsels[i];
   294          }
   311          }
   295      };
   312      };
   296 
   313 
   297      var Trace = function(uri, requestmode) {
   314      var Trace = function(uri, requestmode) {
   298          /* FIXME: We could/should use a sorted list such as
   315          /* FIXME: We could/should use a sorted list such as
   299 http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
   316           http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
   300 to speed up queries based on time */
   317           to speed up queries based on time */
   301          this.obsels = [];
   318          this.obsels = [];
   302          /* Trace URI */
   319          /* Trace URI */
   303          if (uri === undefined)
   320          if (uri === undefined)
   304              uri = "";
   321              uri = "";
   305          this.uri = uri;
   322          this.uri = uri;
   319      };
   336      };
   320      Trace.prototype = Trace_prototype;
   337      Trace.prototype = Trace_prototype;
   321 
   338 
   322      var Obsel_prototype = {
   339      var Obsel_prototype = {
   323          /* The following attributes are here for documentation
   340          /* The following attributes are here for documentation
   324 * purposes. They MUST be defined in the constructor
   341           * purposes. They MUST be defined in the constructor
   325 * function. */
   342           * function. */
   326          trace: undefined,
   343          trace: undefined,
   327          type: undefined,
   344          type: undefined,
   328          begin: undefined,
   345          begin: undefined,
   329          end: undefined,
   346          end: undefined,
   330          subject: undefined,
   347          subject: undefined,
   371          },
   388          },
   372          list_relating_obsels: function (rt) {
   389          list_relating_obsels: function (rt) {
   373              /* FIXME: not implemented yet */
   390              /* FIXME: not implemented yet */
   374          },
   391          },
   375          /*
   392          /*
   376 * Return the value of the given attribute type for this obsel
   393           * Return the value of the given attribute type for this obsel
   377 */
   394           */
   378          get_attribute_value: function(at) {
   395          get_attribute_value: function(at) {
   379              if (typeof at === "string")
   396              if (typeof at === "string")
   380                  /* It is a URI */
   397                  /* It is a URI */
   381                  return this.attributes[at];
   398                  return this.attributes[at];
   382              else
   399              else
   412          del_related_obsel: function(rt, value) {
   429          del_related_obsel: function(rt, value) {
   413              /* FIXME: not implemented yet */
   430              /* FIXME: not implemented yet */
   414          },
   431          },
   415 
   432 
   416          /*
   433          /*
   417 * Return a JSON representation of the obsel
   434           * Return a JSON representation of the obsel
   418 */
   435           */
   419          toJSON: function() {
   436          toJSON: function() {
   420              var r = {
   437              var r = {
   421                  "@id": this.id,
   438                  "@id": this.id,
   422                  "@type": this.type,
   439                  "@type": this.type,
   423                  "begin": this.begin,
   440                  "begin": this.begin,
   430              }
   447              }
   431              return r;
   448              return r;
   432          },
   449          },
   433 
   450 
   434          /*
   451          /*
   435 * Return a compact JSON representation of the obsel.
   452           * Return a compact JSON representation of the obsel.
   436 * Use predefined + custom shorthands for types/properties
   453           * Use predefined + custom shorthands for types/properties
   437 */
   454           */
   438          toCompactJSON: function() {
   455          toCompactJSON: function() {
   439              var r = {
   456              var r = {
   440                  "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type),
   457                  "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type),
   441                  "@b": this.begin,
   458                  "@b": this.begin,
   442              };
   459              };
   483      Obsel.prototype = Obsel_prototype;
   500      Obsel.prototype = Obsel_prototype;
   484 
   501 
   485      var TraceManager_prototype = {
   502      var TraceManager_prototype = {
   486          traces: [],
   503          traces: [],
   487          /*
   504          /*
   488 * Return the trace with id name
   505           * Return the trace with id name
   489 * If it was not registered, return undefined.
   506           * If it was not registered, return undefined.
   490 */
   507           */
   491          get_trace: function(name) {
   508          get_trace: function(name) {
   492              return this.traces[name];
   509              return this.traces[name];
   493          },
   510          },
   494 
   511 
   495          /*
   512          /*
   496 * Explicitly create and initialize a new trace with the given name.
   513           * Explicitly create and initialize a new trace with the given name.
   497 * The optional uri parameter allows to initialize the trace URI.
   514           * The optional uri parameter allows to initialize the trace URI.
   498 *
   515           *
   499 * If another existed with the same name before, then it is replaced by a new one.
   516           * If another existed with the same name before, then it is replaced by a new one.
   500 */
   517           */
   501          init_trace: function(name, params)
   518          init_trace: function(name, params)
   502          {
   519          {
   503              if (window.console) window.console.log("init_trace", params);
   520              if (window.console) window.console.log("init_trace", params);
   504              url = params.url ? params.url : "";
   521              url = params.url ? params.url : "";
   505              requestmode = params.requestmode ? params.requestmode : "POST";
   522              requestmode = params.requestmode ? params.requestmode : "POST";
   516      var TraceManager = function() {
   533      var TraceManager = function() {
   517          this.traces = {};
   534          this.traces = {};
   518      };
   535      };
   519      TraceManager.prototype = TraceManager_prototype;
   536      TraceManager.prototype = TraceManager_prototype;
   520 
   537 
   521      var tracemanager = new TraceManager();
   538      var tracemanager  = new TraceManager();
   522      return tracemanager;
   539      return tracemanager;
   523  };
   540  })(jQuery);