/// <summary>
        /// Save the modified or newly added roles into the database
        /// </summary>
        /// <param action="Data">List of roles to be saved</param>
        /// <returns></returns>
        public int Save(Roles Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Role efRole = null;
            foreach (TopContractsDAL10.SystemTables.Role Role in Data.Entries)
            {
                if (Role.New)
                {
                    efRole = new TopContractsEntities.Role();
                }
                else
                {
                    efRole = context.Roles.Where(c => c.RoleID == Role.ID).SingleOrDefault();
                }

                if (efRole != null)
                {
                    if (Role.CanEdit == true && efRole.Users.Any(u => u.Unrestricted == false))
                    {
                        throw new ExceptionSecurityAdminSaveRoleAssignedToRestrictedUser();
                    }
                }

                if (Role.Deleted == false)
                {
                    efRole.InitCommonFields(efRole, Role, efRole.RolesLNGs, this.organizationIdentifier);
                    efRole.AddActivities = Role.AddActivities;
                    efRole.AddDocs = Role.AddDocs;
                    efRole.AddTodos = Role.AddTodos;
                    efRole.DeleteActivities = Role.DeleteActivities;
                    efRole.DeleteDocs = Role.DeleteDocs;
                    efRole.DeleteTodos = Role.DeleteTodos;
                    efRole.DisplayOrder = Role.DisplayOrder;
                    efRole.EditActivities = Role.EditActivities;
                    efRole.EditApps = Role.EditApps;
                    efRole.EditAuth = Role.EditAuth;
                    efRole.EditDocs = Role.EditDocs;
                    efRole.EditProperties = Role.EditProperties;
                    efRole.EditTodos = Role.EditTodos;
                    efRole.ModifyActivitiesOfOthers = Role.ModifyActivitiesOfOthers;
                    efRole.ModifyDocsOfOthers = Role.ModifyDocsOfOthers;
                    efRole.ModifyTodosOfOthers = Role.ModifyTodosOfOthers;
                    efRole.ViewActivities = Role.ViewActivities;
                    efRole.ViewActivitiesOfOthers = Role.ViewActivitiesOfOthers;
                    efRole.ViewApps = Role.ViewApps;
                    efRole.ViewAuth = Role.ViewAuth;
                    efRole.ViewDocs = Role.ViewDocs;
                    efRole.ViewDocsOfOthers = Role.ViewDocsOfOthers;

                    efRole.ViewProperties = true;   // Role.ViewProperties;     //HardCoded value true entered for roles in every condition on 6-Mar-2013

                    efRole.ViewTodos = Role.ViewTodos;
                    efRole.ViewTodosOfOthers = Role.ViewTodosOfOthers;
                    efRole.EditFields = Role.EditFields;    // Added by Viplav on 7 Mar 2013 for implementing limited roles functionality
                    //******************************************************************
                    // New Properties Add for Image Gallery on 23-April-2013 by Viplav
                    //******************************************************************
                    efRole.ViewGallery = Role.ViewGallery;
                    efRole.EditGallery = Role.EditGallery;
                    efRole.AddGallery = Role.AddGallery;
                    efRole.DeleteGallery = Role.DeleteGallery;
                    efRole.ViewGalleryOfOthers = Role.ViewGalleryOfOthers;
                    efRole.ModifyGalleryOfOthers = Role.ModifyGalleryOfOthers;

                }
                if (Role.New)
                    context.Roles.Add(efRole);
                else
                {
                    if (Role.Deleted && efRole != null)
                    {
                        //******************Condition Implemented for delete entry from FieldGroupsRolesMAP by Viplav on 15-Oct-2013********//
                        if (context.FieldGroupsRolesMAPs.Any(ent => ent.RoleID == efRole.RoleID))
                        {
                            List<FieldGroupsRolesMAP> FieldGroupsRoleMap = context.FieldGroupsRolesMAPs.Where(t => t.RoleID == efRole.RoleID).ToList();
                            foreach (FieldGroupsRolesMAP item in FieldGroupsRoleMap)
                                context.FieldGroupsRolesMAPs.Remove(item);
                        }
                        //********************************************************************************************************************
                        efRole.DeleteLanguageEntries(efRole, context.RolesLNGs, efRole.RolesLNGs);
                        context.Roles.Remove(efRole);
                    }
                }
            }
            //int Roweffected = context.SaveChanges();
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            objectContext.DetectChanges();

            int Roweffected = objectContext.SaveChanges(SaveOptions.None);

            bool structuralChangeOccurred = false;
            long nonStructuralChanges = 0;

            foreach (var changedEntry in context.ChangeTracker.Entries())
            {
                if (changedEntry.Entity is TopContractsEntities.Role && changedEntry.State == System.Data.EntityState.Modified)
                {
                    structuralChangeOccurred = IsRolePermissionsChanged(changedEntry);
                    if (structuralChangeOccurred == false)
                        nonStructuralChanges += 1;
                }
                else if (changedEntry.Entity is TopContractsEntities.Role && changedEntry.State == System.Data.EntityState.Added)
                    nonStructuralChanges += 1;
                else if (changedEntry.Entity is TopContractsEntities.Role && changedEntry.State == System.Data.EntityState.Deleted)
                    nonStructuralChanges += 1;
                else if (changedEntry.Entity is TopContractsEntities.RolesLNG && changedEntry.State == System.Data.EntityState.Modified)
                    nonStructuralChanges += 1;
                //else if (changedEntry.Entity is TopContractsEntities.RolesLNG && changedEntry.State == System.Data.EntityState.Modified)
                //    nonStructuralChanges += 1;
            }

            objectContext.AcceptAllChanges();

            // We use a default value 1 for check that only a single limited role is exist in the role table
            if ((context.Roles.Count(rol => rol.EditFields == true) == 1) && context.FieldGroups.Any(fldgrp => fldgrp.RolesVisibility == (byte)FieldGroupRoleVisibility.VisibleToAll))
                SaveFieldGroupsRolesMAPforLimitedUser();

            ApplicationCachedData.reInitCacheData(); // To re-initialize server side cache.

            if (structuralChangeOccurred)
                ApplicationCachedData.UpdateContractStructureCacheTicks();
            else
                if (nonStructuralChanges > 0)
                    ApplicationCachedData.UpdateContractDisplayCacheTicks();

            return Roweffected;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets names of contracts by using IDs of the contracts
        /// </summary>
        /// <param name="ContractIDs">IDs of the contracts.</param>
        /// <returns>List of TopContractsDAL10.ContractInfo objects containing names and Ids of the contracts.</returns>
        public List<ContractInfo> GetContractsInfo(List<long> ContractIDs)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            List<ContractInfo> contractsInfo = new List<ContractInfo>();

            contractsInfo.AddRange(context.Contracts.Where(c => ContractIDs.Contains(c.ContractID)).Select(c => new ContractInfo { ContractID = c.ContractID, Name = c.Name }));

            return contractsInfo;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets page size value from the AppSettings table in the database.
        /// </summary>
        /// <param name="OrganizationIdentifier">ID of the Orgainzation whose paging size is required.</param>
        /// <param name="contractSection">Contract section whose paging size is required.</param>
        /// <returns>An integer value. Can also be null.</returns>
        public long? GetPagingValueFromDB(Guid OrganizationIdentifier, ContractSections contractSection)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            long? PageSize = null;
            if (context.AppSettings.Any(org => org.OrganizationIdentifier == OrganizationIdentifier))
            {
                AppSetting efAppSettings = context.AppSettings.SingleOrDefault(org => org.OrganizationIdentifier == OrganizationIdentifier);

                switch (contractSection)
                {
                    case ContractSections.AuthorizedEntities:
                        PageSize = efAppSettings.MaxPageSizeUsers;
                        break;
                    case ContractSections.Documents:
                        PageSize = efAppSettings.MaxPageSizeDocs;
                        break;
                    case ContractSections.Events:
                        PageSize = efAppSettings.MaxPageSizeEvents;
                        break;
                    case ContractSections.Gallery:
                        PageSize = efAppSettings.MaxPageSizeGalleries;
                        break;
                    default:
                        break;
                }
            }
            return PageSize;
        }
Exemplo n.º 4
0
 public static void UpdateDocFileName(long docId, string FileName)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     TopContractsEntities.ContractDoc efDoc = context.ContractDocs.SingleOrDefault(cd => cd.DocumentID == docId);
     if (efDoc != null)
         efDoc.FileName = FileName;
     context.SaveChanges();
 }
Exemplo n.º 5
0
        /// <summary>
        /// Gets all Properties of a contract.
        /// </summary>
        /// <param name="CultureIdentifier">Culture identifier to be used to retrieve users information</param>
        /// <returns>List of TopContractsDAL10.ContractUser objects.</returns>
        public Dictionary<long, ContractProperties> GetAllContractProperties(string CultureIdentifier)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            Dictionary<long, ContractProperties> contractProperties = new Dictionary<long, ContractProperties>();

            foreach (TopContractsEntities.Contract contract in context.Contracts)
            {
                ContractProperties cntprop = new ContractProperties();
                cntprop.initDataFields(contract);
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Data Fields initialized");
                cntprop.initPrivateFields(contract, CultureIdentifier);
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Private Fields initialized");
                contractProperties.Add(contract.ContractID, cntprop);
            }
            return contractProperties;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Used to check if the ContractID passed is a contract and not an entity.
 /// </summary>
 /// <param action="ContractID">Contract ID that has to be checked if it is a contract.</param>
 /// <returns>Boolean value used to determine if the Contract ID is a contract and not an entity.</returns>
 public static bool IsContract(long ContractID)
 {
     Logger.WriteGeneralVerbose("Contract class - IsContract", "checking if the contract id {0} passed is a contract and not an entity", ContractID);
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
     long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept
     if (context.Contracts.Any(c => c.ContractID == ContractID && c.ContractType.ParentContractTypeID == ContractTypeContractsID))
         return true;
     else
         return false;
 }
Exemplo n.º 7
0
 public static void RemoveContractDocsDividerIfAny()
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     if (context.ContractDocs.Any(f => f.ExternalID.Contains(Constants.entityFieldsDivider)))
     {
         foreach (TopContractsEntities.ContractDoc cDoc in context.ContractDocs.Where(f => f.ExternalID.Contains(Constants.entityFieldsDivider)).ToList())
         {
             cDoc.ExternalID = cDoc.ExternalID.Substring(0, cDoc.ExternalID.IndexOf(Constants.entityFieldsDivider));
         }
     }
     context.SaveChanges();
 }
Exemplo n.º 8
0
 /// <summary>
 /// Does contract exist with this ID?
 /// </summary>
 /// <param action="ContractID">Contract ID</param>
 /// <returns>True or false</returns>
 public static bool ContractExists(long ContractID)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     return (context.Contracts.Where(c => c.ContractID == ContractID).SingleOrDefault() != null);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Copy Contract Field (Copy Contracts Fields according to Contract Type)
        /// </summary>
        /// <param action="parentContractID">Id of parentContractID</param>
        /// <param action="childContractID">Id of childContractID</param>
        /// <param action="parentContractTypeID">Id of parentContractTypeID</param>
        /// <param action="childContractTypeID">Id of childContractTypeID</param>
        /// <param action="CultureIdentifier">CultureIdentifier</param>        
        /// <returns></returns>
        public static void CopyContractField(long parentContractID, long childContractID, int parentContractTypeID, int childContractTypeID, string CultureIdentifier)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            if (parentContractTypeID == childContractTypeID)
            {
                foreach (var childContractField in context.ContractFields.Where(cFld => cFld.ContractID == childContractID).ToList())
                {
                    context.ContractFields.Remove(childContractField);
                }

                foreach (var parentContractField in context.ContractFields.Where(cFld => cFld.ContractID == parentContractID).ToList())
                {
                    TopContractsEntities.ContractField contractField = new TopContractsEntities.ContractField();
                    contractField = parentContractField;
                    contractField.ContractID = childContractID;

                    if (parentContractField.LinkedDocumentID != null && parentContractField.LinkedDocumentID > 0)
                    {
                        string docIdToSearch = (Constants.entityFieldsDivider + parentContractField.LinkedDocumentID + Constants.entityFieldsDivider);
                        if (context.ContractDocs.Any(cd => cd.ExternalID.Contains(docIdToSearch)) && context.ContractDocs.Count(cd => cd.ExternalID.Contains(docIdToSearch)) == 1)
                        {
                            long ContractDocID = context.ContractDocs.Single(cDoc => cDoc.ExternalID.Contains(docIdToSearch)).DocumentID;
                            contractField.LinkedDocumentID = ContractDocID;
                        }
                    }

                    context.ContractFields.Add(contractField);
                }
            }
            else
            {
                List<int> commonFieldGroupIDs = getCommonContractFieldGroup(parentContractTypeID, childContractTypeID, CultureIdentifier);
                foreach (int cfg in commonFieldGroupIDs)
                {
                    foreach (var childContractField in context.ContractFields.Where(cFld => cFld.ContractID == childContractID && cFld.FieldGroupID == cfg).ToList())
                    {
                        context.ContractFields.Remove(childContractField);
                    }

                    foreach (var parentContractField in context.ContractFields.Where(cFld => cFld.ContractID == parentContractID && cFld.FieldGroupID == cfg).ToList())
                    {
                        TopContractsEntities.ContractField contractField = new TopContractsEntities.ContractField();
                        contractField = parentContractField;
                        contractField.ContractID = childContractID;

                        if (parentContractField.LinkedDocumentID != null && parentContractField.LinkedDocumentID > 0)
                        {
                            string docIdToSearch = (Constants.entityFieldsDivider + parentContractField.LinkedDocumentID + Constants.entityFieldsDivider);
                            if (context.ContractDocs.Any(cd => cd.ExternalID.Contains(docIdToSearch)) && context.ContractDocs.Count(cd => cd.ExternalID.Contains(docIdToSearch)) == 1)
                            {
                                long ContractDocID = context.ContractDocs.Single(cDoc => cDoc.ExternalID.Contains(docIdToSearch)).DocumentID;
                                contractField.LinkedDocumentID = ContractDocID;
                            }
                        }

                        context.ContractFields.Add(contractField);
                    }
                }
            }

            context.SaveChanges();
        }
Exemplo n.º 10
0
 private void updateEfContract(ref TopContractsEntities.Contract efContract, ref TopContractsV01Entities context)
 {
     if (this.Properties != null)
         this.Properties.updateEfFields(ref efContract);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractActivity of {0}", this.ID);
     //Logger.Write("updating ContractActivity of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractActivities != null)
         foreach (ContractActivity activity in this.ContractActivities.Where(ent => (ent.Deleted && ent.New) != true))
             activity.UpdateEntity(ref context, efContract.ContractActivities, context.ContractActivities, this.ID, this.Properties.OrganizationIdentifier);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractApplications of {0}", this.ID);
     //Logger.Write("updating ContractApplications of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractApplications != null)
         foreach (ContractApplication application in this.ContractApplications.Where(ent => (ent.Deleted && ent.New) != true))
             application.UpdateEntity(ref context, efContract.ContractApplications, context.ContractApplications, this.ID, this.Properties.OrganizationIdentifier);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractDocs of {0}", this.ID);
     //Logger.Write("updating ContractDocs of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractDocs != null)
         foreach (ContractDoc doc in this.ContractDocs.Where(ent => (ent.Deleted && ent.New) != true))
             doc.UpdateEntity(ref context, efContract.ContractDocs, context.ContractDocs, this.ID, this.Properties.OrganizationIdentifier);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractGalleries of {0}", this.ID);
     if (this.ContractGalleries != null)
         foreach (ContractGallery gallery in this.ContractGalleries.Where(ent => (ent.Deleted && ent.New) != true))
             gallery.UpdateEntity(ref context, efContract.ContractGalleries, context.ContractGalleries, this.ID, this.Properties.OrganizationIdentifier);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractFieldGroups of {0}", this.ID);
     //Logger.Write("updating ContractFieldGroups of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractFieldGroups != null)
         foreach (ContractFieldGroup FieldGroup in this.ContractFieldGroups.Where(ent => (ent.Deleted && ent.New) != true))
             FieldGroup.UpdateEntity(ref context, efContract.ContractFields, null, ID, this.Properties.OrganizationIdentifier);
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractTodos of {0}", this.ID);
     //Logger.Write("updating ContractTodos of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractTodos != null)
         foreach (ContractTodo Todo in this.ContractTodos.Where(ent => (ent.Deleted && ent.New) != true))
             //Todo.UpdateEntity(ref context, efContract.ContractTodos, efContract.ContractActivities.First().ContractTodos, this.ID);
             Todo.UpdateEntity(ref context, efContract.ContractTodos, context.ContractTodos, this.ID, this.Properties.OrganizationIdentifier);
     //var xxxx = efContract.ContractActivities.FirstOrDefault().ContractTodos;
     //var yyyy = context.ContractTodos;
     Logger.WriteGeneralVerbose("Contract class - updateEfContract", "updating ContractUsers of {0}", this.ID);
     //Logger.Write("updating ContractUsers of " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
     if (this.ContractUsers != null)
         foreach (ContractUser User in this.ContractUsers.Where(ent => (ent.Deleted && ent.New) != true))
             User.UpdateEntity(ref context, efContract.ContractUsers, context.ContractUsers, this.ID, this.Properties.OrganizationIdentifier);
 }
Exemplo n.º 11
0
        /// <summary>
        /// This constructor is ONLY used for creating new contracts, not for getting information about existing contracts.
        /// </summary>
        /// <param action="ContractName">The action for the new contract being created</param>
        /// <param action="ContractTypeID">The type for the new contract being created</param>
        /// <param action="ContractStatusID">The status for the new contract being created</param>
        /// <param action="UserIdCreator">The ID of the user creating the contract</param>
        /// <param action="CultureIdentifier">Culture Identifier used for the selection of Fields and Field-Groups 
        /// names in the appropriate language, in the process of creating those for the contract. The language
        /// is not needed for the creation, so much as it is needed for the population of data in the newly created contract</param>
        public Contract(Guid OrganizationIdentifier, string ContractName, long ContractTypeID, long? ContractStatusID, long UserIdCreator, string CultureIdentifier, long RoleID = 0)
        {
            //int RoleIdCreator = (int)SpecialRoles.OwnerRoleID; //Always the "owner" is the one creating the contracts...
            long RoleIdCreator = RoleID; //RoleID is a default userid used for contractuser table
            try
            {
                Deleted = false;
                Properties = new ContractProperties();
                initLists();

                New = true;
                Properties.Name = ContractName;
                Properties.ContractTypeID = ContractTypeID;
                Properties.StatusID = (int?)ContractStatusID;
                Properties.OrganizationIdentifier = OrganizationIdentifier;

                ContractUsers.Add(new ContractUser() { New = true, EntryId = UserIdCreator, RoleID = RoleIdCreator });

                TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
                //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
                long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept
                TopContractsDAL10.SystemTables.FieldGroups fieldGroups = new SystemTables.FieldGroups();
                if (context.ContractTypes.Any(ct => ct.ParentContractTypeID == ContractTypeContractsID && ct.ContractTypeID == ContractTypeID))
                    fieldGroups = SystemTables.FieldGroups.Get(false, false, true, CultureIdentifier);
                else
                    fieldGroups = SystemTables.FieldGroups.GetWithoutParentContractTypeID(false, false, true, CultureIdentifier);

                string logData1 = "FieldGroups created: " + Environment.NewLine;
                foreach (TopContractsDAL10.SystemTables.FieldGroup fGrp in fieldGroups.Entries)
                {
                    logData1 = logData1 + string.Format("FieldGroup info: groupID-{0}", fGrp.ID) + Environment.NewLine;
                }
                Logger.WriteGeneralVerbose("Contract class - CTOR", logData1);

                Logger.WriteGeneralVerbose("Contract class - CTOR", "Creating FieldGroups collection");
                foreach (TopContractsDAL10.SystemTables.FieldGroup fieldGroup in fieldGroups.Entries)
                {
                    //if (fieldGroup.ContractTypeIDsVisible.Any(ct => ct == ContractTypeID)) //Boaz - 7-Aug-2012
                    if (context.ContractTypes.Any(ct => ct.ParentContractTypeID == ContractTypeContractsID && ct.ContractTypeID == ContractTypeID))
                    {
                        if (fieldGroup.ContractTypeIDsVisible.Any(ct => ct == ContractTypeID) || fieldGroup.VisibleToAllContractTypes)
                            ContractFieldGroups.Add(new ContractFieldGroup(fieldGroup, CultureIdentifier));
                    }
                    else
                    {
                        if (fieldGroup.ContractTypeIDsVisible.Any(ct => ct == ContractTypeID))
                            ContractFieldGroups.Add(new ContractFieldGroup(fieldGroup, CultureIdentifier));
                    }
                }
                string logData2 = "ContractFieldGroups created: " + Environment.NewLine;
                foreach (ContractFieldGroup cFldGrp in ContractFieldGroups)
                {
                    logData2 = logData2 + string.Format("FieldGroup info: groupID-{0}", cFldGrp.EntryId) + Environment.NewLine;
                }
                Logger.WriteGeneralVerbose("Contract class - CTOR", logData2);
            }
            catch (Exception ex)
            {
                Logger.WriteExceptionError("Contract class - Contract", ex);
                ErrorDTO Error = new ErrorDTO(new ExceptionUnknownContractInit(ex));
            }
        }
