Importer.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. MWF.xApplication.query = MWF.xApplication.query || {};
  2. MWF.xApplication.query.Query = MWF.xApplication.query.Query || {};
  3. MWF.require("MWF.widget.Common", null, false);
  4. MWF.require("MWF.xScript.Macro", null, false);
  5. MWF.require("o2.widget.Dialog", null, false);
  6. MWF.xDesktop.requireApp("query.Query", "lp."+o2.language, null, false);
  7. MWF.xApplication.query.Query.Importer = MWF.QImporter = new Class({
  8. Implements: [Options, Events],
  9. Extends: MWF.widget.Common,
  10. options: {
  11. "style": "default",
  12. "moduleEvents": ["queryLoad", "beforeImport", "afterImport", "validImport", "beforeCreateRowData", "afterCreateRowData"]
  13. },
  14. initialize: function(container, json, options, app, parentMacro){
  15. this.setOptions(options);
  16. this.path = "../x_component_query_Query/$Importer/";
  17. this.cssPath = "../x_component_query_Query/$Importer/"+this.options.style+"/css.wcss";
  18. this._loadCss();
  19. this.lp = MWF.xApplication.query.Query.LP;
  20. this.app = app;
  21. this.json = json;
  22. this.container = container;
  23. this.parentMacro = parentMacro;
  24. this.lookupAction = MWF.Actions.get("x_query_assemble_surface");
  25. },
  26. load: function(){
  27. this.excelUtils = new MWF.xApplication.query.Query.Importer.ExcelUtils( this );
  28. this.getImporterJSON( function () {
  29. this.loadMacro( function () {
  30. this._loadModuleEvents();
  31. this.fireEvent("queryLoad");
  32. this.importFromExcel()
  33. }.bind(this))
  34. }.bind(this))
  35. },
  36. loadMacro: function (callback) {
  37. MWF.require("MWF.xScript.Macro", function () {
  38. this.Macro = new MWF.Macro.ViewContext(this);
  39. if (callback) callback();
  40. }.bind(this));
  41. },
  42. createLoadding: function(){
  43. this.loadingAreaNode = new Element("div", {"styles": this.css.viewLoadingAreaNode}).inject(this.contentAreaNode);
  44. new Element("div", {"styles": {"height": "5px"}}).inject(this.loadingAreaNode);
  45. var loadingNode = new Element("div", {"styles": this.css.viewLoadingNode}).inject(this.loadingAreaNode);
  46. new Element("div", {"styles": this.css.viewLoadingIconNode}).inject(loadingNode);
  47. var loadingTextNode = new Element("div", {"styles": this.css.viewLoadingTextNode}).inject(loadingNode);
  48. loadingTextNode.set("text", "loading...");
  49. },
  50. getImporterJSON: function(callback){
  51. if( this.importerJson && this.json ){
  52. if (callback) callback();
  53. }else{
  54. if (this.json.name){
  55. this.lookupAction.getImportModel(this.json.name, this.json.application, function(json){
  56. this.importerId = json.data.id;
  57. this.importerJson = JSON.decode(json.data.data);
  58. json.data.data = this.importerJson;
  59. this.json = Object.merge(this.json, json.data);
  60. if (callback) callback();
  61. }.bind(this));
  62. }else{
  63. this.lookupAction.getImportModelById(this.json.id, function(json){
  64. this.importerId = json.data.id;
  65. this.importerJson = JSON.decode(json.data.data);
  66. json.data.data = this.importerJson;
  67. this.json.application = json.data.query;
  68. this.json = Object.merge(this.json, json.data);
  69. if (callback) callback();
  70. }.bind(this));
  71. }
  72. }
  73. },
  74. _loadModuleEvents : function(){
  75. Object.each(this.json.data.events, function(e, key){
  76. if (e.code){
  77. if (this.options.moduleEvents.indexOf(key)!==-1){
  78. this.addEvent(key, function(event, target){
  79. return this.Macro.fire(e.code, target || this, event);
  80. }.bind(this));
  81. }
  82. }
  83. }.bind(this));
  84. },
  85. getDateColIndexArray: function(){
  86. var dateColIndexArray = [];
  87. this.json.data.columnList.each(function(columnJson, index){
  88. var dataType = this.json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess;
  89. if( ["date","dateTime"].contains(dataType) )dateColIndexArray.push( index );
  90. }.bind(this));
  91. return dateColIndexArray;
  92. },
  93. getOrgColIndexArray : function(){
  94. var orgColIndexArray = [];
  95. this.json.data.columnList.each(function(columnJson, index){
  96. if( columnJson.isName ) {
  97. orgColIndexArray.push(index);
  98. }
  99. // }else if( this.json.type === "cms" ){
  100. // if( columnJson.isPublisher || columnJson.isAuthor || columnJson.isReader ){
  101. // orgColIndexArray.push( index );
  102. // }
  103. // }
  104. }.bind(this));
  105. return orgColIndexArray;
  106. },
  107. importFromExcel : function(){
  108. this.rowList = [];
  109. this.excelUtils.upload( this.getDateColIndexArray(), function (importedData) {
  110. importedData = importedData.filter(function (array) {
  111. for( var i=0; i<array.length; i++ ){
  112. if(array[i])return true;
  113. }
  114. return false;
  115. });
  116. if( !importedData.length )return;
  117. var titleLength = importedData[0].length;
  118. importedData.each(function (array) {
  119. for( var i=0; i<titleLength; i++){
  120. if( typeOf(array[i]) === "null" )array[i] = "";
  121. }
  122. })
  123. this.progressBar = new MWF.xApplication.query.Query.Importer.ProgressBar( this, {
  124. "onPostShow": function(){
  125. this.progressBar.showCheckData();
  126. this.importedData = importedData;
  127. if( this.importedData.length > 0 )this.importedData.shift();
  128. this.fireEvent("beforeImport", [this.importedData]);
  129. this.listAllOrgDataByImport( function () {
  130. this.importedData.each( function( lineData, lineIndex ){
  131. this.rowList.push( new MWF.xApplication.query.Query.Importer.Row( this, lineData, lineIndex ) )
  132. }.bind(this));
  133. var isValid = this.json.enableValid ? this.checkImportedData() : this.checkNecessaryImportedData();
  134. if( isValid ){
  135. this.doImportData();
  136. }else{
  137. this.openImportedErrorDlg();
  138. }
  139. }.bind(this));
  140. }.bind(this)
  141. });
  142. this.progressBar.importerId = this.importerId;
  143. }.bind(this));
  144. },
  145. getData : function(){
  146. var data = ( this.rowList || [] ).map( function(row){
  147. return row.getResult();
  148. });
  149. return data;
  150. },
  151. doImportData: function(){
  152. //创建数据
  153. // this.rowList.each( function( row, i ){
  154. // row.createData();
  155. // }.bind(this));
  156. //再次校验数据(计算的内容)
  157. var flag = true;
  158. this.rowList.each( function(row, index){
  159. if( row.errorTextList.length )flag = false;
  160. }.bind(this));
  161. var arg = {
  162. validted : flag,
  163. data : this.importedData,
  164. rowList : this.rowList
  165. };
  166. this.fireEvent( "validImport", [arg] );
  167. flag = arg.validted;
  168. if( !flag ){
  169. this.openImportedErrorDlg();
  170. return;
  171. }
  172. var data = this.getData();
  173. this.lookupAction.getUUID(function(json){
  174. this.recordId = json.data;
  175. this.lookupAction.executImportModel(this.json.id, {
  176. "recordId": this.recordId,
  177. "data" : data
  178. }, function () {
  179. this.showImportingStatus( data )
  180. }.bind(this), function (xhr) {
  181. var requestJson = JSON.parse(xhr.responseText);
  182. this.app.notice(requestJson.message, "error");
  183. this.progressBar.close();
  184. }.bind(this))
  185. }.bind(this))
  186. },
  187. objectToString: function (obj, type) {
  188. if(!obj)return "";
  189. var arr = [];
  190. Object.each(obj, function (value, key) {
  191. if( type === "style" ){
  192. arr.push( key + ":"+ value +";" )
  193. }else{
  194. arr.push( key + "='"+ value +"'" )
  195. }
  196. })
  197. return arr.join( " " )
  198. },
  199. openImportedErrorDlg : function(){
  200. if(this.progressBar)this.progressBar.close();
  201. var _self = this;
  202. var div = new Element("div", { style : "padding:10px;" });
  203. var dlg = o2.DL.open({
  204. "style" : "user",
  205. "title": this.lp.importFail,
  206. "content": div,
  207. "offset": {"y": 0},
  208. "isMax": true,
  209. "width": 1000,
  210. "height": 700,
  211. "buttonList": [
  212. {
  213. "type": "exportWithError",
  214. "text": this.lp.exportExcel,
  215. "action": function () { _self.exportWithImportDataToExcel(); }
  216. },
  217. {
  218. "type": "cancel",
  219. "text": this.lp.cancel,
  220. "action": function () { dlg.close(); }
  221. }
  222. ],
  223. "onPostShow": function () {
  224. var htmlArray = ["<table "+ this.objectToString( this.css.properties ) +" style='"+this.objectToString( this.css.tableStyles, "style" )+"'>"];
  225. var titleStyle = this.objectToString( this.css.titleStyles, "style" );
  226. htmlArray.push( "<tr>" );
  227. this.json.data.columnList.each( function (columnJson, i) {
  228. htmlArray.push( "<th style='"+titleStyle+"'>"+columnJson.displayName+"</th>" );
  229. });
  230. htmlArray.push( "<th style='"+titleStyle+"'> "+this.lp.validationInfor +"</th>" );
  231. htmlArray.push( "</tr>" );
  232. var contentStyles = Object.clone( this.css.contentStyles );
  233. if( !contentStyles[ "border-bottom" ] && !contentStyles[ "border" ] )contentStyles[ "border-bottom" ] = "1px solid #eee";
  234. var contentStyle = this.objectToString( Object.merge( contentStyles, {"text-align":"left"}) , "style" );
  235. this.rowList.each( function( row, lineIndex ){
  236. var lineData = row.importedData;
  237. htmlArray.push( "<tr>" );
  238. this.json.data.columnList.each( function (columnJson, i) {
  239. htmlArray.push( "<td style='"+contentStyle+"'>"+ ( lineData[ i ] || '' ).replace(/&#10;/g,"<br/>") +"</td>" ); //换行符&#10;
  240. });
  241. htmlArray.push( "<td style='"+contentStyle+"'>"+( row.errorTextList ? row.errorTextList.join("<br/>") : "" )+"</td>" );
  242. htmlArray.push( "</tr>" );
  243. }.bind(this));
  244. htmlArray.push( "</table>" );
  245. div.set("html" , htmlArray.join(""));
  246. }.bind(this),
  247. "onPostClose": function(){
  248. dlg = null;
  249. }.bind(this)
  250. });
  251. },
  252. //必须校验的数据
  253. checkNecessaryImportedData: function(){
  254. var flag = true;
  255. this.rowList.each( function(row, index){
  256. if( !row.checkNecessary() )flag = false;
  257. }.bind(this));
  258. var arg = {
  259. validted : flag,
  260. data : this.importedData,
  261. rowList : this.rowList
  262. };
  263. this.fireEvent( "validImport", [arg] );
  264. return arg.validted;
  265. },
  266. //校验Excel中的数据
  267. checkImportedData : function(){
  268. var flag = true;
  269. this.rowList.each( function(row, index){
  270. if( !row.checkValid() )flag = false;
  271. }.bind(this));
  272. var arg = {
  273. validted : flag,
  274. data : this.importedData,
  275. rowList : this.rowList
  276. };
  277. this.fireEvent( "validImport", [arg] );
  278. return arg.validted;
  279. },
  280. getOrgData : function( str, ignoreNone, isParse ){
  281. str = str.trim();
  282. var flag = str.substr(str.length-2, 2);
  283. var d;
  284. switch (flag.toLowerCase()){
  285. case "@i":
  286. d = this.identityMapImported[str];
  287. break;
  288. case "@p":
  289. d = this.personMapImported[str];
  290. break;
  291. case "@u":
  292. d = this.unitMapImported[str];
  293. break;
  294. case "@g":
  295. d = this.groupMapImported[str];
  296. break;
  297. default:
  298. d = this.identityMapImported[str] ||
  299. this.personMapImported[str] ||
  300. this.unitMapImported[str] ||
  301. this.groupMapImported[str];
  302. break;
  303. }
  304. if( d )return isParse ? MWF.org.parseOrgData(d, true, true) : d;
  305. if( ignoreNone ) {
  306. return null;
  307. }else{
  308. return {"errorText": str + this.lp.notExistInSystem };
  309. }
  310. },
  311. stringToArray: function(string){
  312. return string.replace(/[\n\r]/g,",").replace(/&#10;/g,",").split(/\s*,\s*/g ).filter(function(s){
  313. return !!s;
  314. });
  315. },
  316. listAllOrgDataByImport : function ( callback ) {
  317. var orgColIndexArray = this.getOrgColIndexArray();
  318. if( orgColIndexArray.length === 0 ){
  319. if(callback)callback();
  320. return;
  321. }
  322. var identityList = [], personList = [], unitList = [], groupList = [];
  323. if( orgColIndexArray.length > 0 ){
  324. this.importedData.each( function( lineData, lineIndex ){
  325. // if( lineIndex === 0 )return;
  326. orgColIndexArray.each( function (colIndex, i) {
  327. if( !lineData[colIndex] )return;
  328. var arr = this.stringToArray(lineData[colIndex]);
  329. arr.each( function( a ){
  330. a = a.trim();
  331. var flag = a.substr(a.length-2, 2);
  332. switch (flag.toLowerCase()){
  333. case "@i":
  334. identityList.push( a ); break;
  335. case "@p":
  336. personList.push( a ); break;
  337. case "@u":
  338. unitList.push( a ); break;
  339. case "@g":
  340. groupList.push( a ); break;
  341. default:
  342. identityList.push( a );
  343. personList.push( a );
  344. unitList.push( a );
  345. groupList.push( a );
  346. break;
  347. }
  348. })
  349. }.bind(this))
  350. }.bind(this));
  351. var identityLoaded, personLoaded, unitLoaded, groupLoaded;
  352. var check = function () {
  353. if( identityLoaded && personLoaded && unitLoaded && groupLoaded ){
  354. if(callback)callback();
  355. }
  356. };
  357. this.identityMapImported = {};
  358. if( identityList.length ){
  359. identityList = identityList.unique();
  360. o2.Actions.load("x_organization_assemble_express").IdentityAction.listObject({ identityList : identityList }, function (json) {
  361. json.data.each( function (d) { if(d)this.identityMapImported[ d.matchKey ] = d; }.bind(this));
  362. identityLoaded = true;
  363. check();
  364. }.bind(this))
  365. }else{
  366. identityLoaded = true;
  367. check();
  368. }
  369. this.personMapImported = {};
  370. if( personList.length ){
  371. personList = personList.unique();
  372. o2.Actions.load("x_organization_assemble_express").PersonAction.listObject({ personList : personList }, function (json) {
  373. json.data.each( function (d) { if(d)this.personMapImported[ d.matchKey ] = d; }.bind(this));
  374. personLoaded = true;
  375. check();
  376. }.bind(this))
  377. }else{
  378. personLoaded = true;
  379. check();
  380. }
  381. this.unitMapImported = {};
  382. if( unitList.length ){
  383. unitList = unitList.unique();
  384. o2.Actions.load("x_organization_assemble_express").UnitAction.listObject({ unitList : unitList }, function (json) {
  385. json.data.each( function (d) { if(d)this.unitMapImported[ d.matchKey ] = d; }.bind(this));
  386. unitLoaded = true;
  387. check();
  388. }.bind(this))
  389. }else{
  390. unitLoaded = true;
  391. check();
  392. }
  393. this.groupMapImported = {};
  394. if( groupList.length ){
  395. groupList = groupList.unique();
  396. o2.Actions.load("x_organization_assemble_express").GroupAction.listObject({ groupList : groupList }, function (json) {
  397. json.data.each( function (d) { if(d)this.groupMapImported[ d.matchKey ] = d; }.bind(this));
  398. groupLoaded = true;
  399. check();
  400. }.bind(this))
  401. }else{
  402. groupLoaded = true;
  403. check();
  404. }
  405. }
  406. },
  407. showImportingStatus: function( improtedData ){
  408. this.progressBar.showImporting( this.recordId, function( data ){
  409. data.data = improtedData;
  410. data.rowList = this.rowList;
  411. this.fireEvent("afterImport", data)
  412. }.bind(this));
  413. },
  414. exportWithImportDataToExcel : function ( importData ) {
  415. if( !this.excelUtils ){
  416. this.excelUtils = new MWF.xApplication.query.Query.Importer.ExcelUtils( this );
  417. }
  418. var exportTo = function () {
  419. var resultArr = [];
  420. var titleArr = this.getTitleArray();
  421. titleArr.push( this.lp.validationInfor );
  422. resultArr.push( titleArr );
  423. if( importData ){
  424. importData.each( function (lineData, lineIndex) {
  425. var array = [];
  426. if( o2.typeOf(lineData)==="array" ) {
  427. lineData.each(function (d, i) {
  428. array.push((d || '').replace(/&#10;/g, "\n"));
  429. });
  430. }else if(o2.typeOf(lineData)==="object"){
  431. this.json.data.columnList.each( function (columnJson, i) {
  432. array.push( lineData[columnJson.path] || "" )
  433. }.bind(this));
  434. if( lineData["o2ErrorText"] ){
  435. array.push( lineData["o2ErrorText"] );
  436. }
  437. }
  438. resultArr.push( array );
  439. }.bind(this));
  440. }else{
  441. this.rowList.each( function( row, lineIndex ){
  442. var lineData = row.importedData;
  443. var array = [];
  444. lineData.each( function (d, i) {
  445. array.push( ( d || '' ).replace(/&#10;/g, "\n") );
  446. });
  447. array.push( row.errorTextListExcel ? row.errorTextListExcel.join("\n") : "" );
  448. resultArr.push( array );
  449. }.bind(this));
  450. }
  451. var colWidthArray = this.getColWidthArray();
  452. colWidthArray.push(260);
  453. var arg = {
  454. data : resultArr,
  455. colWidthArray : colWidthArray,
  456. title : this.getFileName()
  457. };
  458. this.fireEvent("export", [arg]);
  459. this.excelUtils.exportToExcel(
  460. arg.data,
  461. arg.title,
  462. arg.colWidthArray,
  463. this.getDateIndexArray()
  464. )
  465. }.bind(this);
  466. if( !this.importerJson ){
  467. this.getImporterJSON( function () {
  468. exportTo();
  469. }.bind(this))
  470. }else{
  471. exportTo();
  472. }
  473. },
  474. downloadTemplate: function(fileName, callback){
  475. if( !this.excelUtils ){
  476. this.excelUtils = new MWF.xApplication.query.Query.Importer.ExcelUtils( this );
  477. }
  478. var doExport = function () {
  479. var arg = {
  480. data : [this.getTitleArray()],
  481. colWidthArray : this.getColWidthArray(),
  482. title : this.getFileName(fileName)
  483. };
  484. if(callback)callback(arg);
  485. this.excelUtils.exportToExcel(
  486. arg.data,
  487. arg.title,
  488. arg.colWidthArray,
  489. this.getDateIndexArray()
  490. )
  491. }.bind(this)
  492. if( !this.importerJson ){
  493. this.getImporterJSON( function () {
  494. doExport()
  495. }.bind(this))
  496. }else{
  497. doExport();
  498. }
  499. },
  500. getFileName: function(fileName){
  501. var title = fileName || this.json.name;
  502. var titleA = title.split(".");
  503. if( ["xls","xlst"].contains( titleA[titleA.length-1].toLowerCase() ) ){
  504. titleA.splice( titleA.length-1 );
  505. }
  506. title = titleA.join(".");
  507. return title;
  508. },
  509. getTitleArray: function(){
  510. var titleArray = [];
  511. this.json.data.columnList.each( function (columnJson, i) {
  512. titleArray.push( columnJson.displayName );
  513. }.bind(this));
  514. return titleArray;
  515. },
  516. getColWidthArray : function(){
  517. var colWidthArr = [];
  518. this.json.data.columnList.each( function (columnJson, i) {
  519. var dataType = this.json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess;
  520. switch ( dataType ) {
  521. case "string":
  522. case "stringList":
  523. if( columnJson.isName ){
  524. colWidthArr.push( 340 );
  525. }else if( columnJson.isSummary ){
  526. colWidthArr.push( 260 );
  527. }else{
  528. colWidthArr.push( 150 );
  529. }
  530. break;
  531. case "number":
  532. case "integer":
  533. case "long":
  534. case "double":
  535. case "numberList":
  536. case "integerList":
  537. case "longList":
  538. case "doubleList":
  539. colWidthArr.push(150);
  540. break;
  541. case "date":
  542. case "dateTime":
  543. case "dateList":
  544. case "dateTimeList":
  545. colWidthArr.push(150);
  546. break;
  547. default:
  548. colWidthArr.push(150);
  549. break;
  550. }
  551. }.bind(this));
  552. return colWidthArr;
  553. },
  554. getDateIndexArray : function(){
  555. var dateIndexArr = []; //日期格式列下标
  556. this.json.data.columnList.each( function (columnJson, i) {
  557. var dataType = this.json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess;
  558. switch ( dataType ) {
  559. case "date":
  560. case "dateTime":
  561. case "dateList":
  562. case "dateTimeList":
  563. dateIndexArr.push(i);
  564. break;
  565. default:
  566. break;
  567. }
  568. }.bind(this));
  569. return dateIndexArr;
  570. },
  571. //api 使用 开始
  572. confirm: function (type, e, title, text, width, height, ok, cancel, callback, mask, style) {
  573. this.app.confirm(type, e, title, text, width, height, ok, cancel, callback, mask, style)
  574. },
  575. alert: function (type, title, text, width, height) {
  576. this.app.alert(type, "center", title, text, width, height);
  577. },
  578. notice: function (content, type, target, where, offset, option) {
  579. this.app.notice(content, type, target, where, offset, option)
  580. }
  581. //api 使用 结束
  582. });
  583. MWF.xApplication.query.Query.Importer.Row = new Class({
  584. initialize: function(importer, importedData, rowIndex){
  585. this.importer = importer;
  586. this.importedData = importedData;
  587. this.clazzType = "row";
  588. this.lp = this.importer.lp;
  589. this.data = {};
  590. this.errorTextList = [];
  591. this.errorTextListExcel = [];
  592. },
  593. checkValid : function(){
  594. var lp = this.lp;
  595. var columnText = lp.importValidationColumnText;
  596. var columnTextExcel = lp.importValidationColumnTextExcel;
  597. var errorTextList = [];
  598. var errorTextListExcel = [];
  599. this.importer.json.data.columnList.each( function (columnJson, i) {
  600. var colInfor = columnText.replace( "{n}", i+1 );
  601. var colInforExcel = columnTextExcel.replace( "{n}", this.importer.excelUtils.index2ColName( i ) );
  602. var value = this.importedData[i] || "";
  603. var dataType = this.importer.json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess;
  604. if( !columnJson.allowEmpty && !value ){
  605. errorTextList.push( colInfor + lp.canNotBeEmpty + lp.fullstop );
  606. errorTextListExcel.push( colInforExcel + lp.canNotBeEmpty + lp.fullstop );
  607. }
  608. if( columnJson.validFieldType !== false && value ){
  609. switch ( dataType ) {
  610. case "string":
  611. case "stringList":
  612. if( columnJson.isName ){
  613. var arr = this.stringToArray(value);
  614. arr.each( function(d, idx){
  615. var obj = this.importer.getOrgData( d );
  616. if( obj.errorText ){
  617. errorTextList.push( colInfor + obj.errorText + lp.fullstop );
  618. errorTextListExcel.push( colInforExcel + obj.errorText + lp.fullstop );
  619. }
  620. }.bind(this));
  621. }
  622. break;
  623. case "number":
  624. case "integer":
  625. case "long":
  626. case "double":
  627. value = value.replace(/&#10;/g,"");
  628. if (isNaN(value)){
  629. errorTextList.push( colInfor + value + lp.notValidNumber + lp.fullstop );
  630. errorTextListExcel.push( colInforExcel + value + lp.notValidNumber + lp.fullstop );
  631. }
  632. break;
  633. case "numberList":
  634. case "integerList":
  635. case "longList":
  636. case "doubleList":
  637. var arr = this.stringToArray(value);
  638. arr.each( function(d, idx){
  639. if (isNaN(d)){
  640. errorTextList.push( colInfor + d + lp.notValidNumber + lp.fullstop );
  641. errorTextListExcel.push( colInforExcel + d + lp.notValidNumber + lp.fullstop );
  642. }
  643. }.bind(this));
  644. break;
  645. case "date":
  646. case "dateTime":
  647. value = value.replace(/&#10;/g,"");
  648. if( !( new Date(value).isValid() )){
  649. errorTextList.push(colInfor + value + lp.notValidDate + lp.fullstop );
  650. errorTextListExcel.push( colInforExcel + value + lp.notValidDate + lp.fullstop );
  651. }
  652. break;
  653. case "dateList":
  654. case "dateTimeList":
  655. var arr = this.stringToArray(value);
  656. arr.each( function(d, idx){
  657. if( !( new Date(d).isValid() )){
  658. errorTextList.push(colInfor + d + lp.notValidDate + lp.fullstop );
  659. errorTextListExcel.push( colInforExcel + d + lp.notValidDate + lp.fullstop );
  660. }
  661. }.bind(this));
  662. break;
  663. case "boolean":
  664. value = value.replace(/&#10;/g,"");
  665. if( !["true","false"].contains(value.trim().toLowerCase()) ){
  666. errorTextList.push(colInfor + value + lp.notValidBoolean + lp.fullstop );
  667. errorTextListExcel.push( colInforExcel + value + lp.notValidBoolean + lp.fullstop );
  668. }
  669. break;
  670. case "booleanList":
  671. var arr = this.stringToArray(value);
  672. arr.each( function(d, idx){
  673. if( !["true","false"].contains(d.trim().toLowerCase())){
  674. errorTextList.push(colInfor + d + lp.notValidBoolean + lp.fullstop );
  675. errorTextListExcel.push( colInforExcel + d + lp.notValidBoolean + lp.fullstop );
  676. }
  677. }.bind(this));
  678. break;
  679. case "json":
  680. case "stringMap":
  681. value = value.replace(/&#10;/g,"");
  682. try{
  683. var d = JSON.parse(value);
  684. }catch (e) {
  685. errorTextList.push(colInfor + value + lp.notValidJson + lp.fullstop );
  686. errorTextListExcel.push( colInforExcel + value + lp.notValidJson + lp.fullstop );
  687. }
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. }.bind(this));
  694. this.errorTextList = this.errorTextList.concat( errorTextList );
  695. this.errorTextListExcel = this.errorTextListExcel.concat( errorTextListExcel );
  696. if(this.errorTextList.length>0){
  697. return false;
  698. }
  699. this.createData();
  700. if( this.importer.json.type === "cms" ){
  701. this.checkCMS( true );
  702. }else if( this.importer.json.type === "process" ){
  703. this.checkProcess( true );
  704. }
  705. if(this.errorTextList.length>0){
  706. return false;
  707. }
  708. return true;
  709. },
  710. checkNecessary: function(){
  711. var lp = this.lp;
  712. var columnText = lp.importValidationColumnText;
  713. var columnTextExcel = lp.importValidationColumnTextExcel;
  714. var errorTextList = [];
  715. var errorTextListExcel = [];
  716. this.importer.json.data.columnList.each( function (columnJson, i) {
  717. var colInfor = columnText.replace( "{n}", i+1 );
  718. var colInforExcel = columnTextExcel.replace( "{n}", this.importer.excelUtils.index2ColName( i ) );
  719. var value = this.importedData[i] || "";
  720. var dataType = this.importer.json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess;
  721. if( columnJson.validFieldType !== false && value ){
  722. switch ( dataType ) {
  723. case "json":
  724. case "stringMap":
  725. value = value.replace(/&#10;/g,"");
  726. try{
  727. var d = JSON.parse(value);
  728. }catch (e) {
  729. errorTextList.push(colInfor + value + lp.notValidJson + lp.fullstop );
  730. errorTextListExcel.push( colInforExcel + value + lp.notValidJson + lp.fullstop );
  731. }
  732. break;
  733. default:
  734. break;
  735. }
  736. }
  737. }.bind(this));
  738. this.errorTextList = this.errorTextList.concat( errorTextList );
  739. this.errorTextListExcel = this.errorTextListExcel.concat( errorTextListExcel );
  740. if(this.errorTextList.length>0){
  741. return false;
  742. }
  743. this.createData();
  744. if( this.importer.json.type === "cms" ){
  745. this.checkCMS();
  746. }else if( this.importer.json.type === "process" ){
  747. this.checkProcess();
  748. }
  749. if(this.errorTextList.length>0){
  750. return false;
  751. }
  752. return true;
  753. // var flag = true;
  754. // if( this.importer.json.type === "cms" ){
  755. // if( !this.checkCMS() )flag = false;
  756. // }else if( this.importer.json.type === "process" ){
  757. // if( !this.checkProcess() )flag = false;
  758. // }
  759. // return flag;
  760. },
  761. getCol: function(key, isExcel){
  762. var lp = this.lp;
  763. if( this.pathIndexMap && typeOf(this.pathIndexMap[key]) === "number"){
  764. var i = this.pathIndexMap[key];
  765. if( isExcel ){
  766. var text = lp.importValidationColumnTextExcel;
  767. return text.replace( "{n}", this.importer.excelUtils.index2ColName( i ) );
  768. }else{
  769. var text = lp.importValidationColumnText;
  770. return text.replace( "{n}", i+1 );
  771. }
  772. }
  773. return ""
  774. },
  775. checkCMS : function( notCheckName ){
  776. var lp = this.lp;
  777. var errorTextList = [];
  778. var errorTextListExcel = [];
  779. var data = this.document.identity;
  780. if(!data){
  781. errorTextList.push( this.getCol("identity", false) + lp.noDrafter + lp.fullstop );
  782. errorTextListExcel.push( this.getCol("identity", true) + lp.noDrafter + lp.fullstop );
  783. }else if(data.split("@").getLast().toLowerCase() !== "i"){
  784. errorTextList.push( this.getCol("identity", false) + '"'+ data +'"'+ lp.drafterIsNotIdentity + lp.fullstop );
  785. errorTextListExcel.push( this.getCol("identity", true) + '"'+ data +'"'+ lp.drafterIsNotIdentity + lp.fullstop );
  786. }
  787. var data = this.document.publishTime;
  788. if(!data){
  789. errorTextList.push(this.getCol("publishTime", false) + lp.noPublishTime + lp.fullstop );
  790. errorTextListExcel.push(this.getCol("publishTime", false) + lp.noPublishTime + lp.fullstop );
  791. }else if( ! new Date(data).isValid() ){
  792. errorTextList.push(this.getCol("publishTime", false) + '"'+ data +'"'+ lp.publishTimeFormatError + lp.fullstop );
  793. errorTextListExcel.push(this.getCol("publishTime", false) + '"'+ data +'"'+ lp.publishTimeFormatError + lp.fullstop );
  794. }
  795. data = this.document.title;
  796. if( data && data.length > 70){
  797. errorTextList.push(this.getCol("title", false) + '"'+ data +'"'+ lp.cmsTitleLengthInfor + lp.fullstop );
  798. errorTextListExcel.push(this.getCol("title", false) + '"'+ data +'"'+ + lp.cmsTitleLengthInfor + lp.fullstop );
  799. }
  800. data = this.document.summary;
  801. if( data && data.length > 70 ){
  802. errorTextList.push(this.getCol("summary", false) + '"'+ data +'"'+ lp.cmsSummaryLengthInfor + lp.fullstop );
  803. errorTextListExcel.push( this.getCol("summary", false) + '"'+ data +'"'+ lp.cmsSummaryLengthInfor + lp.fullstop );
  804. }
  805. this.errorTextList = this.errorTextList.concat( errorTextList );
  806. this.errorTextListExcel = this.errorTextListExcel.concat( errorTextListExcel );
  807. if(errorTextList.length > 0){
  808. return false;
  809. }
  810. return true;
  811. },
  812. checkProcess : function( notCheckName ){
  813. var lp = this.lp;
  814. var json = this.importer.json;
  815. var columnText = lp.importValidationColumnText;
  816. var columnTextExcel = lp.importValidationColumnTextExcel;
  817. var errorTextList = [];
  818. var errorTextListExcel = [];
  819. var data = this.work.identity;
  820. if(!data){
  821. errorTextList.push( this.getCol("identity", false) + lp.noDrafter + lp.fullstop );
  822. errorTextListExcel.push( this.getCol("identity", true) + lp.noDrafter + lp.fullstop );
  823. }else if(data.split("@").getLast().toLowerCase() !== "i"){
  824. errorTextList.push( this.getCol("identity", false) + '"'+ data +'"'+ lp.drafterIsNotIdentity + lp.fullstop );
  825. errorTextListExcel.push( this.getCol("identity", true) + '"'+ data +'"'+ lp.drafterIsNotIdentity + lp.fullstop );
  826. }
  827. if( json.data.processStatus === "completed" ){
  828. if(!this.work.form){
  829. errorTextList.push( lp.noForm + lp.fullstop );
  830. errorTextListExcel.push( lp.noForm + lp.fullstop );
  831. }
  832. var data = this.work.startTime;
  833. if(!data){
  834. errorTextList.push(this.getCol("startTime", false) + lp.noStartTime + lp.fullstop );
  835. errorTextListExcel.push(this.getCol("startTime", false) + lp.noStartTime + lp.fullstop );
  836. }else if( ! new Date(data).isValid() ){
  837. errorTextList.push(this.getCol("startTime", false) + '"'+ data +'"'+ lp.startTimeFormatError + lp.fullstop );
  838. errorTextListExcel.push(this.getCol("startTime", false) + '"'+ data +'"'+ lp.startTimeFormatError + lp.fullstop );
  839. }
  840. var data = this.work.completeTime;
  841. if(!data){
  842. errorTextList.push(this.getCol("completeTime", false) + lp.noEndTime + lp.fullstop );
  843. errorTextListExcel.push(this.getCol("completeTime", false) + lp.noEndTime + lp.fullstop );
  844. }else if( ! new Date(data).isValid() ){
  845. errorTextList.push(this.getCol("completeTime", false) + '"'+ data +'"'+ lp.endTimeFormatError + lp.fullstop );
  846. errorTextListExcel.push(this.getCol("completeTime", false) + '"'+ data +'"'+ lp.endTimeFormatError + lp.fullstop );
  847. }
  848. }
  849. this.errorTextList = this.errorTextList.concat( errorTextList );
  850. this.errorTextListExcel = this.errorTextListExcel.concat( errorTextListExcel );
  851. if(errorTextList.length > 0){
  852. return false;
  853. }
  854. return true;
  855. },
  856. createData : function(){
  857. var json = this.importer.json;
  858. if( json.type === "cms" ){
  859. this.document = {
  860. categoryId : json.data.category.id,
  861. readerList : [],
  862. authorList : [],
  863. docData : this.data
  864. };
  865. }else if( json.type === "process" ){
  866. this.work = {
  867. processFlag : json.data.process.id,
  868. data: this.data
  869. };
  870. }
  871. this.importer.fireEvent("beforeCreateRowData", [this]);
  872. json.data.columnList.each( function (columnJson, i) {
  873. if(!columnJson.path)return;
  874. var value = this.importedData[i] || "";
  875. if( !value )return;
  876. var data = this.parseData(value, (json.type === "dynamicTable" ? columnJson.dataType_Querytable : columnJson.dataType_CMSProcess), columnJson);
  877. if( !data && data !== 0 )return;
  878. if( json.type === "dynamicTable" ){
  879. this.data[ columnJson.path ] = data;
  880. }else{
  881. this.setDataWithPath(this.data, columnJson.path, data);
  882. }
  883. if( json.type === "cms" ){
  884. if( columnJson.isName ) {
  885. if (columnJson.isAuthor) {
  886. var array = this.parseCMSReadAndAuthor(data, "作者");
  887. this.document.authorList = this.document.authorList.concat(array)
  888. }
  889. if (columnJson.isReader) {
  890. var array = this.parseCMSReadAndAuthor(data, "阅读");
  891. this.document.readerList = this.document.readerList.concat(array)
  892. }
  893. }
  894. }
  895. }.bind(this));
  896. json.data.calculateFieldList.each( function (fieldJson, i) {
  897. if( fieldJson.valueScript ){
  898. var data = this.importer.Macro.exec( fieldJson.valueScript, this );
  899. if( o2.typeOf(data) === "null" )return;
  900. if( !fieldJson.path )return;
  901. if( json.type === "dynamicTable" ){
  902. this.data[ fieldJson.path ] = data;
  903. }else{
  904. this.setDataWithPath(this.data, fieldJson.path, data);
  905. }
  906. if( json.type === "cms" ){
  907. if( fieldJson.isAuthor ){
  908. var array = this.parseCMSReadAndAuthor( data, "作者" );
  909. this.document.authorList = this.document.authorList.concat( array )
  910. }
  911. if( fieldJson.isReader ){
  912. var array = this.parseCMSReadAndAuthor( data, "阅读" );
  913. this.document.readerList = this.document.readerList.concat( array )
  914. }
  915. }
  916. }
  917. }.bind(this));
  918. var array;
  919. if( json.type === "cms" ){
  920. this.document.docData = this.data;
  921. if( json.data.documentPublisher === "importer" ){
  922. array = layout.session.user.identityList;
  923. if( array && array.length ){
  924. this.document.identity = array[0].distinguishedName
  925. }
  926. }else{
  927. this.setDataWithField(this.document, "documentPublisherField", "identity", true);
  928. }
  929. if( json.data.documentPublishTime === "importer" ){
  930. this.document.publishTime = new Date().format("db");
  931. }else{
  932. this.setDataWithField(this.document, "documentPublisherTimeField", "publishTime", false);
  933. }
  934. this.setDataWithField(this.document, "documentTitleField", "title", false);
  935. if( !this.document.title )this.document.title = "无标题";
  936. this.setDataWithField(this.document, "documentSummaryField", "summary", false);
  937. }else if( json.type === "process" ){
  938. this.work.data = this.data;
  939. if( json.data.processDrafter === "importer" ){
  940. array = layout.session.user.identityList;
  941. if( array && array.length ){
  942. this.work.identity = array[0].distinguishedName
  943. }
  944. }else{
  945. this.setDataWithField(this.work, "processDrafterField", "identity", true);
  946. }
  947. this.setDataWithField(this.work, "processTitleField", "title", false);
  948. if( !this.work.title )this.work.title = "无标题";
  949. if( json.data.processStatus === "completed" ){
  950. this.work.form = json.data.processForm || "";
  951. this.setDataWithField(this.work, "processSerialField", "serial", false);
  952. this.setDataWithField(this.work, "processStartTimeField", "startTime", false);
  953. this.setDataWithField(this.work, "processCompleteTimeField", "completeTime", false);
  954. }
  955. }
  956. this.importer.fireEvent("afterCreateRowData", [ this]);
  957. },
  958. parseData: function(value, dataType, json){
  959. var data;
  960. var type = this.importer.json.type;
  961. switch ( dataType ) {
  962. case "string":
  963. case "stringList":
  964. if( json.isName ){
  965. var arr = this.stringToArray(value);
  966. if( type === "dynamicTable" ){
  967. data = arr
  968. }else{
  969. data = arr.map( function(d, idx){
  970. return this.importer.getOrgData( d, true, true ) || d;
  971. }.bind(this)).clean();
  972. }
  973. }else{
  974. if( dataType === "string" ){
  975. var linebreak = type === "dynamicTable" ? json.lineBreak_Querytable : json.lineBreak_CMSProcess;
  976. data = value.replace(/&#10;/g, linebreak || "" )
  977. }else{
  978. data = this.stringToArray(value);
  979. }
  980. }
  981. break;
  982. case "number":
  983. case "double":
  984. value = value.replace(/&#10;/g,"");
  985. data = parseFloat(value);
  986. break;
  987. case "integer":
  988. case "long":
  989. value = value.replace(/&#10;/g,"");
  990. data = parseInt( value );
  991. break;
  992. case "numberList":
  993. case "doubleList":
  994. data = this.stringToArray(value).map( function(d, idx){ return parseFloat( d ); }.bind(this)).clean();
  995. break;
  996. case "integerList":
  997. case "longList":
  998. data = this.stringToArray(value).map( function(d, idx){ return parseInt( d ); }.bind(this)).clean();
  999. break;
  1000. case "date":
  1001. value = value.replace(/&#10;/g,"");
  1002. data = Date.parse(value).format( "%Y-%m-%d" );
  1003. break;
  1004. case "dateTime":
  1005. value = value.replace(/&#10;/g,"");
  1006. data = Date.parse(value).format( "db" );
  1007. break;
  1008. case "dateList":
  1009. data = this.stringToArray(value).map( function(d, idx){ return Date.parse(d).format( "%Y-%m-%d" ); }.bind(this)).clean();
  1010. break;
  1011. case "dateTimeList":
  1012. data = this.stringToArray(value).map( function(d, idx){ return Date.parse(d).format( "db" ); }.bind(this)).clean();
  1013. break;
  1014. case "boolean":
  1015. value = value.replace(/&#10;/g,"");
  1016. data = value.trim().toLowerCase() !== "false";
  1017. break;
  1018. case "booleanList":
  1019. data = this.stringToArray(value).map( function(d, idx){ return value.trim().toLowerCase() !== "false"; }.bind(this)).clean();
  1020. break;
  1021. case "json":
  1022. case "stringMap":
  1023. value = value.replace(/&#10;/g,"");
  1024. data = JSON.parse(value);
  1025. break;
  1026. default:
  1027. data = value.replace(/&#10;/g,"");
  1028. break;
  1029. }
  1030. return data;
  1031. },
  1032. stringToArray: function(string){
  1033. return string.replace(/[\n\r]/g,",").replace(/&#10;/g,",").split(/\s*,\s*/g ).filter(function(s){
  1034. return !!s;
  1035. });
  1036. },
  1037. setDataWithPath: function(obj, path, data){
  1038. var names = path.split(".");
  1039. var d = obj;
  1040. Array.each(names, function (n, idx) {
  1041. if( idx === names.length -1 )return;
  1042. if ( !d[n] ){
  1043. var value = this.isNumberString( names[idx+1] ) ? [] : {};
  1044. var n1 = this.isNumberString( n ) ? n.toInt() : n;
  1045. d[n1] = value;
  1046. d = d[n1];
  1047. }else{
  1048. d = d[n];
  1049. }
  1050. }.bind(this));
  1051. d[names[names.length -1]] = data;
  1052. },
  1053. setDataWithField: function(obj, fieldName, path, isName){
  1054. if(!this.pathIndexMap)this.pathIndexMap = {};
  1055. if( !path )return;
  1056. var json = this.importer.json;
  1057. if( json.data[fieldName] ){
  1058. var f = json.data[fieldName];
  1059. json.data.columnList.each(function(json, i){
  1060. if(json.path === f)this.pathIndexMap[path] = i;
  1061. }.bind(this));
  1062. var d = this.data;
  1063. Array.each( f.split("."), function (n) {
  1064. if(this.isNumberString(n))n = n.toInt();
  1065. if (d) d = d[n];
  1066. }.bind(this));
  1067. if(!d)return;
  1068. var _d = (typeOf(d) === "array" && d.length) ? d[0] : d;
  1069. if( _d ){
  1070. if(isName){
  1071. obj[path] = typeOf(_d) === "object" ? _d.distinguishedName : _d;
  1072. }else{
  1073. obj[path] = d;
  1074. }
  1075. }
  1076. }
  1077. },
  1078. isNumberString: function(string){
  1079. return string.toInt().toString() === string;
  1080. },
  1081. parseCMSReadAndAuthor : function( data, t ){
  1082. var cnArray = ["组织","群组","人员","人员","角色"];
  1083. var keyArray = ["U","G","I","P","R"];
  1084. if( typeOf(data) !== "array" )data = [data];
  1085. return data.map( function( d ){
  1086. var dn = typeOf( d ) === "string" ? d : d.distinguishedName;
  1087. var name;
  1088. if( typeOf(d) === "object" && d.name ){
  1089. name = d.name;
  1090. }else if( MWF.name && MWF.name.cn ){
  1091. name = MWF.name.cn( dn );
  1092. }else{
  1093. name = dn.split("@")[0];
  1094. }
  1095. var index = keyArray.indexOf(dn.substr(dn.length-1, 1));
  1096. if( index > -1 ){
  1097. return {
  1098. "permission" : t,
  1099. "permissionObjectType": cnArray[ index ],
  1100. "permissionObjectName": name,
  1101. "permissionObjectCode": dn
  1102. }
  1103. }
  1104. }).clean()
  1105. },
  1106. getSrcData: function(){
  1107. var srcData = {};
  1108. this.importer.json.data.columnList.each( function (columnJson, i) {
  1109. if(columnJson.path)srcData[ columnJson.path ] = this.importedData[i] || "";
  1110. }.bind(this));
  1111. return srcData;
  1112. },
  1113. getResult: function(){
  1114. if( this.importer.json.type === "cms" ){
  1115. this.document.srcData = this.getSrcData(); //this.importedData;
  1116. return this.document;
  1117. }else if( this.importer.json.type === "process" ){
  1118. this.work.srcData = this.getSrcData(); //this.importedData;
  1119. return this.work;
  1120. }else if( this.importer.json.type === "dynamicTable" ){
  1121. this.data.srcData = this.getSrcData(); //this.importedData;
  1122. return this.data;
  1123. }
  1124. }
  1125. });
  1126. MWF.xDesktop.requireApp("Template", "utils.ExcelUtils", null, false);
  1127. MWF.xApplication.query.Query.Importer.ExcelUtils = new Class({
  1128. Extends: MWF.xApplication.Template.utils.ExcelUtils,
  1129. initialize: function(){
  1130. this.sheet2JsonOptions = {header:1};
  1131. }
  1132. });
  1133. MWF.xApplication.query.Query.Importer.ProgressBar = new Class({
  1134. Implements: [Options, Events],
  1135. Extends: o2.widget.Common,
  1136. options: {
  1137. zindex: null,
  1138. disableDetailButton: false
  1139. },
  1140. initialize : function( importer, options ){
  1141. this.setOptions(options);
  1142. this.importer = importer;
  1143. this.actions = this.importer.lookupAction;
  1144. this.lp = MWF.xApplication.query.Query.LP;
  1145. this.css = importer.css;
  1146. this.openDlg();
  1147. this.status = "ready";
  1148. },
  1149. openDlg: function () {
  1150. var _self = this;
  1151. this.contentNode = new Element("div",{"styles": this.css.processContentNode});
  1152. var opt = {
  1153. "style" : "user",
  1154. "title": this.lp.importRecordDetail,
  1155. "content": this.contentNode,
  1156. "offset": {"y": 0},
  1157. "isMax": false,
  1158. "width": 500,
  1159. "height": 200,
  1160. "zindex": this.options.zindex,
  1161. "buttonList": [
  1162. {
  1163. "type": "openImportRecordDetail",
  1164. "text": this.lp.openImportRecordDetail,
  1165. "action": function () { _self.openImportRecordDetail(); }
  1166. },
  1167. {
  1168. "type": "cancel",
  1169. "text": this.lp.close,
  1170. "action": function () { this.dlg.close(); }.bind(this)
  1171. }
  1172. ],
  1173. "onPostShow": function(){
  1174. this.fireEvent("postShow");
  1175. }.bind(this),
  1176. "onPostLoad": function () {
  1177. this.titleAction.hide();
  1178. this.button.hide();
  1179. },
  1180. "onPostClose": function(){
  1181. this.dlg = null;
  1182. }.bind(this)
  1183. };
  1184. if( this.options.disableDetailButton ){
  1185. opt.buttonList.splice(0, 1);
  1186. }
  1187. this.dlg = o2.DL.open(opt);
  1188. },
  1189. createNode: function( noProgress ){
  1190. // var lp = this.lp;
  1191. // this.maskNode = new Element("div",{"styles": this.css.maskNode}).inject(this.importer.container);
  1192. //
  1193. // this.node = new Element("div", {"styles": this.css.progressBarNode}).inject(this.importer.container);
  1194. //
  1195. // this.topNode = new Element("div",{"styles": this.css.progressTopNode}).inject(this.node);
  1196. //
  1197. // this.subjectNode = new Element("div",{"styles": this.css.progressSubjectNode}).inject(this.topNode);
  1198. //
  1199. // this.topCloseAction = new Element("div.topCloseAction", {
  1200. // "styles": this.css.progressTopCloseAction,
  1201. // "text": "x"
  1202. // }).inject(this.topNode);
  1203. // this.topCloseAction.hide();
  1204. // this.topCloseAction.addEvent("click", function(){
  1205. // this.close();
  1206. // }.bind(this))
  1207. //
  1208. // this.contentNode = new Element("div",{"styles": this.css.processContentNode}).inject(this.node);
  1209. //
  1210. // this.bottomNode = new Element("div", {"styles": this.css.progressBottomNode}).inject(this.node);
  1211. // this.bottomNode.hide();
  1212. //
  1213. // this.closeAction = new Element("div.closeAction", {
  1214. // "styles": this.css.progressCloseAction,
  1215. // "text": lp.close
  1216. // }).inject(this.bottomNode);
  1217. // this.closeAction.addEvent("click", function(){
  1218. // this.close();
  1219. // }.bind(this))
  1220. },
  1221. setContentHtml: function(noProgress){
  1222. var lp = this.lp;
  1223. var contentHTML = "";
  1224. if (noProgress){
  1225. // contentHTML = "<div style=\"height: 20px; line-height: 20px\">"+lp.readyToImportData1+"</div></div>" ;
  1226. contentHTML =
  1227. "<div style=\"overflow: hidden\">" +
  1228. " <div class='mwf_progressInforNode'>"+lp.readyToImportData1+"</div>"+
  1229. "</div>" ;
  1230. this.contentNode.set("html", contentHTML );
  1231. this.progressNode = null;
  1232. this.progressPercentNode = null;
  1233. this.progressInforNode = this.contentNode.getElement(".mwf_progressInforNode");
  1234. this.progressInforNode.setStyles(this.css.progressInforNode)
  1235. }else{
  1236. contentHTML =
  1237. "<div style=\"overflow: hidden\">"+
  1238. " <div class='mwf_progressNode'>" +
  1239. " <div class='mwf_progressPercentNode'></div>"+
  1240. " </div>" +
  1241. " <div class='mwf_progressInforNode'>"+lp.readyToImportData1+"</div>"+
  1242. "</div>" ;
  1243. this.contentNode.set("html", contentHTML );
  1244. this.progressNode = this.contentNode.getElement(".mwf_progressNode");
  1245. this.progressNode.setStyles(this.css.progressNode);
  1246. this.progressPercentNode = this.contentNode.getElement(".mwf_progressPercentNode");
  1247. this.progressPercentNode.setStyles(this.css.progressPercentNode);
  1248. this.progressInforNode = this.contentNode.getElement(".mwf_progressInforNode");
  1249. this.progressInforNode.setStyles(this.css.progressInforNode)
  1250. }
  1251. },
  1252. showCheckData : function(){
  1253. // this.node.show();
  1254. this.setContentHtml(true);
  1255. this.setMessageTitle( this.lp.checkDataTitle );
  1256. this.setMessageText( this.lp.checkDataContent );
  1257. this.status = "check";
  1258. // this.setSize();
  1259. },
  1260. showImporting: function( recordId, callback ){
  1261. // this.node.show();
  1262. this.setContentHtml();
  1263. this.recordId = recordId;
  1264. this.currentDate = new Date();
  1265. this.intervalId = window.setInterval( function(){
  1266. this.actions.getImportModelRecordStatus( this.recordId, function( json ){
  1267. var data = json.data;
  1268. this.status = data.status;
  1269. if( data.status === "待导入" ) { //有其他人正在导入或上次导入还未完成
  1270. this.setMessageTitle(this.lp.importWaitingTitle);
  1271. this.setMessageText(this.lp.importWaitingContent);
  1272. }else if( data.status === "导入中" ){ //导入中
  1273. this.setMessageTitle( this.lp.importDataTitle );
  1274. this.setMessageText( this.lp.importDataContent.replace( "{count}", data.executeCount));
  1275. this.updateProgress( data );
  1276. }else{ //已经结束, 状态有 "导入成功","部分成功","导入失败"
  1277. if(this.intervalId)window.clearInterval( this.intervalId );
  1278. this.transferComplete( data );
  1279. if( callback )callback( data );
  1280. }
  1281. }.bind(this), null)
  1282. }.bind(this), 500 );
  1283. // this.setSize();
  1284. },
  1285. showCloseAction: function(){
  1286. this.dlg.titleAction.show();
  1287. this.dlg.button.show();
  1288. // if( this.bottomNode )this.bottomNode.show();
  1289. // if( this.topCloseAction )this.topCloseAction.show();
  1290. // this.setSize();
  1291. },
  1292. close: function(){
  1293. // this.maskNode.destroy();
  1294. // this.node.destroy();
  1295. this.dlg.close();
  1296. },
  1297. // setSize: function(){
  1298. // var containerSize = this.importer.container.getSize();
  1299. // var nodeSize = this.node.getSize();
  1300. // var top = (containerSize.y - nodeSize.y) / 2;
  1301. // var left = (containerSize.x - nodeSize.x) / 2;
  1302. // this.node.setStyles({
  1303. // "top": (top-40)+"px",
  1304. // "left": left+"px"
  1305. // })
  1306. // },
  1307. updateProgress: function(data){
  1308. //status, data.executeCount, data.count, data.failCount
  1309. var lp = this.lp;
  1310. var total = data.count.toInt();
  1311. var processed = data.executeCount.toInt();
  1312. var failCount = data.failCount.toInt();
  1313. var percent = 100*(processed/total);
  1314. var sendDate = new Date();
  1315. var lastDate = this.lastTime || this.currentDate;
  1316. var ms = sendDate.getTime() - lastDate.getTime();
  1317. var speed = ( (processed - ( this.lastProcessed || 0 )) * 1000)/ms ;
  1318. var u = lp.importSpeed;
  1319. speed = speed.round(2);
  1320. this.progressPercentNode.setStyle("width", ""+percent+"%");
  1321. var text = lp.importingDataContent.replace("{speed}",speed).replace("{total}",total).replace("{remaining}",( total - processed ));
  1322. text += failCount ? lp.importingDataErrorContent.replace("{errorCount}",failCount) : "";
  1323. this.progressInforNode.set("text", text);
  1324. this.lastProcessed = processed;
  1325. this.lastTime = new Date();
  1326. },
  1327. transferComplete: function( data ){
  1328. var lp = this.lp;
  1329. var sendDate = new Date();
  1330. var ms = sendDate.getTime()-this.currentDate.getTime();
  1331. if(ms<1000)ms = 1000;
  1332. var timeStr = "";
  1333. if (ms>3600000){
  1334. var h = ms/3600000;
  1335. var m_s = ms % 3600000;
  1336. var m = m_s / 60000;
  1337. var s_s = m_s % 60000;
  1338. var s = s_s/1000;
  1339. timeStr = ""+h.toInt()+lp.hour+m.toInt()+lp.mintue+s.toInt()+lp.second;
  1340. }else if (ms>60000){
  1341. var m = ms / 60000;
  1342. var s_s = ms % 60000;
  1343. var s = s_s/1000;
  1344. timeStr = ""+m.toInt()+lp.mintue+s.toInt()+lp.second;
  1345. }else{
  1346. var s = ms/1000;
  1347. timeStr = ""+s.toInt()+lp.second;
  1348. }
  1349. if( data.status === "导入成功" ){
  1350. var size = data.count;
  1351. var speed = (size * 1000)/ms ;
  1352. var u = lp.importSpeed;
  1353. speed = speed.round(2);
  1354. this.setMessageTitle( lp.importSuccessTitle );
  1355. var text = lp.importSuccessContent.replace("{total}",size).replace("{speed}",speed).replace("{timeStr}",timeStr);
  1356. this.setMessageText( text );
  1357. }else if(data.status === "部分成功"){
  1358. var size = data.count;
  1359. var speed = (size * 1000)/ms ;
  1360. var u = lp.importSpeed;
  1361. speed = speed.round(2);
  1362. this.setMessageTitle( lp.importPartSuccessTitle );
  1363. var text = lp.importPartSuccessContent.replace("{total}",size).replace("{speed}",speed).replace("{errorCount}",data.failCount).replace("{timeStr}",timeStr);
  1364. this.setMessageText( text );
  1365. }else{ //导入失败
  1366. var size = data.count;
  1367. this.setMessageTitle( lp.importFailTitle );
  1368. var text = lp.importFailContent.replace("{errorInfo}",data.distribution || "").replace("{total}",size).replace("{timeStr}",timeStr);
  1369. this.setMessageText( text );
  1370. }
  1371. this.clearMessageProgress();
  1372. this.showCloseAction();
  1373. },
  1374. setMessageText: function( text){
  1375. this.progressInforNode.set("text", text);
  1376. },
  1377. setMessageTitle: function( text){
  1378. this.dlg.titleText.set("text", text);
  1379. },
  1380. clearMessageProgress: function(){
  1381. this.progressNode.destroy();
  1382. },
  1383. openImportRecordDetail: function () {
  1384. MWF.xDesktop.requireApp("query.Query", "ImporterRecord", function () {
  1385. var detail = new MWF.xApplication.query.Query.ImporterRecord.Detail(
  1386. this.importer.container,
  1387. this.importer.app,
  1388. { importerId: this.importerId, recordId: this.recordId }
  1389. );
  1390. detail.load();
  1391. this.dlg.close();
  1392. }.bind(this), false);
  1393. }
  1394. });