void doneAsync() { lock (environment) { ISettings oldSysSettings = environment.getSysSettings(); try { // bool localDebugImport = false; #if DEBUG localDebugImport = false; #endif log.set(MessageCollection.T_MSG_OPERATION_STARTING); // ToolMobile.setRuntimeMsg("Data import starting"); if (!localDebugImport) { checkDataSource(); } // string fileWorkDir = Path.Combine(ToolMobile.curDir(), "data"); ToolMobile.createDir(fileWorkDir); string fileInputZip = Path.Combine(fileWorkDir, "fromava.zip"); string fileInputXml = Path.Combine(fileWorkDir, "fromava.xml"); // bool state = false; //Source if (localDebugImport)//CurrentVersion.ENV.isLocalExim() { //!!// } else { { // log.set(MessageCollection.T_MSG_DATA_RECEIVING); // AgentData ad = new AgentData(environment); ServerResult sr; ad.chackOperationResult(sr = ad.resiveData()); byte[] data = sr.data; // log.set(MessageCollection.T_MSG_DATA_WRITING); // ToolMobile.writeFileData(fileInputZip, data); ToolZip.decompress(fileInputZip, Path.GetDirectoryName(fileInputZip)); } } //Get log.set(MessageCollection.T_MSG_DATA_READING); // XmlDocument doc = new XmlDocument(); doc.Load(fileInputXml); // if (isSameData(doc, oldSysSettings)) { throw new Exception(MessageCollection.T_MSG_DATA_OLD); } // var commited = false; try { environment.beginBatch(); // #region delete firm prm log.set(MessageCollection.T_MSG_DATA_DELETING); // { string[] arrTables = new string[] { TableFIRMPARAMS.TABLE_FULL_NAME }; string[] arrDesc = new string[] { environment.translate(WordCollection.T_ADDITIONAL) }; deleteTables(arrTables, arrDesc); } // // #endregion // #region import firm prm // log.set(MessageCollection.T_MSG_DATA_IMPORT); // { AdapterUserRecords[] arrAdapters = new AdapterUserRecords[] { new AdapterUserFirmParams(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetFirmParams(environment))) }; string[] arrDesc = new string[] { environment.translate(WordCollection.T_ADDITIONAL) }; importRecords(doc, arrAdapters, arrDesc); } #endregion // #region sys settings environment.setSysSettings(getSysSettings()); //environment.getSysSettings().set(SettingsSysMob.MOB_SYS_LAST_IMPORT, DateTime.Now); #endregion // #region delete rec log.set(MessageCollection.T_MSG_DATA_DELETING); // { string[] arrTables = new string[] { TableCLCARD.TABLE_FULL_NAME, TableITEMS.TABLE_FULL_NAME, TableWHOUSE.TABLE_FULL_NAME, TableINFOFIRM.TABLE_FULL_NAME, TableINFOPERIOD.TABLE_FULL_NAME, TableINFODOCSAVE.TABLE_FULL_NAME }; string[] arrDesc = new string[] { environment.translate(WordCollection.T_CLIENT), environment.translate(WordCollection.T_MATERIAL), environment.translate(WordCollection.T_WAREHOUSE), environment.translate(WordCollection.T_INFO), environment.translate(WordCollection.T_INFO), environment.translate(WordCollection.T_INFO) }; deleteTables(arrTables, arrDesc); } // // #endregion // #region docs log.set(MessageCollection.T_MSG_DOCS_DELETING); // DateTime limDate = environment.getSysSettings().getDateTime(SettingsSysMob.MOB_SYS_CMD_DELETE_DOCS_BEFORE, new DateTime(DateTime.Now.Year, 1, 1)); { string[][] arrTablesGroup = new string[][] { new string[] { TableINVOICE.TABLE_FULL_NAME, TableSTLINE.TABLE_LONG }, new string[] { TableORFICHE.TABLE_FULL_NAME, TableORFLINE.TABLE_LONG }, new string[] { TableKSLINES.TABLE_FULL_NAME } }; string[] arrDesc = new string[] { environment.translate(WordCollection.T_DOC_STOCK_TRANS), environment.translate(WordCollection.T_DOC_STOCK_ORDERS), environment.translate(WordCollection.T_DOC_FINANCE) }; deleteDocTables(arrTablesGroup, arrDesc, limDate); } #endregion // #region import // log.set(MessageCollection.T_MSG_DATA_IMPORT); // { AdapterUserRecords[] arrAdapters = new AdapterUserRecords[] { new AdapterUserClient(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetClient(environment))), new AdapterUserMaterial(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetMaterial(environment))), new AdapterUserWarehouse(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetWarehouse(environment))), new AdapterUserInfoFirm(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetInfoFirm(environment))), new AdapterUserInfoPeriod(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetInfoPeriod(environment))), new AdapterUserInfoDocSave(environment, new ImplAdapterDataSetStub(environment, new AdapterDataSetInfoDocSave(environment))) }; string[] arrDesc = new string[] { environment.translate(WordCollection.T_CLIENT), environment.translate(WordCollection.T_MATERIAL), environment.translate(WordCollection.T_WAREHOUSE), environment.translate(WordCollection.T_INFO), environment.translate(WordCollection.T_INFO), environment.translate(WordCollection.T_INFO) }; importRecords(doc, arrAdapters, arrDesc); } #endregion //clear // environment.clearStateData(); // //ToolMobile.setRuntimeMsg("Data importded, commit started"); environment.commitBatch(); // state = true; commited = true; // ToolMobile.setRuntimeMsg("Data importded, commited"); #region files { try { var files = doc.SelectSingleNode("DATA/ITEM[@ei_code='ADP_FILES']"); if (files != null) { foreach (var file in files) { var el = file as XmlElement; if (el != null) { var dir = ToolXml.getAttribValue(el, "dir", null); var name = ToolXml.getAttribValue(el, "name", null); var value_ = ToolXml.getAttribValue(el, "value", null); if (dir != null && name != null && value_ != null) { if (Path.IsPathRooted(dir)) { log.error(WordCollection.T_FILE + ": Path rooted: " + dir); } else { if (!ToolMobile.existsDir(dir)) { ToolMobile.createDir(dir); } var path = Path.Combine(dir, name); log.set(WordCollection.T_FILE + ": " + path); var data = Convert.FromBase64String(value_); ToolMobile.writeFileData(path, data); } } } } } } catch (Exception exc) { ToolMobile.setExceptionInner(exc); log.error("Files import error"); ToolMobile.setException(exc); } } #endregion } catch (Exception exc) { try { ToolMobile.setExceptionInner(exc); } catch { } try { if (!commited) { ToolMobile.setRuntimeMsg("Data import error, rollback started"); environment.rollbackBatch(); ToolMobile.setRuntimeMsg("Data import error, rollback ended"); } } catch { } environment.setSysSettings(oldSysSettings); throw new Exception(exc.Message, exc); } //if (commited) //{ // var dir = "cache"; // if (ToolMobile.existsDir(dir)) // ToolMobile.deleteDir(dir); //} } catch (Exception exc) { ToolMobile.setExceptionInner(exc); var err = ToolException.unwrap(exc); ToolMobile.setRuntimeMsg(err); ToolMobile.getContextLast().RunOnUiThread(() => { try { Android.Widget.Toast.MakeText(Android.App.Application.Context, err, Android.Widget.ToastLength.Long).Show(); } catch { } }); // ActivityExt.errMessageOnResume = err; log.error(ToolException.unwrap(exc)); environment.getExceptionHandler().setException(exc); } finally { log.set(MessageCollection.T_MSG_OPERATION_FINISHING); Thread.Sleep(1000); log.hide(); } } }