Exemplo n.º 12
0
        public long SaveEntityRecord(int UpdatingUserID, bool AuditChanges)
        {
            Logger.Write(LogCategory.General, (this.New ? "Creating new " : (this.Deleted ? "Deleting " : "Updating ")) + "contract", "contract ID " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
            DateTime updateDate = DateTime.Now;
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Contract efContract = null;
            if (this.New)
            {
                efContract = new TopContractsEntities.Contract();
                efContract.ContractUpdateDetail = new ContractUpdateDetail();

                if (context.FieldGroups.Count(grp => grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && grp.FieldGroupsContractTypesMAPs.Any(mp => mp.ContractTypeID == this.Properties.ContractTypeID)) > 0)
                    foreach (FieldGroup grp in context.FieldGroups.Where(grp => grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && grp.Inactive == false && grp.FieldGroupsContractTypesMAPs.Any(mp => mp.ContractTypeID == this.Properties.ContractTypeID)))//Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fieldsgroup
                        foreach (Field fld in grp.Fields.Where(grpf => grpf.Inactive == false)) //Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fields
                        {
                            efContract.ContractFields.Add(new TopContractsEntities.ContractField() { OrganizationIdentifier = this.Properties.OrganizationIdentifier, FieldGroupID = grp.EntryIdentifier, FieldID = fld.EntryIdentifier, FieldValue = (fld.FieldType == (int)FieldTypes.ListSingle && fld.UseFirstAsDefault == true ? fld.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                        }
            }
            else
            {
                efContract = context.Contracts.Where(u => u.ContractID == this.ID).SingleOrDefault();
            }

            if (this.Deleted)
            {
                #region DELETING RELATED ENTITIES -----------------------------------------------------------------------------

                //For some unknown reason, eliminating the if-count>0 from before each of the foreach(s), throws an "object not set" exception even though alerts exist!
                context.ContractUpdateDetails.Remove(context.ContractUpdateDetails.Single(entry => entry.ContractID == this.ID));

                if (context.ContractFields.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractField efContractField in context.ContractFields.Where(entry => entry.ContractID == this.ID))
                        context.ContractFields.Remove(efContractField);

                foreach (TopContractsEntities.ContractUser efContractUser in context.ContractUsers.Where(entry => entry.ContractID == this.ID))
                    context.ContractUsers.Remove(efContractUser);

                #endregion
                context.Contracts.Remove(efContract);

            }
            if (this.New)
            {
                updateEfContract(ref efContract, ref context);
                context.Contracts.Add(efContract);
            }
            if (this.New == false & this.Deleted == false)
            {
                updateEfContract(ref efContract, ref context);
            }

            //Boaz-1 (8-Aug-2012) ------------------------------------------------------
            //All Todos which are linked to new events, should be made their "children" in the EF so that the foreign
            //key values will be  automatically generated when saving to the DB
            foreach (TopContractsEntities.ContractTodo todo in efContract.ContractTodos.Where(td => td.ActivityID < 0))
            {
                efContract.ContractActivities.SingleOrDefault(ca => ca.ActivityID == todo.ActivityID).ContractTodos.Add(todo);
            }

            bool updateContractUpdateDetails = false;
            if (this.Deleted == false)
            {
                //If any of the entities related to the contract has changed, the Update-Date of the contract record itself should be updated...
                foreach (var entry in context.ChangeTracker.Entries())
                {
                    //Boaz 7-Aug-2012
                    //if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified && !(entry.Entity is EFContractRelatedEntryWithUpdate))
                    if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified || entry.State == System.Data.EntityState.Deleted)
                    {
                        updateContractUpdateDetails = true;
                    }
                }

                if (updateContractUpdateDetails)
                    foreach (var entry in context.ChangeTracker.Entries())
                    {
                        //Boaz 7-Aug-2012
                        //If any changed was made to a record in any table (entities related to the contract) which has
                        //UpdateDate and UpdateUserID fields, these fields are updated to reflect the change.
                        //These fields are NOT handled by the individual classes committing the changes to the
                        //database - they are handled centrally here to cut-down on the code
                        if (entry.Entity is EFContractRelatedEntryWithUpdate && entry.State != EntityState.Unchanged)
                        //if (entry.Entity is EFContractRelatedEntryWithUpdate)
                        {
                            ((EFContractRelatedEntryWithUpdate)entry.Entity).UpdatingDate = updateDate;
                            ((EFContractRelatedEntryWithUpdate)entry.Entity).UpdatingUserID = UpdatingUserID;
                        }
                    }
            }
            if (updateContractUpdateDetails)
            {
                efContract.ContractUpdateDetail.UpdateDate = updateDate;
                efContract.ContractUpdateDetail.UpdateUserID = UpdatingUserID;
            }
            //We want to keep tracking the changes after calling SaveChanges, for the purpose of auditing
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            objectContext.DetectChanges();
            try
            {
                //Using SaveOptions.None to keep change-tracking... Requires objectContext.DetectChanges() before and objectContext.AcceptAllChanges() after...
                int rowsAffected = objectContext.SaveChanges(SaveOptions.None);
            }
            catch (System.Data.UpdateException updEx)
            {
                if (updEx.InnerException != null)
                    if (updEx.InnerException.Message.Contains("DELETE statement conflicted with the REFERENCE constraint") &&
                        updEx.InnerException.Message.Contains("FK_ContractTodos_ContractActivities"))
                        //{
                        //    Logger.Write(updEx, System.Diagnostics.TraceEventType.Warning);
                        throw new ExceptionDataContractSaveDeleteEventWithAlert();
                    //}
                    else if (updEx.InnerException.Message.Contains("FK_Contracts_Contracts") &&
                        updEx.InnerException.Message.Contains("ParentContractID"))
                        throw new ExceptionDataContractDeleteDeleteContractWithChild();
                    else
                        //{
                        //  Logger.Write(updEx, System.Diagnostics.TraceEventType.Error);
                        throw updEx;
                //}
            }

            if (AuditChanges)
            {
                HistManager histManager = new HistManager();
                int recordsWrittenToHistory = histManager.AuditChanges(updateDate, UpdatingUserID, this.New, this.Deleted, context.ChangeTracker.Entries().Where(e => e.State != System.Data.EntityState.Unchanged || e.Entity is TopContractsEntities.Contract));
            }

            objectContext.AcceptAllChanges();

            long contractIdSaved = -1; //will indicate failure, if no number is set
            foreach (var entry in context.ChangeTracker.Entries())
            {
                Logger.Write("entering the loop - for context.ChangeTracker.Entries()", System.Diagnostics.TraceEventType.Verbose);
                if (entry.Entity is TopContractsEntities.Contract)
                {
                    contractIdSaved = ((TopContractsEntities.Contract)entry.Entity).ContractID;
                }
                if (entry.Entity is TopContractsEntities.ContractDoc)
                {
                    TopContractsEntities.ContractDoc efDoc = entry.Entity as TopContractsEntities.ContractDoc;
                    TopContractsDAL10.ContractDoc doc = this.ContractDocs.SingleOrDefault(d => d.FileName == efDoc.FileName);
                    if (doc != null)
                    {
                        if (doc.New)
                        {
                            doc.EntryId = efDoc.DocumentID; //This is done especially for the process of file copying...
                        }
                    }
                }
            }

            if (this.Deleted)
                return 0;
            return contractIdSaved;
        }
        /// <summary>
        /// Save the field groups and its fields into the database.
        /// </summary>
        /// <param action="Data">Collection of field groups which are getting saved.</param>
        /// <param action="AuditChanges">A boolean value equal to true, if application license allows audit changes.</param>
        /// <param action="UpdatingUserID">Id of the user who is updating the field groups.</param>
        /// <param action="CascadeDelete">Boolean value if cascade deletion is bieng done.</param>
        /// <returns>Returns an integer value for the total number of records being updated.</returns>
        //public static int SaveFields(FieldGroups Data, bool AuditChanges, long UpdatingUserID, bool CascadeDelete)
        public int SaveFields(FieldGroups Data, bool AuditChanges, long UpdatingUserID, bool CascadeDelete, ref Dictionary<int, long> EntityFieldIDsMap, bool isCatalogField, bool populateMapDictionary = false)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.FieldGroup efFieldGroup = null;
            Dictionary<long, long> checkMax = new Dictionary<long, long>();
            TopContractsEntities.Field efField = null;
            TopContractsEntities.FieldListItem efFieldListItem = null;
            foreach (TopContractsDAL10.SystemTables.FieldGroup fieldGroup in Data.Entries)
            {
                if (fieldGroup.New)
                {
                    efFieldGroup = new TopContractsEntities.FieldGroup();
                }
                else
                {
                    efFieldGroup = context.FieldGroups.Where(c => c.FieldGroupID == fieldGroup.ID).SingleOrDefault();
                }

                if (fieldGroup.Deleted == false)
                {
                    efFieldGroup.InitCommonFields(efFieldGroup, fieldGroup, efFieldGroup.FieldGroupsLNGs, this.organizationIdentifier);
                    efFieldGroup.DisplayIndependent = fieldGroup.DisplayIndependent;

                    if (fieldGroup.VisibleToAllContractTypes)
                    {
                        efFieldGroup.ContractTypeVisibility = (byte)FieldGroupContractTypeVisibility.VisibleToAll;
                        if (efFieldGroup.FieldGroupsContractTypesMAPs != null)
                        {
                            for (int i = efFieldGroup.FieldGroupsContractTypesMAPs.Count - 1; i >= 0; i--)
                            {
                                FieldGroupsContractTypesMAP map = efFieldGroup.FieldGroupsContractTypesMAPs.ElementAt(i);
                                efFieldGroup.FieldGroupsContractTypesMAPs.Remove(map);
                                context.FieldGroupsContractTypesMAPs.Remove(map);
                            }
                        }
                    }
                    else
                    {   //TODO - add implementation for FieldGroupContractTypeVisibility.HiddenFromTypes
                        efFieldGroup.ContractTypeVisibility = (byte)FieldGroupContractTypeVisibility.VisibleToTypes;
                        if (efFieldGroup.FieldGroupsContractTypesMAPs != null)
                        {
                            //Removing those who are missing from the received list
                            for (int i = efFieldGroup.FieldGroupsContractTypesMAPs.Count - 1; i >= 0; i--)
                            {
                                FieldGroupsContractTypesMAP map = efFieldGroup.FieldGroupsContractTypesMAPs.ElementAt(i);
                                if (fieldGroup.ContractTypeIDsVisible != null)
                                {
                                    if (fieldGroup.ContractTypeIDsVisible.Any(v => v == map.ContractTypeID) == false)
                                    {
                                        if (CascadeDelete)
                                        {
                                            foreach (TopContractsEntities.Contract contract in map.ContractType.Contracts)
                                            {
                                                foreach (TopContractsEntities.Field fld in efFieldGroup.Fields)
                                                {
                                                    //Changed by Boaz 16-05-2013: field-id is not used properly in case of catalog-fields - it should be replaced with the "fld.ContractType.SelectorFieldID" for the deletion purpose
                                                    long fieldID = getFieldIdForContractFields(fld);
                                                    if (fld.FieldType == (long)FieldTypes.EntityLink)
                                                    {
                                                        if (contract.ContractFields.Any(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID && cf.CatalogFieldID == fieldID))
                                                        {
                                                            foreach (long recordCounter in contract.ContractFields.Where(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID && cf.CatalogFieldID == fieldID).Select(u => u.RecordCounter).ToList())
                                                            {
                                                                context.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID && cf.CatalogFieldID == fieldID && cf.RecordCounter == recordCounter));
                                                            }
                                                        }
                                                        //context.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID && cf.CatalogFieldID == fieldID));
                                                    }
                                                    else
                                                    {
                                                        if (contract.ContractFields.Any(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID))
                                                        {
                                                            foreach (long recordCounter in contract.ContractFields.Where(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fld.FieldID).Select(u => u.RecordCounter).ToList())
                                                            {
                                                                context.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fieldID && cf.RecordCounter == recordCounter));
                                                            }
                                                        }
                                                        //context.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldGroupID == fld.FieldGroupID && cf.FieldID == fieldID ));
                                                    }
                                                }
                                            }
                                        }
                                        efFieldGroup.FieldGroupsContractTypesMAPs.Remove(map);
                                        context.FieldGroupsContractTypesMAPs.Remove(map);
                                    }
                                }
                                else
                                {
                                    if (CascadeDelete)
                                    {
                                        foreach (TopContractsEntities.Contract contract in map.ContractType.Contracts)
                                        {
                                            for (int j = contract.ContractFields.Count - 1; j >= 0; j--)
                                            {
                                                contract.ContractFields.Remove(contract.ContractFields.ElementAt(j));
                                            }
                                        }
                                    }
                                    efFieldGroup.FieldGroupsContractTypesMAPs.Remove(map);
                                    context.FieldGroupsContractTypesMAPs.Remove(map);
                                }
                            }

                        }
                        //Adding entries from the received list which don't exist yet
                        if (fieldGroup.ContractTypeIDsVisible != null)
                        {
                            foreach (int indx in fieldGroup.ContractTypeIDsVisible)
                                if (efFieldGroup.FieldGroupsContractTypesMAPs.Any(v => v.ContractTypeID == indx) == false)
                                    efFieldGroup.FieldGroupsContractTypesMAPs.Add(new FieldGroupsContractTypesMAP() { OrganizationIdentifier = this.organizationIdentifier, ContractTypeID = indx, FieldGroupID = (int)fieldGroup.ID });
                        }
                    }

                    if (fieldGroup.VisibleToAllRoles)
                    {
                        efFieldGroup.RolesVisibility = (byte)FieldGroupRoleVisibility.VisibleToAll;
                        if (efFieldGroup.FieldGroupsRolesMAPs != null)
                        {
                            for (int i = efFieldGroup.FieldGroupsRolesMAPs.Count - 1; i >= 0; i--)
                            {
                                FieldGroupsRolesMAP map = efFieldGroup.FieldGroupsRolesMAPs.ElementAt(i);
                                efFieldGroup.FieldGroupsRolesMAPs.Remove(map);
                                context.FieldGroupsRolesMAPs.Remove(map);
                            }
                        }
                    }
                    else
                    {   //TODO - add implementation for FieldGroupRoleVisibility.HiddenFromRoles
                        efFieldGroup.RolesVisibility = (byte)FieldGroupRoleVisibility.VisibleToRoles;
                        if (efFieldGroup.FieldGroupsRolesMAPs != null)
                        {
                            //Removing those who are missing from the received list
                            for (int i = efFieldGroup.FieldGroupsRolesMAPs.Count - 1; i >= 0; i--)
                            {
                                FieldGroupsRolesMAP map = efFieldGroup.FieldGroupsRolesMAPs.ElementAt(i);
                                if (fieldGroup.RoleIDsVisible != null)
                                {
                                    if (fieldGroup.RoleIDsVisible.Any(v => v == map.RoleID) == false)
                                    {
                                        efFieldGroup.FieldGroupsRolesMAPs.Remove(map);
                                        context.FieldGroupsRolesMAPs.Remove(map);
                                    }
                                }
                                else
                                {
                                    efFieldGroup.FieldGroupsRolesMAPs.Remove(map);
                                    context.FieldGroupsRolesMAPs.Remove(map);
                                }
                            }

                        }
                        //Adding entries from the received list which don't exist yet
                        if (fieldGroup.RoleIDsVisible != null)
                        {
                            foreach (int indx in fieldGroup.RoleIDsVisible)
                                if (efFieldGroup.FieldGroupsRolesMAPs.Any(v => v.RoleID == indx) == false)
                                    efFieldGroup.FieldGroupsRolesMAPs.Add(new FieldGroupsRolesMAP() { OrganizationIdentifier = this.organizationIdentifier, RoleID = indx, FieldGroupID = (int)fieldGroup.ID, AllowView = true, AllowEdit = fieldGroup.RoleIDsEditable.Any(x => x == indx) });
                                else
                                {
                                    efFieldGroup.FieldGroupsRolesMAPs.Single(v => v.RoleID == indx).AllowView = true;
                                    efFieldGroup.FieldGroupsRolesMAPs.Single(v => v.RoleID == indx).AllowEdit = fieldGroup.RoleIDsEditable.Any(x => x == indx);
                                }
                        }
                    }

                    if (fieldGroup.New == false)
                    {
                        // code to change record counters for the contracts fields (i.e from -999999 to 1) when a
                        // single record field group is converted into a multiple record field group.
                        if (context.FieldGroups.SingleOrDefault(fg => fg.FieldGroupID == efFieldGroup.FieldGroupID).SingleRecord == true && fieldGroup.SingleRecord == false)
                        {
                            List<TopContractsEntities.ContractField> contractFldsToRemoveIfAny = new List<TopContractsEntities.ContractField>();
                            foreach (TopContractsEntities.ContractField cFld in efFieldGroup.ContractFields)
                            {
                                if (cFld.RecordCounter > 0)
                                    contractFldsToRemoveIfAny.Add(cFld);
                                else if (cFld.RecordCounter == (long)RecordCounter.Default)
                                    cFld.RecordCounter = 1;
                            }

                            if (contractFldsToRemoveIfAny.Count > 0)
                                foreach (TopContractsEntities.ContractField contractFldToRemove in contractFldsToRemoveIfAny)
                                {
                                    context.ContractFields.Remove(contractFldToRemove);
                                }
                        }
                    }

                    efFieldGroup.SingleRecord = fieldGroup.SingleRecord;

                    // code area to  remove contract fields if field group is being changed from multiple
                    // record to single record.
                    if (Convert.ToBoolean(efFieldGroup.SingleRecord) && efFieldGroup.ContractFields.Count > 0)
                    {
                        List<TopContractsEntities.ContractField> contractFldsToRemove = new List<TopContractsEntities.ContractField>();
                        if (efFieldGroup.ContractFields.Any(cf => cf.RecordCounter > 0))
                        {
                            List<long> mappedContractIds = efFieldGroup.ContractFields.Where(cf => cf.RecordCounter > 0).Select(cf => cf.ContractID).Distinct().ToList();

                            foreach (long mappedContractId in mappedContractIds)
                            {
                                foreach (TopContractsEntities.ContractField contractFld in efFieldGroup.ContractFields.Where(ctFld => ctFld.ContractID == mappedContractId && ctFld.FieldGroupID == efFieldGroup.FieldGroupID))
                                {
                                    if (contractFld.RecordCounter == 1)
                                    {
                                        contractFld.RecordCounter = (long)RecordCounter.Default;
                                        contractFld.FieldValue = null;
                                        contractFld.LinkedUserID = null;
                                        contractFld.FieldValueNumeric = null;
                                        contractFld.FieldValueDate = null;
                                        contractFld.FieldValueTime = null;
                                        contractFld.FieldCurrencyID = null;
                                        contractFld.LinkedEventID = null;
                                        contractFld.LinkedUserID = null;
                                        contractFld.LinkedDocumentID = null;
                                        contractFld.LinkedContractID = null;
                                    }
                                    else
                                        contractFldsToRemove.Add(contractFld);
                                }
                            }
                        }

                        if (contractFldsToRemove.Count > 0)
                            foreach (TopContractsEntities.ContractField contractFldToRemove in contractFldsToRemove)
                            {
                                context.ContractFields.Remove(contractFldToRemove);
                            }
                    }

                    if (fieldGroup.GroupFields == null)
                    {
                        //do nothing here - no fields
                    }
                    else
                    {
                        foreach (TopContractsDAL10.SystemTables.Field field in fieldGroup.GroupFields.Entries)
                        {
                            if (field.New)
                            {
                                efField = new TopContractsEntities.Field();
                            }
                            else
                            {
                                efField = context.Fields.Where(c => c.FieldID == field.ID).SingleOrDefault();
                            }
                            if (field.Deleted == false)
                            {
                                efField.InitCommonFields(efField, field, efField.FieldsLNGs, this.organizationIdentifier);
                                efField.FieldLength = field.FieldLength;
                                efField.UseFirstAsDefault = field.UseFirstAsDefault;
                                efField.FieldType = field.FieldType;

                                efField.Mandatory = field.Mandatory;
                                efField.AllowMultipleSelection = field.AllowMultipleSelection;
                                efField.MandatoryCurrencyID = field.MandatoryCurrencyID;
                                efField.DisplayFormat = field.DisplayFormat;
                                efField.MandatoryEventTypeID = field.MandatoryEventTypeID;
                                efField.Formula = field.Formula;
                                efField.IsCalculatedCurrency = field.IsCalculatedCurrency;
                                efField.FieldIDForCurrency = field.FieldIDForCurrency;
                                efField.AllowUnauthorizedUsers = field.AllowUnauthorizedUsers;
                                efField.LinkedFieldID = field.LinkedFieldID;
                                efField.DefaultValue = field.DefaultValue;
                                efField.IsWholeNumber = field.IsWholeNumber;
                                efField.MinimumNumericValue = field.MinimumNumericValue;
                                efField.MaximumNumericValue = field.MaximumNumericValue;

                                //if (!string.IsNullOrEmpty(field.ExternalID))
                                //    efField.ExternalID = field.ExternalID.Substring(0, field.ExternalID.IndexOf(Constants.entityFieldsDivider));

                                //Modified by Salil on 19-July-2013; 
                                //this condition is true if the catalog field is changed in fieldgroup. Eg:- Catalog A is replcaed by Catalog B in a FieldGroup FG.
                                if (efField.LinkedEntityID != field.LinkedEntityID)
                                {
                                    List<long> ctlFldLst = context.ContractTypesFieldsToCreateDefaults.Where(t => t.ContractTypeID == efField.LinkedEntityID).Select(t => t.FieldID).ToList();
                                    foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(fld => fld.FieldGroupID == fieldGroup.ID && fld.FieldID == field.ID && fld.CatalogFieldID != null && ctlFldLst.Contains((long)fld.CatalogFieldID)).ToList())
                                    {
                                        context.ContractFields.Remove(contractField);
                                    }
                                }

                                efField.LinkedEntityID = field.LinkedEntityID;

                                //if (field.FieldListItems != null && field.FieldType == (int)FieldTypes.ListSingle)
                                if (field.FieldListItems != null)
                                {
                                    if (field.FieldListItems.Entries.Count > 0)
                                    {
                                        //------------------------------------------------
                                        foreach (TopContractsDAL10.SystemTables.FieldListItem fieldListItem in field.FieldListItems.Entries)
                                        {
                                            if (fieldListItem.New)
                                            {
                                                efFieldListItem = new TopContractsEntities.FieldListItem();
                                            }
                                            else
                                            {
                                                efFieldListItem = context.FieldListItems.Where(c => c.FieldListItemID == fieldListItem.ID).SingleOrDefault();
                                            }
                                            if (fieldListItem.Deleted == false)
                                            {
                                                efFieldListItem.InitCommonFields(efFieldListItem, fieldListItem, efFieldListItem.FieldListItemsLNGs, this.organizationIdentifier);
                                                //efFieldListItem.FieldID = (int)fieldListItem.FieldID;
                                                //TODO - any more?...
                                            }

                                            if (fieldListItem.New)
                                                efField.FieldListItems.Add(efFieldListItem);
                                            else
                                            {
                                                if (fieldListItem.Deleted && efFieldListItem != null)
                                                {
                                                    if (CascadeDelete)
                                                    {
                                                        string strFieldListItemID = fieldListItem.ID.ToString();
                                                        var t = context.Contracts.Where(ct => ct.ContractFields.Any(ctfld => ctfld.FieldID == field.ID && ctfld.FieldValue == strFieldListItemID));
                                                        foreach (TopContractsEntities.Contract contract in context.Contracts.Where(ct => ct.ContractFields.Any(ctfld => ctfld.FieldID == field.ID && ctfld.FieldValue == strFieldListItemID)))
                                                        {
                                                            if (field.FieldType == (int)FieldTypes.ListSingle)
                                                                foreach (var ctrlfld in contract.ContractFields.Where(ctfld => ctfld.FieldID == field.ID && ctfld.FieldValue == strFieldListItemID))
                                                                    ctrlfld.FieldValue = null;
                                                            else
                                                                contract.ContractFields.Where(ctfld => ctfld.FieldID == field.ID).SingleOrDefault().FieldValue = null;
                                                        }

                                                        if (context.ContractFields.Any(ctfld => ctfld.CatalogFieldID == field.ID && ctfld.FieldValue == strFieldListItemID))
                                                        {
                                                            foreach (TopContractsEntities.Contract contract in context.Contracts.Where(ct => ct.ContractFields.Any(ctfld => ctfld.CatalogFieldID == field.ID && ctfld.FieldValue == strFieldListItemID)))
                                                            {
                                                                if (field.FieldType == (int)FieldTypes.ListSingle)
                                                                    foreach (var ctrlfld in contract.ContractFields.Where(ctfld => ctfld.CatalogFieldID == field.ID && ctfld.FieldValue == strFieldListItemID))
                                                                        ctrlfld.FieldValue = null;
                                                            }
                                                        }
                                                    }


                                                    efFieldListItem.DeleteLanguageEntries(efFieldListItem, context.FieldListItemsLNGs, efFieldListItem.FieldListItemsLNGs);


                                                    efFieldListItem.Field.FieldListItems.Remove(efFieldListItem);
                                                    context.FieldListItems.Remove(efFieldListItem);
                                                }
                                            }
                                        }
                                    }
                                    //------------------------------------------------
                                }
                                //TODO - any more?...
                            }

                            if (field.New)
                                efFieldGroup.Fields.Add(efField);
                            else
                            {
                                if (field.Deleted && efField != null)
                                {
                                    //if (CascadeDelete && efField.FieldGroup.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll)
                                    //{
                                    //    foreach (TopContractsEntities.Contract contract in context.Contracts)
                                    //    {
                                    //        contract.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldID == efField.EntryIdentifier));
                                    //    }
                                    //    foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(cf => cf.FieldID == efField.EntryIdentifier))
                                    //    {
                                    //        context.ContractFields.Remove(contractField);
                                    //    }
                                    //}
                                    //if (CascadeDelete && efField.FieldGroup.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && efField.FieldGroup.FieldGroupsContractTypesMAPs != null)
                                    //{
                                    //    foreach (TopContractsEntities.FieldGroupsContractTypesMAP contractTypeMap in efField.FieldGroup.FieldGroupsContractTypesMAPs)
                                    //    {
                                    //        foreach (TopContractsEntities.Contract contract in contractTypeMap.ContractType.Contracts)
                                    //        {
                                    //            contract.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldID == efField.EntryIdentifier));
                                    //        }
                                    //        foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(cf => cf.FieldID == efField.EntryIdentifier))
                                    //        {
                                    //            context.ContractFields.Remove(contractField);
                                    //        }
                                    //    }
                                    //}
                                    if (CascadeDelete)
                                    {
                                        foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(fld => fld.FieldID == efField.FieldID).ToList())
                                        {
                                            context.ContractFields.Remove(contractField);
                                        }

                                        if (efFieldGroup.FieldGroupsContractTypesMAPs.Count() == 1)
                                        {
                                            long ContractTypeContractID = Utilities.contractTypeContractsID;
                                            if (efFieldGroup.FieldGroupsContractTypesMAPs.First().ContractType.ParentContractTypeID != ContractTypeContractID || efFieldGroup.FieldGroupsContractTypesMAPs.First().ContractType.ParentContractTypeID == null)
                                            {
                                                foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(fld => fld.CatalogFieldID == efField.FieldID).ToList())
                                                {
                                                    context.ContractFields.Remove(contractField);
                                                }
                                            }
                                        }
                                    }

                                    efField.DeleteLanguageEntries(efField, context.FieldsLNGs, efField.FieldsLNGs);
                                    for (int indx = efField.FieldListItems.Count() - 1; indx >= 0; indx--)
                                    {
                                        TopContractsEntities.FieldListItem itm = efField.FieldListItems.ElementAt(indx);
                                        itm.DeleteLanguageEntries(itm, context.FieldListItemsLNGs, itm.FieldListItemsLNGs);
                                        context.FieldListItems.Remove(itm);
                                    }
                                    efField.FieldGroup.Fields.Remove(efField);
                                    context.Fields.Remove(efField);
                                }
                            }
                        }
                    }

                }

                if (fieldGroup.New)
                    context.FieldGroups.Add(efFieldGroup);
                else
                {
                    if (fieldGroup.Deleted && efFieldGroup != null)
                    {
                        efFieldGroup.DeleteLanguageEntries(efFieldGroup, context.FieldGroupsLNGs, efFieldGroup.FieldGroupsLNGs);
                        for (int indx = efFieldGroup.Fields.Count() - 1; indx >= 0; indx--)
                        {
                            TopContractsEntities.Field fld = efFieldGroup.Fields.ElementAt(indx);
                            fld.DeleteLanguageEntries(fld, context.FieldsLNGs, fld.FieldsLNGs);
                            for (int indx2 = fld.FieldListItems.Count() - 1; indx2 >= 0; indx2--)
                            {
                                TopContractsEntities.FieldListItem itm = fld.FieldListItems.ElementAt(indx2);
                                itm.DeleteLanguageEntries(itm, context.FieldListItemsLNGs, itm.FieldListItemsLNGs);
                                context.FieldListItems.Remove(itm);
                            }

                            //if (CascadeDelete && fld.FieldGroup.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll)
                            //{
                            //    foreach (TopContractsEntities.Contract contract in context.Contracts)
                            //    {
                            //        contract.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldID == fld.EntryIdentifier));
                            //    }
                            //    foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(cf => cf.FieldID == fld.EntryIdentifier))
                            //    {
                            //        context.ContractFields.Remove(contractField);
                            //    }
                            //}
                            //if (CascadeDelete && fld.FieldGroup.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && fld.FieldGroup.FieldGroupsContractTypesMAPs != null)
                            //{
                            //    foreach (TopContractsEntities.FieldGroupsContractTypesMAP contractTypeMap in fld.FieldGroup.FieldGroupsContractTypesMAPs)
                            //    {
                            //        foreach (TopContractsEntities.Contract contract in contractTypeMap.ContractType.Contracts)
                            //        {
                            //            contract.ContractFields.Remove(contract.ContractFields.Single(cf => cf.FieldID == fld.EntryIdentifier));
                            //        }
                            //        foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(cf => cf.FieldID == fld.EntryIdentifier))
                            //        {
                            //            context.ContractFields.Remove(contractField);
                            //        }
                            //    }
                            //}

                            // Mohit - Added code to remove all the contract field of a Field group being removed.
                            if (CascadeDelete)
                                foreach (TopContractsEntities.ContractField contractField in context.ContractFields.Where(fg => fg.FieldGroupID == efFieldGroup.FieldGroupID).ToList())
                                {
                                    context.ContractFields.Remove(contractField);
                                }


                            context.Fields.Remove(fld);
                        }
                        for (int indx = efFieldGroup.FieldGroupsContractTypesMAPs.Count() - 1; indx >= 0; indx--)
                        {
                            TopContractsEntities.FieldGroupsContractTypesMAP map = efFieldGroup.FieldGroupsContractTypesMAPs.ElementAt(indx);
                            context.FieldGroupsContractTypesMAPs.Remove(map);
                        }
                        for (int indx = efFieldGroup.FieldGroupsRolesMAPs.Count() - 1; indx >= 0; indx--)
                        {
                            TopContractsEntities.FieldGroupsRolesMAP map = efFieldGroup.FieldGroupsRolesMAPs.ElementAt(indx);
                            context.FieldGroupsRolesMAPs.Remove(map);
                        }
                        context.FieldGroups.Remove(efFieldGroup);
                    }
                }
            }

            if (!populateMapDictionary)
            {
                context.SaveChanges();
            }
            else
            {
                ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
                objectContext.DetectChanges();

                objectContext.SaveChanges(SaveOptions.None); //This will fill the IDs with new values, needed for the next part...

                bool structuralChangeOccurred = false; // flag raised when a structural change is occurred.
                //bool informationalChange = false; // flag raised when a change other than structure is occurred.
                long nonStructuralChanges = 0;

                foreach (var changedEntry in context.ChangeTracker.Entries())
                {
                    if (changedEntry.State == System.Data.EntityState.Added && changedEntry.Entity is TopContractsEntities.Field)
                    {
                        if (changedEntry.CurrentValues["ExternalID"] != null)
                        {
                            string externalId = changedEntry.CurrentValues["ExternalID"].ToString();
                            if (externalId.IndexOf(Constants.entityFieldsDivider) >= 0)
                            {
                                string id = externalId.Substring(externalId.IndexOf(Constants.entityFieldsDivider));
                                id = id.Replace(Constants.entityFieldsDivider, "");
                                int originalFieldID = Convert.ToInt32(id);
                                EntityFieldIDsMap.Add(originalFieldID, Convert.ToInt64(changedEntry.CurrentValues["FieldID"]));
                            }
                        }

                        if (isCatalogField == false)
                            structuralChangeOccurred = true;
                        else
                            nonStructuralChanges += 1;
                    }
                    else if (changedEntry.Entity is TopContractsEntities.Field && changedEntry.State == System.Data.EntityState.Deleted)
                    {
                        if (isCatalogField == false)
                            structuralChangeOccurred = true;
                        else
                            nonStructuralChanges += 1;
                    }
                    else if (changedEntry.Entity is TopContractsEntities.Field && changedEntry.State == System.Data.EntityState.Modified)
                    {
                        if (isCatalogField == false)
                            nonStructuralChanges += 1;
                        else
                            nonStructuralChanges += 1;
                    }
                    else if (changedEntry.Entity is TopContractsEntities.FieldsLNG && changedEntry.State == System.Data.EntityState.Modified)
                        nonStructuralChanges += 1;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroup && changedEntry.State == System.Data.EntityState.Added)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroup && changedEntry.State == System.Data.EntityState.Deleted)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroup && changedEntry.State == System.Data.EntityState.Modified)
                        nonStructuralChanges += 1;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsLNG && changedEntry.State == System.Data.EntityState.Modified)
                        nonStructuralChanges += 1;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsContractTypesMAP && changedEntry.State == System.Data.EntityState.Added)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsContractTypesMAP && changedEntry.State == System.Data.EntityState.Deleted)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsRolesMAP && changedEntry.State == System.Data.EntityState.Added)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsRolesMAP && changedEntry.State == System.Data.EntityState.Deleted)
                        structuralChangeOccurred = true;
                    else if (changedEntry.Entity is TopContractsEntities.FieldGroupsRolesMAP && changedEntry.State == System.Data.EntityState.Modified)
                        structuralChangeOccurred = true;
                }

                objectContext.AcceptAllChanges();

                //if (isCatalogField && structuralChangeOccurred == false)
                //{
                //    foreach (long fieldId in addedFieldIDs)
                //    {
                //        if(structuralChangeOccurred==false)
                //    }
                //}

                TopContractsDAL10.CachedData.ClearCachedData(); // To clear cached data of Fields Lng.
                ApplicationCachedData.reInitCacheData(); // To re-initialize server side cache.

                if (structuralChangeOccurred)
                    ApplicationCachedData.UpdateContractStructureCacheTicks();
                else
                    if (nonStructuralChanges > 0)
                        if (isCatalogField)
                            ApplicationCachedData.UpdateCatalogCacheTicks();
                        else
                            ApplicationCachedData.UpdateContractDisplayCacheTicks();
            }

            //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
            long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept

            foreach (TopContractsEntities.FieldGroup efFldGrp in context.FieldGroups.Where(fgrp => fgrp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll || fgrp.FieldGroupsContractTypesMAPs.Any(cTypeMap => cTypeMap.ContractType.ParentContractTypeID == ContractTypeContractsID) == true))
            {
                List<long> contractIDs = new List<long>();
                List<long?> recordcounters = new List<long?>();

                if (efFldGrp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll)
                {
                    foreach (TopContractsEntities.Field efFld in efFldGrp.Fields)
                        foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => c.ContractType.ParentContractTypeID == ContractTypeContractsID && c.ContractFields.Any(cf => cf.FieldID == efFld.FieldID) == false))
                        {
                            if (efFld.FieldType == (int)FieldTypes.EntityLink)
                            {
                                foreach (TopContractsEntities.ContractTypesFieldsToCreateDefault contDef in context.ContractTypesFieldsToCreateDefaults.Where(c => c.ContractTypeID == efFld.LinkedEntityID))
                                {
                                    /* change this check */
                                    if (contract.ContractFields.Any(cf => cf.FieldID == efFld.FieldID && cf.FieldGroupID == efFldGrp.FieldGroupID && cf.CatalogFieldID == contDef.FieldID) == false)
                                    {
                                        recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == efFldGrp.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                        TopContractsEntities.Field efDefFld = context.Fields.Where(defld => defld.FieldID == contDef.FieldID).SingleOrDefault();
                                        if (recordcounters.Count != 0)
                                        {
                                            foreach (long? recordcounter in recordcounters)
                                            {
                                                //**************Code Commented by Viplav on 3 mat 2013 for resolving the issue of save undefined text in contract fields**********

                                                //if ((efFld.FieldType == (int)FieldTypes.Autonumber && recordcounter > 0) || efFld.FieldGroup.SingleRecord == true)
                                                //{
                                                //    CreateCounterforNewField(context, checkMax, efFld, contract, recordcounter);
                                                //}
                                                //else
                                                //{

                                                //*****************************************************************************************
                                                contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                                {
                                                    FieldGroupID = efFld.FieldGroupID,
                                                    FieldID = efFld.FieldID,
                                                    CatalogFieldID = efDefFld.FieldID,
                                                    FieldValue = (efDefFld.FieldType == (int)FieldTypes.ListSingle && efDefFld.FieldListItems.Count > 0 && efDefFld.UseFirstAsDefault == true ? efDefFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                    RecordCounter = (recordcounter == null ? Convert.ToInt64(RecordCounter.Default) : recordcounter)
                                                });
                                                //}
                                            }
                                        }
                                        else
                                        {
                                            contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                            {
                                                FieldGroupID = efFld.FieldGroupID,
                                                FieldID = efFld.FieldID,
                                                CatalogFieldID = efDefFld.FieldID,
                                                FieldValue = (efDefFld.FieldType == (int)FieldTypes.ListSingle && efDefFld.FieldListItems.Count > 0 && efDefFld.UseFirstAsDefault == true ? efDefFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                RecordCounter = Convert.ToInt64(RecordCounter.Default)
                                            });
                                        }
                                    }
                                }
                            }
                            else
                            {
                                recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == efFldGrp.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                if (recordcounters.Count != 0)
                                {
                                    foreach (long? recordcounter in recordcounters)
                                    {
                                        if ((efFld.FieldType == (int)FieldTypes.Autonumber && recordcounter > 0) || (efFld.FieldGroup.SingleRecord == true && efFld.FieldType == (int)FieldTypes.Autonumber))
                                        {
                                            CreateCounterforNewField(context, checkMax, efFld, contract, recordcounter);
                                        }
                                        else
                                        {
                                            contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                            {
                                                FieldGroupID = efFld.FieldGroupID,
                                                FieldID = efFld.FieldID,
                                                FieldValue = (efFld.FieldType == (int)FieldTypes.ListSingle && efFld.FieldListItems.Count > 0 && efFld.UseFirstAsDefault == true ? efFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                RecordCounter = (recordcounter == null ? Convert.ToInt64(RecordCounter.Default) : recordcounter)
                                            });
                                        }
                                    }
                                }
                                else
                                {
                                    contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                    {
                                        FieldGroupID = efFld.FieldGroupID,
                                        FieldID = efFld.FieldID,
                                        FieldValue = (efFld.FieldType == (int)FieldTypes.ListSingle && efFld.FieldListItems.Count > 0 && efFld.UseFirstAsDefault == true ? efFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                        RecordCounter = Convert.ToInt64(RecordCounter.Default)
                                    });
                                }
                            }
                        }
                }
                else
                {
                    if (efFldGrp.FieldGroupsContractTypesMAPs != null)
                        foreach (FieldGroupsContractTypesMAP fgcMap in efFldGrp.FieldGroupsContractTypesMAPs)
                        {
                            if (context.Contracts.Any(c => c.ContractTypeID == fgcMap.ContractTypeID))
                                contractIDs.AddRange(context.Contracts.Where(c => c.ContractTypeID == fgcMap.ContractTypeID && c.ContractType.ParentContractTypeID == ContractTypeContractsID).Select(c => c.ContractID));
                        }
                    if (contractIDs.Count > 0)
                        foreach (TopContractsEntities.Field efFld in efFldGrp.Fields)
                        {
                            foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => (c.ContractFields.Any(cf => cf.FieldID == efFld.FieldID) == false) && c.ContractType.ParentContractTypeID == ContractTypeContractsID))
                            {
                                if (contractIDs.Contains(contract.ContractID))
                                {
                                    if (efFld.FieldType == (int)FieldTypes.EntityLink)
                                    {
                                        foreach (TopContractsEntities.ContractTypesFieldsToCreateDefault contDef in context.ContractTypesFieldsToCreateDefaults.Where(c => c.ContractTypeID == efFld.LinkedEntityID))
                                        {
                                            /* change this check */
                                            if (contract.ContractFields.Any(cf => cf.FieldID == efFld.FieldID && cf.FieldGroupID == efFldGrp.FieldGroupID && cf.CatalogFieldID == contDef.FieldID) == false)
                                            {
                                                recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == efFldGrp.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                                if (recordcounters.Count != 0)
                                                {
                                                    foreach (long? recordcounter in recordcounters)
                                                    {
                                                        //**************Code Commented by Viplav on 3 mat 2013 for resolving the issue of save undefined text in contract fields**********

                                                        //if ((efFld.FieldType == (int)FieldTypes.Autonumber && recordcounter > 0) || efFld.FieldGroup.SingleRecord == true)
                                                        //{
                                                        //    CreateCounterforNewField(context, checkMax, efFld, contract, recordcounter);
                                                        //}
                                                        //else
                                                        //{
                                                        //********************************************************************************
                                                        contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                                        {
                                                            FieldGroupID = efFld.FieldGroupID,
                                                            FieldID = efFld.FieldID,
                                                            CatalogFieldID = contDef.Field.FieldID,
                                                            FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.FieldListItems.Count > 0 && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                            RecordCounter = (recordcounter == null ? Convert.ToInt64(RecordCounter.Default) : recordcounter)
                                                        });
                                                        //}
                                                    }
                                                }
                                                else
                                                {
                                                    contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                                    {
                                                        FieldGroupID = efFld.FieldGroupID,
                                                        FieldID = efFld.FieldID,
                                                        CatalogFieldID = contDef.Field.FieldID,
                                                        FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.FieldListItems.Count > 0 && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                        RecordCounter = Convert.ToInt64(RecordCounter.Default)
                                                    });
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {

                                        recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == efFldGrp.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                        if (recordcounters.Count != 0)
                                        {
                                            foreach (long? recordcounter in recordcounters)
                                            {
                                                if ((efFld.FieldType == (int)FieldTypes.Autonumber && recordcounter > 0) || (efFld.FieldGroup.SingleRecord == true && efFld.FieldType == (int)FieldTypes.Autonumber))
                                                {
                                                    CreateCounterforNewField(context, checkMax, efFld, contract, recordcounter);
                                                }
                                                else
                                                {
                                                    contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                                    {
                                                        FieldGroupID = efFld.FieldGroupID,
                                                        FieldID = efFld.FieldID,
                                                        FieldValue = (efFld.FieldType == (int)FieldTypes.ListSingle && efFld.FieldListItems.Count > 0 && efFld.UseFirstAsDefault == true ? efFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                        RecordCounter = (recordcounter == null ? Convert.ToInt64(RecordCounter.Default) : recordcounter)
                                                    });
                                                }
                                            }
                                        }
                                        else
                                        {
                                            contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                            {
                                                FieldGroupID = efFld.FieldGroupID,
                                                FieldID = efFld.FieldID,
                                                FieldValue = (efFld.FieldType == (int)FieldTypes.ListSingle && efFld.FieldListItems.Count > 0 && efFld.UseFirstAsDefault == true ? efFld.FieldListItems.First().FieldListItemID.ToString() : ""),
                                                RecordCounter = Convert.ToInt64(RecordCounter.Default)
                                            });
                                        }
                                    }
                                }
                            }
                        }


                    contractIDs = new List<long>();
                    bool noMap = false;
                    if (efFldGrp.FieldGroupsContractTypesMAPs == null)
                        noMap = true;
                    else
                        if (efFldGrp.FieldGroupsContractTypesMAPs.Count == 0)
                            noMap = true;
                    if (noMap)
                        contractIDs.AddRange(context.Contracts.Select(c => c.ContractID));
                    else
                        foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => c.ContractType.ParentContractTypeID == ContractTypeContractsID))
                        {
                            if (efFldGrp.FieldGroupsContractTypesMAPs.Any(fgcMap => fgcMap.ContractTypeID == contract.ContractTypeID) == false)
                                contractIDs.Add(contract.ContractID);
                        }
                    DateTime updateDate = DateTime.Now;
                    if (contractIDs.Count > 0)
                        foreach (int cId in contractIDs)
                        {
                            TopContractsEntities.Contract contract = context.Contracts.Single(c => c.ContractID == cId);
                            if (contract.ContractFields.Any(cf => cf.FieldGroupID == efFldGrp.FieldGroupID))
                            {
                                List<TopContractsEntities.ContractField> cFields = contract.ContractFields.Where(cf => cf.FieldGroupID == efFldGrp.FieldGroupID).ToList();
                                for (int indx = cFields.Count - 1; indx >= 0; indx--)
                                {
                                    TopContractsEntities.ContractField efCtrctFld = cFields[indx];
                                    context.ContractFields.Remove(efCtrctFld);
                                    if (AuditChanges)
                                    {
                                        HistManager histManager = new HistManager();
                                        histManager.AuditFieldRemoval(updateDate, UpdatingUserID, efCtrctFld);
                                    }
                                }
                            }
                        }
                }
            }


            return context.SaveChanges();
            //return -1;
        }
        public int Save(SaveSearchs Data, int UpdatingUserID)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            TopContractsEntities.SaveSearch efSaveSearch = null;
            TopContractsEntities.SearchResultsContract efSaveContracts = null;
            foreach (TopContractsDAL10.DasboardTables.SaveSearch svSearch in Data.Entries)
            {
                if (svSearch.New)
                {
                    efSaveSearch = new TopContractsEntities.SaveSearch();
                }
                else
                {
                    efSaveSearch = context.SaveSearches.Where(c => c.SearchId == svSearch.ID).SingleOrDefault();
                }
                if (svSearch.Deleted == false)
                {
                    efSaveSearch.DisplayOrder = svSearch.DisplayOrder;
                    efSaveSearch.Description = svSearch.Description;
                    efSaveSearch.Name = svSearch.Name;
                    efSaveSearch.ExternalID = svSearch.ExternalID;
                    efSaveSearch.SearchQuery = svSearch.SearchQuery;
                    efSaveSearch.Inactive = svSearch.Inactive;
                    efSaveSearch.Locked = svSearch.Locked;
                    efSaveSearch.OrganizationIdentifier = this.organizationIdentifier;
                    foreach (TopContractsDAL10.DasboardTables.SearchResultsContract svContracts in svSearch.searchResultsContracts.Entries)
                    {
                        if (svContracts.New)
                        {
                            efSaveContracts = new TopContractsEntities.SearchResultsContract();
                        }
                        else
                        {
                            efSaveContracts = context.SearchResultsContracts.Where(c => c.SearchContractId == svContracts.ID).SingleOrDefault();
                        }
                        if (svContracts.Deleted == false)
                        {
                            efSaveContracts.SearchId = svContracts.SearchId;
                            efSaveContracts.ContractId = svContracts.ContractId;
                        }
                        if ((svContracts.New == true && svContracts.Deleted == false) || (svContracts.New == false && svContracts.Deleted == true))
                        {
                            if (svContracts.New)
                                efSaveSearch.SearchResultsContracts.Add(efSaveContracts);
                            else
                            {
                                if (svContracts.Deleted && efSaveContracts != null)
                                {
                                    efSaveSearch.SearchResultsContracts.Remove(efSaveContracts);
                                    context.SearchResultsContracts.Remove(efSaveContracts);
                                }
                            }
                        }
                    }
                }
                if (svSearch.New)
                {
                    context.SaveSearches.Add(efSaveSearch);
                }
                else
                {
                    if (svSearch.Deleted && efSaveSearch != null)
                    {
                        foreach (TopContractsDAL10.DasboardTables.SearchResultsContract svContracts in svSearch.searchResultsContracts.Entries)
                        {
                            efSaveContracts = context.SearchResultsContracts.Where(c => c.SearchContractId == svContracts.ID).SingleOrDefault();
                            context.SearchResultsContracts.Remove(efSaveContracts);
                        }
                        context.SaveSearches.Remove(efSaveSearch);
                    }
                }

            }
            bool updateSavedSearchDetails = false;
            foreach (var entry in context.ChangeTracker.Entries())
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified)
                {
                    updateSavedSearchDetails = true;
                }
            }
            if (updateSavedSearchDetails)
                foreach (var entry in context.ChangeTracker.Entries())
                {
                    if (entry.Entity is TopContractsEntities.SaveSearch && (entry.State == EntityState.Added || entry.State == EntityState.Modified))
                    {
                        ((TopContractsEntities.SaveSearch)entry.Entity).UpdateDate = DateTime.Now;
                        ((TopContractsEntities.SaveSearch)entry.Entity).UpdateUserID = UpdatingUserID;
                    }
                }
            return context.SaveChanges();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Get Role ID of user in contract (role of user in the authorized entities list)
        /// </summary>
        /// <param action="ContractID">Contract ID</param>
        /// <param action="UserID">User ID</param>
        /// <returns>Role ID</returns>
        public static long GetRoleIDInContract(long ContractID, long UserID)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            try
            {
                long roleID =
                    context.Contracts.Where(c => c.ContractID == ContractID).SingleOrDefault().ContractUsers.Where(u => u.UserID == UserID).SingleOrDefault().Role.RoleID;

                return roleID;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// find shared groups (groups shared by all contracts)
        /// </summary>
        /// <param name="contractIds">List of contract IDs - this list will be checked, so that only groups
        /// shared by all these contracts will be returned</param>
        /// <param name="fieldGroupIDsToUse">
        /// Optional parameter; If provided, it acts as a filter - any field groups not contained in this filter, will
        /// be removed from the shared group list. Example, if shared group was found to contain IDs 1,5,7,8 and the
        /// filter is 2,5,8 then only 5,8 will be returned
        /// </param>
        /// <returns></returns>
        public static List<long> GetAllSharedGroupIDs(List<long> contractIds, List<long> fieldGroupIDsToUse = null)
        {
            List<long> sharedGroupIDs = new List<long>();

            if (fieldGroupIDsToUse != null)
                if (fieldGroupIDsToUse.Count == 0)
                    return sharedGroupIDs;

            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            //First find all groups of all contracts (distinct); example, if contract A contains 1,2,3
            //and contract B contains 2,3,7, then the list is 1,2,3,7
            sharedGroupIDs = context.ContractFields.Where(cf => contractIds.Contains(cf.ContractID)).Select(cf => cf.FieldGroupID).Distinct().ToList();

            //now remove any group missing from at least one of the contracts (or from the filter list)
            for (int indx = sharedGroupIDs.Count - 1; indx >= 0; indx--)
            {
                long sharedGroupID = sharedGroupIDs[indx];
                bool removed = false;
                //If filter exists, and does not contain field-group ID - remove it...
                if (fieldGroupIDsToUse != null)
                    if (fieldGroupIDsToUse.Any(ftu => ftu == sharedGroupID) == false)
                    {
                        sharedGroupIDs.RemoveAt(indx);
                        removed = true;
                    }

                if (removed == false)
                    foreach (long contractID in contractIds)
                    {
                        if (context.ContractFields.Any(cf => cf.ContractID == contractID && cf.FieldGroupID == sharedGroupID) == false)
                        {
                            sharedGroupIDs.RemoveAt(indx);
                            break;
                        }
                    }
            }
            return sharedGroupIDs;
        }
Exemplo n.º 17
0
 /// <summary>
 /// This function will check that contract is Normal contract or catalog type.
 /// </summary>
 /// <param name="ContractID"></param>
 /// <returns></returns>
 public static bool IsCatalogTypeContract(long ContractID)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     long ContractTypeContractsID = TopContractsDAL10.Utilities.contractTypeContractsID;
     long cTypeId = context.Contracts.FirstOrDefault(t => t.ContractID == ContractID).ContractTypeID;
     return context.ContractTypes.Any(pct => (pct.ParentContractTypeID != ContractTypeContractsID || pct.ParentContractTypeID == null) && pct.ContractTypeID == cTypeId);
 }
