1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
3 | |
---|
4 | import mx.collections.ArrayCollection; |
---|
5 | import mx.controls.Alert; |
---|
6 | import mx.messaging.messages.HTTPRequestMessage; |
---|
7 | import mx.rpc.AsyncToken; |
---|
8 | import mx.rpc.Responder; |
---|
9 | import mx.rpc.events.FaultEvent; |
---|
10 | import mx.rpc.events.ResultEvent; |
---|
11 | import mx.rpc.http.HTTPService; |
---|
12 | import mx.utils.StringUtil; |
---|
13 | |
---|
14 | public class BrowserService { |
---|
15 | |
---|
16 | private var service:HTTPService; |
---|
17 | |
---|
18 | /** |
---|
19 | * Typed ArrayCollection publicly available for outside code to bind to and watch. |
---|
20 | */ |
---|
21 | [Bindable] |
---|
22 | [ArrayElementType("ItemDescription")] |
---|
23 | public var itemDescriptions:ArrayCollection; |
---|
24 | |
---|
25 | // Not bindable needed for lookups over the whole collections of itemDescriptions |
---|
26 | protected var unFilteredItemDescriptions:ArrayCollection; |
---|
27 | |
---|
28 | |
---|
29 | public function BrowserService(restUrl:String) { |
---|
30 | this.service = new HTTPService(); |
---|
31 | this.service.method = HTTPRequestMessage.GET_METHOD; |
---|
32 | this.service.resultFormat = HTTPService.RESULT_FORMAT_E4X; |
---|
33 | this.service.url = restUrl; |
---|
34 | } |
---|
35 | |
---|
36 | public function load():void { |
---|
37 | var token:AsyncToken = this.service.send(); |
---|
38 | token.addResponder(new Responder(result, fault)); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Override in concrete subclasses |
---|
43 | */ |
---|
44 | protected function result(resultEvent:ResultEvent):void { |
---|
45 | } |
---|
46 | |
---|
47 | public function fault(faultEvent:FaultEvent):void { |
---|
48 | var errorMessage:String = StringUtil.substitute("Error in {0}: {1} - {2}", this, faultEvent.fault.faultString, faultEvent.fault.faultDetail); |
---|
49 | Alert.show(errorMessage); |
---|
50 | } |
---|
51 | |
---|
52 | protected function setItemDescriptions(items:ArrayCollection):void { |
---|
53 | itemDescriptions = items; |
---|
54 | unFilteredItemDescriptions = new ArrayCollection(); //create a copy |
---|
55 | for each (var item:Object in items) { |
---|
56 | unFilteredItemDescriptions.addItem(item); |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|