+ //resource_granularity = model_resource.granularity === undefined ? RESOURCE_DEFAULT_GRANULARITY : model_resource.granularity;\r
+\r
+ console.log("Selected", index, model_lease, model_resource);\r
+\r
+ var day_timestamp = SchedulerDateSelected.getTime() / 1000;\r
+ var start_time = day_timestamp + index * model_resource.granularity; // XXX resource_granularity\r
+ var end_time = day_timestamp + (index + 1) * model_resource.granularity; //\r
+ var start_date = new Date(start_time * 1000);\r
+ var end_date = new Date(end_time * 1000);\r
+\r
+ var lease_key = manifold.metadata.get_key('lease');\r
+\r
+ // We search for leases in the cache we previously constructed\r
+ var resource_leases = $scope._leases_by_resource[model_resource.urn];\r
+\r
+ switch (model_lease.status)\r
+ {\r
+ case 'free': // out\r
+ case 'pendingout':\r
+ if (resource_leases) {\r
+ /* Search for leases before */\r
+ $.each(resource_leases, function(i, other) {\r
+ if (other.end_time != start_time)\r
+ return true; // ~ continue\r
+ \r
+ /* The lease 'other' is just before, and there should not exist\r
+ * any other lease before it */\r
+ start_time = other.start_time;\r
+ \r
+ other_key = {\r
+ resource: other.resource,\r
+ start_time: other.start_time,\r
+ end_time: other.end_time\r
+ }\r
+ // This is needed to create a hashable object\r
+ other_key.hashCode = manifold.record_hashcode(lease_key.sort());\r
+ other_key.equals = manifold.record_equals(lease_key);\r
+ \r
+ data = {\r
+ state: STATE_SET,\r
+ key : null,\r
+ op : STATE_SET_REMOVE,\r
+ value: other_key\r
+ }\r
+ manifold.raise_event($scope.instance.options.query_lease_uuid, FIELD_STATE_CHANGED, data);\r
+ /* Remove from local cache also, unless we listen to events from outside */\r
+ $scope._leases_by_resource[model_resource.urn] = $.grep($scope._leases_by_resource[model_resource.urn], function(x) { return x != other; });\r
+ return false; // ~ break\r
+ });\r
+ \r
+ /* Search for leases after */\r
+ $.each(resource_leases, function(i, other) {\r
+ if (other.start_time != end_time)\r
+ return true; // ~ continue\r
+ \r
+ /* The lease 'other' is just after, and there should not exist\r
+ * any other lease after it */\r
+ end_time = other.end_time;\r
+ other_key = {\r
+ resource: other.resource,\r
+ start_time: other.start_time,\r
+ end_time: other.end_time\r
+ }\r
+ // This is needed to create a hashable object\r
+ other_key.hashCode = manifold.record_hashcode(lease_key.sort());\r
+ other_key.equals = manifold.record_equals(lease_key);\r
+ \r
+ data = {\r
+ state: STATE_SET,\r
+ key : null,\r
+ op : STATE_SET_REMOVE,\r
+ value: other_key\r
+ }\r
+ manifold.raise_event($scope.instance.options.query_lease_uuid, FIELD_STATE_CHANGED, data);\r
+ /* Remove from local cache also, unless we listen to events from outside */\r
+ $scope._leases_by_resource[model_resource.urn] = $.grep($scope._leases_by_resource[model_resource.urn], function(x) { return x != other; });\r
+ return false; // ~ break\r
+ });\r
+ }\r
+ \r
+ $scope._create_new_lease(model_resource.urn, start_time, end_time);\r
+ model_lease.status = (model_lease.status == 'free') ? 'pendingin' : 'selected';\r
+ // unless the exact same lease already existed (pending_out status for the lease, not the cell !!)\r
+\r
+ break;\r
+\r
+ case 'selected':\r
+ case 'pendingin':\r
+ // We remove the cell\r
+\r
+ /* We search for leases including this cell. Either 0, 1 or 2.\r
+ * 0 : NOT POSSIBLE, should be checked.\r
+ * 1 : either IN or OUT, we have make no change in the session\r
+ * 2 : both will be pending, since we have made a change in the session\r
+ * /!\ need to properly remove pending_in leases when removed again\r
+ */\r
+ if (resource_leases) {\r
+ $.each(resource_leases, function(i, other) {\r
+ if ((other.start_time <= start_time) && (other.end_time >= end_time)) {\r
+ // The cell is part of this lease.\r
+\r
+ // If the cell is not at the beginning of the lease, we recreate a lease with cells before\r
+ if (start_time > other.start_time) {\r
+ $scope._create_new_lease(model_resource.urn, other.start_time, start_time);\r
+ }\r
+\r
+ // If the cell is not at the end of the lease, we recreate a lease with cells after\r
+ if (end_time < other.end_time) {\r
+ $scope._create_new_lease(model_resource.urn, end_time, other.end_time);\r
+ }\r
+ \r
+ // The other lease will be removed\r
+ $scope._remove_lease(other);\r
+ }\r
+ // NOTE: We can interrupt the search if we know that there is a single lease (depending on the status).\r
+ });\r
+ }\r
+ \r
+ // cf comment in previous switch case\r
+ model_lease.status = (model_lease.status == 'selected') ? 'pendingout' : 'free';\r
+\r
+ break;\r
+\r
+ case 'reserved':\r
+ case 'maintainance':\r
+ // Do nothing\r
+ break;\r
+ }\r
+ \r
+\r
+ $scope._dump_leases();\r
+ };\r
+ \r
+ $scope._dump_leases = function()\r
+ {\r
+ // DEBUG: display all leases and their status in the log\r
+ var leases = manifold.query_store.get_records($scope.instance.options.query_lease_uuid);\r
+ console.log("--------------------");\r
+ $.each(leases, function(i, lease) {\r
+ var key = manifold.metadata.get_key('lease');\r
+ var lease_key = manifold.record_get_value(lease, key);\r
+ var state = manifold.query_store.get_record_state($scope.instance.options.query_lease_uuid, lease_key, STATE_SET);\r
+ var state_str;\r
+ switch(state) {\r
+ case STATE_SET_IN:\r
+ state_str = 'STATE_SET_IN';\r
+ break;\r
+ case STATE_SET_OUT:\r
+ state_str = 'STATE_SET_OUT';\r
+ break;\r
+ case STATE_SET_IN_PENDING:\r
+ state_str = 'STATE_SET_IN_PENDING';\r
+ break;\r
+ case STATE_SET_OUT_PENDING:\r
+ state_str = 'STATE_SET_OUT_PENDING';\r
+ break;\r
+ case STATE_SET_IN_SUCCESS:\r
+ state_str = 'STATE_SET_IN_SUCCESS';\r
+ break;\r
+ case STATE_SET_OUT_SUCCESS:\r
+ state_str = 'STATE_SET_OUT_SUCCESS';\r
+ break;\r
+ case STATE_SET_IN_FAILURE:\r
+ state_str = 'STATE_SET_IN_FAILURE';\r
+ break;\r
+ case STATE_SET_OUT_FAILURE:\r
+ state_str = 'STATE_SET_OUT_FAILURE';\r
+ break;\r
+ }\r
+ console.log("LEASE", new Date(lease.start_time * 1000), new Date(lease.end_time * 1000), lease.resource, state_str);\r
+ });\r
+ };\r
+\r
+ // Return this object reference.\r
+ return (this);\r
+\r
+ }\r
+\r
+ // Define the Controller as the constructor function.\r
+ app.controller("SchedulerCtrl", Controller);\r
+\r
+})(angular, ManifoldApp);\r
+\r
+/******************************************************************************\r
+ * MANIFOLD PLUGIN *\r
+ ******************************************************************************/\r
+\r
+(function($) {\r
+ scheduler2 = Plugin.extend({\r
+\r
+ /** XXX to check\r