Exemplo n.º 18
0
        /// <summary>
        /// This constructor is only used to fetch an existing contract, and not for creating a new one.
        /// </summary>
        /// <param action="ContractID">ID of contract to fetch</param>
        /// <param action="CultureIdentifier">Culture Identifier used for the selection of Fields and Field-Groups 
        /// names in the appropriate language.</param>
        /// <param name="paging">Object of ContractSectionPaging class to provide page sizes of various contract sections.</param>
        /// <param action="UserId">UserID may be passed to set the read-only value of RoleName of that user 
        /// in the selected contract</param>
        public Contract(long ContractID, string CultureIdentifier, ContractSectionPaging paging, long? UserId = null, ContractSections Content = ContractSections.All, List<long> GroupIDs = null)
        {
            Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Commencing CTOR for ContractID {0}, UserId {1}, Content {2}", ContractID, UserId, Content.ToString());
            //ErrorDTO Error = null;
            //try
            //{
            Deleted = false;
            Properties = new ContractProperties();
            initLists();
            Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Lists initialized");

            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Contract efContract = context.Contracts.Where(u => u.ContractID == ContractID).SingleOrDefault();
            this.ID = efContract.ContractID;
            this.Properties.initDataFields(efContract);
            Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Data Fields initialized");
            this.Properties.initPrivateFields(efContract, CultureIdentifier, UserId);
            Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Private Fields initialized");

            if ((Content & ContractSections.Events) == ContractSections.Events)
            {
                this.ContractActivities.AddRange(efContract.ContractActivities.Select(ca => new ContractActivity(ca, CultureIdentifier)));
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Events added for ContractSections.Events");
            }
            if ((Content & ContractSections.Alerts) == ContractSections.Alerts)
            {
                this.ContractTodos.AddRange(efContract.ContractTodos.Select(ca => new ContractTodo(ca, CultureIdentifier)));
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Alerts added for ContractSections.Alerts");
            }
            if ((Content & ContractSections.AuthorizedEntities) == ContractSections.AuthorizedEntities)
            {
                bool showAllUser = false;
                long? MaxPageSizeUsers = null;

                if (paging.UsersPaging.PagingBehaviour == PagingParameter.DontPage)
                    showAllUser = true;
                else if (paging.UsersPaging.PagingBehaviour == PagingParameter.OverrideDatabaseValues)
                    MaxPageSizeUsers = paging.UsersPaging.PagingValue;
                else
                    MaxPageSizeUsers = GetPagingValueFromDB(efContract.OrganizationIdentifier, ContractSections.AuthorizedEntities);

                if (showAllUser == true || MaxPageSizeUsers == null)
                    this.ContractUsers.AddRange(efContract.ContractUsers.Select(ca => new ContractUser(ca, CultureIdentifier)));
                else
                    this.ContractUsers.AddRange(GetContractUsersByPaging(ContractID, CultureIdentifier, Convert.ToInt64(MaxPageSizeUsers)));

                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Users added for ContractSections.AuthorizedEntities");
            }
            if ((Content & ContractSections.Applications) == ContractSections.Applications)
            {
                this.ContractApplications.AddRange(efContract.ContractApplications.Select(ca => new ContractApplication(ca)));
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Applications added for ContractSections.Applications");
            }
            if ((Content & ContractSections.Fields) == ContractSections.Fields)
            {
                //Code Commented By Viplav for implement sorting related to (DisplayOrder / Alphabatically)

                //long groupID = 0;
                //foreach (TopContractsEntities.ContractField field in efContract.ContractFields.OrderBy(fl => fl.FieldGroupID))
                //{
                //    if (field.FieldGroupID != groupID)
                //    {
                //        groupID = field.FieldGroupID;
                //        this.ContractFieldGroups.Add(new ContractFieldGroup(field, CultureIdentifier));
                //    }
                //}

                //Viplav

                //------------Boaz 25-July-2013-------------
                //If this "contract" is a catalog record, and it is now fetched as part of a process of
                //fetching many such catalog records of the same type, then groupFields collection should
                //be initialized only once...
                List<FieldGroup> efFieldGroups = null;
                if (Contract.initializingCatalogRecords == false || Contract.efFieldGroupsForContractInit == null)
                {
                    efFieldGroups = new List<FieldGroup>();
                    //if (efContract.ContractFields.Any(fg => fg.FieldGroup.DisplayOrder == null))
                    //{
                    //    efFieldGroups = efContract.ContractFields.Select(fg => fg.FieldGroup).Distinct().OrderBy(fgl => fgl.FieldGroupsLNGs.FirstOrDefault(cul => cul.CultureId.Trim() == CultureIdentifier).DescShort).ToList();
                    //}
                    //else
                    //{
                    //    efFieldGroups = efContract.ContractFields.Select(fg => fg.FieldGroup).Distinct().OrderBy(fgd => fgd.DisplayOrder).ToList();
                    //}

                    //List<ContractTypeFieldGroupsMap> groupsMap = new List<ContractTypeFieldGroupsMap>();
                    //groupsMap = ApplicationCachedData.ContractTypeFieldGroupsMap;
                    if (efContract.ContractType.ParentContractTypeID == Utilities.contractTypeContractsID)
                    {
                        if (ApplicationCachedData.ContractTypeFieldGroupsMap.Any(ct => ct.ContractTypeID == efContract.ContractTypeID))
                        {
                            Dictionary<string, List<FieldGroup>> FieldGroupsByLang = ApplicationCachedData.ContractTypeFieldGroupsMap.Single(ct => ct.ContractTypeID == efContract.ContractTypeID).FieldGroupsByLang;
                            if (FieldGroupsByLang.Count() > 0)
                                if (FieldGroupsByLang.Any(lng => lng.Key == CultureIdentifier))
                                    efFieldGroups = FieldGroupsByLang.Single(lng => lng.Key == CultureIdentifier).Value;
                        }
                    }
                    else
                    {
                        if (ApplicationCachedData.CatalogFieldGroupsMap.Any(ct => ct.ContractTypeID == efContract.ContractTypeID))
                        {
                            Dictionary<string, List<FieldGroup>> FieldGroupsByLang = ApplicationCachedData.CatalogFieldGroupsMap.Single(ct => ct.ContractTypeID == efContract.ContractTypeID).FieldGroupsByLang;
                            if (FieldGroupsByLang.Count() > 0)
                                if (FieldGroupsByLang.Any(lng => lng.Key == CultureIdentifier))
                                    efFieldGroups = FieldGroupsByLang.Single(lng => lng.Key == CultureIdentifier).Value;
                        }
                    }

                    Contract.efFieldGroupsForContractInit = efFieldGroups;
                }
                else
                    efFieldGroups = Contract.efFieldGroupsForContractInit;

                // if list of GroupIDs is not null
                if (GroupIDs != null)
                {
                    if (GroupIDs.Count() > 0)
                    {
                        // retrieve only those fieldgroups which are requested.
                        efFieldGroups = efFieldGroups.Where(fg => GroupIDs.Contains(fg.FieldGroupID)).ToList();
                    }
                }

                foreach (FieldGroup fieldgroup in efFieldGroups)
                {
                    this.ContractFieldGroups.Add(new ContractFieldGroup(context, fieldgroup, CultureIdentifier, ContractID));
                }
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Fields added for ContractSections.Fields");
            }
            if ((Content & ContractSections.Documents) == ContractSections.Documents)
            {
                this.ContractDocs.AddRange(efContract.ContractDocs.Select(ca => new ContractDoc(ca)));
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Documents added for ContractSections.Documents");
            }
            if ((Content & ContractSections.Gallery) == ContractSections.Gallery)
            {
                this.ContractGalleries.AddRange(efContract.ContractGalleries.OrderByDescending(cg => cg.CreatedOn).Select(cg => new ContractGallery(cg)));
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Galleries added for ContractSections.Gallery");
            }

            if ((Content & ContractSections.ChildContract) == ContractSections.ChildContract)   // Condition Implemented for getting only neccessary Data by Viplav on 14 May 2013
            {
                this.ChildContracts = new ChildContract(ref context, efContract).ChildContracts;
                Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Child Contracts collection initialized");
            }

            if ((Content & ContractSections.ParantContract) == ContractSections.ParantContract) // Condition Implemented for getting only neccessary Data by Viplav on 14 May 2013
            {
                if (efContract.ParentContractID != null)
                {
                    //this.ParentContract = new Contract((long)efContract.ParentContractID, CultureIdentifier, UserId, Content);
                    this.ParentContract = new Contract((long)efContract.ParentContractID, CultureIdentifier, paging, UserId, Content); // Kai Cohen - Modified code to pass paging parameters.
                    Logger.WriteGeneralVerbose("Contract class - CTOR 2", "Parent Contracts initialized");
                }
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// get last value of ID field of contracts-table
 /// </summary>
 /// <returns></returns>
 public static long LastContractID()
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     return context.Contracts.Max(c => c.ContractID);
 }
Exemplo n.º 20
0
        public static TopContractsDAL10.ContractApplication GetApplications(long contractID, int ApplicationID)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Contract efContract = context.Contracts.Where(u => u.ContractID == contractID).SingleOrDefault();
            if (efContract == null)
                throw new ExceptionDataContractReadNoSuchContract();

            TopContractsEntities.ContractApplication efConApp = new TopContractsEntities.ContractApplication();
            efConApp.ApplicationID = ApplicationID;
            efConApp.ContractID = contractID;
            efConApp.Application = context.Applications.SingleOrDefault(ca => ca.ApplicationID == ApplicationID);
            efConApp.Contract = context.Contracts.SingleOrDefault(ca => ca.ContractID == contractID);

            TopContractsDAL10.ContractApplication cApp = new ContractApplication(efConApp);

            return cApp;
        }
Exemplo n.º 21
0
 /// <summary>
 /// UpdateCopyContractDocDirectory (Update the directory action according to new contract)
 /// </summary>
 /// <param action="NewContractID">Id of NewContractID performing the update</param>
 /// <returns></returns>
 public static void UpdateCopyContractDocDirectory(long NewContractID)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     foreach (var Contractdoc in context.ContractDocs.Where(cdoc => cdoc.ContractID == NewContractID).ToList())
     {
         Contractdoc.FileDirectory = Storage.DocsPath(NewContractID);
     }
     context.SaveChanges();
 }
Exemplo n.º 22
0
        //end code
        /// <summary>--Added by Viplav Bhateja (28/02/2013)
        /// Used to get only common field group (this functionality is used for copy contract when user change his current contract type))
        /// </summary>
        /// <param action="ContractTypeID">Contract TypeID is used to get contractfieldgroup according to this contract type.</param>
        /// <param action="ContractToSave">Contract to save is an existing loaded contract so we can inplement a functionality to add only common field group.</param>
        /// <param action="CultureIdentifier">Culture Identifier used for the selection of Fields and Field-Groups 
        /// names in the appropriate language.</param>
        /// <returns></returns>
        public static List<int> getCommonContractFieldGroup(int parentContractTypeID, int childContractTypeID, string CultureIdentifier)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
            long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept
            TopContractsDAL10.SystemTables.FieldGroups fieldGroups = new SystemTables.FieldGroups();
            List<int> parentFieldGroupIDs = new List<int>();
            List<int> childFieldGroupIDs = new List<int>();
            List<int> commonFieldGroupIDs = new List<int>();
            if (context.ContractTypes.Any(ct => ct.ParentContractTypeID == ContractTypeContractsID && ct.ContractTypeID == parentContractTypeID))
                fieldGroups = SystemTables.FieldGroups.Get(false, false, true, CultureIdentifier);
            else
                fieldGroups = SystemTables.FieldGroups.GetWithoutParentContractTypeID(false, false, true, CultureIdentifier);

            foreach (TopContractsDAL10.SystemTables.FieldGroup fieldGroup in fieldGroups.Entries)
            {
                if (context.ContractTypes.Any(ct => ct.ParentContractTypeID == ContractTypeContractsID && ct.ContractTypeID == parentContractTypeID))
                {
                    if (fieldGroup.ContractTypeIDsVisible.Any(ct => ct == parentContractTypeID) || fieldGroup.VisibleToAllContractTypes)
                        parentFieldGroupIDs.Add((int)fieldGroup.ID);
                }
            }
            foreach (TopContractsDAL10.SystemTables.FieldGroup fieldGroup in fieldGroups.Entries)
            {
                if (context.ContractTypes.Any(ct => ct.ParentContractTypeID == ContractTypeContractsID && ct.ContractTypeID == childContractTypeID))
                {
                    if (fieldGroup.ContractTypeIDsVisible.Any(ct => ct == childContractTypeID) || fieldGroup.VisibleToAllContractTypes)
                        childFieldGroupIDs.Add((int)fieldGroup.ID);
                }
            }
            foreach (int cfg in childFieldGroupIDs)
            {
                if (parentFieldGroupIDs.Any(ctrgroup => ctrgroup == cfg))
                {
                    commonFieldGroupIDs.Add(cfg);
                }
            }
            return commonFieldGroupIDs;
        }
Exemplo n.º 23
0
 public static void UpdateGalleryImageFilePath(long galleryId, string FilePath)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     foreach (TopContractsEntities.ContractGalleryImage efContractGalleryImage in context.ContractGalleryImages.Where(cd => cd.GalleryID == galleryId).ToList())
     {
         efContractGalleryImage.FileDirectory = FilePath;
     }
     context.SaveChanges();
 }
Exemplo n.º 24
0
 //Get the Contracts with Contract type ID --Added by deepak dhamija (28/02/2013)
 public static List<long> getContractsbyContractTypeID(long contractTypeID)
 {
     List<Contract> contracts = new List<Contract>();
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     List<long> contractIDs = context.Contracts.Where(cnts => cnts.ContractTypeID == contractTypeID).Select(cnt => cnt.ContractID).ToList();
     return contractIDs;
 }
Exemplo n.º 25
0
        /// <summary>
        /// Gets all users of a contract.
        /// </summary>
        /// <param name="ContractID">ID of contract</param>
        /// <param name="CultureIdentifier">Culture identifier to be used to retrieve users information</param>
        /// <returns>List of TopContractsDAL10.ContractUser objects.</returns>
        public List<ContractUser> GetAllContractUsers(long ContractID, string CultureIdentifier)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            List<ContractUser> users = new List<ContractUser>();

            if (context.Contracts.Any(cu => cu.ContractID == ContractID))
            {
                TopContractsEntities.Contract efContract = context.Contracts.SingleOrDefault(cu => cu.ContractID == ContractID);
                users.AddRange(efContract.ContractUsers.Select(ca => new ContractUser(ca, CultureIdentifier)));
            }

            return users;
        }
Exemplo n.º 26
0
 /// <summary>
 /// Get a list of users linked to the contract (authorized entities)
 /// </summary>
 /// <param action="ContractID">Contract ID</param>
 /// <returns>List of TopContractsEntities.ContractUser objects</returns>
 public static List<TopContractsEntities.ContractUser> GetContractUsers(long ContractID)
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     return (context.Contracts.Where(c => c.ContractID == ContractID).SingleOrDefault().ContractUsers.ToList());
 }
Exemplo n.º 27
0
        /// <summary>
        /// Gets contract users by a specific page size.
        /// </summary>
        /// <param name="ContractID">ID of the contract.</param>
        /// <param name="CultureIdentifier">CultureID in which names are required.</param>
        /// <param name="MaxPageSizeUsers">Number of users to be required.</param>
        /// <returns>List of TopContractsDAL10.ContractUser objects.</returns>
        public List<ContractUser> GetContractUsersByPaging(long ContractID, string CultureIdentifier, long MaxPageSizeUsers)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            List<ContractUser> contractUsers = new List<ContractUser>();
            if (context.Contracts.Any(ct => ct.ContractID == ContractID))
            {
                TopContractsEntities.Contract efContract = context.Contracts.SingleOrDefault(ct => ct.ContractID == ContractID);
                if (efContract.ContractUsers.Count(ct => ct.ContractID == ContractID) <= MaxPageSizeUsers)
                {
                    contractUsers = efContract.ContractUsers.Where(ct => ct.ContractID == ContractID).Select(ca => new ContractUser(ca, CultureIdentifier)).ToList();
                }
                else
                {
                    List<TopContractsEntities.ContractUser> usersWithEditPermissions = new List<TopContractsEntities.ContractUser>();
                    List<TopContractsEntities.ContractUser> usersWithViewPermissions = new List<TopContractsEntities.ContractUser>();
                    bool roleCanEdit = false;
                    //bool roleCanEditGroups = false;
                    //List<ContractUser> contractUsersToFilter = context.ContractUsers.Where(ct => ct.ContractID == ContractID).Select(ca => new ContractUser(ca, CultureIdentifier)).ToList();
                    List<TopContractsEntities.ContractUser> efContractUsers = efContract.ContractUsers.Where(ct => ct.ContractID == ContractID).ToList();

                    List<TopContractsEntities.FieldGroup> efFieldGroups = context.ContractFields.Where(cf => cf.ContractID == ContractID).Select(fg => fg.FieldGroup).Distinct().ToList();

                    foreach (TopContractsEntities.ContractUser efContractUser in efContractUsers)
                    {
                        roleCanEdit = (efContractUser.Role.EditProperties == true || efContractUser.Role.EditActivities == true ||
                                 efContractUser.Role.AddActivities == true || efContractUser.Role.DeleteActivities == true ||
                                 efContractUser.Role.EditTodos == true || efContractUser.Role.AddTodos == true || efContractUser.Role.DeleteTodos == true ||
                                 efContractUser.Role.EditAuth == true || efContractUser.Role.EditDocs == true ||
                                 efContractUser.Role.AddDocs == true || efContractUser.Role.DeleteDocs == true ||
                                 efContractUser.Role.EditApps == true || efContractUser.Role.EditGallery == true ||
                                 efContractUser.Role.AddGallery == true || efContractUser.Role.DeleteGallery == true);

                        roleCanEdit = roleCanEdit || RolePermittedToEditContractFields(efFieldGroups, efContractUser.Role);

                        if (roleCanEdit == true)
                            usersWithEditPermissions.Add(efContractUser);
                        else
                            usersWithViewPermissions.Add(efContractUser);
                    }

                    if (usersWithEditPermissions.Count >= MaxPageSizeUsers)
                        contractUsers.AddRange(usersWithEditPermissions.Take((int)MaxPageSizeUsers).Select(ca => new ContractUser(ca, CultureIdentifier)).ToList());
                    else
                    {
                        contractUsers.AddRange(usersWithEditPermissions.Select(ca => new ContractUser(ca, CultureIdentifier)).ToList());
                        //int usersToAdd = (int)MaxPageSizeUsers - usersWithEditPermissions.Count();
                        contractUsers.AddRange(usersWithViewPermissions.Take((int)MaxPageSizeUsers - usersWithEditPermissions.Count()).Select(ca => new ContractUser(ca, CultureIdentifier)).ToList());
                    }
                }
            }
            return contractUsers;
        }
Exemplo n.º 28
0
 /// <summary>
 /// getEntityContracts Method used to get all contracts according to contracttypeID for entities records
 /// </summary>
 /// <param action="contractTypeID">contractTypeID is used to retreive contracts from DB</param>
 /// <param action="CultureIdentifier">CultureIdentifier is used to get data according to current culture</param>
 /// <returns>List of contracts</returns>
 public static List<TopContractsDAL10.Contract> getEntityContracts(long contractTypeID, string CultureIdentifier, Guid OrganizationIdentifier, int UserID)
 {
     List<Contract> contracts = new List<Contract>();
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
     List<long> contractIDs = context.Contracts.Where(cnts => cnts.ContractTypeID == contractTypeID).Select(cnt => cnt.ContractID).ToList();
     if (contractIDs.Count > 0)
     {
         //This flag is used to inform the contract initialization process that many "contracts" (catalog records)
         //of the same type are now being created, so it needs to avoid re-creating structures which are common
         //to all these "contracts"...
         initializingCatalogRecords = true;
         TopContractsDAL10.Contract.efFieldGroupsForContractInit = null;
         foreach (long contractID in contractIDs)
         {
             //TopContractsDAL10.Contract conts = new Contract(contractID, CultureIdentifier, null, ContractSections.Fields);
             ContractSectionPaging dontPage = new ContractSectionPaging().SetDontPage();
             TopContractsDAL10.Contract conts = new Contract(contractID, CultureIdentifier, dontPage, null, ContractSections.Fields); // Kai Cohen - Modified code to pass paging parameters. -1 tells the method to bring all records.
             contracts.Add(conts);
         }
         initializingCatalogRecords = false;
     }
     else
     {
         TopContractsDAL10.Contract conts = new Contract(OrganizationIdentifier, "", contractTypeID, 1, UserID, CultureIdentifier);  // 1 is used for default status ID (HardCoded) and "" foe Contractname
         contracts.Add(conts);
     }
     return contracts;
 }
Exemplo n.º 29
0
        /// <summary>
        /// Save contract (delete or add according to Deleted and New properties)
        /// </summary>
        /// <param action="UpdatingUserID">Id of user performing the update</param>
        /// <param action="AuditChanges">Audit changes of update</param>
        /// <returns>The ID of the contract saved, or 0 if contract was deleted, or -1 to indicate a failure to save contract</returns>
        public long Save(int UpdatingUserID, bool AuditChanges)
        {
            Logger.WriteGeneralVerbose("Contract class - save", "updating ContractActivity of {0}", this.ID);
            //Logger.Write(LogCategory.General, (this.New ? "Creating new " : (this.Deleted ? "Deleting " : "Updating ")) + "contract", "contract ID " + this.ID.ToString(), System.Diagnostics.TraceEventType.Information);
            DateTime updateDate = DateTime.Now;
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Contract efContract = null;
            if (this.New)
            {
                efContract = new TopContractsEntities.Contract();
                efContract.ContractUpdateDetail = new ContractUpdateDetail();
                //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
                long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept
                if (context.FieldGroups.Count(grp => grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll) > 0)
                    //foreach (FieldGroup grp in context.FieldGroups.Where(
                    //          grp => (grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll || grp.FieldGroupsContractTypesMAPs.Any(cTypeMap => cTypeMap.ContractType.ParentContractTypeID == ContractTypeContractsID) == true) && grp.Inactive == false)) //Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fieldsgroup
                    foreach (FieldGroup grp in context.FieldGroups.Where(
                              grp => (grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToAll && grp.Inactive == false)).ToList()) // Jan 31, 2013 - Need to be tested properly as now loop is running for VisibleToAll efFieldGroupsForContractInit
                    {
                        foreach (Field fld in grp.Fields.Where(grpf => grpf.Inactive == false)) //Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fields
                        {
                            if (fld.FieldType == (int)FieldTypes.EntityLink)
                            {
                                foreach (TopContractsEntities.ContractTypesFieldsToCreateDefault contDef in context.ContractTypesFieldsToCreateDefaults.Where(c => c.ContractTypeID == fld.LinkedEntityID))
                                {
                                    //efContract.ContractFields.Add(new TopContractsEntities.ContractField() { FieldGroupID = grp.EntryIdentifier, FieldID = contDef.FieldID, FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                                    efContract.ContractFields.Add(new TopContractsEntities.ContractField() { OrganizationIdentifier = this.Properties.OrganizationIdentifier, FieldGroupID = grp.EntryIdentifier, FieldID = fld.FieldID, CatalogFieldID = contDef.FieldID, FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                                }
                            }
                            else
                                efContract.ContractFields.Add(new TopContractsEntities.ContractField() { OrganizationIdentifier = this.Properties.OrganizationIdentifier, FieldGroupID = grp.EntryIdentifier, FieldID = fld.EntryIdentifier, FieldValue = (fld.FieldType == (int)FieldTypes.ListSingle && fld.UseFirstAsDefault == true ? fld.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                        }
                    }
                if (context.FieldGroups.Count(grp => grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && grp.FieldGroupsContractTypesMAPs.Any(mp => mp.ContractTypeID == this.Properties.ContractTypeID)) > 0)
                    foreach (FieldGroup grp in context.FieldGroups.Where(grp => grp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && grp.Inactive == false && grp.FieldGroupsContractTypesMAPs.Any(mp => mp.ContractTypeID == this.Properties.ContractTypeID)))//Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fieldsgroup
                        foreach (Field fld in grp.Fields.Where(grpf => grpf.Inactive == false)) //Viplav - 16-Oct-2012 // Implemented inactive condition for getting only active fields
                        {
                            if (fld.FieldType == (int)FieldTypes.EntityLink)
                            {
                                foreach (TopContractsEntities.ContractTypesFieldsToCreateDefault contDef in context.ContractTypesFieldsToCreateDefaults.Where(c => c.ContractTypeID == fld.LinkedEntityID))
                                {
                                    //efContract.ContractFields.Add(new TopContractsEntities.ContractField() { FieldGroupID = grp.EntryIdentifier, FieldID = contDef.FieldID, FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                                    efContract.ContractFields.Add(new TopContractsEntities.ContractField() { OrganizationIdentifier = this.Properties.OrganizationIdentifier, FieldGroupID = grp.EntryIdentifier, FieldID = fld.FieldID, CatalogFieldID = contDef.FieldID, FieldValue = (contDef.Field.FieldType == (int)FieldTypes.ListSingle && contDef.Field.UseFirstAsDefault == true ? contDef.Field.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                                }
                            }
                            else
                                efContract.ContractFields.Add(new TopContractsEntities.ContractField() { OrganizationIdentifier = this.Properties.OrganizationIdentifier, FieldGroupID = grp.EntryIdentifier, FieldID = fld.EntryIdentifier, FieldValue = (fld.FieldType == (int)FieldTypes.ListSingle && fld.UseFirstAsDefault == true ? fld.FieldListItems.ElementAt(0).FieldListItemID.ToString() : ""), RecordCounter = Convert.ToInt64(RecordCounter.Default) });
                        }
            }
            else
            {
                efContract = context.Contracts.Where(u => u.ContractID == this.ID).SingleOrDefault();
            }

            if (this.Deleted)
            {
                #region DELETING RELATED ENTITIES -----------------------------------------------------------------------------

                //For some unknown reason, eliminating the if-count>0 from before each of the foreach(s), throws an "object not set" exception even though alerts exist!
                context.ContractUpdateDetails.Remove(context.ContractUpdateDetails.Single(entry => entry.ContractID == this.ID));
                if (context.ContractApplications.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractApplication efContractApplication in context.ContractApplications.Where(entry => entry.ContractID == this.ID))
                        context.ContractApplications.Remove(efContractApplication);
                if (context.ContractActivities.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractActivity efContractActivity in context.ContractActivities.Where(entry => entry.ContractID == this.ID))
                    {
                        context.ContractActivities.Remove(efContractActivity);
                    }
                if (context.ContractDocs.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractDoc efContractDoc in context.ContractDocs.Where(entry => entry.ContractID == this.ID))
                        context.ContractDocs.Remove(efContractDoc);
                if (context.ContractGalleries.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractGallery efContractGallery in context.ContractGalleries.Where(entry => entry.ContractID == this.ID))
                    {
                        if (efContractGallery.ContractGalleryImages != null && efContractGallery.ContractGalleryImages.Count() > 0)
                        {
                            foreach (TopContractsEntities.ContractGalleryImage image in efContractGallery.ContractGalleryImages.ToList())
                            {
                                context.ContractGalleryImages.Remove(image);
                            }
                        }
                        context.ContractGalleries.Remove(efContractGallery);
                    }
                if (context.ContractFields.Count(entry => entry.ContractID == this.ID) > 0)
                    foreach (TopContractsEntities.ContractField efContractField in context.ContractFields.Where(entry => entry.ContractID == this.ID))
                        context.ContractFields.Remove(efContractField);
                if (context.ContractTodos.Count(entry => entry.ContractID == this.ID) > 0)
                {
                    foreach (TopContractsEntities.ContractTodo efContractTodo in context.ContractTodos.Where(entry => entry.ContractID == this.ID))
                    {
                        if (efContractTodo.ContractTodoRecipients != null)
                            for (int indx = efContractTodo.ContractTodoRecipients.Count - 1; indx >= 0; indx--)
                                context.ContractTodoRecipients.Remove(efContractTodo.ContractTodoRecipients.ElementAt(indx));
                        if (efContractTodo.ContractTodoUnits != null)
                            for (int indx = efContractTodo.ContractTodoUnits.Count - 1; indx >= 0; indx--)
                                context.ContractTodoUnits.Remove(efContractTodo.ContractTodoUnits.ElementAt(indx));
                        if (efContractTodo.DoneTodos != null)
                            for (int indx = efContractTodo.DoneTodos.Count - 1; indx >= 0; indx--)
                                context.DoneTodos.Remove(efContractTodo.DoneTodos.ElementAt(indx));
                        context.ContractTodos.Remove(efContractTodo);
                    }
                }
                foreach (TopContractsEntities.ContractUser efContractUser in context.ContractUsers.Where(entry => entry.ContractID == this.ID))
                    context.ContractUsers.Remove(efContractUser);

                #endregion
                context.Contracts.Remove(efContract);

            }
            if (this.New)
            {
                updateEfContract(ref efContract, ref context);
                context.Contracts.Add(efContract);
            }
            if (this.New == false & this.Deleted == false)
            {
                updateEfContract(ref efContract, ref context);
            }

            //Boaz-1 (8-Aug-2012) ------------------------------------------------------
            //All Todos which are linked to new events, should be made their "children" in the EF so that the foreign
            //key values will be  automatically generated when saving to the DB
            foreach (TopContractsEntities.ContractTodo todo in efContract.ContractTodos.Where(td => td.ActivityID < 0))
            {
                efContract.ContractActivities.SingleOrDefault(ca => ca.ActivityID == todo.ActivityID).ContractTodos.Add(todo);
            }

            // code to remove images from context for a deleted gallery
            foreach (ContractGallery gallery in this.ContractGalleries)
            {
                if (gallery.Deleted)
                {
                    foreach (TopContractsEntities.ContractGalleryImage galleryImage in context.ContractGalleryImages.Where(g => g.GalleryID == gallery.EntryId))
                        context.ContractGalleryImages.Remove(galleryImage);
                }

                // code to remove a deleted gallery image
                if (!gallery.New)
                {
                    foreach (ContractGalleryImage image in gallery.Images)
                    {
                        if (image.Deleted)
                        {
                            if (context.ContractGalleryImages.Any(g => g.GalleryImageID == image.EntryId))
                            {
                                TopContractsEntities.ContractGalleryImage galleryImage = context.ContractGalleryImages.SingleOrDefault(g => g.GalleryImageID == image.EntryId);
                                context.ContractGalleryImages.Remove(galleryImage);
                            }
                        }
                    }
                }
            }

            bool updateContractUpdateDetails = false;
            if (this.Deleted == false)
            {
                //If any of the entities related to the contract has changed, the Update-Date of the contract record itself should be updated...
                foreach (var entry in context.ChangeTracker.Entries())
                {
                    //Boaz 7-Aug-2012
                    //if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified && !(entry.Entity is EFContractRelatedEntryWithUpdate))
                    if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified || entry.State == System.Data.EntityState.Deleted)
                    {
                        updateContractUpdateDetails = true;
                    }
                }

                if (updateContractUpdateDetails)
                    foreach (var entry in context.ChangeTracker.Entries())
                    {
                        //Boaz 7-Aug-2012
                        //If any changed was made to a record in any table (entities related to the contract) which has
                        //UpdateDate and UpdateUserID fields, these fields are updated to reflect the change.
                        //These fields are NOT handled by the individual classes committing the changes to the
                        //database - they are handled centrally here to cut-down on the code
                        if (entry.Entity is EFContractRelatedEntryWithUpdate && entry.State != EntityState.Unchanged)
                        //if (entry.Entity is EFContractRelatedEntryWithUpdate)
                        {
                            ((EFContractRelatedEntryWithUpdate)entry.Entity).UpdatingDate = updateDate;
                            ((EFContractRelatedEntryWithUpdate)entry.Entity).UpdatingUserID = UpdatingUserID;
                        }
                        if (entry.Entity is TopContractsEntities.ContractDoc && entry.State == EntityState.Deleted)
                        {
                            TopContractsEntities.User Userdetail = context.Users.Where(usr => usr.UserID == UpdatingUserID).SingleOrDefault();
                            if (Userdetail != null)
                            {
                                if (context.Roles.Where(rol => rol.RoleID == Userdetail.DefaultRoleID).SingleOrDefault().DeleteDocs == false)
                                    entry.State = EntityState.Unchanged;
                            }
                        }
                        if (entry.Entity is TopContractsEntities.ContractActivity && entry.State == EntityState.Deleted)
                        {
                            TopContractsEntities.User Userdetail = context.Users.Where(usr => usr.UserID == UpdatingUserID).SingleOrDefault();
                            if (Userdetail != null)
                            {
                                if (context.Roles.Where(rol => rol.RoleID == Userdetail.DefaultRoleID).SingleOrDefault().DeleteActivities == false)
                                    entry.State = EntityState.Unchanged;
                                else  //********** Code Implemented for remove contract event with all associated Alerts,AlertsRecipiets & AlertsDone by Viplav on 20-Nov-2013**********************************
                                {
                                    List<TopContractsEntities.ContractTodo> contToDos = context.ContractTodos.Where(tod => tod.ActivityID == ((TopContractsEntities.ContractActivity)entry.Entity).ActivityID).ToList();
                                    foreach (TopContractsEntities.ContractTodo cntToDos in contToDos)
                                    {
                                        foreach (TopContractsEntities.ContractTodoRecipient cntToDosRec in context.ContractTodoRecipients.Where(tod => tod.TodoID == cntToDos.TodoID))
                                            context.ContractTodoRecipients.Remove(cntToDosRec);
                                        foreach (TopContractsEntities.DoneTodo doneToDos in context.DoneTodos.Where(todo => todo.OriginalTodoID == cntToDos.TodoID))
                                            context.DoneTodos.Remove(doneToDos);

                                        context.ContractTodos.Remove(cntToDos);
                                    }
                                }//*****************************************************************************************************************************************************************************
                            }
                        }
                    }
            }
            if (updateContractUpdateDetails)
            {
                efContract.ContractUpdateDetail.UpdateDate = updateDate;
                efContract.ContractUpdateDetail.UpdateUserID = UpdatingUserID;
            }
            //We want to keep tracking the changes after calling SaveChanges, for the purpose of auditing
            ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
            objectContext.DetectChanges();
            try
            {
                //Using SaveOptions.None to keep change-tracking... Requires objectContext.DetectChanges() before and objectContext.AcceptAllChanges() after...
                int rowsAffected = objectContext.SaveChanges(SaveOptions.None);
            }
            catch (System.Data.UpdateException updEx)
            {
                if (updEx.InnerException != null)
                    if (updEx.InnerException.Message.Contains("DELETE statement conflicted with the REFERENCE constraint") &&
                        updEx.InnerException.Message.Contains("FK_ContractTodos_ContractActivities"))
                    {
                        //    Logger.Write(updEx, System.Diagnostics.TraceEventType.Warning);
                        Logger.WriteGeneralWarning("Contract - Save()", "An attempt has been made to delete an Event With an Alert, in contract #{0}, by user #{1}", this.ID, UpdatingUserID);
                        throw new ExceptionDataContractSaveDeleteEventWithAlert();
                    }
                    else if (updEx.InnerException.Message.Contains("DELETE statement conflicted with the REFERENCE constraint") &&
                        updEx.InnerException.Message.Contains("FK_ContractFields_ContractActivities"))
                    {
                        Logger.WriteGeneralWarning("Contract - Save()", "An attempt has been made to delete an Event linked to an event type field, in contract #{0}, by user #{1}", this.ID, UpdatingUserID);
                        throw new ExceptionDataContractSaveDeleteEventWithEventTypeFields();
                    }
                    else if (updEx.InnerException.Message.Contains("FK_Contracts_Contracts") &&
                       updEx.InnerException.Message.Contains("ParentContractID"))
                    {
                        Logger.WriteGeneralWarning("Contract - Save()", "An attempt has been made to delete contract #{0}, by user #{1}, when this contract is a parenty of another contract", this.ID, UpdatingUserID);
                        throw new ExceptionDataContractDeleteDeleteContractWithChild();
                    }
                    else if (updEx.InnerException.Message.Contains("DELETE statement conflicted with the REFERENCE constraint") &&
                        updEx.InnerException.Message.Contains("FK_ContractFields_ContractDocs"))
                    {
                        Logger.WriteGeneralWarning("Contract - Save()", "Contract - Save()", "An attempt has been made to delete a document linked to a document-type field, in contract #{0}, by user #{1}", this.ID, UpdatingUserID);
                        throw new ExceptionDataContractSaveDeleteDocsWithDocumentTypeFields();
                    }
                    else
                    {
                        //  Logger.Write(updEx, System.Diagnostics.TraceEventType.Error);
                        Logger.WriteExceptionError("Contract - Save()", updEx, "The attempt to save contract #{0}, by user #{1}, has failed", this.ID, UpdatingUserID);
                        throw updEx;
                    }
            }

            //Boaz-1 (8-Aug-2012) -------------------------------------------------------
            //#region Setting the connection between new activities and their related new todos
            //if (this.Deleted == false)
            //{
            //    foreach (var newActivity in context.ChangeTracker.Entries())
            //    {
            //        if (newActivity.State == System.Data.EntityState.Added && newActivity.Entity is TopContractsEntities.ContractActivity)
            //        {
            //            foreach (var newTodo in context.ChangeTracker.Entries())
            //            {
            //                if (newTodo.State == System.Data.EntityState.Added && newTodo.Entity is TopContractsEntities.ContractTodo)
            //                {
            //                    if ((newTodo.Entity as TopContractsEntities.ContractTodo).ActivityID == (newActivity.Entity as TopContractsEntities.ContractActivity).ActivityID)
            //                    {
            //                        (newActivity.Entity as TopContractsEntities.ContractActivity).ContractTodos.Add((newTodo.Entity as TopContractsEntities.ContractTodo));
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            //#endregion

            //TopContractsEntities.ContractTodo todoToChange = null;

            if (AuditChanges)
            {
                HistManager histManager = new HistManager();
                int recordsWrittenToHistory = histManager.AuditChanges(updateDate, UpdatingUserID, this.New, this.Deleted, context.ChangeTracker.Entries().Where(e => e.State != System.Data.EntityState.Unchanged || e.Entity is TopContractsEntities.Contract));
            }

            objectContext.AcceptAllChanges();

            long contractIdSaved = -1; //will indicate failure, if no number is set
            foreach (var entry in context.ChangeTracker.Entries())
            {
                Logger.Write("entering the loop - for context.ChangeTracker.Entries()", System.Diagnostics.TraceEventType.Verbose);
                if (entry.Entity is TopContractsEntities.Contract)
                {
                    contractIdSaved = ((TopContractsEntities.Contract)entry.Entity).ContractID;
                }
                if (entry.Entity is TopContractsEntities.ContractDoc)
                {
                    TopContractsEntities.ContractDoc efDoc = entry.Entity as TopContractsEntities.ContractDoc;
                    TopContractsDAL10.ContractDoc doc = this.ContractDocs.SingleOrDefault(d => d.FileName == efDoc.FileName);
                    if (doc != null)
                    {
                        if (doc.New)
                        {
                            doc.EntryId = efDoc.DocumentID; //This is done especially for the process of file copying...
                        }
                    }
                }
                if (entry.Entity is TopContractsEntities.ContractGallery)
                {
                    TopContractsEntities.ContractGallery efGallery = entry.Entity as TopContractsEntities.ContractGallery;
                    foreach (TopContractsDAL10.ContractGallery gallery in this.ContractGalleries.Where(g => g.GalleryName == efGallery.GalleryName).ToList())
                    {
                        if (gallery.New)
                        {
                            gallery.EntryId = efGallery.GalleryID; //This is done especially for the process of file copying...
                        }
                    }
                }
            }

            if (this.Deleted)
                return 0;
            return contractIdSaved;
        }
        public int Save(Statuses Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.Status efStatus = null;
            foreach (TopContractsDAL10.SystemTables.Status Status in Data.Entries)
            {
                if (Status.New)
                {
                    efStatus = new TopContractsEntities.Status();
                }
                else
                {
                    efStatus = context.Statuses.Where(c => c.StatusID == Status.ID).SingleOrDefault();
                }

                if (Status.Deleted == false)
                    efStatus.InitCommonFields(efStatus, Status, efStatus.StatusesLNGs, this.organizationIdentifier);

                if (Status.New)
                    context.Statuses.Add(efStatus);
                else
                {
                    if (Status.Deleted && efStatus != null)
                    {
                        efStatus.DeleteLanguageEntries(efStatus, context.StatusesLNGs, efStatus.StatusesLNGs);
                        //for (int indx = efStatus.StatusesLNGs.Count() - 1; indx >= 0; indx--)
                        //{
                        //    TopContractsEntities.StatusesLNG lng = efStatus.StatusesLNGs.ElementAt(indx);
                        //    context.StatusesLNGs.Remove(lng);
                        //}
                        context.Statuses.Remove(efStatus);
                    }
                }
            }
            return context.SaveChanges();
        }