source: ComponentRegistry/trunk/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/importer/Importer.mxml @ 2126

Last change on this file since 2126 was 2126, checked in by twagoo, 12 years ago

Merged changes from 1.12.0 branch that fix #217 and #218 + CHANGES file for that release

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