예제 #1
0
        private static void sync(string xmlFilePath, string ignoreSyncID)
        {
            XDocument xDoc;
            XElement newDataSetEl;
            DateTime lastSync;

            xDoc = XDocument.Load(xmlFilePath);
            newDataSetEl = xDoc.Element("NewDataSet");
            lastSync = GetLastUpdatedClient(ignoreSyncID);

            UserInfoTableAdapter userInfoTA = new UserInfoTableAdapter();
            TransactionHistoryTableAdapter transactionHistoryTA = new TransactionHistoryTableAdapter();

            userInfoTA.Connection.Open();
            transactionHistoryTA.Connection = userInfoTA.Connection;

            using (SqlTransaction transaction = userInfoTA.Connection.BeginTransaction())
            {
                userInfoTA.AttachTransaction(transaction);
                transactionHistoryTA.AttachTransaction(transaction);

                try
                {
                    IList<XElement> ElList = newDataSetEl.Descendants("UserInfo").ToList();

                    foreach (XElement userInfoEl in ElList)
                    {
                        // not null value parse
                        string username = userInfoEl.Element("Username").Value;

                        // nullable value parse
                        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);
                        }

                        DataTable userInfoDT = new UserInfoTableAdapter().GetDataByUsername(username);
                        if (userInfoDT.Rows.Count != 1)
                        {
                            throw new Exception("Comflic while Sync. The updated row doesn't exist.");
                        }

                        DataRow userInfoRow = userInfoDT.Rows[0];
                        DateTime? oriLastUpdateFingerPrint = userInfoRow.Field<DateTime?>("LastUpdatedFingerPrint");
                        if (oriLastUpdateFingerPrint == null || (lastUpdatedFingerPrint != null
                            && lastUpdatedFingerPrint > oriLastUpdateFingerPrint))
                        {
                            //userInfoTA.Update(userInfoRow.Field<string>("Name"), userInfoRow.Field<string>("TypeShortName")
                            //	, userInfoRow.Field<int>("AmountOfMoney"), userInfoRow.Field<DateTime>("LastUpdatedMoney"), fingerPrintIMG
                            //	, lastUpdatedFingerPrint, fingerPosition, userInfoRow.Field<bool>("IsCafeteriaStaff")
                            //	, userInfoRow.Field<bool>("IsActive"), userInfoRow.Field<DateTime>("InsertedDate"), userInfoRow.Field<string>("UpdatedBy")
                            //	, userInfoRow.Field<DateTime>("LastUpdated"), username);

                            userInfoTA.Update(userInfoRow.Field<string>("Name"), userInfoRow.Field<string>("TypeShortName")
                                , userInfoRow.Field<int>("AmountOfMoney"), userInfoRow.Field<DateTime>("LastUpdatedMoney"), fingerPrintIMG
                                , lastUpdatedFingerPrint, fingerPosition, userInfoRow.Field<bool>("IsCafeteriaStaff")
                                , true, userInfoRow.Field<DateTime>("InsertedDate"), userInfoRow.Field<string>("UpdatedBy")
                                , userInfoRow.Field<DateTime>("LastUpdated"), username);

                            //SaveUserInfoXml(userInfoRow.Field<string>("Username"), userInfoRow.Field<string>("Name"), userInfoRow.Field<string>("TypeShortName")
                            //	, userInfoRow.Field<int>("AmountOfMoney"), userInfoRow.Field<DateTime>("LastUpdatedMoney"), fingerPrintIMG
                            //	, lastUpdatedFingerPrint, fingerPosition, userInfoRow.Field<bool>("IsCafeteriaStaff")
                            //	, userInfoRow.Field<bool>("IsActive"), userInfoRow.Field<DateTime>("InsertedDate"), userInfoRow.Field<string>("UpdatedBy")
                            //	, userInfoRow.Field<DateTime>("LastUpdated"), ignoreSyncID);

                            SaveUserInfoXml(userInfoRow.Field<string>("Username"), userInfoRow.Field<string>("Name"), userInfoRow.Field<string>("TypeShortName")
                                , userInfoRow.Field<int>("AmountOfMoney"), userInfoRow.Field<DateTime>("LastUpdatedMoney"), fingerPrintIMG
                                , lastUpdatedFingerPrint, fingerPosition, userInfoRow.Field<bool>("IsCafeteriaStaff")
                                , true, userInfoRow.Field<DateTime>("InsertedDate"), userInfoRow.Field<string>("UpdatedBy")
                                , userInfoRow.Field<DateTime>("LastUpdated"), ignoreSyncID);
                        }
                    }

                    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)
                        {
                            if (scheduleMealSetDetailID != null)
                            {
                                int? check = new ScheduleMealSetDetailTableAdapter().CheckID(scheduleMealSetDetailID.Value);

                                if (check == null)
                                {
                                    scheduleMealSetDetailID = null;
                                }
                            }

                            transactionHistoryTA.Insert(username, transactionTypeID, value, transactionContent, scheduleMealSetDetailID,
                                isAuto, insertedDate, updatedBy, lastUpdated);

                            SaveTransactionHistoryXml(transactionHistoryID, username, transactionTypeID, value, transactionContent
                                , scheduleMealSetDetailID, isAuto, insertedDate, updatedBy, lastUpdated, ignoreSyncID);
                        }
                    }

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }