예제 #1
0
        private static void Sync(string xmlPath)
        {
            XDocument xDoc;
            XElement newDataSetEl;
            DateTime lastSync;
            try
            {
                xDoc = XDocument.Load(xmlPath);
                newDataSetEl = xDoc.Element("NewDataSet");

                DateTime? lastSync_ = GetLastSync();
                if (lastSync_ == null)
                {
                    lastSync = new DateTime();
                }
                else
                {
                    lastSync = lastSync_.Value;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            UserTypeTableAdapter userTypeTA = new UserTypeTableAdapter();
            UserInfoTableAdapter userInfoTA = new UserInfoTableAdapter();
            MealSetTableAdapter mealSetTA = new MealSetTableAdapter();
            ServingTimeTableAdapter servingTimeTA = new ServingTimeTableAdapter();
            ScheduleTableAdapter scheduleTA = new ScheduleTableAdapter();
            ScheduleMealSetDetailTableAdapter scheduleMealSetDetailTA = new ScheduleMealSetDetailTableAdapter();
            TransactionTypeTableAdapter transactionTypeTA = new TransactionTypeTableAdapter();
            TransactionHistoryTableAdapter transactionHistoryTA = new TransactionHistoryTableAdapter();

            userTypeTA.Connection.Open();
            userInfoTA.Connection = userTypeTA.Connection;
            mealSetTA.Connection = userTypeTA.Connection;
            servingTimeTA.Connection = userTypeTA.Connection;
            scheduleTA.Connection = userTypeTA.Connection;
            scheduleMealSetDetailTA.Connection = userTypeTA.Connection;
            transactionTypeTA.Connection = userTypeTA.Connection;
            transactionHistoryTA.Connection = userTypeTA.Connection;

            using (SqlTransaction transaction = userTypeTA.Connection.BeginTransaction())
            {
                userTypeTA.AttachTransaction(transaction);
                userInfoTA.AttachTransaction(transaction);
                mealSetTA.AttachTransaction(transaction);
                servingTimeTA.AttachTransaction(transaction);
                scheduleTA.AttachTransaction(transaction);
                scheduleMealSetDetailTA.AttachTransaction(transaction);
                transactionTypeTA.AttachTransaction(transaction);
                transactionHistoryTA.AttachTransaction(transaction);

                try
                {
                    // sync usertype table
                    // NOTE: table UserType in subDB only get value from db in web app.
                    // so in the desktop app we only insert and update
                    IList<XElement> ElList = newDataSetEl.Descendants("UserType").ToList();

                    foreach (XElement userTypeEl in ElList)
                    {
                        // not null value parse
                        string typeShortName = userTypeEl.Element("TypeShortName").Value;
                        string typeName = userTypeEl.Element("TypeName").Value;
                        int mealValue = int.Parse(userTypeEl.Element("MealValue").Value);
                        bool canEatMore = bool.Parse(userTypeEl.Element("CanEatMore").Value);
                        DateTime insertedDate = DateTime.Parse(userTypeEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(userTypeEl.Element("LastUpdated").Value);
                        bool canDebt = bool.Parse(userTypeEl.Element("CanDebt").Value);

                        // nullable value parse
                        int? moreMealValue;
                        if (userTypeEl.Element("MoreMealValue") == null)
                        {
                            moreMealValue = null;
                        }
                        else
                        {
                            moreMealValue = int.Parse(userTypeEl.Element("MoreMealValue").Value.ToString());
                        }

                        string description;
                        if (userTypeEl.Element("Description") == null)
                        {
                            description = null;
                        }
                        else
                        {
                            description = userTypeEl.Element("Description").Value;
                        }

                        string updatedBy;
                        if (userTypeEl.Element("UpdatedBy") == null)
                        {
                            updatedBy = null;
                        }
                        else
                        {
                            updatedBy = userTypeEl.Element("UpdatedBy").Value;
                        }

                        if (insertedDate > lastSync)
                        {
                            if ((int)userTypeTA.CheckDuplicateKey(typeShortName) == 0)
                            {
                                userTypeTA.Insert(typeShortName, typeName, mealValue, moreMealValue, description, canDebt, canEatMore, insertedDate, updatedBy, lastUpdated);
                                continue;
                            }
                        }

                        userTypeTA.Update(typeName, mealValue, moreMealValue, description, canDebt, canEatMore, insertedDate, updatedBy, lastUpdated, typeShortName);
                    }

                    // sync userinfo table
                    ElList = newDataSetEl.Descendants("UserInfo").ToList();

                    foreach (XElement userInfoEl in ElList)
                    {
                        // not null value parse
                        string username = userInfoEl.Element("Username").Value;
                        int amountOfMoney = int.Parse(userInfoEl.Element("AmountOfMoney").Value);
                        DateTime lastUpdatedMoney = DateTime.Parse(userInfoEl.Element("LastUpdatedMoney").Value);
                        bool isCafeteriaStaff = bool.Parse(userInfoEl.Element("IsCafeteriaStaff").Value);
                        bool isActive = bool.Parse(userInfoEl.Element("IsActive").Value);
                        DateTime insertedDate = DateTime.Parse(userInfoEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(userInfoEl.Element("LastUpdated").Value);

                        // nullable value parse
                        string name = userInfoEl.TryGetElementValue("Name");
                        string typeShortName = userInfoEl.TryGetElementValue("TypeShortName");
                        string updatedBy = userInfoEl.TryGetElementValue("UpdatedBy");

                        byte[] fingerPrintIMG;
                        if (userInfoEl.Element("FingerPrintIMG") == null)
                        {
                            fingerPrintIMG = null;
                        }
                        else
                        {
                            fingerPrintIMG = Convert.FromBase64String(userInfoEl.Element("FingerPrintIMG").Value);
                        }

                        DateTime? lastUpdatedFingerPrint;
                        if (userInfoEl.Element("LastUpdatedFingerPrint") == null)
                        {
                            lastUpdatedFingerPrint = null;
                        }
                        else
                        {
                            lastUpdatedFingerPrint = DateTime.Parse(userInfoEl.Element("LastUpdatedFingerPrint").Value);
                        }

                        int? fingerPosition;
                        if (userInfoEl.Element("FingerPosition") == null)
                        {
                            fingerPosition = null;
                        }
                        else
                        {
                            fingerPosition = int.Parse(userInfoEl.Element("FingerPosition").Value);
                        }

                        if (insertedDate > lastSync)
                        {
                            if ((int)userInfoTA.CheckDuplicateKey(username) == 0)
                            {
                                userInfoTA.Insert(username, name, typeShortName, amountOfMoney, lastUpdatedMoney, fingerPrintIMG
                                    , lastUpdatedFingerPrint, fingerPosition, isCafeteriaStaff, isActive, insertedDate
                                    , updatedBy, lastUpdated);
                                continue;
                            }
                        }

                        DataTable userInfoDT = userInfoTA.GetUserInfo(username);
                        if (userInfoDT.Rows.Count != 1)
                        {
                            throw new Exception("Comflic while Sync. The updated row doesn't exist.");
                        }
                        DataRow userInfoRow = userInfoDT.Rows[0];
                        DateTime oriLastUpdatedMoney = (DateTime)userInfoRow["LastUpdatedMoney"];
                        DateTime? oriLastUpdateFingerPrint = userInfoRow.Field<DateTime?>("LastUpdatedFingerPrint");

                        userInfoTA.UpdateGeneric(name, typeShortName, isCafeteriaStaff, isActive, insertedDate, updatedBy, lastUpdated, username);
                        if (oriLastUpdatedMoney > lastSync)
                        {
                            userInfoTA.UpdateMoney(amountOfMoney, lastUpdatedMoney, username);
                        }

                        if (oriLastUpdateFingerPrint == null || oriLastUpdateFingerPrint.Value > lastSync)
                        {
                            userInfoTA.UpdateFingerPrint(fingerPrintIMG, lastUpdatedFingerPrint, fingerPosition, username);
                        }
                    }

                    //sync mealset table
                    // NOTE: table MealSet in subDB only get value from db in web app.
                    // so in the desktop app we only insert and update
                    ElList = newDataSetEl.Descendants("MealSet").ToList();

                    foreach (XElement mealSetEl in ElList)
                    {
                        // not null value parse
                        int mealSetID = int.Parse(mealSetEl.Element("MealSetID").Value);
                        string name = mealSetEl.Element("Name").Value;
                        bool canEatMore = bool.Parse(mealSetEl.Element("CanEatMore").Value);
                        DateTime insertedDate = DateTime.Parse(mealSetEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(mealSetEl.Element("LastUpdated").Value);

                        // nullable value parse
                        string updatedBy = mealSetEl.TryGetElementValue("UpdatedBy");

                        if (insertedDate > lastSync)
                        {
                            mealSetTA.InsertWithID(mealSetID, name, canEatMore, insertedDate, updatedBy, lastUpdated);
                            continue;
                        }

                        mealSetTA.Update(name, canEatMore, insertedDate, updatedBy, lastUpdated, mealSetID);
                    }

                    // sync serving time table
                    // NOTE: table serving time in subDB only get value from db in web app.
                    // so in the desktop app we only insert and update
                    ElList = newDataSetEl.Descendants("ServingTime").ToList();

                    foreach (XElement servingTimeEl in ElList)
                    {
                        // not null value parse
                        int servingTimeID = int.Parse(servingTimeEl.Element("ServingTimeID").Value);
                        string name = servingTimeEl.Element("Name").Value;
                        string a = servingTimeEl.Element("StartTime").Value;
                        TimeSpan startTime = XmlConvert.ToTimeSpan(servingTimeEl.Element("StartTime").Value);
                        TimeSpan endTime = XmlConvert.ToTimeSpan(servingTimeEl.Element("EndTime").Value);
                        DateTime insertedDate = DateTime.Parse(servingTimeEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(servingTimeEl.Element("LastUpdated").Value);

                        // nullable value parse
                        if (insertedDate > lastSync)
                        {
                            servingTimeTA.InsertWithID(servingTimeID, name, startTime, endTime, insertedDate, lastUpdated);
                            continue;
                        }

                        servingTimeTA.Update(name, startTime, endTime, insertedDate, lastUpdated, servingTimeID);
                    }

                    // sync schedule table
                    ElList = newDataSetEl.Descendants("Schedule").ToList();

                    foreach (XElement scheduleEl in ElList)
                    {
                        // not null value parse
                        int scheduleID = int.Parse(scheduleEl.Element("ScheduleID").Value);
                        DateTime date = DateTime.Parse(scheduleEl.Element("Date").Value);
                        int servingTimeID = int.Parse(scheduleEl.Element("ServingTimeID").Value);
                        bool isDayOn = bool.Parse(scheduleEl.Element("IsDayOn").Value);
                        DateTime insertedDate = DateTime.Parse(scheduleEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(scheduleEl.Element("LastUpdated").Value);

                        // nullable value parse
                        string updatedBy = scheduleEl.TryGetElementValue("UpdatedBy");

                        if (insertedDate > lastSync)
                        {
                            scheduleTA.InsertWithID(scheduleID, servingTimeID, date, isDayOn, insertedDate, updatedBy, lastUpdated);
                            continue;
                        }

                        scheduleTA.Update(date, servingTimeID, isDayOn, insertedDate, updatedBy, lastUpdated, scheduleID);
                    }

                    // sync schedule meal set detail table
                    ElList = newDataSetEl.Descendants("ScheduleMealSetDetail").ToList();

                    foreach (XElement scheduleMealSetDetailEl in ElList)
                    {
                        // not null value parse
                        int scheduleMealSetDetailID = int.Parse(scheduleMealSetDetailEl.Element("ScheduleMealSetDetailID").Value);
                        int mealSetID = int.Parse(scheduleMealSetDetailEl.Element("MealSetID").Value);
                        int scheduleID = int.Parse(scheduleMealSetDetailEl.Element("ScheduleID").Value);
                        DateTime insertedDate = DateTime.Parse(scheduleMealSetDetailEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(scheduleMealSetDetailEl.Element("LastUpdated").Value);

                        if (insertedDate > lastSync)
                        {
                            scheduleMealSetDetailTA.InsertWithID(scheduleMealSetDetailID, mealSetID, scheduleID, insertedDate, lastUpdated);
                            continue;
                        }

                        scheduleMealSetDetailTA.Update(mealSetID, scheduleID, insertedDate, lastUpdated, scheduleMealSetDetailID);
                    }

                    // sync transaction type table
                    ElList = newDataSetEl.Descendants("TransactionType").ToList();

                    foreach (XElement transactionTypeEl in ElList)
                    {
                        // not null value parse
                        int transactionTypeID = int.Parse(transactionTypeEl.Element("TransactionTypeID").Value);
                        string name = transactionTypeEl.Element("Name").Value;
                        DateTime insertedDate = DateTime.Parse(transactionTypeEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(transactionTypeEl.Element("LastUpdated").Value);

                        if (insertedDate > lastSync)
                        {
                            transactionTypeTA.InsertWithID(transactionTypeID, name, insertedDate, lastUpdated);
                            continue;
                        }

                        transactionTypeTA.Update(name, insertedDate, lastUpdated, transactionTypeID);
                    }

                    // sync transaction history table
                    ElList = newDataSetEl.Descendants("TransactionHistory").ToList();

                    foreach (XElement transactionHistoryEl in ElList)
                    {
                        // not null value parse
                        int transactionHistoryID = int.Parse(transactionHistoryEl.Element("TransactionHistoryID").Value);
                        string username = transactionHistoryEl.Element("Username").Value;
                        int transactionTypeID = int.Parse(transactionHistoryEl.Element("TransactionTypeID").Value);
                        int value = int.Parse(transactionHistoryEl.Element("Value").Value);
                        string transactionContent = transactionHistoryEl.Element("TransactionContent").Value;
                        bool isAuto = bool.Parse(transactionHistoryEl.Element("IsAuto").Value);
                        DateTime insertedDate = DateTime.Parse(transactionHistoryEl.Element("InsertedDate").Value);
                        DateTime lastUpdated = DateTime.Parse(transactionHistoryEl.Element("LastUpdated").Value);

                        // nullable value parse
                        int? scheduleMealSetDetailID;
                        if (transactionHistoryEl.Element("ScheduleMealSetDetailID") == null)
                        {
                            scheduleMealSetDetailID = null;
                        }
                        else
                        {
                            scheduleMealSetDetailID = int.Parse(transactionHistoryEl.Element("ScheduleMealSetDetailID").Value);
                        }

                        string updatedBy = transactionHistoryEl.TryGetElementValue("UpdatedBy");

                        if (insertedDate > lastSync)
                        {
                            transactionHistoryTA.Insert(username, transactionTypeID, value, transactionContent
                                , scheduleMealSetDetailID, isAuto, insertedDate, updatedBy, lastUpdated);
                            continue;
                        }

                        transactionHistoryTA.Update(username, transactionTypeID, value, transactionContent, scheduleMealSetDetailID
                            , isAuto, insertedDate, updatedBy, lastUpdated, transactionHistoryID);
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }
예제 #2
0
        private void btnUpdateFingerPrint_Click(object sender, EventArgs e)
        {
            if (dataGridView.SelectedRows.Count != 1)
            {
                MessageBox.Show("Xin hãy chọn người dùng trước.");
                return;
            }

            if (bwSend.IsBusy)
            {
                MessageBox.Show("Xin hãy đợi hệ thống hoàn thành việc gửi ảnh.");
                return;
            }

            if (_scannerPortNumber < 0)
            {
                using (var form = new SelectScanner())
                {
                    var result = form.ShowDialog();
                    _scannerPortNumber = form.scannerPort;
                    _scannerAddr = form.scannerAddr;
                }

                if (_scannerPortNumber < 0 || _scannerAddr == 0)
                {
                    MessageBox.Show("Không thể kết nối tới máy quét.");
                    return;
                }

                btnStopDevice.Enabled = true;
            }

            string username = dataGridView.SelectedRows[0].Cells["Username"].Value.ToString();
            string imageFile;
            int? fingerPosition;
            DateTime? lastUpdatedFingerPrint;

            using (var form = new UpdateFingerPrint(_scannerAddr, username))
            {
                var result = form.ShowDialog();
                imageFile = form.fingerImageFile;
                fingerPosition = form.fingerPostition;
            }

            if (imageFile != null)
            {
                Image savedImage = Image.FromFile(imageFile);
                ImageConverter converter = new ImageConverter();
                byte[] fingerImage = (byte[])converter.ConvertTo(savedImage, typeof(byte[]));

                lastUpdatedFingerPrint = DateTime.Now;

                DataRow userInfoRow = null;
                foreach (DataRow row in customerDT.Rows)
                {
                    if (row["Username"].ToString() == username)
                    {
                        row["FingerPrintIMG"] = fingerImage;
                        row["FingerPosition"] = fingerPosition;
                        row["LastUpdatedFingerPrint"] = lastUpdatedFingerPrint;
                        row["IsActive"] = true;

                        userInfoRow = row;
                        break;
                    }
                }

                dataGridView.DataSource = customerDT;

                UserInfoTableAdapter userInfoTA = new UserInfoTableAdapter();
                userInfoTA.UpdateFingerPrint(fingerImage, lastUpdatedFingerPrint, fingerPosition, username);

                if (cbSendToServer.Checked)
                {
                    lblSendReport.Text = "Đang gửi hình ảnh lên server.";

                    string paramUsername = username;
                    byte[] paramFingerImage = fingerImage;
                    int? paramFingerPosition = fingerPosition;
                    DateTime paramLastUpdatedFingerPrint = lastUpdatedFingerPrint.Value;
                    DataRow paramUserInfoRow = userInfoRow;

                    object[] parameters = new object[] { paramUsername, paramFingerImage, paramFingerPosition
                        , paramLastUpdatedFingerPrint, paramUserInfoRow};

                    bwSend.RunWorkerAsync(parameters);
                }
                else
                {
                    XmlSync.SaveUserInfoXml(userInfoRow.Field<string>("Username"), userInfoRow.Field<string>("Name"), userInfoRow.Field<string>("TypeShortName")
                    , userInfoRow.Field<int>("AmountOfMoney"), userInfoRow.Field<DateTime>("LastUpdatedMoney"), fingerImage, lastUpdatedFingerPrint
                    , fingerPosition, userInfoRow.Field<bool>("IsCafeteriaStaff"), userInfoRow.Field<bool>("IsActive"), userInfoRow.Field<DateTime>("InsertedDate")
                    , userInfoRow.Field<string>("UpdatedBy"), userInfoRow.Field<DateTime>("LastUpdated"));
                }

                Home.LoadCustomerData();

                int? fingerPrintCount_ = (int?)(new UserInfoTableAdapter().GetUsingFingerPrintCout());
                if (fingerPrintCount_ == null)
                {
                    fingerPrintCount_ = 0;
                }
                Home.fingerPrintCount = fingerPrintCount_.ToString();
                MainForm.homeForm.ReloadClientInfo();
            }
        }