1 | package clarin.cmdi.componentregistry.services { |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.common.Component; |
---|
4 | import clarin.cmdi.componentregistry.common.ComponentMD; |
---|
5 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
6 | |
---|
7 | import mx.controls.Alert; |
---|
8 | import mx.messaging.messages.HTTPRequestMessage; |
---|
9 | import mx.rpc.AsyncToken; |
---|
10 | import mx.rpc.Responder; |
---|
11 | import mx.rpc.events.FaultEvent; |
---|
12 | import mx.rpc.events.ResultEvent; |
---|
13 | import mx.rpc.http.HTTPService; |
---|
14 | import mx.utils.StringUtil; |
---|
15 | |
---|
16 | [Event(name="ComponentLoaded", type="flash.events.Event")] |
---|
17 | public class ComponentInfoService { |
---|
18 | public static const COMPONENT_LOADED:String = "ComponentLoaded"; |
---|
19 | |
---|
20 | private var service:HTTPService; |
---|
21 | |
---|
22 | [Bindable] |
---|
23 | public var component:Component; |
---|
24 | |
---|
25 | |
---|
26 | public function ComponentInfoService() { |
---|
27 | this.service = new HTTPService(); |
---|
28 | this.service.method = HTTPRequestMessage.GET_METHOD; |
---|
29 | this.service.resultFormat = HTTPService.RESULT_FORMAT_E4X; |
---|
30 | } |
---|
31 | |
---|
32 | public function load(item:ItemDescription):void { |
---|
33 | this.component = new Component(); |
---|
34 | component.description = item; |
---|
35 | this.service.url = item.dataUrl; |
---|
36 | var token:AsyncToken = this.service.send(); |
---|
37 | token.addResponder(new Responder(result, fault)); |
---|
38 | } |
---|
39 | |
---|
40 | private function result(resultEvent:ResultEvent):void { |
---|
41 | var resultXml:XML = resultEvent.result as XML; |
---|
42 | var metaData:ComponentMD = new ComponentMD(); |
---|
43 | metaData.name = resultXml.CMD_Component.@name; |
---|
44 | metaData.xml = resultXml; |
---|
45 | component.componentMD = metaData; |
---|
46 | dispatchEvent(new Event(COMPONENT_LOADED)); |
---|
47 | } |
---|
48 | |
---|
49 | public function fault(faultEvent:FaultEvent):void { |
---|
50 | var errorMessage:String = StringUtil.substitute("Error in {0}: {1} - {2}", this, faultEvent.fault.faultString, faultEvent.fault.faultDetail); |
---|
51 | Alert.show(errorMessage); |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|