mwf.xapplication.query = mwf.xapplication.query || {};
mwf.xapplication.query.query = mwf.xapplication.query.query || {};
mwf.require("mwf.widget.common", null, false);
mwf.require("mwf.xscript.macro", null, false);
mwf.require("o2.widget.dialog", null, false);
mwf.xdesktop.requireapp("query.query", "lp." o2.language, null, false);
/** @classdesc importer 数据中心的导入模型。
* @class
* @o2cn 导入模型
* @o2category queryimporter
* @o2range {queryimporter}
* @hideconstructor
* @example
* //在导入模型的事件中获取该类
* var view = this.target;
* */
mwf.xapplication.query.query.importer = mwf.qimporter = new class(
/** @lends mwf.xapplication.query.query.importer# */
{
implements: [options, events],
extends: mwf.widget.common,
options: {
"style": "default",
"moduleevents": [
/**
* 加载importer(导入模型对象)的时候执行。可通过this.target获取当前对象。
* @event mwf.xapplication.query.query.importer#queryload
*/
"queryload",
/**
* 导入前触发,this.event指向导入的数据,您可以通过修改this.event来修改数据。
* @event mwf.xapplication.query.query.importer#beforeimport
* @example
* this.event数据格式如下:
*[
* [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
* [ "标题二","李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
*]
*/
"beforeimport",
/**
* 前台校验成功,并且后台执行完导入后触发,this.event指向后台返回的导入结果。
* @event mwf.xapplication.query.query.importer#afterimport
* @example
* this.event格式如下:
* {
* "status": "导入成功", //导入结果:状态有 "导入成功","部分成功","导入失败"
* "data": {}, //前台组织好的需要导入的数据
* "rowlist": [], //前台组织的行对象数组
* "count" : 10, //导入总数量
* "failcount": 0, //失败数量
* "distribution": "" //导入时候时的错误信息
* }
*/
"afterimport",
/**
* 数据已经生成,前台进行数据校验时触发,this.event指向导入的数据。
* @event mwf.xapplication.query.query.importer#validimport
* @example
* this.event数据格式如下:
* {
* "data" : [
* [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //第一行数据
* [ "标题二","李四","男","大学专科","数学","1998-1-2","2018-9-2" ] //第二行数据
* ],
* "rowlist": [], //导入的行对象,数据格式常见本章api的aftercreaterowdata说明。
* "validted" : true //是否校验通过,可以在本事件中修改该参数,确定是否强制导入
* }
*/
"validimport",
/**
* 创建每行需要导入的数据前触发,this.event指向当前行对象,您可以通过修改this.event.importdata来修改数据。
* @event mwf.xapplication.query.query.importer#beforecreaterowdata
*/
"beforecreaterowdata",
/**
* 创建每行需要导入的数据后触发,this.event指向当前行对象。
* @event mwf.xapplication.query.query.importer#aftercreaterowdata
* @example
* this.event格式如下:
* {
* "importdata": [ "标题一","张三","男","大学本科","计算机","2001-1-2","2019-9-2" ], //导入的数据
* "data" : {//根据导入模型生成的业务数据
* {
* "subject", "标题一", //subject为导入模型列配置的路径
* "name" : "张三",
* ...
* },
* "document": { //如果导入目标是内容管理,则包含document对象
* "title": "标题一"
* "identity": "xxx@xxx@i"
* ...
* },
* "work": { //如果导入目标是流程管理,则包含work对象
* "title": "标题一"
* "identity": "xxx@xxx@i"
* ...
* },
* "errortextlist" : [], //错误信息
* "errortextlistexcel": [] //在出错界面导出excel时的错误信息
* }
*/
"aftercreaterowdata"
]
},
initialize: function(container, json, options, app, parentmacro){
this.setoptions(options);
this.path = "../x_component_query_query/$importer/";
this.csspath = "../x_component_query_query/$importer/" this.options.style "/css.wcss";
this._loadcss();
this.lp = mwf.xapplication.query.query.lp;
this.app = app;
this.json = json;
this.container = container;
this.parentmacro = parentmacro;
this.lookupaction = mwf.actions.get("x_query_assemble_surface");
},
load: function(){
this.excelutils = new mwf.xapplication.query.query.importer.excelutils( this );
this.getimporterjson( function () {
this.loadmacro( function () {
this._loadmoduleevents();
this.fireevent("queryload");
this.importfromexcel()
}.bind(this))
}.bind(this))
},
loadmacro: function (callback) {
mwf.require("mwf.xscript.macro", function () {
this.macro = new mwf.macro.viewcontext(this);
if (callback) callback();
}.bind(this));
},
createloadding: function(){
this.loadingareanode = new element("div", {"styles": this.css.viewloadingareanode}).inject(this.contentareanode);
new element("div", {"styles": {"height": "5px"}}).inject(this.loadingareanode);
var loadingnode = new element("div", {"styles": this.css.viewloadingnode}).inject(this.loadingareanode);
new element("div", {"styles": this.css.viewloadingiconnode}).inject(loadingnode);
var loadingtextnode = new element("div", {"styles": this.css.viewloadingtextnode}).inject(loadingnode);
loadingtextnode.set("text", "loading...");
},
getimporterjson: function(callback){
if( this.importerjson && this.json ){
if (callback) callback();
}else{
if (this.json.name){
this.lookupaction.getimportmodel(this.json.name, this.json.application, function(json){
this.importerid = json.data.id;
this.importerjson = json.decode(json.data.data);
json.data.data = this.importerjson;
this.json = object.merge(this.json, json.data);
if (callback) callback();
}.bind(this));
}else{
this.lookupaction.getimportmodelbyid(this.json.id, function(json){
this.importerid = json.data.id;
this.importerjson = json.decode(json.data.data);
json.data.data = this.importerjson;
this.json.application = json.data.query;
this.json = object.merge(this.json, json.data);
if (callback) callback();
}.bind(this));
}
}
},
_loadmoduleevents : function(){
object.each(this.json.data.events, function(e, key){
if (e.code){
if (this.options.moduleevents.indexof(key)!==-1){
this.addevent(key, function(event, target){
return this.macro.fire(e.code, target || this, event);
}.bind(this));
}
}
}.bind(this));
},
getdatecolindexarray: function(){
var datecolindexarray = [];
this.json.data.columnlist.each(function(columnjson, index){
var datatype = this.json.type === "dynamictable" ? columnjson.datatype_querytable : columnjson.datatype_cmsprocess;
if( ["date","datetime"].contains(datatype) )datecolindexarray.push( index );
}.bind(this));
return datecolindexarray;
},
getorgcolindexarray : function(){
var orgcolindexarray = [];
this.json.data.columnlist.each(function(columnjson, index){
if( columnjson.isname ) {
orgcolindexarray.push(index);
}
// }else if( this.json.type === "cms" ){
// if( columnjson.ispublisher || columnjson.isauthor || columnjson.isreader ){
// orgcolindexarray.push( index );
// }
// }
}.bind(this));
return orgcolindexarray;
},
importfromexcel : function(){
this.rowlist = [];
this.excelutils.upload( this.getdatecolindexarray(), function (importeddata) {
importeddata = importeddata.filter(function (array) {
for( var i=0; i 0 )this.importeddata.shift();
this.fireevent("beforeimport", [this.importeddata]);
promise.resolve( this.importeddata.promise ).then(function () {
this.listallorgdatabyimport( function () {
this.importeddata.each( function( linedata, lineindex ){
this.rowlist.push( new mwf.xapplication.query.query.importer.row( this, linedata, lineindex ) )
}.bind(this));
var isvalid = this.json.enablevalid ? this.checkimporteddata() : this.checknecessaryimporteddata();
promise.resolve(isvalid).then(function ( isvalid ) {
if( isvalid ){
this.doimportdata();
}else{
this.openimportederrordlg();
}
}.bind(this));
}.bind(this));
}.bind(this));
}.bind(this)
});
this.progressbar.importerid = this.importerid;
}.bind(this));
},
getdata : function(){
var data = ( this.rowlist || [] ).map( function(row){
return row.getresult();
});
return data;
},
doimportdata: function(){
//创建数据
// this.rowlist.each( function( row, i ){
// row.createdata();
// }.bind(this));
//再次校验数据(计算的内容)
var date = new date();
var flag = true;
this.rowlist.each( function(row, index){
if( row.errortextlist.length )flag = false;
}.bind(this));
var arg = {
validted : flag,
data : this.importeddata,
rowlist : this.rowlist
};
this.fireevent( "validimport", [arg] );
promise.resolve( arg.promise ).then(function(){
flag = arg.validted;
if( !flag ){
this.openimportederrordlg();
return;
}
var data = this.getdata();
this.lookupaction.getuuid(function(json){
this.recordid = json.data;
this.lookupaction.executimportmodel(this.json.id, {
"recordid": this.recordid,
"data" : data
}, function () {
//this.showimportingstatus( data, date )
this.progressbar.showimporting( this.recordid, function( data ){
data.data = data;
data.rowlist = this.rowlist;
this.fireevent("afterimport", data);
return data;
}.bind(this), date);
}.bind(this), function (xhr) {
var requestjson = json.parse(xhr.responsetext);
this.app.notice(requestjson.message, "error");
this.progressbar.close();
}.bind(this))
}.bind(this))
}.bind(this))
},
objecttostring: function (obj, type) {
if(!obj)return "";
var arr = [];
object.each(obj, function (value, key) {
if( type === "style" ){
arr.push( key ":" value ";" )
}else{
arr.push( key "='" value "'" )
}
})
return arr.join( " " )
},
openimportederrordlg : function(){
if(this.progressbar)this.progressbar.close();
var _self = this;
var div = new element("div", { style : "padding:10px;" });
var dlg = o2.dl.open({
"style" : "user",
"title": this.lp.importfail,
"content": div,
"offset": {"y": 0},
"ismax": true,
"width": 1000,
"height": 700,
"buttonlist": [
{
"type": "exportwitherror",
"text": this.lp.exportexcel,
"action": function () { _self.exportwithimportdatatoexcel(); }
},
{
"type": "cancel",
"text": this.lp.cancel,
"action": function () { dlg.close(); }
}
],
"onpostshow": function () {
var htmlarray = [""];
var titlestyle = this.objecttostring( this.css.titlestyles, "style" );
htmlarray.push( "" );
this.json.data.columnlist.each( function (columnjson, i) {
htmlarray.push( "" );
});
htmlarray.push( "" );
htmlarray.push( "" );
var contentstyles = object.clone( this.css.contentstyles );
if( !contentstyles[ "border-bottom" ] && !contentstyles[ "border" ] )contentstyles[ "border-bottom" ] = "1px solid #eee";
var contentstyle = this.objecttostring( object.merge( contentstyles, {"text-align":"left"}) , "style" );
this.rowlist.each( function( row, lineindex ){
var linedata = row.importeddata;
htmlarray.push( "" );
this.json.data.columnlist.each( function (columnjson, i) {
htmlarray.push( "" ); //换行符
});
htmlarray.push( "" );
htmlarray.push( "" );
}.bind(this));
htmlarray.push( "" columnjson.displayname " " this.lp.validationinfor " " ( linedata[ i ] || '' ).replace(/
/g,"
") " " ( row.errortextlist ? row.errortextlist.join("
") : "" ) "
" );
div.set("html" , htmlarray.join(""));
}.bind(this),
"onpostclose": function(){
dlg = null;
}.bind(this)
});
},
//必须校验的数据
checknecessaryimporteddata: function(){
var flag = true;
this.rowlist.each( function(row, index){
if( !row.checknecessary() )flag = false;
}.bind(this));
var arg = {
validted : flag,
data : this.importeddata,
rowlist : this.rowlist
};
this.fireevent( "validimport", [arg] );
return promise.resolve( arg.promise ).then(function () {
return arg.validted;
});
},
//校验excel中的数据
checkimporteddata : function(){
var flag = true;
this.rowlist.each( function(row, index){
if( !row.checkvalid() )flag = false;
}.bind(this));
var arg = {
validted : flag,
data : this.importeddata,
rowlist : this.rowlist
};
this.fireevent( "validimport", [arg] );
return promise.resolve( arg.promise ).then(function () {
return arg.validted;
});
},
getorgdata : function( str, ignorenone, isparse ){
str = str.trim();
var flag = str.substr(str.length-2, 2);
var d;
switch (flag.tolowercase()){
case "@i":
d = this.identitymapimported[str];
break;
case "@p":
d = this.personmapimported[str];
break;
case "@u":
d = this.unitmapimported[str];
break;
case "@g":
d = this.groupmapimported[str];
break;
default:
d = this.identitymapimported[str] ||
this.personmapimported[str] ||
this.unitmapimported[str] ||
this.groupmapimported[str];
break;
}
if( d )return isparse ? mwf.org.parseorgdata(d, true, true) : d;
if( ignorenone ) {
return null;
}else{
return {"errortext": str this.lp.notexistinsystem };
}
},
stringtoarray: function(string){
return string.replace(/[\n\r]/g,",").replace(/
/g,",").split(/\s*,\s*/g ).filter(function(s){
return !!s;
});
},
listallorgdatabyimport : function ( callback ) {
var orgcolindexarray = this.getorgcolindexarray();
if( orgcolindexarray.length === 0 ){
if(callback)callback();
return;
}
var identitylist = [], personlist = [], unitlist = [], grouplist = [];
if( orgcolindexarray.length > 0 ){
this.importeddata.each( function( linedata, lineindex ){
// if( lineindex === 0 )return;
orgcolindexarray.each( function (colindex, i) {
if( !linedata[colindex] )return;
var arr = this.stringtoarray(linedata[colindex]);
arr.each( function( a ){
a = a.trim();
var flag = a.substr(a.length-2, 2);
switch (flag.tolowercase()){
case "@i":
identitylist.push( a ); break;
case "@p":
personlist.push( a ); break;
case "@u":
unitlist.push( a ); break;
case "@g":
grouplist.push( a ); break;
default:
identitylist.push( a );
personlist.push( a );
unitlist.push( a );
grouplist.push( a );
break;
}
})
}.bind(this))
}.bind(this));
var identityloaded, personloaded, unitloaded, grouploaded;
var check = function () {
if( identityloaded && personloaded && unitloaded && grouploaded ){
if(callback)callback();
}
};
this.identitymapimported = {};
if( identitylist.length ){
identitylist = identitylist.unique();
o2.actions.load("x_organization_assemble_express").identityaction.listobject({ identitylist : identitylist }, function (json) {
json.data.each( function (d) { if(d)this.identitymapimported[ d.matchkey ] = d; }.bind(this));
identityloaded = true;
check();
}.bind(this))
}else{
identityloaded = true;
check();
}
this.personmapimported = {};
if( personlist.length ){
personlist = personlist.unique();
o2.actions.load("x_organization_assemble_express").personaction.listobject({ personlist : personlist }, function (json) {
json.data.each( function (d) { if(d)this.personmapimported[ d.matchkey ] = d; }.bind(this));
personloaded = true;
check();
}.bind(this))
}else{
personloaded = true;
check();
}
this.unitmapimported = {};
if( unitlist.length ){
unitlist = unitlist.unique();
o2.actions.load("x_organization_assemble_express").unitaction.listobject({ unitlist : unitlist }, function (json) {
json.data.each( function (d) { if(d)this.unitmapimported[ d.matchkey ] = d; }.bind(this));
unitloaded = true;
check();
}.bind(this))
}else{
unitloaded = true;
check();
}
this.groupmapimported = {};
if( grouplist.length ){
grouplist = grouplist.unique();
o2.actions.load("x_organization_assemble_express").groupaction.listobject({ grouplist : grouplist }, function (json) {
json.data.each( function (d) { if(d)this.groupmapimported[ d.matchkey ] = d; }.bind(this));
grouploaded = true;
check();
}.bind(this))
}else{
grouploaded = true;
check();
}
}
},
// showimportingstatus: function( improteddata, date ){
// this.progressbar.showimporting( this.recordid, function( data ){
// data.data = improteddata;
// data.rowlist = this.rowlist;
// this.fireevent("afterimport", data)
// }.bind(this), date);
// },
exportwithimportdatatoexcel : function ( importdata ) {
if( !this.excelutils ){
this.excelutils = new mwf.xapplication.query.query.importer.excelutils( this );
}
var exportto = function () {
var resultarr = [];
var titlearr = this.gettitlearray();
titlearr.push( this.lp.validationinfor );
resultarr.push( titlearr );
if( importdata ){
importdata.each( function (linedata, lineindex) {
var array = [];
if( o2.typeof(linedata)==="array" ) {
linedata.each(function (d, i) {
array.push((d || '').replace(/
/g, "\n"));
});
}else if(o2.typeof(linedata)==="object"){
this.json.data.columnlist.each( function (columnjson, i) {
array.push( linedata[columnjson.path] || "" )
}.bind(this));
if( linedata["o2errortext"] ){
array.push( linedata["o2errortext"] );
}
}
resultarr.push( array );
}.bind(this));
}else{
this.rowlist.each( function( row, lineindex ){
var linedata = row.importeddata;
var array = [];
for( var i=0; i0){
return false;
}
this.createdata();
if( this.importer.json.type === "cms" ){
this.checkcms( true );
}else if( this.importer.json.type === "process" ){
this.checkprocess( true );
}
if(this.errortextlist.length>0){
return false;
}
return true;
},
checknecessary: function(){
var lp = this.lp;
var columntext = lp.importvalidationcolumntext;
var columntextexcel = lp.importvalidationcolumntextexcel;
var errortextlist = [];
var errortextlistexcel = [];
this.importer.json.data.columnlist.each( function (columnjson, i) {
var colinfor = columntext.replace( "{n}", i 1 );
var colinforexcel = columntextexcel.replace( "{n}", this.importer.excelutils.index2colname( i ) );
var value = this.importeddata[i] || "";
var datatype = this.importer.json.type === "dynamictable" ? columnjson.datatype_querytable : columnjson.datatype_cmsprocess;
if( columnjson.validfieldtype !== false && value ){
switch ( datatype ) {
case "json":
case "stringmap":
value = value.replace(/
/g,"");
try{
var d = json.parse(value);
}catch (e) {
errortextlist.push(colinfor value lp.notvalidjson lp.fullstop );
errortextlistexcel.push( colinforexcel value lp.notvalidjson lp.fullstop );
}
break;
default:
break;
}
}
}.bind(this));
this.errortextlist = this.errortextlist.concat( errortextlist );
this.errortextlistexcel = this.errortextlistexcel.concat( errortextlistexcel );
if(this.errortextlist.length>0){
return false;
}
this.createdata();
if( this.importer.json.type === "cms" ){
this.checkcms();
}else if( this.importer.json.type === "process" ){
this.checkprocess();
}
if(this.errortextlist.length>0){
return false;
}
return true;
// var flag = true;
// if( this.importer.json.type === "cms" ){
// if( !this.checkcms() )flag = false;
// }else if( this.importer.json.type === "process" ){
// if( !this.checkprocess() )flag = false;
// }
// return flag;
},
getcol: function(key, isexcel){
var lp = this.lp;
if( this.pathindexmap && typeof(this.pathindexmap[key]) === "number"){
var i = this.pathindexmap[key];
if( isexcel ){
var text = lp.importvalidationcolumntextexcel;
return text.replace( "{n}", this.importer.excelutils.index2colname( i ) );
}else{
var text = lp.importvalidationcolumntext;
return text.replace( "{n}", i 1 );
}
}
return ""
},
checkcms : function( notcheckname ){
var lp = this.lp;
var errortextlist = [];
var errortextlistexcel = [];
var data = this.document.identity;
if(!data){
errortextlist.push( this.getcol("identity", false) lp.nodrafter lp.fullstop );
errortextlistexcel.push( this.getcol("identity", true) lp.nodrafter lp.fullstop );
}else if(data.split("@").getlast().tolowercase() !== "i"){
errortextlist.push( this.getcol("identity", false) '"' data '"' lp.drafterisnotidentity lp.fullstop );
errortextlistexcel.push( this.getcol("identity", true) '"' data '"' lp.drafterisnotidentity lp.fullstop );
}
var data = this.document.publishtime;
if(!data){
errortextlist.push(this.getcol("publishtime", false) lp.nopublishtime lp.fullstop );
errortextlistexcel.push(this.getcol("publishtime", false) lp.nopublishtime lp.fullstop );
}else if( ! new date(data).isvalid() ){
errortextlist.push(this.getcol("publishtime", false) '"' data '"' lp.publishtimeformaterror lp.fullstop );
errortextlistexcel.push(this.getcol("publishtime", false) '"' data '"' lp.publishtimeformaterror lp.fullstop );
}
data = this.document.title;
if( data && data.length > 70){
errortextlist.push(this.getcol("title", false) '"' data '"' lp.cmstitlelengthinfor lp.fullstop );
errortextlistexcel.push(this.getcol("title", false) '"' data '"' lp.cmstitlelengthinfor lp.fullstop );
}
data = this.document.summary;
if( data && data.length > 70 ){
errortextlist.push(this.getcol("summary", false) '"' data '"' lp.cmssummarylengthinfor lp.fullstop );
errortextlistexcel.push( this.getcol("summary", false) '"' data '"' lp.cmssummarylengthinfor lp.fullstop );
}
this.errortextlist = this.errortextlist.concat( errortextlist );
this.errortextlistexcel = this.errortextlistexcel.concat( errortextlistexcel );
if(errortextlist.length > 0){
return false;
}
return true;
},
checkprocess : function( notcheckname ){
var lp = this.lp;
var json = this.importer.json;
var columntext = lp.importvalidationcolumntext;
var columntextexcel = lp.importvalidationcolumntextexcel;
var errortextlist = [];
var errortextlistexcel = [];
var data = this.work.identity;
if(!data){
errortextlist.push( this.getcol("identity", false) lp.nodrafter lp.fullstop );
errortextlistexcel.push( this.getcol("identity", true) lp.nodrafter lp.fullstop );
}else if(data.split("@").getlast().tolowercase() !== "i"){
errortextlist.push( this.getcol("identity", false) '"' data '"' lp.drafterisnotidentity lp.fullstop );
errortextlistexcel.push( this.getcol("identity", true) '"' data '"' lp.drafterisnotidentity lp.fullstop );
}
if( json.data.processstatus === "completed" ){
if(!this.work.form){
errortextlist.push( lp.noform lp.fullstop );
errortextlistexcel.push( lp.noform lp.fullstop );
}
var data = this.work.starttime;
if(!data){
errortextlist.push(this.getcol("starttime", false) lp.nostarttime lp.fullstop );
errortextlistexcel.push(this.getcol("starttime", false) lp.nostarttime lp.fullstop );
}else if( ! new date(data).isvalid() ){
errortextlist.push(this.getcol("starttime", false) '"' data '"' lp.starttimeformaterror lp.fullstop );
errortextlistexcel.push(this.getcol("starttime", false) '"' data '"' lp.starttimeformaterror lp.fullstop );
}
var data = this.work.completetime;
if(!data){
errortextlist.push(this.getcol("completetime", false) lp.noendtime lp.fullstop );
errortextlistexcel.push(this.getcol("completetime", false) lp.noendtime lp.fullstop );
}else if( ! new date(data).isvalid() ){
errortextlist.push(this.getcol("completetime", false) '"' data '"' lp.endtimeformaterror lp.fullstop );
errortextlistexcel.push(this.getcol("completetime", false) '"' data '"' lp.endtimeformaterror lp.fullstop );
}
}
this.errortextlist = this.errortextlist.concat( errortextlist );
this.errortextlistexcel = this.errortextlistexcel.concat( errortextlistexcel );
if(errortextlist.length > 0){
return false;
}
return true;
},
createdata : function(){
var json = this.importer.json;
if( json.type === "cms" ){
this.document = {
categoryid : json.data.category.id,
readerlist : [],
authorlist : [],
docdata : this.data
};
}else if( json.type === "process" ){
this.work = {
processflag : json.data.process.id,
data: this.data
};
}
this.importer.fireevent("beforecreaterowdata", [this]);
json.data.columnlist.each( function (columnjson, i) {
if(!columnjson.path)return;
var value = this.importeddata[i] || "";
if( !value )return;
var data = this.parsedata(value, (json.type === "dynamictable" ? columnjson.datatype_querytable : columnjson.datatype_cmsprocess), columnjson);
if( !data && data !== 0 )return;
if( json.type === "dynamictable" ){
this.data[ columnjson.path ] = data;
}else{
this.setdatawithpath(this.data, columnjson.path, data);
}
if( json.type === "cms" ){
if( columnjson.isname ) {
if (columnjson.isauthor) {
var array = this.parsecmsreadandauthor(data, "作者");
this.document.authorlist = this.document.authorlist.concat(array)
}
if (columnjson.isreader) {
var array = this.parsecmsreadandauthor(data, "阅读");
this.document.readerlist = this.document.readerlist.concat(array)
}
}
}
}.bind(this));
json.data.calculatefieldlist.each( function (fieldjson, i) {
if( fieldjson.valuescript ){
var data = this.importer.macro.exec( fieldjson.valuescript, this );
if( o2.typeof(data) === "null" )return;
if( !fieldjson.path )return;
if( json.type === "dynamictable" ){
this.data[ fieldjson.path ] = data;
}else{
this.setdatawithpath(this.data, fieldjson.path, data);
}
if( json.type === "cms" ){
if( fieldjson.isauthor ){
var array = this.parsecmsreadandauthor( data, "作者" );
this.document.authorlist = this.document.authorlist.concat( array )
}
if( fieldjson.isreader ){
var array = this.parsecmsreadandauthor( data, "阅读" );
this.document.readerlist = this.document.readerlist.concat( array )
}
}
}
}.bind(this));
var array;
if( json.type === "cms" ){
this.document.docdata = this.data;
if( json.data.documentpublisher === "importer" ){
array = layout.session.user.identitylist;
if( array && array.length ){
this.document.identity = array[0].distinguishedname
}
}else{
this.setdatawithfield(this.document, "documentpublisherfield", "identity", true);
}
if( json.data.documentpublishtime === "importer" ){
this.document.publishtime = new date().format("db");
}else{
this.setdatawithfield(this.document, "documentpublishertimefield", "publishtime", false);
}
this.setdatawithfield(this.document, "documenttitlefield", "title", false);
if( !this.document.title )this.document.title = "无标题";
this.setdatawithfield(this.document, "documentsummaryfield", "summary", false);
}else if( json.type === "process" ){
this.work.data = this.data;
if( json.data.processdrafter === "importer" ){
array = layout.session.user.identitylist;
if( array && array.length ){
this.work.identity = array[0].distinguishedname
}
}else{
this.setdatawithfield(this.work, "processdrafterfield", "identity", true);
}
this.setdatawithfield(this.work, "processtitlefield", "title", false);
if( !this.work.title )this.work.title = "无标题";
if( json.data.processstatus === "completed" ){
this.work.form = json.data.processform || "";
this.setdatawithfield(this.work, "processserialfield", "serial", false);
this.setdatawithfield(this.work, "processstarttimefield", "starttime", false);
this.setdatawithfield(this.work, "processcompletetimefield", "completetime", false);
}
}
this.importer.fireevent("aftercreaterowdata", [ this]);
},
parsedata: function(value, datatype, json){
var data;
var type = this.importer.json.type;
switch ( datatype ) {
case "string":
case "stringlist":
if( json.isname ){
var arr = this.stringtoarray(value);
if( type === "dynamictable" ){
data = arr
}else{
data = arr.map( function(d, idx){
return this.importer.getorgdata( d, true, true ) || d;
}.bind(this)).clean();
}
}else{
if( datatype === "string" ){
var linebreak = type === "dynamictable" ? json.linebreak_querytable : json.linebreak_cmsprocess;
data = value.replace(/
/g, linebreak || "" )
}else{
data = this.stringtoarray(value);
}
}
break;
case "number":
case "double":
value = value.replace(/
/g,"");
data = parsefloat(value);
break;
case "integer":
case "long":
value = value.replace(/
/g,"");
data = parseint( value );
break;
case "numberlist":
case "doublelist":
data = this.stringtoarray(value).map( function(d, idx){ return parsefloat( d ); }.bind(this)).clean();
break;
case "integerlist":
case "longlist":
data = this.stringtoarray(value).map( function(d, idx){ return parseint( d ); }.bind(this)).clean();
break;
case "date":
value = value.replace(/
/g,"");
data = date.parse(value).format( "%y-%m-%d" );
break;
case "datetime":
value = value.replace(/
/g,"");
data = date.parse(value).format( "db" );
break;
case "datelist":
data = this.stringtoarray(value).map( function(d, idx){ return date.parse(d).format( "%y-%m-%d" ); }.bind(this)).clean();
break;
case "datetimelist":
data = this.stringtoarray(value).map( function(d, idx){ return date.parse(d).format( "db" ); }.bind(this)).clean();
break;
case "boolean":
value = value.replace(/
/g,"");
data = value.trim().tolowercase() !== "false";
break;
case "booleanlist":
data = this.stringtoarray(value).map( function(d, idx){ return value.trim().tolowercase() !== "false"; }.bind(this)).clean();
break;
case "json":
case "stringmap":
value = value.replace(/
/g,"");
data = json.parse(value);
break;
default:
data = value.replace(/
/g,"");
break;
}
return data;
},
stringtoarray: function(string){
return string.replace(/[\n\r]/g,",").replace(/
/g,",").split(/\s*,\s*/g ).filter(function(s){
return !!s;
});
},
setdatawithpath: function(obj, path, data){
var names = path.split(".");
var d = obj;
array.each(names, function (n, idx) {
if( idx === names.length -1 )return;
if ( !d[n] ){
var value = this.isnumberstring( names[idx 1] ) ? [] : {};
var n1 = this.isnumberstring( n ) ? n.toint() : n;
d[n1] = value;
d = d[n1];
}else{
d = d[n];
}
}.bind(this));
d[names[names.length -1]] = data;
},
setdatawithfield: function(obj, fieldname, path, isname){
if(!this.pathindexmap)this.pathindexmap = {};
if( !path )return;
var json = this.importer.json;
if( json.data[fieldname] ){
var f = json.data[fieldname];
json.data.columnlist.each(function(json, i){
if(json.path === f)this.pathindexmap[path] = i;
}.bind(this));
var d = this.data;
array.each( f.split("."), function (n) {
if(this.isnumberstring(n))n = n.toint();
if (d) d = d[n];
}.bind(this));
if(!d)return;
var _d = (typeof(d) === "array" && d.length) ? d[0] : d;
if( _d ){
if(isname){
obj[path] = typeof(_d) === "object" ? _d.distinguishedname : _d;
}else{
obj[path] = d;
}
}
}
},
isnumberstring: function(string){
return string.toint().tostring() === string;
},
parsecmsreadandauthor : function( data, t ){
var cnarray = ["组织","群组","人员","人员","角色"];
var keyarray = ["u","g","i","p","r"];
if( typeof(data) !== "array" )data = [data];
return data.map( function( d ){
var dn = typeof( d ) === "string" ? d : d.distinguishedname;
var name;
if( typeof(d) === "object" && d.name ){
name = d.name;
}else if( mwf.name && mwf.name.cn ){
name = mwf.name.cn( dn );
}else{
name = dn.split("@")[0];
}
var index = keyarray.indexof(dn.substr(dn.length-1, 1));
if( index > -1 ){
return {
"permission" : t,
"permissionobjecttype": cnarray[ index ],
"permissionobjectname": name,
"permissionobjectcode": dn
}
}
}).clean()
},
getsrcdata: function(){
var srcdata = {};
this.importer.json.data.columnlist.each( function (columnjson, i) {
if(columnjson.path)srcdata[ columnjson.path ] = this.importeddata[i] || "";
}.bind(this));
return srcdata;
},
getresult: function(){
if( this.importer.json.type === "cms" ){
this.document.srcdata = this.getsrcdata(); //this.importeddata;
return this.document;
}else if( this.importer.json.type === "process" ){
this.work.srcdata = this.getsrcdata(); //this.importeddata;
return this.work;
}else if( this.importer.json.type === "dynamictable" ){
this.data.srcdata = this.getsrcdata(); //this.importeddata;
return this.data;
}
}
});
mwf.xdesktop.requireapp("template", "utils.excelutils", null, false);
mwf.xapplication.query.query.importer.excelutils = new class({
extends: mwf.xapplication.template.utils.excelutils,
initialize: function(){
this.sheet2jsonoptions = {header:1};
}
});
mwf.xapplication.query.query.importer.progressbar = new class({
implements: [options, events],
extends: o2.widget.common,
options: {
zindex: null,
disabledetailbutton: false
},
initialize : function( importer, options ){
this.setoptions(options);
this.importer = importer;
this.actions = this.importer.lookupaction;
this.lp = mwf.xapplication.query.query.lp;
this.css = importer.css;
this.opendlg();
this.status = "ready";
},
opendlg: function () {
var _self = this;
this.contentnode = new element("div",{"styles": this.css.processcontentnode});
var opt = {
"style" : "user",
"title": this.lp.importrecorddetail,
"content": this.contentnode,
"offset": {"y": 0},
"ismax": false,
"width": 500,
"height": 200,
"zindex": this.options.zindex,
"buttonlist": [
{
"type": "openimportrecorddetail",
"text": this.lp.openimportrecorddetail,
"action": function () { _self.openimportrecorddetail(); }
},
{
"type": "cancel",
"text": this.lp.close,
"action": function () { this.dlg.close(); }.bind(this)
}
],
"onpostshow": function(){
this.fireevent("postshow");
}.bind(this),
"onpostload": function () {
this.titleaction.hide();
this.button.hide();
},
"onpostclose": function(){
this.dlg = null;
}.bind(this)
};
if( this.options.disabledetailbutton ){
opt.buttonlist.splice(0, 1);
}
this.dlg = o2.dl.open(opt);
},
createnode: function( noprogress ){
// var lp = this.lp;
// this.masknode = new element("div",{"styles": this.css.masknode}).inject(this.importer.container);
//
// this.node = new element("div", {"styles": this.css.progressbarnode}).inject(this.importer.container);
//
// this.topnode = new element("div",{"styles": this.css.progresstopnode}).inject(this.node);
//
// this.subjectnode = new element("div",{"styles": this.css.progresssubjectnode}).inject(this.topnode);
//
// this.topcloseaction = new element("div.topcloseaction", {
// "styles": this.css.progresstopcloseaction,
// "text": "x"
// }).inject(this.topnode);
// this.topcloseaction.hide();
// this.topcloseaction.addevent("click", function(){
// this.close();
// }.bind(this))
//
// this.contentnode = new element("div",{"styles": this.css.processcontentnode}).inject(this.node);
//
// this.bottomnode = new element("div", {"styles": this.css.progressbottomnode}).inject(this.node);
// this.bottomnode.hide();
//
// this.closeaction = new element("div.closeaction", {
// "styles": this.css.progresscloseaction,
// "text": lp.close
// }).inject(this.bottomnode);
// this.closeaction.addevent("click", function(){
// this.close();
// }.bind(this))
},
setcontenthtml: function(noprogress){
var lp = this.lp;
var contenthtml = "";
if (noprogress){
// contenthtml = "" lp.readytoimportdata1 "
source
" ;
contenthtml =
""
" " lp.readytoimportdata1 ""
"" ;
this.contentnode.set("html", contenthtml );
this.progressnode = null;
this.progresspercentnode = null;
this.progressinfornode = this.contentnode.getelement(".mwf_progressinfornode");
this.progressinfornode.setstyles(this.css.progressinfornode)
}else{
contenthtml =
""
" "
" "
" "
" " lp.readytoimportdata1 ""
"" ;
this.contentnode.set("html", contenthtml );
this.progressnode = this.contentnode.getelement(".mwf_progressnode");
this.progressnode.setstyles(this.css.progressnode);
this.progresspercentnode = this.contentnode.getelement(".mwf_progresspercentnode");
this.progresspercentnode.setstyles(this.css.progresspercentnode);
this.progressinfornode = this.contentnode.getelement(".mwf_progressinfornode");
this.progressinfornode.setstyles(this.css.progressinfornode)
}
},
showcheckdata : function(){
// this.node.show();
this.setcontenthtml(true);
this.setmessagetitle( this.lp.checkdatatitle );
this.setmessagetext( this.lp.checkdatacontent );
this.status = "check";
// this.setsize();
},
showimporting: function( recordid, callback, date ){
// this.node.show();
this.setcontenthtml();
this.recordid = recordid;
this.currentdate = date || new date();
this.intervalid = window.setinterval( function(){
this.actions.getimportmodelrecordstatus( this.recordid, function( json ){
var data = json.data;
this.status = data.status;
if( data.status === "待导入" ) { //有其他人正在导入或上次导入还未完成
this.setmessagetitle(this.lp.importwaitingtitle);
this.setmessagetext(this.lp.importwaitingcontent);
}else if( data.status === "导入中" ){ //导入中
this.setmessagetitle( this.lp.importdatatitle );
this.setmessagetext( this.lp.importdatacontent.replace( "{count}", data.executecount));
this.updateprogress( data );
}else{ //已经结束, 状态有 "导入成功","部分成功","导入失败"
if( callback )callback( data );
if( data.promise && typeof(data.promise.then) === "function" ){
promise.resolve( data.promise ).then(function () {
if(this.intervalid)window.clearinterval( this.intervalid );
this.transfercomplete( data );
}.bind(this))
}else{
if(this.intervalid)window.clearinterval( this.intervalid );
this.transfercomplete( data );
}
}
}.bind(this), null)
}.bind(this), 500 );
// this.setsize();
},
showcloseaction: function(){
this.dlg.titleaction.show();
this.dlg.button.show();
// if( this.bottomnode )this.bottomnode.show();
// if( this.topcloseaction )this.topcloseaction.show();
// this.setsize();
},
close: function(){
// this.masknode.destroy();
// this.node.destroy();
this.dlg.close();
},
// setsize: function(){
// var containersize = this.importer.container.getsize();
// var nodesize = this.node.getsize();
// var top = (containersize.y - nodesize.y) / 2;
// var left = (containersize.x - nodesize.x) / 2;
// this.node.setstyles({
// "top": (top-40) "px",
// "left": left "px"
// })
// },
updateprogress: function(data){
//status, data.executecount, data.count, data.failcount
var lp = this.lp;
var total = data.count.toint();
var processed = data.executecount.toint();
var failcount = data.failcount.toint();
var percent = 100*(processed/total);
var senddate = new date();
var lastdate = this.lasttime || this.currentdate;
var ms = senddate.gettime() - lastdate.gettime();
var speed = ( (processed - ( this.lastprocessed || 0 )) * 1000)/ms ;
var u = lp.importspeed;
speed = speed.round(2);
this.progresspercentnode.setstyle("width", "" percent "%");
var text = lp.importingdatacontent.replace("{speed}",speed).replace("{total}",total).replace("{remaining}",( total - processed ));
text = failcount ? lp.importingdataerrorcontent.replace("{errorcount}",failcount) : "";
this.progressinfornode.set("text", text);
this.lastprocessed = processed;
this.lasttime = new date();
},
transfercomplete: function( data ){
var lp = this.lp;
var senddate = new date();
var ms = senddate.gettime()-this.currentdate.gettime();
if(ms<1000)ms = 1000;
var timestr = "";
if (ms>3600000){
var h = ms/3600000;
var m_s = ms % 3600000;
var m = m_s / 60000;
var s_s = m_s % 60000;
var s = s_s/1000;
timestr = "" h.toint() lp.hour m.toint() lp.mintue s.toint() lp.second;
}else if (ms>60000){
var m = ms / 60000;
var s_s = ms % 60000;
var s = s_s/1000;
timestr = "" m.toint() lp.mintue s.toint() lp.second;
}else{
var s = ms/1000;
timestr = "" s.toint() lp.second;
}
if( data.status === "导入成功" ){
var size = data.count;
var speed = (size * 1000)/ms ;
var u = lp.importspeed;
speed = speed.round(2);
this.setmessagetitle( lp.importsuccesstitle );
var text = lp.importsuccesscontent.replace("{total}",size).replace("{speed}",speed).replace("{timestr}",timestr);
this.setmessagetext( text );
}else if(data.status === "部分成功"){
var size = data.count;
var speed = (size * 1000)/ms ;
var u = lp.importspeed;
speed = speed.round(2);
this.setmessagetitle( lp.importpartsuccesstitle );
var text = lp.importpartsuccesscontent.replace("{total}",size).replace("{speed}",speed).replace("{errorcount}",data.failcount).replace("{timestr}",timestr);
this.setmessagetext( text );
}else{ //导入失败
var size = data.count;
this.setmessagetitle( lp.importfailtitle );
var text = lp.importfailcontent.replace("{errorinfo}",data.distribution || "").replace("{total}",size).replace("{timestr}",timestr);
this.setmessagetext( text );
}
this.clearmessageprogress();
this.showcloseaction();
},
setmessagetext: function( text){
this.progressinfornode.set("text", text);
},
setmessagetitle: function( text){
this.dlg.titletext.set("text", text);
},
clearmessageprogress: function(){
this.progressnode.destroy();
},
openimportrecorddetail: function () {
mwf.xdesktop.requireapp("query.query", "importerrecord", function () {
var detail = new mwf.xapplication.query.query.importerrecord.detail(
this.importer.container,
this.importer.app,
{ importerid: this.importerid, recordid: this.recordid }
);
detail.load();
this.dlg.close();
}.bind(this), false);
}
});