1 /*
  2  *  Copyright (C) 2008-2009 WaveMaker Software, Inc.
  3  *
  4  *  This file is part of the WaveMaker Client Runtime.
  5  *
  6  *  Licensed under the Apache License, Version 2.0 (the "License");
  7  *  you may not use this file except in compliance with the License.
  8  *  You may obtain a copy of the License at
  9  *
 10  *      http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  *  Unless required by applicable law or agreed to in writing, software
 13  *  distributed under the License is distributed on an "AS IS" BASIS,
 14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *  See the License for the specific language governing permissions and
 16  *  limitations under the License.
 17  */
 18 dojo.provide("wm.base.components.ServiceQueue");
 19 
 20 dojo.declare("wm.ServiceQueue", wm.Component, {
 21 	services: "",
 22 	init: function() {
 23 		this._services = [];
 24 		this._serviceConnections = [];
 25 		this.inherited(arguments);
 26 	},
 27 	getServicesCount: function() {
 28 		return this._services && this._services.length;
 29 	},
 30 	getServicesList: function() {
 31 		for (var i=0, l=[], ss=this.services.split(","), s, v; (s=ss[i]); i++) {
 32 			v = this.getValueById(dojo.string.trim(s));
 33 			if (v)
 34 				l.push(v);
 35 		}
 36 		return l;
 37 	},
 38 	update: function() {
 39 		this.beginUpdate();
 40 	},
 41 	beginUpdate: function() {
 42 		this._services = this.getServicesList();
 43 		this.connectServices();
 44 		this._currentService = 0;
 45 		this.updateNextService();
 46 	},
 47 	getCurrentService: function() {
 48 		return this._services[this._currentService];
 49 	},
 50 	updateNextService: function() {
 51 		if (this._currentService < this.getServicesCount()) {
 52 			var s = this.getCurrentService();
 53 			this._currentService++;
 54 			s.update();
 55 		} else
 56 			this.completeUpdate();
 57 	},
 58 	completeUpdate: function() {
 59 		this.disconnectServices();
 60 	},
 61 	abortUpdate: function() {
 62 		this.disconnectServices();
 63 	},
 64 	connectServices: function() {
 65 		this.disconnectServices();
 66 		dojo.forEach(this._services, dojo.hitch(this, function(s) {
 67 			this._serviceConnections.push(dojo.connect(s, "onResult", this, "updateNextService"));
 68 			this._serviceConnections.push(dojo.connect(s, "onError", this, "abortUpdate"));
 69 		}));
 70 	},
 71 	disconnectServices: function() {
 72 		dojo.forEach(this._serviceConnections, function(s) {
 73 			dojo.disconnect(s);
 74 		});
 75 	}
 76 });
 77 
 78 wm.ServiceQueue.extend({
 79 	getAvailableServicesList: function() {
 80 		var d = wm.listComponentIds([studio.application, studio.page], wm.ServiceVariable);
 81 		d = d.concat(wm.listComponentIds([studio.application, studio.page], wm.NavigationCall));
 82 		// don't show this!
 83 		var i = dojo.indexOf(d, this.owner.getId());
 84 		if (i != -1)
 85 			d.splice(i, 1);
 86 		return d;
 87 	},
 88 	write: function(inIndent) {
 89 		return this.services ? this.inherited(arguments): null;
 90 	}
 91 });
 92