Exemplo n.º 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;
                }
            }
        }
Exemplo n.º 2
0
        internal static string NewSync()
        {
            Log.ActivityLog("-----------------------New sync transaction-----------------------");
            Log.ActivityLog("Clear all table.");

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

            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
                {
                    transactionHistoryTA.ClearTable();
                    transactionTypeTA.ClearTable();
                    scheduleMealSetDetailTA.ClearTable();
                    scheduleTA.ClearTable();
                    servingTimeTA.ClearTable();
                    mealSetTA.ClearTable();
                    userInfoTA.ClearTable();
                    userTypeTA.ClearTable();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }

                transaction.Commit();
            }

            Log.ActivityLog("Clear all completed.");
            Log.ActivityLog("Register sync to server.");

            ServiceReference.WebServiceSoapClient soapClient = new ServiceReference.WebServiceSoapClient();

            DateTime syncDate = DateTime.Now;
            string oldSyncID = XmlSync.GetSyncID();
            string newSyncID = soapClient.NewSyncData(WebServiceAuth.AuthSoapHeader(), syncDate, oldSyncID);
            XmlSync.SaveNewSync(newSyncID);

            Log.ActivityLog("Register completed.");
            Log.ActivityLog("Geting xml data.");

            string fileNameList = XmlSync.RequestXmlFileName(newSyncID, true);
            IList<string> xmlPathList = XmlSync.SaveXmlFile(fileNameList);

            Log.ActivityLog("Save xml data completed.");
            Log.ActivityLog("Start sync.");

            XmlSync.Sync(xmlPathList);

            Log.ActivityLog("Sync completed.");
            Log.ActivityLog("Update last sync date.");

            XmlSync.SetLastSync(syncDate);
            soapClient.SetLastSyncAndInactiveFile(WebServiceAuth.AuthSoapHeader(), syncDate, fileNameList,newSyncID);
            Log.ActivityLog("-----------------------Sync transaction done-----------------------");
            return newSyncID;
        }