예제 #1
0
        public static SurveyMetaData ToSurveyMetadata(this SurveyInfoBO surveyInfoBO)
        {
            SurveyMetaData surveyMetaData = new SurveyMetaData();

            surveyMetaData.SurveyId         = new Guid(surveyInfoBO.SurveyId);
            surveyMetaData.SurveyName       = surveyInfoBO.SurveyName;
            surveyMetaData.SurveyNumber     = surveyInfoBO.SurveyNumber;
            surveyMetaData.IntroductionText = surveyInfoBO.IntroductionText;
            surveyMetaData.ExitText         = surveyInfoBO.ExitText;
            surveyMetaData.OrganizationName = surveyInfoBO.OrganizationName;
            surveyMetaData.DepartmentName   = surveyInfoBO.DepartmentName;
            surveyMetaData.ClosingDate      = surveyInfoBO.ClosingDate;
            surveyMetaData.UserPublishKey   = surveyInfoBO.UserPublishKey;
            surveyMetaData.SurveyTypeId     = surveyInfoBO.SurveyType;
            surveyMetaData.DateCreated      = surveyInfoBO.DateCreated;
            surveyMetaData.IsDraftMode      = surveyInfoBO.IsDraftMode;
            surveyMetaData.StartDate        = surveyInfoBO.StartDate;
            surveyMetaData.OwnerId          = surveyInfoBO.OwnerId;
            surveyMetaData.ViewId           = surveyInfoBO.ViewId;
            surveyMetaData.IsSQLProject     = surveyInfoBO.IsSqlProject;
            surveyMetaData.IsShareable      = surveyInfoBO.IsShareable;
            surveyMetaData.DataAccessRuleId = surveyInfoBO.DataAccessRuleId;
            if (!string.IsNullOrEmpty(surveyInfoBO.ParentFormId))
            {
                surveyMetaData.ParentId = new Guid(surveyInfoBO.ParentFormId);
            }

            return(surveyMetaData);
        }
