1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import clarin.cmdi.componentregistry.common.Comment; |
---|
3 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
4 | |
---|
5 | import com.adobe.net.URI; |
---|
6 | |
---|
7 | import flash.events.Event; |
---|
8 | import flash.events.EventDispatcher; |
---|
9 | |
---|
10 | import mx.controls.Alert; |
---|
11 | import mx.managers.CursorManager; |
---|
12 | import mx.messaging.messages.HTTPRequestMessage; |
---|
13 | import mx.rpc.events.FaultEvent; |
---|
14 | import mx.rpc.events.ResultEvent; |
---|
15 | import mx.rpc.http.HTTPService; |
---|
16 | import mx.utils.StringUtil; |
---|
17 | |
---|
18 | [Event(name="itemDeleted", type="flash.events.Event")] |
---|
19 | |
---|
20 | public class DeleteService extends EventDispatcher { |
---|
21 | public const ITEM_DELETED:String = "itemDeleted"; |
---|
22 | |
---|
23 | private var service:HTTPService; |
---|
24 | private const DELETE_METHOD:Object = {"method": "delete"}; |
---|
25 | |
---|
26 | |
---|
27 | public function DeleteService() { |
---|
28 | service = new HTTPService(); |
---|
29 | service.addEventListener(FaultEvent.FAULT, handleError); |
---|
30 | service.addEventListener(ResultEvent.RESULT, handleResult); |
---|
31 | service.method = HTTPRequestMessage.POST_METHOD; |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | public function deleteItem(item:ItemDescription):void { |
---|
36 | CursorManager.setBusyCursor(); |
---|
37 | |
---|
38 | var deleteUrl:URI = new URI(item.dataUrl); |
---|
39 | if (item.isInUserSpace) { |
---|
40 | deleteUrl.setQueryValue(Config.PARAM_USERSPACE, "true"); |
---|
41 | } |
---|
42 | |
---|
43 | var usageService:ComponentUsageService = new ComponentUsageService(item, item.isInUserSpace); |
---|
44 | usageService.addEventListener(ComponentUsageCheckEvent.COMPONENT_IN_USE, |
---|
45 | function (event:ComponentUsageCheckEvent):void{ |
---|
46 | if(event.isComponentInUse){ |
---|
47 | onComponentInUse(event); |
---|
48 | } else { |
---|
49 | sendDelete(deleteUrl); |
---|
50 | } |
---|
51 | }); |
---|
52 | usageService.checkUsage(); |
---|
53 | } |
---|
54 | |
---|
55 | public function onComponentInUse(event:ComponentUsageCheckEvent):void{ |
---|
56 | var messageBody:String = "The component cannot be deleted because it is used by the following component(s) and/or profile(s):\n\n"; |
---|
57 | for each(var name:String in event.itemUsingComponent){ |
---|
58 | messageBody += " - " + name + "\n"; |
---|
59 | } |
---|
60 | CursorManager.removeBusyCursor(); |
---|
61 | Alert.show(messageBody,"Component is used"); |
---|
62 | } |
---|
63 | |
---|
64 | public function deleteComment(comment:Comment):void { |
---|
65 | CursorManager.setBusyCursor(); |
---|
66 | var url:URI = new URI(comment.dataUrl); |
---|
67 | if (comment.itemDescription.isInUserSpace) { |
---|
68 | url.setQueryValue(Config.PARAM_USERSPACE, "true"); |
---|
69 | } |
---|
70 | sendDelete(url); |
---|
71 | } |
---|
72 | |
---|
73 | private function sendDelete(url:URI):void { |
---|
74 | service.url = url.toString(); |
---|
75 | service.send(DELETE_METHOD); |
---|
76 | } |
---|
77 | |
---|
78 | private function handleResult(resultEvent:ResultEvent):void { |
---|
79 | CursorManager.removeBusyCursor(); |
---|
80 | if (resultEvent.statusCode >= 200 && resultEvent.statusCode < 300) { |
---|
81 | dispatchEvent(new Event(ITEM_DELETED)); |
---|
82 | } else { |
---|
83 | Alert.show("Unexpected error, server returned status: " + resultEvent.statusCode + "\n Message = "); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | public function handleError(faultEvent:FaultEvent):void { |
---|
88 | CursorManager.removeBusyCursor(); |
---|
89 | if (faultEvent.statusCode == 401) { //Apparrently depending on browser status codes and errormessages are sometimes not passed along to flash. |
---|
90 | Alert.show("Item not deleted:" + faultEvent.message.body); |
---|
91 | } else if (faultEvent.statusCode == 403) { |
---|
92 | Alert.show("Item not deleted:" + faultEvent.message.body); |
---|
93 | } else { |
---|
94 | Alert.show("Item not deleted: Item is either public or in the case of a component still referenced by other components/profiles."); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | } |
---|
100 | } |
---|