1 | // ActionScript file |
---|
2 | import clarin.cmdi.componentregistry.browser.BrowserColumns; |
---|
3 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
4 | import clarin.cmdi.componentregistry.editor.model.CMDModelFactory; |
---|
5 | import clarin.cmdi.componentregistry.editor.model.CMDSpec; |
---|
6 | import clarin.cmdi.componentregistry.importer.UploadCompleteEvent; |
---|
7 | import clarin.cmdi.componentregistry.services.ComponentInfoService; |
---|
8 | import clarin.cmdi.componentregistry.services.ComponentListService; |
---|
9 | import clarin.cmdi.componentregistry.services.ComponentUsageCheckEvent; |
---|
10 | import clarin.cmdi.componentregistry.services.ComponentUsageService; |
---|
11 | import clarin.cmdi.componentregistry.services.Config; |
---|
12 | import clarin.cmdi.componentregistry.services.ProfileInfoService; |
---|
13 | import clarin.cmdi.componentregistry.services.UploadService; |
---|
14 | |
---|
15 | import flash.events.Event; |
---|
16 | |
---|
17 | import mx.controls.Alert; |
---|
18 | import mx.events.CloseEvent; |
---|
19 | import mx.managers.CursorManager; |
---|
20 | |
---|
21 | |
---|
22 | private var profileSrv:ProfileInfoService = new ProfileInfoService(); |
---|
23 | private var componentSrv:ComponentInfoService = new ComponentInfoService(); |
---|
24 | private var itemDescription:ItemDescription; |
---|
25 | |
---|
26 | [Bindable] |
---|
27 | private var componentsSrv:ComponentListService = ComponentListService.getInstance(Config.instance.userSpace); |
---|
28 | |
---|
29 | [Bindable] |
---|
30 | public var cmdComponent:XML; |
---|
31 | |
---|
32 | [Bindable] |
---|
33 | private var cmdSpec:CMDSpec; |
---|
34 | |
---|
35 | [Bindable] |
---|
36 | private var browserColumns:BrowserColumns = new BrowserColumns(); |
---|
37 | |
---|
38 | [Bindable] |
---|
39 | private var uploadService:UploadService = new UploadService(); |
---|
40 | |
---|
41 | |
---|
42 | public function init():void { |
---|
43 | cmdSpec = CMDSpec.createEmptyProfile(); |
---|
44 | profileSrv.addEventListener(ProfileInfoService.PROFILE_LOADED, profileLoaded); |
---|
45 | componentSrv.addEventListener(ComponentInfoService.COMPONENT_LOADED, componentLoaded); |
---|
46 | uploadService.addEventListener(UploadCompleteEvent.UPLOAD_COMPLETE, handleSaveComplete); |
---|
47 | uploadService.init(uploadProgress); |
---|
48 | Config.instance.addEventListener(Config.USER_SPACE_TOGGLE_EVENT, toggleUserSpace); |
---|
49 | } |
---|
50 | |
---|
51 | private function toggleUserSpace(event:Event):void { |
---|
52 | componentsSrv = ComponentListService.getInstance(Config.instance.userSpace); |
---|
53 | } |
---|
54 | |
---|
55 | private function profileLoaded(event:Event):void { |
---|
56 | var cmdComponent:XML = profileSrv.profile.profileSource; |
---|
57 | this.cmdSpec = CMDModelFactory.createModel(cmdComponent, profileSrv.profile.description); |
---|
58 | CursorManager.removeBusyCursor(); |
---|
59 | } |
---|
60 | |
---|
61 | private function componentLoaded(event:Event):void { |
---|
62 | var cmdComponent:XML = componentSrv.component.componentMD.xml; |
---|
63 | this.cmdSpec = CMDModelFactory.createModel(cmdComponent, componentSrv.component.description); |
---|
64 | CursorManager.removeBusyCursor(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | public function setDescription(itemDescription:ItemDescription):void { |
---|
69 | if (itemDescription) { |
---|
70 | this.itemDescription = itemDescription; |
---|
71 | CursorManager.setBusyCursor(); |
---|
72 | if (itemDescription.isProfile) { |
---|
73 | profileSrv.load(itemDescription); |
---|
74 | } else { |
---|
75 | componentSrv.load(itemDescription); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | private function publishSpec():void { |
---|
81 | Alert.show("If your profile/component is ready to be used by other people press ok, otherwise press cancel and save it in your workspace or continue editing.", "Publish", Alert.OK | Alert.CANCEL, null, handlePublishAlert); |
---|
82 | } |
---|
83 | |
---|
84 | private function handlePublishAlert(event:CloseEvent):void { |
---|
85 | if (event.detail == Alert.OK) { |
---|
86 | saveSpec(false, UploadService.PUBLISH); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | private function saveSpec(inUserSpace:Boolean, uploadAction:int):void { |
---|
91 | // Alert.show(xmlEditor.cmdSpec.toXml()); |
---|
92 | if (xmlEditor.validate()) { |
---|
93 | var item:ItemDescription = new ItemDescription(); |
---|
94 | item.description = xmlEditor.cmdSpec.headerDescription; |
---|
95 | item.name = xmlEditor.cmdSpec.headerName; |
---|
96 | item.isProfile = xmlEditor.cmdSpec.isProfile; |
---|
97 | item.groupName = xmlEditor.cmdSpec.groupName; |
---|
98 | item.domainName = xmlEditor.cmdSpec.domainName; |
---|
99 | item.isInUserSpace = inUserSpace; |
---|
100 | if (itemDescription && itemDescription.isInUserSpace) { |
---|
101 | item.id = xmlEditor.cmdSpec.headerId; |
---|
102 | } |
---|
103 | |
---|
104 | // Private components that are in updated require usage check call. If in use, the user can choose whether or not to save the changes . |
---|
105 | if(inUserSpace && uploadAction == UploadService.UPDATE && !item.isProfile){ |
---|
106 | checkUsage(item, inUserSpace); |
---|
107 | }else{ |
---|
108 | doUpload(uploadAction,item); |
---|
109 | } |
---|
110 | } else { |
---|
111 | errorMessageField.text = "Validation errors: red colored fields are invalid."; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Calls usage check for the specified component. If in use, asks user whether to proceed; if positive, initiates update. |
---|
117 | */ |
---|
118 | private function checkUsage(item:ItemDescription, inUserSpace:Boolean = true, uploadAction:int = UploadService.UPDATE):void{ |
---|
119 | var componentUsageService:ComponentUsageService = new ComponentUsageService(item,inUserSpace); |
---|
120 | componentUsageService.addEventListener(ComponentUsageCheckEvent.COMPONENT_IN_USE, |
---|
121 | function (event:ComponentUsageCheckEvent):void{ |
---|
122 | if(event.isComponentInUse){ |
---|
123 | var messageBody:String = "The component you are about to save is used by the following component(s) and/or profile(s):\n\n"; |
---|
124 | for each(var name:String in event.itemUsingComponent){ |
---|
125 | messageBody += " - " + name + "\n"; |
---|
126 | } |
---|
127 | messageBody += "\nChanges in this component will affect the above. Do you want to proceed?"; |
---|
128 | Alert.show(messageBody,"Component is used", Alert.YES|Alert.NO,null, |
---|
129 | function (eventObj:CloseEvent):void{ |
---|
130 | if(eventObj.detail == Alert.YES){ |
---|
131 | doUpload(uploadAction, item); |
---|
132 | } |
---|
133 | }); |
---|
134 | } else { |
---|
135 | doUpload(uploadAction,item); |
---|
136 | } |
---|
137 | }); |
---|
138 | componentUsageService.checkUsage(); |
---|
139 | } |
---|
140 | |
---|
141 | private function doUpload(uploadAction:int, item:ItemDescription):void{ |
---|
142 | uploadService.upload(uploadAction, item, xmlEditor.cmdSpec.toXml()); |
---|
143 | } |
---|
144 | |
---|
145 | private function handleSaveComplete(event:UploadCompleteEvent):void { |
---|
146 | setDescription(event.itemDescription); |
---|
147 | parentApplication.viewStack.switchToBrowse(event.itemDescription); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | private function handleEditorChange(event:Event):void { |
---|
152 | errorMessageField.text = ""; |
---|
153 | uploadProgress.visible = false; |
---|
154 | uploadProgress.includeInLayout = false; |
---|
155 | } |
---|
156 | |
---|
157 | private function initPaletteOverview():void { |
---|
158 | componentsPaletteOverview.dataGrid.dragEnabled = true; |
---|
159 | componentsPaletteOverview.dataGrid.allowMultipleSelection = true; |
---|
160 | componentsPaletteOverview.dataGrid.resizableColumns = true; |
---|
161 | } |
---|
162 | |
---|
163 | public function getType():String { |
---|
164 | return Config.VIEW_EDIT; |
---|
165 | } |
---|