PageRenderTime 44ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/ishafoundation/archives/transcript/db/DatabaseManagerUsingEXist.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 180 lines | 138 code | 14 blank | 28 comment | 22 complexity | 7c295bbd6f3f149a2e7bd52dc900f091 MD5 | raw file
  1/*
  2   Transcript Studio for Isha Foundation: An XML based application that allows users to define 
  3   and store contextual metadata for contiguous sections within a text document. 
  4
  5   Copyright 2008 Mark Carter, Swami Kevala
  6
  7   This file is part of Transcript Studio for Isha Foundation.
  8
  9   Transcript Studio for Isha Foundation is free software: you can redistribute it and/or modify it 
 10   under the terms of the GNU General Public License as published by the Free Software 
 11   Foundation, either version 3 of the License, or (at your option) any later version.
 12
 13   Transcript Studio for Isha Foundation is distributed in the hope that it will be useful, but 
 14   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 15   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License along with 
 18   Transcript Studio for Isha Foundation. If not, see http://www.gnu.org/licenses/.
 19*/
 20
 21package org.ishafoundation.archives.transcript.db
 22{
 23	import mx.controls.Alert;
 24	import mx.rpc.http.HTTPService;
 25	
 26	import name.carter.mark.flex.exist.EXistRESTClient;
 27	import name.carter.mark.flex.exist.EXistXMLRPCClient;
 28	import name.carter.mark.flex.util.Utils;
 29	import name.carter.mark.flex.util.XMLUtils;
 30	import name.carter.mark.flex.util.remote.ClientManager;
 31	
 32	import org.ishafoundation.archives.transcript.util.ApplicationUtils;
 33	import org.ishafoundation.archives.transcript.util.PreferencesSharedObject;
 34	
 35	public class DatabaseManagerUsingEXist implements DatabaseManager
 36	{
 37		[Bindable]
 38		public var user:User;
 39		
 40		private var remoteMgr:ClientManager;
 41		private var loggedIn:Boolean = false;
 42		
 43		public function DatabaseManagerUsingEXist(username:String, password:String, successFunc:Function, failureFunc:Function) {
 44			trace("Using eXist URL: " + DatabaseConstants.EXIST_URL);
 45			this.remoteMgr = new ClientManager(DatabaseConstants.EXIST_URL, username, password);
 46			remoteMgr.testConnection(function(response:Object):void {
 47				loggedIn = true;
 48				// write db config to the shared object
 49				PreferencesSharedObject.writeDbURL(DatabaseConstants.EXIST_URL);
 50				PreferencesSharedObject.writeDbUsername(username);
 51				
 52				checkClientVersionAllowed(function():void {
 53					getUserGroupNames(function(groupNames:Array):void {
 54						user = new User(username, groupNames);
 55						successFunc();
 56					});
 57				}, failureFunc);
 58			},
 59			function(msg:String):void {
 60				loggedIn = false;
 61				failureFunc("Login failed because: " + msg);
 62			});
 63		}
 64
 65		public function testConnection(successFunction:Function, failureFunction:Function):void {
 66			remoteMgr.testConnection(function(response:Object):void {
 67				loggedIn = true;
 68				successFunction();
 69			},
 70			function(msg:String):void {
 71				loggedIn = false;
 72				failureFunction("Login failed because: " + msg);
 73			});
 74		}
 75		
 76		public function retrieveXML(successFunc:Function, failureFunc:Function, tagName:String = null, id:String = null, collectionPath:String = null):void {
 77			var params:Object = {}
 78			if (tagName != null) {
 79				params.tagName = tagName;
 80			}
 81			if (id != null) {
 82				params.id = id;
 83			}
 84			if (collectionPath != null) {
 85				params.collectionPath = collectionPath;
 86			}
 87			executeStoredXQuery("retrieve-xml-doc.xql", params, function(returnVal:Object):void {
 88				var returnXML:XML = XMLUtils.convertToXML(returnVal, true);
 89				if (returnXML == null) {
 90					successFunc(null);
 91					return;
 92				}
 93				var items:XMLList;
 94				if (returnXML.localName() == "result") {
 95					items = returnXML.*;
 96				}
 97				else {
 98					items = new XMLList(returnXML);
 99				}
100				switch (items.length()) {
101					case 1:
102						successFunc(items[0]);
103						break;
104					default:
105						var msg:String = (items.length() == 0) ? "Could not find" : "More than one"; 
106						msg += (tagName == null) ? "" : " " + tagName;
107						msg += " xml doc";
108						msg += (id == null) ? "" : " with id: " + id;
109						if (items.length() > 0) {
110							msg + ": ";
111							msg += items.attribute('_document-uri').toXMLString();
112						}
113						failureFunc(msg);
114						break;
115				}
116			}, failureFunc, HTTPService.RESULT_FORMAT_E4X);
117		}
118		
119		/*public function storeXML(xml:XML, successFunc:Function, failureFunc:Function):void {
120			//new EXistXMLRPCClient(remoteMgr.getXMLRPCClient()).storeXML(xmlPath, xml, successFunction, failureFunction);
121			var params:Object = {xmlStr:xml.toXMLString()};
122			executeStoredXQuery("store-xml-doc.xql", params, function(id:String):void {
123				trace("Successfully stored xml doc: " + id);
124				successFunc(id);
125			}, failureFunc);
126		}*/
127		
128		public function query(xQuery:String, args:Array, successFunc:Function, failureFunc:Function):void {
129			if (!this.loggedIn) {
130				testConnection(function():void {
131					query(xQuery, args, successFunc, failureFunc);
132				}, function(msg:String):void {
133					failureFunc("Tried to execute query but not connected to database");					
134				});
135				return;
136			}
137			new EXistXMLRPCClient(remoteMgr.getXMLRPCClient()).query(xQuery, args, successFunc, failureFunc);			
138		}
139		
140		public function executeStoredXQuery(xQueryFilename:String, params:Object, successFunc:Function, failureFunc:Function, resultFormat:String = null):void {
141			if (!this.loggedIn) {
142				testConnection(function():void {
143					executeStoredXQuery(xQueryFilename, params, successFunc, failureFunc);
144				}, function(msg:String):void {
145					failureFunc("Tried to execute stored query but not connected to database");					
146				});
147				return;
148			}
149			new EXistRESTClient(remoteMgr.getRESTClient()).executeStoredXQuery(DatabaseConstants.XQUERY_COLLECTION_PATH + "/" + xQueryFilename, params, successFunc, failureFunc, resultFormat);
150		}
151
152		private function checkClientVersionAllowed(successFunc:Function, failureFunc:Function):void {
153			var clientVersion:String = ApplicationUtils.getApplicationVersion();
154			executeStoredXQuery("check-client-version.xql", {clientVersion:clientVersion}, function(minClientVersion:String):void {
155				if (minClientVersion == "") {
156					trace("Client version allowed: " + clientVersion);
157					successFunc();
158				}
159				else {
160					failureFunc("Client version '" + clientVersion + "' is out of date. Min version required: " + minClientVersion);  
161				}
162			}, failureFunc);
163		}
164		
165		private function getUserGroupNames(successFunc:Function):void {
166			var thisObj:DatabaseManagerUsingEXist = this;
167			query("xmldb:get-user-groups(xmldb:get-current-user())", [], function(xml:XML):void {
168				var arr:Array = []
169				for each (var groupName:String in xml.*) {
170					arr.push(groupName);
171				}
172				arr = Utils.condenseWhitespaceForArray(arr);
173				successFunc(arr);
174			}, function(msg:String):void {
175				Alert.show(msg, "Failed getting user's group names");
176				successFunc([]);
177			});
178		}
179	}
180}