1 | <?xml version="1.0" encoding="utf-8"?> |
---|
2 | <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" |
---|
3 | xmlns:local="clarin.cmdi.componentregistry.services.*" |
---|
4 | width="100%" |
---|
5 | height="100%" |
---|
6 | label="Import file" |
---|
7 | implements="clarin.cmdi.componentregistry.common.components.RegistryView"> |
---|
8 | |
---|
9 | <mx:Script> |
---|
10 | <![CDATA[ |
---|
11 | import clarin.cmdi.componentregistry.common.ComponentMD; |
---|
12 | import clarin.cmdi.componentregistry.common.Credentials; |
---|
13 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
14 | import clarin.cmdi.componentregistry.common.LabelConstants; |
---|
15 | import clarin.cmdi.componentregistry.common.Profile; |
---|
16 | import clarin.cmdi.componentregistry.common.components.RegistryViewStack; |
---|
17 | import clarin.cmdi.componentregistry.editor.InputValidators; |
---|
18 | import clarin.cmdi.componentregistry.services.Config; |
---|
19 | |
---|
20 | import mx.collections.ArrayCollection; |
---|
21 | import mx.controls.Alert; |
---|
22 | import mx.core.Application; |
---|
23 | import mx.core.UIComponent; |
---|
24 | import mx.events.ValidationResultEvent; |
---|
25 | import mx.validators.Validator; |
---|
26 | |
---|
27 | private var validators:ArrayCollection = new ArrayCollection(); |
---|
28 | |
---|
29 | [Bindable] |
---|
30 | private var isProfile:Boolean = true; |
---|
31 | [Bindable] |
---|
32 | private var importTypeString:String = ""; |
---|
33 | |
---|
34 | private var loadedXML:XML; |
---|
35 | |
---|
36 | private function submit(event:Event):void { |
---|
37 | if (validate()) { |
---|
38 | var item:ItemDescription = new ItemDescription(); |
---|
39 | item.description = description.text; |
---|
40 | item.name = nameInput.text; |
---|
41 | item.groupName = groupName.text; |
---|
42 | if (domainName.selectedIndex != -1) |
---|
43 | item.domainName = domainName.selectedItem.data; |
---|
44 | item.isProfile = isProfile; |
---|
45 | item.isInUserSpace = true; //We only allow import into workspace it can be published from there. |
---|
46 | if (loadedXML) { |
---|
47 | updateXMLFromFormFields(loadedXML); |
---|
48 | uploadSrv.upload(UploadService.NEW, item, loadedXML.toXMLString()); |
---|
49 | } else { |
---|
50 | // No loaded XML. Let the upload service try with file reference |
---|
51 | uploadSrv.upload(UploadService.NEW, item); |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | private function close():void{ |
---|
57 | var viewStack:RegistryViewStack = this.parent as RegistryViewStack; |
---|
58 | viewStack.switchToBrowse(null); |
---|
59 | } |
---|
60 | |
---|
61 | private function validate():Boolean { |
---|
62 | var result:Boolean = true; |
---|
63 | for (var i:int; i < validators.length; i++) { |
---|
64 | var val:Validator = validators.getItemAt(i) as Validator; |
---|
65 | if (val.listener.visible) { |
---|
66 | var event:ValidationResultEvent = val.validate(); |
---|
67 | if (event.type != ValidationResultEvent.VALID) { |
---|
68 | result = false; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | return result; |
---|
73 | } |
---|
74 | |
---|
75 | private function setIsProfile(isProfileValue:Boolean):void { |
---|
76 | isProfile = isProfileValue; |
---|
77 | if (isProfile) { |
---|
78 | importTypeString = "Profile"; |
---|
79 | } else { |
---|
80 | importTypeString = "Component"; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | private function registryChange(event:UploadCompleteEvent):void { |
---|
85 | // Upload completed. Reset upload services and form |
---|
86 | uploadSrv.reset(); |
---|
87 | clearForm(); |
---|
88 | // Browse to uploaded item |
---|
89 | parentApplication.viewStack.switchToBrowse(event.itemDescription); |
---|
90 | } |
---|
91 | |
---|
92 | private function fileLoaded(event:FileLoadedEvent):void { |
---|
93 | var data:ByteArray = event.fileReference.data; |
---|
94 | if (data && data.bytesAvailable) { |
---|
95 | try { |
---|
96 | var utfBytes:String = data.readUTFBytes(data.length); |
---|
97 | loadedXML = new XML(utfBytes); |
---|
98 | |
---|
99 | // New file selected, clear all previous entries |
---|
100 | clearForm(); |
---|
101 | // Set import type (profile or component) from attribute in XML |
---|
102 | setImportTypeFromFile(loadedXML); |
---|
103 | // Set name, description from values in XML |
---|
104 | setFormFieldsFromFile(loadedXML); |
---|
105 | } catch (error:Error) { |
---|
106 | Alert.show("Could not read the selected file. Not a valid XML file?", "Error"); |
---|
107 | importTypeString = "Error"; |
---|
108 | trace("Error while reading loaded file " + event.fileReference.name); |
---|
109 | return; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | private function setImportTypeFromFile(resultXml:XML):void { |
---|
115 | if (resultXml.hasOwnProperty("@isProfile")) { |
---|
116 | setIsProfile(("true" == resultXml.@isProfile)); |
---|
117 | } else { |
---|
118 | Alert.show("Unable to determine import type. Selected file may not be a valid component specification!", "Warning"); |
---|
119 | importTypeString = "Unknown"; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | private function setFormFieldsFromFile(resultXml:XML):void { |
---|
124 | var header:XML = getHeaderXML(resultXml); |
---|
125 | var component:XML = getComponentXML(resultXml); |
---|
126 | if (header) { |
---|
127 | description.text = header.Description; |
---|
128 | } |
---|
129 | if(component){ |
---|
130 | nameInput.text = component.@name; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | private function updateXMLFromFormFields(xml:XML):void { |
---|
135 | var header:XML = getHeaderXML(xml); |
---|
136 | var component:XML = getComponentXML(xml); |
---|
137 | if (header) { |
---|
138 | header.Name = nameInput.text; |
---|
139 | header.Description = description.text; |
---|
140 | } |
---|
141 | if(component){ |
---|
142 | component.@name = nameInput.text; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | private function getHeaderXML(xml:XML):XML { |
---|
147 | if (xml.hasOwnProperty("Header")) { |
---|
148 | var nodes:XMLList = xml.Header; |
---|
149 | if (nodes && nodes.length() > 0) { |
---|
150 | return nodes[0]; |
---|
151 | } |
---|
152 | } |
---|
153 | return null; |
---|
154 | } |
---|
155 | |
---|
156 | private function getComponentXML(xml:XML):XML { |
---|
157 | if (xml.hasOwnProperty("CMD_Component")) { |
---|
158 | var nodes:XMLList = xml.CMD_Component; |
---|
159 | if (nodes && nodes.length() > 0) { |
---|
160 | return nodes[0]; |
---|
161 | } |
---|
162 | } |
---|
163 | return null; |
---|
164 | } |
---|
165 | |
---|
166 | private function clearForm():void { |
---|
167 | importTypeString = ""; |
---|
168 | nameInput.text = ""; |
---|
169 | description.text = ""; |
---|
170 | groupName.text = ""; |
---|
171 | domainName.selectedItem = null; |
---|
172 | |
---|
173 | // Reset all error strings on components with validators, so previous validation error message don't show anymore |
---|
174 | for (var i:int; i < validators.length; i++) { |
---|
175 | var val:Validator = validators.getItemAt(i) as Validator; |
---|
176 | if (val.listener.visible) { |
---|
177 | (val.source as UIComponent).errorString = ""; |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | private function registerNameValidator(event:Event):void { |
---|
183 | var validator:Validator = InputValidators.getNameValidator(); |
---|
184 | validator.listener = event.currentTarget; |
---|
185 | validator.source = event.currentTarget; |
---|
186 | validator.property = "text"; |
---|
187 | validators.addItem(validator); |
---|
188 | } |
---|
189 | |
---|
190 | private function registerIsRequiredValidator(event:Event, source:Object = null):void { |
---|
191 | var validator:Validator = InputValidators.getIsRequiredValidator(); |
---|
192 | validator.listener = event.currentTarget; |
---|
193 | if (!source) { |
---|
194 | validator.source = event.currentTarget; |
---|
195 | } else { |
---|
196 | validator.source = source; |
---|
197 | } |
---|
198 | validator.property = "text"; |
---|
199 | validators.addItem(validator); |
---|
200 | } |
---|
201 | |
---|
202 | public function getType():String { |
---|
203 | return Config.VIEW_IMPORT; |
---|
204 | } |
---|
205 | ]]> |
---|
206 | </mx:Script> |
---|
207 | |
---|
208 | <local:UploadService id="uploadSrv" |
---|
209 | loadBeforeUploading="true" |
---|
210 | fileLoaded="fileLoaded(event)" |
---|
211 | uploadComplete="registryChange(event)"/> |
---|
212 | <mx:VBox> |
---|
213 | <mx:Form> |
---|
214 | <mx:HBox> |
---|
215 | <mx:Button label="Select xml..." |
---|
216 | id="btnSelect" |
---|
217 | click="uploadSrv.selectXmlFile(event)" |
---|
218 | creationComplete="registerIsRequiredValidator(event, txtSelectedFile)"/> |
---|
219 | <mx:Text id="txtSelectedFile" |
---|
220 | text="{uploadSrv.selectedFile}"/> |
---|
221 | </mx:HBox> |
---|
222 | |
---|
223 | <mx:FormItem label="Type"> |
---|
224 | <mx:Label id="importType" |
---|
225 | width="300" |
---|
226 | text="{importTypeString}"/> |
---|
227 | </mx:FormItem> |
---|
228 | <mx:FormItem label="Name"> |
---|
229 | <mx:TextInput id="nameInput" |
---|
230 | width="300" |
---|
231 | creationComplete="registerNameValidator(event)"/> |
---|
232 | </mx:FormItem> |
---|
233 | <mx:FormItem label="Description"> |
---|
234 | <mx:TextArea id="description" |
---|
235 | width="300" |
---|
236 | height="120" |
---|
237 | creationComplete="registerIsRequiredValidator(event)"/> |
---|
238 | </mx:FormItem> |
---|
239 | <mx:FormItem label="Creator Name"> |
---|
240 | <mx:Text id="creatorName" |
---|
241 | width="300" |
---|
242 | text="{Credentials.instance.userName}"/> |
---|
243 | </mx:FormItem> |
---|
244 | <mx:FormItem label="Domain Name"> |
---|
245 | <mx:ComboBox id="domainName" |
---|
246 | selectedIndex="-1" |
---|
247 | prompt="{LabelConstants.DOMAIN_NAME_PROMPT}" |
---|
248 | dataProvider="{LabelConstants.DOMAIN_NAME_DATA}"/> |
---|
249 | </mx:FormItem> |
---|
250 | <mx:FormItem label="Group Name"> |
---|
251 | <mx:TextInput id="groupName" |
---|
252 | width="300"/> |
---|
253 | </mx:FormItem> |
---|
254 | |
---|
255 | <mx:HBox> |
---|
256 | <mx:Button label="Submit" |
---|
257 | click="submit(event)"/> |
---|
258 | <mx:Button label="Close" |
---|
259 | click="close()"/> |
---|
260 | </mx:HBox> |
---|
261 | |
---|
262 | <mx:ProgressBar id="uploadProgress" |
---|
263 | label="" |
---|
264 | mode="manual" |
---|
265 | creationComplete="uploadSrv.init(uploadProgress)" |
---|
266 | visible="false"/> |
---|
267 | </mx:Form> |
---|
268 | |
---|
269 | <mx:Text id="errorMessageField" |
---|
270 | text="{uploadSrv.message}"/> |
---|
271 | |
---|
272 | </mx:VBox> |
---|
273 | |
---|
274 | </mx:Canvas> |
---|
275 | |
---|