예제 #2
0
        public List <CacheDependencyBO> GetCacheDependencyInfo(List <string> surveyKeys)
        {
            List <CacheDependencyBO> result = new List <CacheDependencyBO>();

            if (surveyKeys.Count > 0)
            {
                try
                {
                    foreach (string key in surveyKeys)
                    {
                        Guid guid = new Guid(key);

                        using (var Context = DataObjectFactory.CreateContext())
                        {
                            SurveyMetaData    surveyMetaDatas   = Context.SurveyMetaDatas.FirstOrDefault(x => x.SurveyId == guid);
                            CacheDependencyBO cacheDependencyBO = Mapper.MapDependency(surveyMetaDatas);
                            result.Add(cacheDependencyBO);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

            return(result);
        }
        public List <UserBO> GetOrgAdminsByFormId(string formId)
        {
            List <UserBO>            userBOList = new List <UserBO>();
            Dictionary <int, string> orgAdmins  = new Dictionary <int, string>();
            Guid id = new Guid(formId);

            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    SurveyMetaData response   = context.SurveyMetaDatas.First(x => x.SurveyId == id);
                    var            orgHashSet = new HashSet <int>(response.Organizations.Select(x => x.OrganizationId));
                    var            orgs       = context.Organizations.Where(t => orgHashSet.Contains(t.OrganizationId)).ToList();

                    foreach (var org in orgs)
                    {
                        var adminList = context.UserOrganizations.Where(x => x.OrganizationID == org.OrganizationId && x.RoleId == Roles.Administrator && x.Active == true);
                        foreach (var admin in adminList)
                        {
                            UserBO userBO = new UserBO();
                            userBO.EmailAddress = admin.User.EmailAddress;
                            userBO.UserId       = admin.User.UserID;
                            userBOList.Add(userBO);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return(userBOList);
        }
예제 #4
0
        /// <summary>
        /// Maps SurveyInfoBO business object to SurveyMetaData entity.
        /// </summary>
        /// <param name="businessobject">A SurveyInfoBO business object.</param>
        /// <returns>A SurveyMetaData entity.</returns>
        internal static SurveyMetaData Map(SurveyInfoBO businessobject)
        {
            SurveyMetaData SurveyMetaData = new SurveyMetaData();

            SurveyMetaData.SurveyId         = new Guid(businessobject.SurveyId);
            SurveyMetaData.SurveyName       = businessobject.SurveyName;
            SurveyMetaData.SurveyNumber     = businessobject.SurveyNumber;
            SurveyMetaData.TemplateXML      = businessobject.XML;
            SurveyMetaData.IntroductionText = businessobject.IntroductionText;
            SurveyMetaData.ExitText         = businessobject.ExitText;
            SurveyMetaData.OrganizationName = businessobject.OrganizationName;
            SurveyMetaData.DepartmentName   = businessobject.DepartmentName;
            SurveyMetaData.ClosingDate      = businessobject.ClosingDate;
            SurveyMetaData.UserPublishKey   = businessobject.UserPublishKey;
            SurveyMetaData.SurveyTypeId     = businessobject.SurveyType;
            SurveyMetaData.TemplateXMLSize  = businessobject.TemplateXMLSize;
            SurveyMetaData.DateCreated      = businessobject.DateCreated;
            SurveyMetaData.IsDraftMode      = businessobject.IsDraftMode;
            SurveyMetaData.StartDate        = businessobject.StartDate;
            SurveyMetaData.OwnerId          = businessobject.OwnerId;
            SurveyMetaData.ViewId           = businessobject.ViewId;
            SurveyMetaData.IsSQLProject     = businessobject.IsSqlProject;
            SurveyMetaData.IsShareable      = businessobject.IsShareable;
            SurveyMetaData.DataAccessRuleId = businessobject.DataAccessRuleId;
            //  SurveyMetaData.DataAccessRuleId = businessobject.;
            if (!string.IsNullOrEmpty(businessobject.ParentId))
            {
                SurveyMetaData.ParentId = new Guid(businessobject.ParentId);
            }


            return(SurveyMetaData);
        }
        public List <UserBO> GetOrgAdminsByFormId(string FormId)
        {
            List <UserBO>            BoList       = new List <UserBO>();
            Dictionary <int, string> GetOrgAdmins = new Dictionary <int, string>();
            Guid Id = new Guid(FormId);

            try
            {
                using (var Context = DataObjectFactory.CreateContext())
                {
                    SurveyMetaData Response = Context.SurveyMetaDatas.First(x => x.SurveyId == Id);
                    var            _Org     = new HashSet <int>(Response.Organizations.Select(x => x.OrganizationId));
                    var            Orgs     = Context.Organizations.Where(t => _Org.Contains(t.OrganizationId)).ToList();


                    foreach (var Org in Orgs)
                    {
                        var AdminList = Context.UserOrganizations.Where(x => x.OrganizationID == Org.OrganizationId && x.RoleId == 2 && x.Active == true);
                        foreach (var Admin in AdminList)
                        {
                            UserBO UserBO = new UserBO();
                            UserBO.EmailAddress = Admin.User.EmailAddress;
                            UserBO.UserId       = Admin.User.UserID;
                            BoList.Add(UserBO);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return(BoList);
        }
        /// <summary>
        /// Updates a SurveyInfo.
        /// </summary>
        /// <param name="SurveyInfo">SurveyInfo.</param>
        public void UpdateSurveyInfo(SurveyInfoBO SurveyInfo)
        {
            try
            {
                Guid Id = new Guid(SurveyInfo.SurveyId);

                //Update Survey
                using (var Context = DataObjectFactory.CreateContext())
                {
                    //var Query = from response in Context.SurveyMetaDatas
                    //            where response.SurveyId == Id
                    //            select response;

                    //var DataRow = Query.Single();
                    //DataRow = Mapper.ToEF(SurveyInfo);

                    SurveyMetaData Row = Context.SurveyMetaDatas.First(x => x.SurveyId == Id);
                    Row.IsSQLProject     = SurveyInfo.IsSqlProject;
                    Row.TemplateXML      = SurveyInfo.XML;
                    Row.IsDraftMode      = SurveyInfo.IsDraftMode;
                    Row.IsShareable      = SurveyInfo.IsShareable;
                    Row.DataAccessRuleId = SurveyInfo.DataAccessRuleId;
                    Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
예제 #7
0
        /// <summary>
        /// Maps SurveyMetaData entity to SurveyInfoBO business object.
        /// </summary>
        /// <param name="entity">A SurveyMetaData entity to be transformed.</param>
        /// <returns>A SurveyInfoBO business object.</returns>
        internal static SurveyInfoBO Map(SurveyMetaData entity)
        {
            SurveyInfoBO result = new SurveyInfoBO();

            result.SurveyId         = entity.SurveyId.ToString();
            result.SurveyName       = entity.SurveyName;
            result.SurveyNumber     = entity.SurveyNumber;
            result.XML              = entity.TemplateXML;
            result.IntroductionText = entity.IntroductionText;
            result.ExitText         = entity.ExitText;
            result.OrganizationName = entity.OrganizationName;
            result.DepartmentName   = entity.DepartmentName;
            result.ClosingDate      = entity.ClosingDate;
            result.TemplateXMLSize  = (long)entity.TemplateXMLSize;
            result.DateCreated      = entity.DateCreated;
            result.IsDraftMode      = entity.IsDraftMode;
            result.StartDate        = entity.StartDate;
            result.OrganizationId   = entity.OrganizationId;
            if (entity.UserPublishKey != null)
            {
                result.UserPublishKey = entity.UserPublishKey;
            }
            result.SurveyType = entity.SurveyTypeId;
            result.ParentId   = entity.ParentId.ToString();

            return(result);
        }
예제 #8
0
        private static List <KeyValuePair <int, string> > GetSharedForms(int currentOrgId, OSELS_EWEEntities context)
        {
            List <KeyValuePair <int, string> > shared    = new List <KeyValuePair <int, string> >();
            IQueryable <SurveyMetaData>        AllForms1 = context.SurveyMetaDatas.Where(x => x.ParentId == null &&
                                                                                         x.Organizations.Any(r => r.OrganizationId == currentOrgId)
                                                                                         ).Distinct();

            foreach (var form in AllForms1)
            {
                // checking if the form is shared with any organization
                SurveyMetaData Response = context.SurveyMetaDatas.First(x => x.SurveyId == form.SurveyId);
                var            _Org     = new HashSet <int>(Response.Organizations.Select(x => x.OrganizationId));
                var            Orgs     = context.Organizations.Where(t => _Org.Contains(t.OrganizationId)).ToList();
                //if form is shared
                if (Orgs.Count > 0)
                {
                    foreach (var org in Orgs)
                    {
                        KeyValuePair <int, string> Item = new KeyValuePair <int, string>(org.OrganizationId, form.SurveyId.ToString());

                        shared.Add(Item);
                    }
                }
            }
            return(shared);
        }
예제 #9
0
        /// <summary>
        /// Maps SurveyMetaData entity to FormInfoBO business object.
        /// </summary>
        /// <param name="entity">A SurveyMetaData entity to be transformed.</param>
        /// <returns>A FormInfoBO business object.</returns>
        internal static FormInfoBO MapToFormInfoBO(SurveyMetaData entity, User UserEntity, bool GetXml = false)
        {
            FormInfoBO result = new FormInfoBO();

            result.IsSQLProject     = (entity.IsSQLProject == null) ? false : (bool)entity.IsSQLProject;
            result.FormId           = entity.SurveyId.ToString();
            result.FormName         = entity.SurveyName;
            result.FormNumber       = entity.SurveyNumber;
            result.OrganizationName = entity.OrganizationName;
            result.OrganizationId   = entity.OrganizationId;
            result.IsDraftMode      = entity.IsDraftMode;
            result.UserId           = entity.OwnerId;

            if (entity.IsShareable != null)
            {
                result.IsShareable = (bool)entity.IsShareable;
            }
            if (entity.DataAccessRuleId != null)
            {
                // result. =  entity.DataAccessRuleId;
            }
            result.OwnerFName = UserEntity.FirstName;
            result.OwnerLName = UserEntity.LastName;

            if (GetXml)
            {
                result.Xml = entity.TemplateXML;
            }
            result.ParentId = entity.ParentId.ToString();
            return(result);
        }
예제 #10
0
        /// <summary>
        /// Maps SurveyMetaData entity to SurveyInfoBO business object.
        /// </summary>
        /// <param name="entity">A SurveyMetaData entity to be transformed.</param>
        /// <returns>A SurveyInfoBO business object.</returns>
        internal static SurveyInfoBO Map(SurveyMetaData entity)
        {
            SurveyInfoBO result = new SurveyInfoBO();

            result.SurveyId         = entity.SurveyId.ToString();
            result.SurveyName       = entity.SurveyName;
            result.SurveyNumber     = entity.SurveyNumber;
            result.XML              = entity.TemplateXML;
            result.IntroductionText = entity.IntroductionText;
            result.ExitText         = entity.ExitText;
            result.OrganizationName = entity.OrganizationName;
            result.DepartmentName   = entity.DepartmentName;
            result.ClosingDate      = entity.ClosingDate;
            result.TemplateXMLSize  = (long)entity.TemplateXMLSize;
            result.DateCreated      = entity.DateCreated;
            result.IsDraftMode      = entity.IsDraftMode;
            result.StartDate        = entity.StartDate;
            result.IsSqlProject     = (bool)entity.IsSQLProject;
            result.OwnerId          = entity.OwnerId;
            if (entity.UserPublishKey != null)
            {
                // result.UserPublishKey = (Guid)entity.UserPublishKey.Value;
                result.UserPublishKey = entity.UserPublishKey;
            }
            result.SurveyType = entity.SurveyTypeId;
            result.ParentId   = entity.ParentId.ToString();
            if (entity.ViewId != null)
            {
                result.ViewId = (int)entity.ViewId;
            }
            //result. = (bool)entity.ShowAllRecords;
            result.IsShareable = (bool)entity.IsShareable;
            return(result);
        }
        public void UpdateSettingsList(FormSettingBO formSettingBO, string formId)
        {
            Guid id = new Guid(formId);

            try
            {
                using (var context = DataObjectFactory.CreateContext())
                {
                    SurveyMetaData response = context.SurveyMetaDatas.First(x => x.SurveyId == id);

                    //Remove old Users
                    var userHashSet = new HashSet <string>(response.Users.Select(x => x.UserName));
                    var users       = context.Users.Where(t => userHashSet.Contains(t.UserName)).ToList();

                    foreach (User user in users)
                    {
                        response.Users.Remove(user);
                    }
                    context.SaveChanges();

                    //insert new users
                    foreach (var item in formSettingBO.AssignedUserList)
                    {
                        User user = context.Users.FirstOrDefault(x => x.UserName == item.Value);
                        response.Users.Add(user);
                    }
                    context.SaveChanges();

                    //Remove old Orgs
                    var _Org = new HashSet <int>(response.Organizations.Select(x => x.OrganizationId));
                    var Orgs = context.Organizations.Where(t => _Org.Contains(t.OrganizationId)).ToList();

                    foreach (Organization org in Orgs)
                    {
                        response.Organizations.Remove(org);
                    }
                    context.SaveChanges();

                    //insert new Orgs
                    List <User> OrgAdmis = new List <User>();

                    foreach (var item in formSettingBO.SelectedOrgList)
                    {
                        int          OrgId = int.Parse(item.Value);
                        Organization Org   = context.Organizations.FirstOrDefault(x => x.OrganizationId == OrgId);
                        response.Organizations.Add(Org);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
예제 #12
0
        internal static void Map(SurveyMetaData entity, out CacheDependencyBO cacheDependencyBO)
        {
            cacheDependencyBO = new CacheDependencyBO();

            cacheDependencyBO.SurveyId = entity.SurveyId.ToString();

            if (entity.LastUpdate == null)
            {
                entity.LastUpdate = entity.DateCreated;
            }

            cacheDependencyBO.LastUpdate = (DateTime)entity.LastUpdate;
        }
예제 #13
0
        internal static CacheDependencyBO MapDependency(SurveyMetaData entity)
        {
            CacheDependencyBO cacheDependencyBO = new CacheDependencyBO();

            cacheDependencyBO.SurveyId = entity.SurveyId.ToString();

            if (entity.LastUpdate != null)
            {
                cacheDependencyBO.LastUpdate = (DateTime)entity.LastUpdate;
            }

            return(cacheDependencyBO);
        }
예제 #14
0
        public FormInfoBO GetFormByFormId(string formId, int userId)
        {
            // TODO: Refactor to remove dependency on SurveyMetadatas

            FormInfoBO formInfoBO = new FormInfoBO();

            try
            {
                var formOwnerUserId = GetFormDigest(formId).OwnerUserId;

                Guid Id = new Guid(formId);

                using (var context = DataObjectFactory.CreateContext())
                {
                    var items = from FormInfo in context.SurveyMetaDatas
                                join UserInfo in context.Users
                                on FormInfo.OwnerId equals UserInfo.UserID
                                into temp
                                from UserInfo in temp.DefaultIfEmpty()
                                where FormInfo.SurveyId == Id
                                select new { FormInfo, UserInfo };
                    SurveyMetaData Response   = context.SurveyMetaDatas.First(x => x.SurveyId == Id);
                    var            orgHashSet = new HashSet <int>(Response.Organizations.Select(x => x.OrganizationId));
                    var            orgList    = context.Organizations.Where(t => orgHashSet.Contains(t.OrganizationId)).ToList();

                    bool isShared = false;

                    foreach (var org in orgList)
                    {
                        isShared = context.UserOrganizations.Any(x => x.OrganizationID == org.OrganizationId && x.UserID == userId && x.RoleId == Roles.Administrator);
                        if (isShared)
                        {
                            break;
                        }
                    }

                    foreach (var item in items)
                    {
                        formInfoBO          = Mapper.MapToFormInfoBO(item.FormInfo, item.UserInfo);
                        formInfoBO.IsShared = isShared;
                        formInfoBO.IsOwner  = item.UserInfo.UserID == userId;
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return(formInfoBO);
        }
예제 #15
0
 public static FormInfoBO ToFormInfoBO(SurveyMetaData entity)
 {
     return(new FormInfoBO
     {
         FormId = entity.SurveyId.ToString(),
         FormNumber = entity.SurveyNumber,
         FormName = entity.SurveyName,
         OrganizationName = entity.OrganizationName,
         OrganizationId = entity.OrganizationId,
         IsDraftMode = entity.IsDraftMode,
         UserId = entity.OwnerId,
         ParentId = (entity.ParentId != null) ? entity.ParentId.ToString() : ""
     });
 }
예제 #16
0
        public static SurveyMetaData ToEF(this SurveyInfoBO surveyInfoBO)
        {
            SurveyMetaData surveyMetadata = new SurveyMetaData();

            surveyMetadata.SurveyName       = surveyInfoBO.SurveyName;
            surveyMetadata.SurveyNumber     = surveyInfoBO.SurveyNumber;
            surveyMetadata.IntroductionText = surveyInfoBO.IntroductionText;
            surveyMetadata.ExitText         = surveyInfoBO.ExitText;
            surveyMetadata.OrganizationName = surveyInfoBO.OrganizationName;
            surveyMetadata.DepartmentName   = surveyInfoBO.DepartmentName;
            surveyMetadata.ClosingDate      = surveyInfoBO.ClosingDate;
            surveyMetadata.SurveyTypeId     = surveyInfoBO.SurveyType;
            surveyMetadata.UserPublishKey   = surveyInfoBO.UserPublishKey;
            surveyMetadata.IsDraftMode      = surveyInfoBO.IsDraftMode;
            surveyMetadata.StartDate        = surveyInfoBO.StartDate;
            return(surveyMetadata);
        }
        public List <UserBO> GetUserByFormId(string FormId)
        {
            Guid          id       = new Guid(FormId);
            List <UserBO> UserList = new List <UserBO>();
            UserBO        UserBO   = new UserBO();

            using (var Context = DataObjectFactory.CreateContext())
            {
                SurveyMetaData SelectedUserQuery = Context.SurveyMetaDatas.First(x => x.SurveyId == id);

                IQueryable <User> Users = SelectedUserQuery.Users.AsQueryable();
                foreach (User user in Users)
                {
                    UserList.Add(Mapper.MapToUserBO(user));
                }
            }
            return(UserList);
        }
예제 #18
0
        internal static SurveyMetaData ToEF(SurveyInfoBO SurveyInfo)
        {
            SurveyMetaData DataRow = new SurveyMetaData();

            DataRow.SurveyName       = SurveyInfo.SurveyName;
            DataRow.SurveyNumber     = SurveyInfo.SurveyNumber;
            DataRow.TemplateXML      = SurveyInfo.XML;
            DataRow.IntroductionText = SurveyInfo.IntroductionText;
            DataRow.ExitText         = SurveyInfo.ExitText;
            DataRow.OrganizationName = SurveyInfo.OrganizationName;
            DataRow.DepartmentName   = SurveyInfo.DepartmentName;
            DataRow.ClosingDate      = SurveyInfo.ClosingDate;
            DataRow.SurveyTypeId     = SurveyInfo.SurveyType;
            DataRow.UserPublishKey   = SurveyInfo.UserPublishKey;
            DataRow.TemplateXMLSize  = RemoveWhitespace(SurveyInfo.XML).Length;
            DataRow.IsDraftMode      = SurveyInfo.IsDraftMode;
            DataRow.StartDate        = SurveyInfo.StartDate;
            return(DataRow);
        }
예제 #19
0
        public FormInfoBO GetFormByFormId(string formId)
        {
            FormInfoBO formInfoBO = new FormInfoBO();

            try
            {
                Guid Id = new Guid(formId);

                using (var Context = DataObjectFactory.CreateContext())
                {
                    SurveyMetaData SurveyMetaData = Context.SurveyMetaDatas.Single(x => x.SurveyId == Id);
                    formInfoBO = Mapper.ToFormInfoBO(SurveyMetaData);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return(formInfoBO);
        }
        public FormSettingBO GetFormSettings(string FormId, int CurrentOrgId)
        {
            FormSettingBO               FormSettingBO             = new FormSettingBO();
            Dictionary <int, string>    ColumnNameList            = new Dictionary <int, string>();
            Dictionary <int, string>    AvailableUsers            = new Dictionary <int, string>();
            Dictionary <int, string>    SelectedUsers             = new Dictionary <int, string>();
            Dictionary <int, string>    AvailableOrgs             = new Dictionary <int, string>();
            Dictionary <int, string>    SelectedOrgs              = new Dictionary <int, string>();
            Dictionary <int, string>    DataAccessRuleIds         = new Dictionary <int, string>();
            Dictionary <string, string> DataAccessRuleDescription = new Dictionary <string, string>();
            int selectedDataAccessRuleId;

            try
            {
                Guid id = new Guid(FormId);
                using (var Context = DataObjectFactory.CreateContext())
                {
                    var Query = from response in Context.ResponseDisplaySettings
                                where response.FormId == id
                                select response;

                    var DataRow = Query;

                    foreach (var Row in DataRow)
                    {
                        ColumnNameList.Add(Row.SortOrder, Row.ColumnName);
                    }



                    //var SelectedUserQuery = from FormInfo in Context.SurveyMetaDatas
                    //            join UserInfo in Context.Users
                    //            on FormInfo.OwnerId equals UserInfo.UserID
                    //            into temp
                    //            from UserInfo in temp.DefaultIfEmpty()
                    //            where FormInfo.SurveyId == id
                    //            select new { FormInfo, UserInfo };

                    SurveyMetaData SelectedUserQuery = Context.SurveyMetaDatas.First(x => x.SurveyId == id);

                    // var SelectedOrgId = SelectedUserQuery.OrganizationId;
                    var SelectedOrgId = CurrentOrgId;
                    var query         = (from user in SelectedUserQuery.Users
                                         join userorg in Context.UserOrganizations
                                         on user.UserID equals userorg.UserID
                                         where userorg.Active == true &&
                                         userorg.OrganizationID == SelectedOrgId
                                         // orderby user.UserName
                                         select user).Distinct().OrderBy(user => user.UserName);

                    // IEnumerable<User> Users = SelectedUserQuery.Users;
                    foreach (var user in query)
                    {
                        SelectedUsers.Add(user.UserID, user.UserName);
                    }

                    //foreach (var Selecteduser in SelectedUserQuery)
                    //    {
                    //    SelectedUsers.Add(Selecteduser.UserInfo.UserID, Selecteduser.UserInfo.UserName);
                    //    }


                    var UserQuery = (from user in Context.Users
                                     join userorg in Context.UserOrganizations
                                     on user.UserID equals userorg.UserID
                                     where userorg.Active == true &&
                                     userorg.OrganizationID == SelectedOrgId
                                     //orderby user.UserName
                                     select user).Distinct().OrderBy(user => user.UserName);



                    foreach (var user in UserQuery)
                    {
                        if (!SelectedUsers.ContainsValue(user.UserName) && user.UserID != SelectedUserQuery.OwnerId)
                        {
                            AvailableUsers.Add(user.UserID, user.UserName);
                        }
                    }

                    //// Select Orgnization list
                    var OrganizationQuery = Context.Organizations.Where(c => c.SurveyMetaDatas.Any(a => a.SurveyId == id)).ToList();


                    foreach (var org in OrganizationQuery)
                    {
                        SelectedOrgs.Add(org.OrganizationId, org.Organization1);
                    }
                    ////  Available Orgnization list

                    IQueryable <Organization> OrganizationList = Context.Organizations.ToList().AsQueryable();
                    foreach (var Org in OrganizationList)
                    {
                        if (!SelectedOrgs.ContainsValue(Org.Organization1) && Org.IsEnabled == true)
                        {
                            AvailableOrgs.Add(Org.OrganizationId, Org.Organization1);
                        }
                    }
                    //// Select DataAccess Rule Ids  list
                    var MetaData = Context.SurveyMetaDatas.Where(a => a.SurveyId == id).Single();


                    selectedDataAccessRuleId = int.Parse(MetaData.DataAccessRuleId.ToString());
                    ////  Available DataAccess Rule Ids  list

                    IQueryable <DataAccessRule> RuleIDs = Context.DataAccessRules.ToList().AsQueryable();
                    foreach (var Rule in RuleIDs)
                    {
                        DataAccessRuleIds.Add(Rule.RuleId, Rule.RuleName);
                        DataAccessRuleDescription.Add(Rule.RuleName, Rule.RuleDescription);
                    }
                    FormSettingBO.ColumnNameList = ColumnNameList;

                    FormSettingBO.UserList                  = AvailableUsers;
                    FormSettingBO.AssignedUserList          = SelectedUsers;
                    FormSettingBO.AvailableOrgList          = AvailableOrgs;
                    FormSettingBO.SelectedOrgList           = SelectedOrgs;
                    FormSettingBO.DataAccessRuleIds         = DataAccessRuleIds;
                    FormSettingBO.SelectedDataAccessRule    = selectedDataAccessRuleId;
                    FormSettingBO.DataAccessRuleDescription = DataAccessRuleDescription;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return(FormSettingBO);
        }
        public FormSettingBO GetFormSettings(string FormId, int CurrentOrgId)
        {
            FormSettingBO               formSettingBO             = new FormSettingBO();
            Dictionary <int, string>    columnNameList            = new Dictionary <int, string>();
            Dictionary <int, string>    availableUsers            = new Dictionary <int, string>();
            Dictionary <int, string>    selectedUsers             = new Dictionary <int, string>();
            Dictionary <int, string>    availableOrgs             = new Dictionary <int, string>();
            Dictionary <int, string>    selectedOrgs              = new Dictionary <int, string>();
            Dictionary <int, string>    dataAccessRuleIds         = new Dictionary <int, string>();
            Dictionary <string, string> dataAccessRuleDescription = new Dictionary <string, string>();
            int selectedDataAccessRuleId;

            try
            {
                Guid id = new Guid(FormId);
                using (var Context = DataObjectFactory.CreateContext())
                {
                    var Query = from response in Context.ResponseDisplaySettings
                                where response.FormId == id
                                select response;

                    var DataRow = Query;

                    foreach (var Row in DataRow)
                    {
                        columnNameList.Add(Row.SortOrder, Row.ColumnName);
                    }

                    SurveyMetaData SelectedUserQuery = Context.SurveyMetaDatas.First(x => x.SurveyId == id);

                    var SelectedOrgId = CurrentOrgId;
                    var query         = (from user in SelectedUserQuery.Users
                                         join userorg in Context.UserOrganizations
                                         on user.UserID equals userorg.UserID
                                         where userorg.Active == true &&
                                         userorg.OrganizationID == SelectedOrgId
                                         select user).Distinct().OrderBy(user => user.UserName);

                    foreach (var user in query)
                    {
                        selectedUsers.Add(user.UserID, user.UserName);
                    }

                    var UserQuery = (from user in Context.Users
                                     join userorg in Context.UserOrganizations
                                     on user.UserID equals userorg.UserID
                                     where userorg.Active == true &&
                                     userorg.OrganizationID == SelectedOrgId
                                     select user).Distinct().OrderBy(user => user.UserName);



                    foreach (var user in UserQuery)
                    {
                        if (!selectedUsers.ContainsValue(user.UserName) && user.UserID != SelectedUserQuery.OwnerId)
                        {
                            availableUsers.Add(user.UserID, user.UserName);
                        }
                    }

                    //// Select Orgnization list
                    var OrganizationQuery = Context.Organizations.Where(c => c.SurveyMetaDatas.Any(a => a.SurveyId == id)).ToList();


                    foreach (var org in OrganizationQuery)
                    {
                        selectedOrgs.Add(org.OrganizationId, org.Organization1);
                    }
                    ////  Available Orgnization list

                    IQueryable <Organization> OrganizationList = Context.Organizations.ToList().AsQueryable();
                    foreach (var Org in OrganizationList)
                    {
                        if (!selectedOrgs.ContainsValue(Org.Organization1) && Org.IsEnabled == true)
                        {
                            availableOrgs.Add(Org.OrganizationId, Org.Organization1);
                        }
                    }
                    //// Select DataAccess Rule Ids  list
                    var MetaData = Context.SurveyMetaDatas.Where(a => a.SurveyId == id).Single();


                    selectedDataAccessRuleId = int.Parse(MetaData.DataAccessRuleId.ToString());
                    ////  Available DataAccess Rule Ids  list

                    IQueryable <DataAccessRule> RuleIDs = Context.DataAccessRules.ToList().AsQueryable();
                    foreach (var Rule in RuleIDs)
                    {
                        dataAccessRuleIds.Add(Rule.RuleId, Rule.RuleName);
                        dataAccessRuleDescription.Add(Rule.RuleName, Rule.RuleDescription);
                    }
                    formSettingBO.ColumnNameList            = columnNameList;
                    formSettingBO.UserList                  = availableUsers;
                    formSettingBO.AssignedUserList          = selectedUsers;
                    formSettingBO.AvailableOrgList          = availableOrgs;
                    formSettingBO.SelectedOrgList           = selectedOrgs;
                    formSettingBO.DataAccessRuleIds         = dataAccessRuleIds;
                    formSettingBO.SelectedDataAccessRule    = selectedDataAccessRuleId;
                    formSettingBO.DataAccessRuleDescription = dataAccessRuleDescription;
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return(formSettingBO);
        }
        public FormInfoBO GetFormByFormId(string FormId, bool GetXml, int UserId)
        {
            FormInfoBO FormInfoBO = new FormInfoBO();

            try
            {
                Guid Id = new Guid(FormId);

                using (var Context = DataObjectFactory.CreateContext())
                {
                    var items = from FormInfo in Context.SurveyMetaDatas
                                join UserInfo in Context.Users
                                on FormInfo.OwnerId equals UserInfo.UserID
                                into temp
                                from UserInfo in temp.DefaultIfEmpty()
                                where FormInfo.SurveyId == Id
                                select new { FormInfo, UserInfo };
                    SurveyMetaData Response = Context.SurveyMetaDatas.First(x => x.SurveyId == Id);
                    var            _Org     = new HashSet <int>(Response.Organizations.Select(x => x.OrganizationId));
                    var            Orgs     = Context.Organizations.Where(t => _Org.Contains(t.OrganizationId)).ToList();



                    bool IsShared = false;

                    foreach (var org in Orgs)
                    {
                        var UserInfo = Context.UserOrganizations.Where(x => x.OrganizationID == org.OrganizationId && x.UserID == UserId && x.RoleId == 2);
                        if (UserInfo.Count() > 0)
                        {
                            IsShared = true;
                            break;
                        }
                    }



                    foreach (var item in items)
                    {
                        FormInfoBO          = Mapper.MapToFormInfoBO(item.FormInfo, item.UserInfo, GetXml);
                        FormInfoBO.IsShared = IsShared;

                        if (item.UserInfo.UserID == UserId)
                        {
                            FormInfoBO.IsOwner = true;
                        }
                        else
                        {
                            FormInfoBO.IsOwner = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }



            return(FormInfoBO);
        }
        public void UpDateSettingsList(FormSettingBO FormSettingBO, string FormId, int CurrentOrg = -1)
        {
            Guid Id = new Guid(FormId);

            try
            {
                using (var Context = DataObjectFactory.CreateContext())
                {
                    SurveyMetaData Response = Context.SurveyMetaDatas.First(x => x.SurveyId == Id);
                    //Remove old Users

                    var _User = new HashSet <string>(Response.Users.Select(x => x.UserName));
                    var Users = Context.Users.Where(t => _User.Contains(t.UserName)).ToList();

                    foreach (User user in Users)
                    {
                        if (user.UserOrganizations.Where(x => x.OrganizationID == CurrentOrg).Count() > 0)
                        {
                            Response.Users.Remove(user);
                        }
                    }
                    Context.SaveChanges();



                    //insert new users
                    foreach (var item in FormSettingBO.AssignedUserList)
                    {
                        User User = Context.Users.FirstOrDefault(x => x.UserName == item.Value);
                        Response.Users.Add(User);
                    }
                    Context.SaveChanges();



                    //Remove old Orgs

                    var _Org = new HashSet <int>(Response.Organizations.Select(x => x.OrganizationId));
                    var Orgs = Context.Organizations.Where(t => _Org.Contains(t.OrganizationId)).ToList();

                    foreach (Organization org in Orgs)
                    {
                        Response.Organizations.Remove(org);
                    }
                    Context.SaveChanges();



                    //insert new Orgs
                    List <User> OrgAdmis = new List <User>();

                    foreach (var item in FormSettingBO.SelectedOrgList)
                    {
                        int          OrgId = int.Parse(item.Value);
                        Organization Org   = Context.Organizations.FirstOrDefault(x => x.OrganizationId == OrgId);
                        Response.Organizations.Add(Org);
                    }
                    Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }