public Boolean CreateNewAccount(Int32 UserID, ServiceAccountDTO newAccount)
        {
            Boolean         succeed        = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            UserLinkDAO       newUserLink = new UserLinkDAO();
            ServiceAccountDAO target      = newAccount.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();

            target.AccountVersion   = 0;
            newUserLink.UserOwnerID = UserID;
            newUserLink.Account     = target;
            newUserLink.Owner       = true;
            newUserLink.UserId      = UserID;

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(newUserLink);
                    transaction.Commit();
                    succeed = transaction.WasCommitted;
                }
            }
            return(succeed);
        }
        public Boolean CreateNewAccount(Int32 UserID, ServiceAccountDTO newAccount)
        {
            Boolean succeed = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            UserLinkDAO newUserLink = new UserLinkDAO();
            ServiceAccountDAO target = newAccount.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();
            target.AccountVersion = 0;
            newUserLink.UserOwnerID = UserID;
            newUserLink.Account = target;
            newUserLink.Owner = true;
            newUserLink.UserId = UserID;

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {

                    session.Save(newUserLink);
                    transaction.Commit();
                    succeed = transaction.WasCommitted;
                }

            }
            return succeed;
        }
        public Boolean DeleteAccount(Int32 UserID, ServiceAccountDTO accountToDelete, Boolean DeleteForAllUsers)
        {
            Boolean succeed = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);
            ServiceAccountDAO targetAccountToDelete = accountToDelete.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {

                    UserLinkDAO userLink = session.QueryOver<UserLinkDAO>().Where(x => x.Account.ServiceAccountId == accountToDelete.ServiceAccountId).And(x => x.UserId == UserID).SingleOrDefault();
                    if (userLink != null)
                    {
                        try
                        {
                            IList<UserLinkDAO> Links = session.QueryOver<UserLinkDAO>().Where(x => x.Account.ServiceAccountId == accountToDelete.ServiceAccountId).List();

                            if (DeleteForAllUsers)
                            {
                                foreach (var item in Links)
                                {
                                    session.Delete(item);
                                }
                                session.Delete(targetAccountToDelete);
                            }
                            else
                            {
                                if (Links.Count == 1)
                                {
                                    session.Delete(targetAccountToDelete);
                                }
                                session.Delete(userLink);
                            }

                            transaction.Commit();
                            succeed = transaction.WasCommitted;
                        }
                        catch (Exception)
                        {
                            transaction.Rollback();
                            return succeed;
                        }
                    }

                }

            }
            return succeed;
        }
        public ServiceAccountDTO TestExcelAccount(ServiceAccountDTO accountForTest, Byte[] fileForParce)
        {
            IAccountSettings currentAccountForTest = SettingsManager.GetCurrentInstance(accountForTest.Source);

            currentAccountForTest = currentAccountForTest.Convert(accountForTest.ServiceAccountDTOToDomain());
            ExcelAdapter currentAdapter = new ExcelAdapter(currentAccountForTest, fileForParce);

            IAccountSettings testResult   = currentAdapter.TestAccount(currentAccountForTest);
            ServiceAccount   resultDomain = new ServiceAccount();

            resultDomain = testResult.Convert(testResult);
            ServiceAccountDTO result = resultDomain.ServiceAccountDomainToDTO();

            return(result);
        }
        public Boolean DeleteAccount(Int32 UserID, ServiceAccountDTO accountToDelete, Boolean DeleteForAllUsers)
        {
            Boolean           succeed               = false;
            ISessionFactory   sessionFactory        = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);
            ServiceAccountDAO targetAccountToDelete = accountToDelete.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();

            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    UserLinkDAO userLink = session.QueryOver <UserLinkDAO>().Where(x => x.Account.ServiceAccountId == accountToDelete.ServiceAccountId).And(x => x.UserId == UserID).SingleOrDefault();
                    if (userLink != null)
                    {
                        try
                        {
                            IList <UserLinkDAO> Links = session.QueryOver <UserLinkDAO>().Where(x => x.Account.ServiceAccountId == accountToDelete.ServiceAccountId).List();

                            if (DeleteForAllUsers)
                            {
                                foreach (var item in Links)
                                {
                                    session.Delete(item);
                                }
                                session.Delete(targetAccountToDelete);
                            }
                            else
                            {
                                if (Links.Count == 1)
                                {
                                    session.Delete(targetAccountToDelete);
                                }
                                session.Delete(userLink);
                            }

                            transaction.Commit();
                            succeed = transaction.WasCommitted;
                        }
                        catch (Exception)
                        {
                            transaction.Rollback();
                            return(succeed);
                        }
                    }
                }
            }
            return(succeed);
        }
        public Boolean SaveOrUdateAccount(ServiceAccountDTO account)
        {
            Boolean         succeed        = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            ServiceAccountDAO target = account.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();

            using (ISession session = sessionFactory.OpenSession())
            {
                target.AccountVersion += 1;
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(target);
                    transaction.Commit();
                    succeed = transaction.WasCommitted;
                }
            }
            return(succeed);
        }
        public ServiceAccountDTO TestAccount(ServiceAccountDTO accountForTest)
        {
            IAdapter currentAdapter = AdapterInstanceFactory.GetCurentAdapterInstance(accountForTest.Source);

            if (currentAdapter != null)
            {
                IAccountSettings currentAccountForTest = SettingsManager.GetCurrentInstance(accountForTest.Source);

                currentAccountForTest = currentAccountForTest.Convert(accountForTest.ServiceAccountDTOToDomain());

                IAccountSettings testResult   = currentAdapter.TestAccount(currentAccountForTest);
                ServiceAccount   resultDomain = new ServiceAccount();
                resultDomain = testResult.Convert(testResult);
                ServiceAccountDTO result = resultDomain.ServiceAccountDomainToDTO();
                return(result);
            }
            else
            {
                accountForTest.TestResult = false;
                return(accountForTest);
            }
        }
        public Boolean ShareTheSettingAccount(Int32 currentUserID, ServiceAccountDTO accountToShare, String shareUserName, Boolean owner)
        {
            Boolean         succeed        = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            UserDAO           shareUser;
            UserLinkDAO       newUserLink        = new UserLinkDAO();
            ServiceAccountDAO targetShareAccount = accountToShare.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();

            newUserLink.Account     = targetShareAccount;
            newUserLink.Owner       = owner;
            newUserLink.UserOwnerID = currentUserID;

            using (ISession session = sessionFactory.OpenSession())
            {
                shareUser = session.QueryOver <UserDAO>().Where(user => user.UserId == shareUserName).SingleOrDefault();
                if (shareUser != null)
                {
                    newUserLink.UserId = shareUser.ID;
                    UserLinkDAO checkLink = session.QueryOver <UserLinkDAO>().Where(link => link.Account.ServiceAccountId == targetShareAccount.ServiceAccountId).And(link => link.UserId == shareUser.ID).SingleOrDefault();
                    if (checkLink == null)
                    {
                        using (ITransaction transaction = session.BeginTransaction())
                        {
                            session.Save(newUserLink);
                            transaction.Commit();
                            succeed = transaction.WasCommitted;
                        }
                    }
                    else
                    {
                        return(succeed);
                    }
                }
            }
            return(succeed);
        }
        public ServiceAccountDTO TestExcelAccount(ServiceAccountDTO accountForTest, Byte[] fileForParce)
        {
            IAccountSettings currentAccountForTest = SettingsManager.GetCurrentInstance(accountForTest.Source);

            currentAccountForTest = currentAccountForTest.Convert(accountForTest.ServiceAccountDTOToDomain());
            ExcelAdapter currentAdapter = new ExcelAdapter(currentAccountForTest, fileForParce);

            IAccountSettings testResult = currentAdapter.TestAccount(currentAccountForTest);
            ServiceAccount resultDomain = new ServiceAccount();
            resultDomain = testResult.Convert(testResult);
            ServiceAccountDTO result = resultDomain.ServiceAccountDomainToDTO();
            return result;
        }
        public ServiceAccountDTO TestAccount(ServiceAccountDTO accountForTest)
        {
            IAdapter currentAdapter = AdapterInstanceFactory.GetCurentAdapterInstance(accountForTest.Source);
            if (currentAdapter != null)
            {
                IAccountSettings currentAccountForTest = SettingsManager.GetCurrentInstance(accountForTest.Source);

                currentAccountForTest = currentAccountForTest.Convert(accountForTest.ServiceAccountDTOToDomain());

                IAccountSettings testResult = currentAdapter.TestAccount(currentAccountForTest);
                ServiceAccount resultDomain = new ServiceAccount();
                resultDomain = testResult.Convert(testResult);
                ServiceAccountDTO result = resultDomain.ServiceAccountDomainToDTO();
                return result;
            }
            else
            {
                accountForTest.TestResult = false;
                return accountForTest;
            }
        }
        public Boolean ShareTheSettingAccount(Int32 currentUserID, ServiceAccountDTO accountToShare, String shareUserName, Boolean owner)
        {
            Boolean succeed = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            UserDAO shareUser;
            UserLinkDAO newUserLink = new UserLinkDAO();
            ServiceAccountDAO targetShareAccount = accountToShare.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();
            newUserLink.Account = targetShareAccount;
            newUserLink.Owner = owner;
            newUserLink.UserOwnerID = currentUserID;

            using (ISession session = sessionFactory.OpenSession())
            {
                shareUser = session.QueryOver<UserDAO>().Where(user => user.UserId == shareUserName).SingleOrDefault();
                if (shareUser != null)
                {
                    newUserLink.UserId = shareUser.ID;
                    UserLinkDAO checkLink = session.QueryOver<UserLinkDAO>().Where(link => link.Account.ServiceAccountId == targetShareAccount.ServiceAccountId).And(link => link.UserId == shareUser.ID).SingleOrDefault();
                    if (checkLink == null)
                    {
                        using (ITransaction transaction = session.BeginTransaction())
                        {
                            session.Save(newUserLink);
                            transaction.Commit();
                            succeed = transaction.WasCommitted;
                        }
                    }
                    else
                    {
                        return succeed;
                    }
                }
            }
            return succeed;
        }
        public Boolean SaveOrUdateAccount(ServiceAccountDTO account)
        {
            Boolean succeed = false;
            ISessionFactory sessionFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            ServiceAccountDAO target = account.ServiceAccountDTOToDomain().ServiceAccountDomainToDAO();
            using (ISession session = sessionFactory.OpenSession())
            {
                target.AccountVersion += 1;
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(target);
                    transaction.Commit();
                    succeed = transaction.WasCommitted;
                }
            }
            return succeed;
        }