1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
3 | |
---|
4 | import com.adobe.net.URI; |
---|
5 | |
---|
6 | import flash.events.Event; |
---|
7 | import flash.events.EventDispatcher; |
---|
8 | import flash.net.URLRequest; |
---|
9 | import flash.net.navigateToURL; |
---|
10 | |
---|
11 | import mx.collections.ArrayCollection; |
---|
12 | import mx.controls.Alert; |
---|
13 | import mx.messaging.messages.HTTPRequestMessage; |
---|
14 | import mx.rpc.AsyncToken; |
---|
15 | import mx.rpc.Responder; |
---|
16 | import mx.rpc.events.FaultEvent; |
---|
17 | import mx.rpc.events.ResultEvent; |
---|
18 | import mx.rpc.http.HTTPService; |
---|
19 | import mx.utils.StringUtil; |
---|
20 | |
---|
21 | [Event(name="itemsLoaded", type="flash.events.Event")] |
---|
22 | public class BrowserService extends EventDispatcher { |
---|
23 | public static const ITEMS_LOADED:String = "itemsLoaded"; |
---|
24 | private var service:HTTPService; |
---|
25 | |
---|
26 | /** |
---|
27 | * Typed ArrayCollection publicly available for outside code to bind to and watch. |
---|
28 | */ |
---|
29 | [Bindable] |
---|
30 | [ArrayElementType("ItemDescription")] |
---|
31 | public var itemDescriptions:ArrayCollection; |
---|
32 | |
---|
33 | private var serviceUrl:String; |
---|
34 | |
---|
35 | // Not bindable needed for lookups over the whole collections of itemDescriptions |
---|
36 | protected var unFilteredItemDescriptions:ArrayCollection; |
---|
37 | protected var userSpace:Boolean; |
---|
38 | |
---|
39 | public function BrowserService(restUrl:String, userSpace:Boolean) { |
---|
40 | this.serviceUrl = restUrl; |
---|
41 | service = new HTTPService(); |
---|
42 | service.method = HTTPRequestMessage.GET_METHOD; |
---|
43 | service.resultFormat = HTTPService.RESULT_FORMAT_E4X; |
---|
44 | this.userSpace = userSpace; |
---|
45 | } |
---|
46 | |
---|
47 | private function initService():void { |
---|
48 | var url:URI = new URI(serviceUrl); |
---|
49 | url.setQueryValue("unique", new Date().getTime().toString()); |
---|
50 | if (userSpace) { |
---|
51 | url.setQueryValue(Config.PARAM_USERSPACE, "true"); |
---|
52 | } |
---|
53 | service.url = url.toString(); |
---|
54 | } |
---|
55 | |
---|
56 | public function load():void { |
---|
57 | initService(); |
---|
58 | var token:AsyncToken = this.service.send(); |
---|
59 | token.addResponder(new Responder(result, fault)); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Override in concrete subclasses |
---|
64 | */ |
---|
65 | protected function result(resultEvent:ResultEvent):void { |
---|
66 | dispatchEvent(new Event(ITEMS_LOADED)); |
---|
67 | } |
---|
68 | |
---|
69 | public function fault(faultEvent:FaultEvent):void { |
---|
70 | var errorMessage:String = StringUtil.substitute("Error in {0}: \n Fault: {1} - {2}", this, faultEvent.fault.faultString, faultEvent.fault.faultDetail); |
---|
71 | trace(errorMessage); |
---|
72 | Alert.show("Internal Server error cannot process the data, try reloading the application."); |
---|
73 | } |
---|
74 | |
---|
75 | protected function setItemDescriptions(items:ArrayCollection):void { |
---|
76 | itemDescriptions = items; |
---|
77 | unFilteredItemDescriptions = new ArrayCollection(); //create a copy |
---|
78 | for each (var item:Object in items) { |
---|
79 | unFilteredItemDescriptions.addItem(item); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Looks up itemDescription, returns null if not found. |
---|
85 | */ |
---|
86 | public function lookUpDescription(componentId:String):ItemDescription { |
---|
87 | for each (var item:ItemDescription in unFilteredItemDescriptions) { |
---|
88 | if (item.id == componentId) { |
---|
89 | return item; |
---|
90 | } |
---|
91 | } |
---|
92 | return null; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|