1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import com.adobe.net.URI; |
---|
3 | |
---|
4 | import flash.events.ErrorEvent; |
---|
5 | import flash.events.EventDispatcher; |
---|
6 | |
---|
7 | import mx.controls.Alert; |
---|
8 | import mx.managers.CursorManager; |
---|
9 | import mx.utils.StringUtil; |
---|
10 | |
---|
11 | import org.httpclient.HttpClient; |
---|
12 | import org.httpclient.events.HttpDataEvent; |
---|
13 | import org.httpclient.events.HttpResponseEvent; |
---|
14 | import org.httpclient.http.Get; |
---|
15 | |
---|
16 | public class IsocatService extends EventDispatcher { |
---|
17 | public static const PROFILE_LOADED:String = "ProfileLoaded"; |
---|
18 | namespace dcif = "http://www.isocat.org/ns/dcif"; |
---|
19 | |
---|
20 | |
---|
21 | private var service:HttpClient; |
---|
22 | |
---|
23 | [Bindable] |
---|
24 | public var searchResults:XMLList; |
---|
25 | |
---|
26 | public function IsocatService() { |
---|
27 | service = new HttpClient(); |
---|
28 | service.listener.onComplete = handleResult; |
---|
29 | service.listener.onError = handleError; |
---|
30 | service.listener.onData = handleData; |
---|
31 | } |
---|
32 | |
---|
33 | public function load(keyword:String):void { |
---|
34 | if (keyword) { |
---|
35 | CursorManager.setBusyCursor(); |
---|
36 | var uri:URI = new URI(Config.instance.isocatSearchUrl); |
---|
37 | uri.setQueryValue("keywords", keyword); |
---|
38 | uri.setQueryValue("dcif-mode", "list"); |
---|
39 | |
---|
40 | var httpGet:Get = new Get(); |
---|
41 | httpGet.addHeader("Accept", "application/dcif+xml"); |
---|
42 | service.request(uri, httpGet); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | public function handleData(event:HttpDataEvent):void { |
---|
47 | var data:XML = new XML(event.bytes); |
---|
48 | searchResults = data.dcif::dataCategory; |
---|
49 | } |
---|
50 | |
---|
51 | private function handleResult(resultEvent:HttpResponseEvent):void { |
---|
52 | CursorManager.removeBusyCursor(); |
---|
53 | if (resultEvent.response.isClientError) { |
---|
54 | Alert.show("Unauthorized search, server return status:" + resultEvent.response.code); |
---|
55 | } else if (resultEvent.response.isServerError) { |
---|
56 | Alert.show("Unexpected error, server returned status: " + resultEvent.response.code); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | public function handleError(faultEvent:ErrorEvent):void { |
---|
61 | CursorManager.removeBusyCursor(); |
---|
62 | var errorMessage:String = StringUtil.substitute("Error in {0}: {1}", this, faultEvent.text); |
---|
63 | Alert.show(errorMessage); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|