Importer.js 12.3 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
MWF.xApplication.process.ApplicationExplorer.Importer = new Class({
    Extends: MWF.widget.Common,
    Implements: [Options, Events],

    options: {
        "style": "default"
    },
    initialize: function(app, e, options){
        this.setOptions(options);
        this.app = app;
        this.container = this.app.content;
        this.actions = this.app.restActions;
        this.event = e;

        this.path = "/x_component_process_ApplicationExplorer/$Importer/";
        this.cssPath = "/x_component_process_ApplicationExplorer/$Importer/"+this.options.style+"/css.wcss";
        this._loadCss();
    },
    load: function(){
        this.container.mask({
            "destroyOnHide": true,
            "style": {
                "background-color": "#666",
                "opacity": 0.6
            }
        });
        this.node = new Element("div", {"styles": this.css.content});
        this.titleNode = new Element("div", {"styles": this.css.titleNode, "text": this.app.lp.application.import}).inject(this.node);
        this.contentNode = new Element("div", {"styles": this.css.contentNode}).inject(this.node);
        this.buttonAreaNode = new Element("div", {"styles": this.css.buttonAreaNode}).inject(this.node);

        this.cancelButton = new Element("div", {"styles": this.css.button, "text": this.app.lp.application.export_cancel}).inject(this.buttonAreaNode);
        this.okButton = new Element("div", {"styles": this.css.okButton, "text": this.app.lp.application.export_ok}).inject(this.buttonAreaNode);

        this.loadContent();

        this.setEvent();

        this.node.inject(this.container);
        this.node.position({
            relativeTo: this.container,
            position: 'center',
            edge: 'center'
        });
    },
    loadContent: function(){
        this.textarea = new Element("textarea", {"styles": this.css.textarea}).inject(this.contentNode);
    },

    setEvent: function(){
        this.cancelButton.addEvent("click", function(e){
            this.close();
        }.bind(this));

        this.okButton.addEvent("click", function(e){
            this.importApplication();
        }.bind(this));
    },

    close: function(){
        this.container.unmask();
        this.node.destroy();

        this.cancelButton = null;
        this.okButton = null;
        this.buttonAreaNode = null;
        this.contentNode = null;
        this.titleNode = null;
        this.node = null;

        this.fireEvent("close");
    },
    importApplication: function(){
        var str = this.textarea.get("value");
        if (str){
            this.applicationJson = JSON.decode(str);

            var name = this.applicationJson.application.name
            this.actions.listApplicationSummary("", function(json){
                var flag = "create";
                var overApplication = null;
                if (json.data){
                    for (var i=0; i<json.data.length; i++){
                        if (json.data[i].name == name){
                            overApplication = json.data[i];
                            flag = "overwrite";
                            break;
                        }
                    }
                }
                if (flag == "overwrite"){
                    var _self = this;
                    this.app.confirm("infor", this.event, this.app.lp.application.import_confirm_title, this.app.lp.application.import_confirm, 400, 180, function(){
                        _self.doImportOverwriteApplication(overApplication);
                        this.close();
                    }, function(){
                        this.close();
                    })
                }else{
                    this.doImportApplication();
                }
            }.bind(this));
        }
    },
    readyProrressBar: function(){
        this.createProgressBar();
        this.status = {
            "count": this.applicationJson.processList.length+this.applicationJson.formList.length+this.applicationJson.dictionaryList.length+this.applicationJson.scriptList.length+1,
            "complete": 0
        }
    },
    doImportApplication: function(){
        this.readyProrressBar();

        this.actions.action.invoke({"name": "addApplication","data": this.applicationJson.application,"success": function(){
            this.progressBarTextNode.set("text", "Import Application Property ...");
            this.checkExport();

            this.importOverwriteProcessList();
            this.importOverwriteFormList();
            this.importOverwriteDictionaryList();
            this.importOverwriteScriptList();

        }.bind(this)});
    },
    doImportOverwriteApplication: function(overApplication){
        this.readyProrressBar();
        this.applicationJson.application.id = overApplication.id;
        this.actions.saveApplication(this.applicationJson.application, function(){
            this.progressBarTextNode.set("text", "Import Application Property ...");
            this.checkExport();
        }.bind(this));

        this.importOverwriteProcessList();
        this.importOverwriteFormList();
        this.importOverwriteDictionaryList();
        this.importOverwriteScriptList();
    },
    importOverwriteProcessList: function() {
        this.actions.listProcess(this.applicationJson.application.id, function(json){
            var processList = json.data || [];

            this.applicationJson.processList.each(function(process){
                var id = "";
                process.application = this.applicationJson.application.id;
                for (var i=0; i<processList.length; i++){
                    if (processList[i].name==process.name){
                        id = processList[i].id;
                        break;
                    }
                }

                if (id) {
                    process.id = id;
                    process.begin.process = id;
                    process.agentList.each(function(a){a.process = id;});
                    process.cancelList.each(function(a){a.process = id;});
                    process.choiceList.each(function(a){a.process = id;});
                    process.embedList.each(function(a){a.process = id;});
                    process.endList.each(function(a){a.process = id;});
                    process.invokeList.each(function(a){a.process = id;});
                    process.manualList.each(function(a){a.process = id;});
                    process.mergeList.each(function(a){a.process = id;});
                    process.messageList.each(function(a){a.process = id;});
                    process.routeList.each(function(a){a.process = id;});
                    process.parallelList.each(function(a){a.process = id;});
                    process.serviceList.each(function(a){a.process = id;});
                    process.splitList.each(function(a){a.process = id;});

                    this.actions.updateProcess(process, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+process.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }else{


                    this.actions.addProcess(process, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+process.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }
            }.bind(this));
        }.bind(this));
    },



    importOverwriteFormList: function(){
        this.actions.listForm(this.applicationJson.application.id, function(json){
            var formList = json.data || [];
            this.applicationJson.formList.each(function(form){
                form.application = this.applicationJson.application.id;
                var id = "";
                for (var i=0; i<formList.length; i++){
                    if (formList[i].name==form.name){
                        id = formList[i].id;
                        break;
                    }
                }
                if (id) {
                    form.id = id;
                    this.actions.action.invoke({"name": "updataForm","data": form,"parameter": {"id": form.id},"success": function(){
                        this.progressBarTextNode.set("text", "Import Form \""+form.name+"\" ...");
                        this.checkExport();
                    }.bind(this)});
                }else{
                    this.actions.action.invoke({"name": "addForm","data": form,"parameter": {"id": form.id},"success": function(){
                        this.progressBarTextNode.set("text", "Import Form \""+form.name+"\" ...");
                        this.checkExport();
                    }.bind(this)});
                }

            }.bind(this));
        }.bind(this));
    },
    importOverwriteDictionaryList: function(){
        this.actions.listDictionary(this.applicationJson.application.id, function(json){
            var dicList = json.data || [];
            this.applicationJson.dictionaryList.each(function(dictionary){
                dictionary.application = this.applicationJson.application.id;
                var id = "";
                for (var i=0; i<dicList.length; i++){
                    if (dicList[i].name==dictionary.name){
                        id = dicList[i].id;
                        break;
                    }
                }
                if (id){
                    dictionary.id = id;
                    this.actions.updateDictionary(dictionary, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+dictionary.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }else{
                    this.actions.addDictionary(dictionary, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+dictionary.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }
            }.bind(this));
        }.bind(this));
    },
    importOverwriteScriptList: function(){
        this.actions.listScript(this.applicationJson.application.id, function(json){
            var scriptList = json.data || [];
            this.applicationJson.scriptList.each(function(script){
                script.application = this.applicationJson.application.id;
                var id = "";
                for (var i=0; i<scriptList.length; i++){
                    if (scriptList[i].name==script.name){
                        id = scriptList[i].id;
                        break;
                    }
                }
                if (id){
                    script.id = id;
                    this.actions.updateScript(script, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+script.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }else{
                    this.actions.addScript(script, function(){
                        this.progressBarTextNode.set("text", "Import Process \""+script.name+"\" ...");
                        this.checkExport();
                    }.bind(this));
                }
            }.bind(this));
        }.bind(this));
    },


    checkExport: function(){
        this.status.complete = this.status.complete+1;
        var x = 358*(this.status.complete/this.status.count);
        this.progressBarPercent.setStyle("width", ""+x+"px");

        if (this.status.complete == this.status.count){
            this.progressBarNode.destroy();
            this.progressBarNode = null;
            this.progressBarTextNode = null;
            this.progressBar = null;
            this.progressBarPercent = null;

            this.close();
        }
    },
    createProgressBar: function(){
        this.node.hide();
        this.progressBarNode = new Element("div", {"styles": this.css.progressBarNode});
        this.progressBarNode.inject(this.container);
        this.progressBarNode.position({
            relativeTo: this.container,
            position: 'center',
            edge: 'center'
        });

        this.progressBarTextNode = new Element("div", {"styles": this.css.progressBarTextNode}).inject(this.progressBarNode);
        this.progressBar = new Element("div", {"styles": this.css.progressBar}).inject(this.progressBarNode);
        this.progressBarPercent = new Element("div", {"styles": this.css.progressBarPercent}).inject(this.progressBar);

    }

});