/// <summary>
        /// Saving the applications table 
        /// </summary>
        /// <param action="Data"></param>
        /// <returns></returns>
        public int Save(Applications Data)
        {
            int result = 0;
            try
            {
                TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
                TopContractsEntities.Application efApplication = null;

                //to avoid duplicacy of application action
                if (Data.Entries.Where(ent => ent.Deleted == false).Select(ent => ent.ApplicationName).Distinct().Count() < Data.Entries.Where(ent => ent.Deleted == false).Select(ent => ent.ApplicationName).Count())
                    throw new ExceptionDataAdminSaveDuplicateApplicationName();

                foreach (TopContractsDAL10.Application Application in Data.Entries)
                {
                    if (Application.New)
                        efApplication = new TopContractsEntities.Application();
                    else
                        efApplication = context.Applications.Where(c => c.ApplicationID == Application.ApplicationID).SingleOrDefault();

                    if (Application.Deleted == false)
                    {
                        efApplication.InitCommonFields(efApplication, Application, this.organizationIdentifier);
                        efApplication.ApplicationID = Application.ApplicationID;
                        efApplication.ApplicationName = Application.ApplicationName;
                        efApplication.URLContract = Application.URLContract;
                        efApplication.URLContractActivity = Application.URLContractActivity;
                        efApplication.URLContractDoc = Application.URLContractDoc;
                        efApplication.URLContractField = Application.URLContractField;
                        efApplication.URLContractTodo = Application.URLContractTodo;
                        efApplication.URLContractUser = Application.URLContractUser;
                        efApplication.OrganizationIdentifier = this.organizationIdentifier;
                    }
                    if (Application.New)
                        context.Applications.Add(efApplication);
                    else
                    {
                        if (Application.Deleted && efApplication != null)
                        {
                            context.Applications.Remove(efApplication);
                        }
                    }
                }
                result = context.SaveChanges();
            }
            catch (Exception ex)
            {
                if (ex.InnerException.ToString().Contains("FK_ContractApplications_Applications"))
                    throw new ExceptionDataAdminSaveDeleteApplicationWithContract();
            }
            return result;
        }
        public int Save(MsgTypes Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.MsgType efMsgType = null;
            foreach (TopContractsDAL10.SystemTables.MsgType MsgType in Data.Entries)
            {
                if (MsgType.New)
                {
                    efMsgType = new TopContractsEntities.MsgType();
                }
                else
                {
                    efMsgType = context.MsgTypes.Where(c => c.MsgTypeID == MsgType.ID).SingleOrDefault();
                }

                if (MsgType.Deleted == false)
                {
                    efMsgType.InitCommonFields(efMsgType, MsgType, efMsgType.MsgTypesLNGs, this.organizationIdentifier);
                    efMsgType.ContractTypesVisibility = MsgType.ContractTypesVisibility;
                    efMsgType.IncludeID1Body = MsgType.IncludeID1Body;
                    efMsgType.IncludeID1Subject = MsgType.IncludeID1Subject;
                    efMsgType.IncludeID2Body = MsgType.IncludeID2Body;
                    efMsgType.IncludeID2Subject = MsgType.IncludeID2Subject;
                    efMsgType.IncludeNameBody = MsgType.IncludeNameBody;
                    efMsgType.IncludeNameSubject = MsgType.IncludeNameSubject;
                    efMsgType.IncludeSysCodeBody = MsgType.IncludeSysCodeBody;
                    efMsgType.IncludeSysCodeSubject = MsgType.IncludeSysCodeSubject;
                    efMsgType.LinkInBody = MsgType.LinkInBody;
                    efMsgType.LinkInSubject = MsgType.LinkInSubject;
                    efMsgType.MsgBodyPrefix = MsgType.MsgBodyPrefix;
                    efMsgType.MsgSubjectPrefix = MsgType.MsgSubjectPrefix;
                }
                if (MsgType.New)
                    context.MsgTypes.Add(efMsgType);
                else
                {
                    if (MsgType.Deleted && efMsgType != null)
                    {
                        efMsgType.DeleteLanguageEntries(efMsgType, context.MsgTypesLNGs, efMsgType.MsgTypesLNGs);
                        //for (int indx = efMsgType.MsgTypesLNGs.Count() - 1; indx >= 0; indx--)
                        //{
                        //    TopContractsEntities.MsgTypesLNG lng = efMsgType.MsgTypesLNGs.ElementAt(indx);
                        //    context.MsgTypesLNGs.Remove(lng);
                        //}
                        context.MsgTypes.Remove(efMsgType);
                    }
                }
            }
            return context.SaveChanges();
        }
        public int Save(Users Data, bool IsWebHosted, ref Dictionary<string, string> updatedUserRecords)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.User efUser = null;
            foreach (TopContractsDAL10.SystemTables.User user in Data.Entries)
            {
                if (user.New)
                {
                    efUser = new TopContractsEntities.User();
                }
                else
                {
                    efUser = context.Users.Where(c => c.UserID == user.ID).SingleOrDefault();
                }

                if (user.Deleted == false)
                {
                    efUser.AddContracts = user.AddContracts;
                    efUser.DefaultRoleID = user.DefaultRoleID;
                    efUser.DeleteContracts = user.DeleteContracts;
                    efUser.AccessIdentifier = user.AccessIdentifier;
                    efUser.UICutlture = user.DefaultCultureIdentifier.Trim();
                    efUser.eMail1 = user.eMail1;
                    efUser.eMail2 = user.eMail2;
                    efUser.FirstName = user.FirstName;
                    efUser.LastName = user.LastName;
                    efUser.MiddleName = user.MiddleName;
                    efUser.Phone1 = user.Phone1;
                    efUser.Phone2 = user.Phone2;
                    efUser.SuperAdmin = user.SuperAdmin;
                    efUser.SysAdmin = user.SysAdmin;
                    efUser.Unrestricted = user.Unrestricted;
                    efUser.Title = user.Title;
                    efUser.UnitID = user.UnitID;
                    efUser.OrganizationIdentifier = this.organizationIdentifier;
                }

                if (user.New)
                    context.Users.Add(efUser);
                else
                {
                    if (user.Deleted && efUser != null)
                    {
                        //Boaz - 13-December-2012
                        foreach (TopContractsEntities.DoneTodo doneTodo in context.DoneTodos.Where(ent => ent.UserID == efUser.UserID))
                            context.DoneTodos.Remove(doneTodo);
                        foreach (TopContractsEntities.ContractTodoRecipient contractTodoRecipient in context.ContractTodoRecipients.Where(ent => ent.UserID == efUser.UserID))
                            context.ContractTodoRecipients.Remove(contractTodoRecipient);
                        //----------
                        //Viplav - 30-Nov-2013
                        foreach (TopContractsEntities.UserUiPref useruipref in context.UserUiPrefs.Where(ent => ent.UserId == efUser.UserID))
                            context.UserUiPrefs.Remove(useruipref);
                        //-----------

                        // Kai Cohen - 05-Dec-2013 - Removes this users entry from Auth DB in case of Web application
                        //if (IsWebHosted)
                        //    RemoveUserEntryFromAuthDB(efUser);
                        //--------------------
                        context.Users.Remove(efUser);
                    }
                }
            }

            int rowsAffected = 0;
            if (IsWebHosted == false)
            {
                rowsAffected = context.SaveChanges();
            }
            else
            {
                ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
                objectContext.DetectChanges();

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

                long changesOccurred = 0;
                foreach (var newField in context.ChangeTracker.Entries())
                {
                    if (newField.Entity is TopContractsEntities.User)
                    {
                        if (newField.State == System.Data.EntityState.Modified)
                        {
                            if (newField.OriginalValues["AccessIdentifier"].ToString() != newField.CurrentValues["AccessIdentifier"].ToString())
                            {
                                if (newField.OriginalValues["AccessIdentifier"].ToString() != newField.CurrentValues["AccessIdentifier"].ToString())
                                    updatedUserRecords.Add(newField.OriginalValues["AccessIdentifier"].ToString(), newField.CurrentValues["AccessIdentifier"].ToString());
                            }

                            changesOccurred += 1;
                        }
                        else if (newField.State == System.Data.EntityState.Added)
                            changesOccurred += 1;
                        //AddUserEntryInAuthDB(newField.Entity as TopContractsEntities.User);
                        else if (newField.State == System.Data.EntityState.Deleted)
                            changesOccurred += 1;
                    }
                }

                if (changesOccurred > 0)
                    ApplicationCachedData.UpdateContractDisplayCacheTicks();

                objectContext.AcceptAllChanges();
            }
            return rowsAffected;
        }
Ejemplo 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();
 }
        public int Save(TopContractsDAL10.SystemTables.Organization Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            TopContractsEntities.Organization efOrg = null;
            foreach (TopContractsDAL10.SystemTables.Unit unit in Data.Entries)
            {
                if (unit.New)
                    efOrg = new TopContractsEntities.Organization();
                else
                    efOrg = context.Organizations.Where(c => c.UnitID == unit.ID).SingleOrDefault();

                if (unit.Deleted == false)
                {
                    efOrg.InitCommonFields(efOrg, unit, efOrg.OrganizationLNGs, this.organizationIdentifier);
                    efOrg.UnitLevel = unit.UnitLevel;
                    efOrg.ParentUnitID = unit.ParentUnitID;
                }

                if (unit.New)
                    context.Organizations.Add(efOrg);
                else
                {
                    if (unit.Deleted && efOrg != null)
                    {
                        efOrg.DeleteLanguageEntries(efOrg, context.OrganizationLNGs, efOrg.OrganizationLNGs);
                        context.Organizations.Remove(efOrg);

                        //List<TopContractsEntities.Organization> childUnits = context.Organizations.Where(p => p.ParentUnitID == unit.ID).ToList();
                        //foreach (TopContractsEntities.Organization child in childUnits)
                        //{
                        //    context.Organizations.Remove(child);
                        //    child.DeleteLanguageEntries(child, context.OrganizationLNGs, child.OrganizationLNGs); 
                        //}
                    }
                }

            }
            return context.SaveChanges();
        }
        /// <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;
        }
Ejemplo 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();
 }
 /// <summary>
 /// Used to save Field Group Role Map when limited Role entered in Role Table
 /// </summary>
 public void SaveFieldGroupsRolesMAPforLimitedUser()
 {
     TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName()); ;
     TopContractsDAL10.SystemTables.FieldGroups fieldGroups = new SystemTables.FieldGroups();
     foreach (TopContractsEntities.FieldGroup fldGrp in context.FieldGroups.Where(fld => fld.RolesVisibility == (byte)FieldGroupRoleVisibility.VisibleToAll))
     {
         fldGrp.RolesVisibility = (byte)FieldGroupRoleVisibility.VisibleToRoles;
         //Adding entries in FieldGroupsRolesMAP according to roles and efFieldGroupsForContractInit
         foreach (TopContractsEntities.Role role in context.Roles.Where(rl => rl.EditFields == null || rl.EditFields == false))
         {
             fldGrp.FieldGroupsRolesMAPs.Add(new FieldGroupsRolesMAP() { OrganizationIdentifier = this.organizationIdentifier, RoleID = role.RoleID, FieldGroupID = fldGrp.FieldGroupID, AllowView = true, AllowEdit = true });
         }
     }
     context.SaveChanges();
 }
        public void SaveEntityFieldsCopyInContract(ContractTypes entities)
        {
            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
            List<long?> recordcounters = new List<long?>();
            TopContractsEntities.FieldGroup entityFieldGroup = null;
            foreach (TopContractsDAL10.SystemTables.ContractType entity in entities.Entries)
            {
                if (context.Fields.Any(fld => fld.LinkedEntityID == entity.ID))
                {
                    if (context.FieldGroupsContractTypesMAPs.Any(f => f.ContractTypeID == entity.ID) && context.FieldGroupsContractTypesMAPs.Count(f => f.ContractTypeID == entity.ID) == 1)
                    {
                        entityFieldGroup = context.FieldGroupsContractTypesMAPs.First(f => f.ContractTypeID == entity.ID).FieldGroup;
                    }
                    // code area to a create a new contract field entry if an entity field has been selected to make a
                    // copy in contract.
                    foreach (ContractTypesFieldsToCreateDefault DefEntFld in entity.DefaultEntityFields.Where(fld => fld.Deleted == false))
                    {
                        //foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => c.ContractType.ParentContractTypeID == ContractTypeContractsID && c.ContractFields.Any(cf => cf.FieldID == DefEntFld.FieldID) == false))
                        foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => c.ContractType.ParentContractTypeID == ContractTypeContractsID && c.ContractFields.Any(cf => cf.CatalogFieldID == DefEntFld.FieldID) == false))
                        {
                            foreach (TopContractsEntities.Field field in context.Fields.Where(fg => fg.LinkedEntityID == DefEntFld.ContractTypeID))
                            {
                                //if (contract.ContractFields.Any(cf => cf.FieldID == DefEntFld.FieldID && cf.FieldGroupID == fieldGroupId) == false)
                                if (contract.ContractFields.Any(cf => cf.CatalogFieldID == DefEntFld.FieldID && cf.FieldGroupID == field.FieldGroupID) == false)
                                {
                                    recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == field.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                    TopContractsEntities.Field efDefFld = context.Fields.Where(defld => defld.FieldID == DefEntFld.FieldID).SingleOrDefault();
                                    if (recordcounters.Count > 0)
                                    {
                                        foreach (long? recordcounter in recordcounters)
                                        {
                                            contract.ContractFields.Add(new TopContractsEntities.ContractField()
                                            {
                                                FieldGroupID = field.FieldGroupID,
                                                FieldID = field.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 = 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)
                                    //    });
                                    //}
                                }
                            }
                        }
                    }

                    foreach (long fieldGroupId in context.Fields.Where(fg => fg.LinkedEntityID == entity.ID).Select(u => u.FieldGroupID))
                    {
                        foreach (TopContractsEntities.Contract contract in context.Contracts.Where(c => c.ContractType.ParentContractTypeID == ContractTypeContractsID))
                        {
                            foreach (TopContractsEntities.ContractField cFld in contract.ContractFields.Where(cf => cf.FieldGroupID == fieldGroupId).ToList())
                            {
                                if (entity.DefaultEntityFields.Any(f => f.FieldID == cFld.FieldID) == false)
                                {
                                    if (context.Fields.Any(f => f.FieldID == cFld.FieldID))
                                    {
                                        TopContractsEntities.FieldGroup fieldgroup = context.Fields.SingleOrDefault(f => f.FieldID == cFld.FieldID).FieldGroup;

                                        if (entityFieldGroup != null)
                                            if (entityFieldGroup.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && entityFieldGroup.FieldGroupsContractTypesMAPs.ToList().Count == 1)
                                            {
                                                if (entityFieldGroup.FieldGroupsContractTypesMAPs.First().ContractType.ParentContractTypeID != ContractTypeContractsID && entityFieldGroup.FieldGroupsContractTypesMAPs.First().ContractTypeID != ContractTypeContractsID)
                                                {
                                                    if (entityFieldGroup.FieldGroupID == fieldgroup.FieldGroupID)
                                                        context.ContractFields.Remove(cFld);
                                                }
                                            }
                                        //if (entities.Cast<TopContractsDAL10.SystemTables.ContractType>().Any(ent => ent.ID == fieldgroup.FieldGroupID))
                                        //{
                                        //    context.ContractFields.Remove(cFld);
                                        //}
                                    }
                                }
                            }
                        }
                    }
                }
            }
            context.SaveChanges();
        }
        public int Save(Currencies Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            TopContractsEntities.Currency efCurrency = null;
            foreach (TopContractsDAL10.SystemTables.Currency Currency in Data.Entries)
            {
                if (Currency.New)
                {
                    efCurrency = new TopContractsEntities.Currency();
                }
                else
                {
                    efCurrency = context.Currencies.Where(c => c.CurrencyID == Currency.ID).SingleOrDefault();
                }
                if (Currency.Deleted == false)
                {
                    efCurrency.InitCommonFields(efCurrency, Currency, efCurrency.CurrenciesLNGs, this.organizationIdentifier);
                    efCurrency.Rate = Currency.Rate;
                    efCurrency.RateDate = Currency.RateDate;
                }

                if (Currency.New)
                {
                    context.Currencies.Add(efCurrency);
                }
                else
                {
                    if (Currency.Deleted && efCurrency != null)
                    {
                        efCurrency.DeleteLanguageEntries(efCurrency, context.CurrenciesLNGs, efCurrency.CurrenciesLNGs);
                        //for (int indx = efCurrency.CurrenciesLNGs.Count() - 1; indx >= 0; indx--)
                        //{
                        //    TopContractsEntities.CurrenciesLNG lng = efCurrency.CurrenciesLNGs.ElementAt(indx);
                        //    context.CurrenciesLNGs.Remove(lng);
                        //}
                        context.Currencies.Remove(efCurrency);
                    }
                }

            }
            return context.SaveChanges();
        }
        public long Save(TopContractsDAL10.DasboardTables.SaveSearch search, bool overwrite, long searchID)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.SaveSearch efSearch = null;
            if (searchID == 0)
            {
                efSearch = new TopContractsEntities.SaveSearch();
            }
            else
            {
                efSearch = context.SaveSearches.Where(c => c.SearchId == searchID).SingleOrDefault();
            }

            efSearch.SearchQuery = search.SearchQuery;
            efSearch.OrganizationIdentifier = this.organizationIdentifier;
            efSearch.Name = search.Name;
            efSearch.Description = search.Description;
            efSearch.DisplayOrder = search.DisplayOrder;
            efSearch.UpdateDate = DateTime.Now;
            efSearch.UpdateUserID = search.UpdateUserId;
            //efSearch.Inactive = true;
            efSearch.Inactive = false;
            if (!overwrite)
            {
                efSearch.UserId = search.UserId;
                efSearch.Shared = search.Shared;
                efSearch.DateCreated = search.CreatedDate;
            }

            //Added By Salil + Boaz
            //1-May-2013
            if (search.searchResultsContracts != null)
                if (search.searchResultsContracts.Entries != null)
                    if (search.searchResultsContracts.Entries.Count > 0)
                    {
                        foreach (TopContractsDAL10.DasboardTables.SearchResultsContract _searchResultsContracts in search.searchResultsContracts.Entries)
                        {
                            context.SearchResultsContracts.Add(new TopContractsEntities.SearchResultsContract() { SearchId = _searchResultsContracts.SearchId, ContractId = _searchResultsContracts.ContractId });
                        }
                    }
            if (!overwrite)
                context.SaveSearches.Add(efSearch);
            context.SaveChanges();
            return Convert.ToInt64(efSearch.SearchId);
        }
        /// <summary>
        /// Updating catalogs (only their names, not their fields or records)
        /// </summary>
        /// <param name="entity">The contract-type defining the catalog</param>
        /// <param name="newEntryTexts">Name/Description of the catalog</param>
        /// <param name="CreateNew">true to signal the creation of a new catalog, false for updating existing catalogs</param>
        public void SaveEntityGroup(TopContractsEntities.ContractType entity, Dictionary<string, SysTableEntryText> newEntryTexts, bool CreateNew)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.FieldGroup efFieldGroup = new TopContractsEntities.FieldGroup();

            //efFieldGroup.InitCommonFields(efFieldGroup, entity, efFieldGroup.FieldGroupsLNGs);
            if (CreateNew)
            {
                foreach (var cTypeLng in entity.ContractTypesLNGs)
                {
                    efFieldGroup.FieldGroupsLNGs.Add(new FieldGroupsLNG { OrganizationIdentifier = this.organizationIdentifier, CultureId = cTypeLng.CultureId, DescShort = cTypeLng.DescShort, DescLong = cTypeLng.DescLong });
                }
                efFieldGroup.OrganizationIdentifier = this.organizationIdentifier;
                efFieldGroup.ContractTypeVisibility = (byte)FieldGroupContractTypeVisibility.VisibleToTypes;
                efFieldGroup.RolesVisibility = (byte)FieldGroupRoleVisibility.VisibleToAll;
                efFieldGroup.Inactive = false;
                efFieldGroup.Locked = false;
                efFieldGroup.DisplayOrder = null;
                efFieldGroup.ExternalID = null;
                efFieldGroup.SingleRecord = true;
                efFieldGroup.FieldGroupsContractTypesMAPs.Add(new FieldGroupsContractTypesMAP { OrganizationIdentifier = this.organizationIdentifier, ContractTypeID = entity.ContractTypeID });
                context.FieldGroups.Add(efFieldGroup);
            }
            else
            {
                // code to modify action of the entity field group when action of an entity is changed.
                if (context.FieldGroupsContractTypesMAPs.Any(u => u.ContractTypeID == entity.ContractTypeID))
                {
                    List<FieldGroupsLNG> fgLngs = context.FieldGroupsContractTypesMAPs.SingleOrDefault(u => u.ContractTypeID == entity.ContractTypeID).FieldGroup.FieldGroupsLNGs.ToList();
                    foreach (var fgLng in fgLngs)
                    {
                        fgLng.DescShort = newEntryTexts.SingleOrDefault(l => l.Key == fgLng.CultureId.Trim()).Value.DescShort;
                        fgLng.DescLong = newEntryTexts.SingleOrDefault(l => l.Key == fgLng.CultureId.Trim()).Value.DescLong;
                    }
                }
            }
            context.SaveChanges();
        }
        public int SaveEntities(ContractTypes Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.ContractType efContractType = null;
            foreach (TopContractsDAL10.SystemTables.ContractType contractType in Data.Entries)
            {
                if (contractType.New)
                {
                    efContractType = new TopContractsEntities.ContractType();
                }
                else
                {
                    efContractType = context.ContractTypes.Where(c => c.ContractTypeID == contractType.ID).SingleOrDefault();
                    if (contractType.Deleted == false)
                    {
                        SaveDefaultEntityFields(contractType.DefaultEntityFields.ToList(), contractType.ID);
                        SaveEntityGroup(efContractType, contractType.EntryTexts, false);
                    }
                }

                if (contractType.Deleted == false)
                {
                    efContractType.InitCommonFields(efContractType, contractType, efContractType.ContractTypesLNGs, this.organizationIdentifier);
                    //                    efContractType.SelectorFieldID = (contractType.SelectorField == (long)DefaultValues.SingleRecordID ? null : contractType.SelectorField); // To be used in case of entities
                    //Boaz 26-02-2013
                    efContractType.SelectorFieldID = (contractType.SelectorField == (long)DefaultValues.SingleRecordID || contractType.SelectorField == 0 ? null : contractType.SelectorField); // To be used in case of entities
                }

                if (contractType.New)
                {
                    context.ContractTypes.Add(efContractType);
                    context.SaveChanges(); // Saved the new contract type so that contract type id must be created and used.
                    SaveEntityGroup(efContractType, contractType.EntryTexts, true); // to create a new field group and to map it with current contract type.
                }
                else
                {
                    if (contractType.Deleted && efContractType != null)
                    {
                        // Need to be discussed with Boaz, whether the catalog type fields will be removed or LinkedEntityField will be removed.
                        foreach (TopContractsEntities.Field efFld in context.Fields.Where(fld => fld.LinkedEntityID == contractType.ID).ToList())
                        {
                            efFld.DeleteLanguageEntries(efFld, context.FieldsLNGs, efFld.FieldsLNGs);
                            context.Fields.Remove(efFld);
                        }

                        //Deleted entity records,entity fields,entity,FieldGroupsContractTypesMAPs,and Contract type --Added by deepak dhamija (28/02/2013)
                        efContractType.DeleteLanguageEntries(efContractType, context.ContractTypesLNGs, efContractType.ContractTypesLNGs);
                        FieldGroupsContractTypesMAP cTypeMap = context.FieldGroupsContractTypesMAPs.SingleOrDefault(map => map.ContractTypeID == efContractType.ContractTypeID);
                        if (cTypeMap != null)
                        {
                            //******************Condition Implemented for delete entry from FieldGroupsRolesMAP by Viplav on 15-Oct-2013********//
                            if (context.FieldGroupsRolesMAPs.Any(ent => ent.FieldGroupID == cTypeMap.FieldGroupID))
                            {
                                List<FieldGroupsRolesMAP> FieldGroupsRoleMap = context.FieldGroupsRolesMAPs.Where(t => t.FieldGroupID == cTypeMap.FieldGroupID).ToList();
                                foreach (FieldGroupsRolesMAP item in FieldGroupsRoleMap)
                                    context.FieldGroupsRolesMAPs.Remove(item);
                            }
                            //********************************************************************************************************************
                            TopContractsEntities.FieldGroup entityGroup = context.FieldGroups.SingleOrDefault(eg => eg.FieldGroupID == cTypeMap.FieldGroupID);
                            if (entityGroup != null)
                            {
                                List<TopContractsEntities.Field> entityfield = context.Fields.Where(ef => ef.FieldGroupID == cTypeMap.FieldGroupID).ToList();
                                if (entityfield != null)
                                {
                                    foreach (var flds in entityfield)
                                    {
                                        flds.DeleteLanguageEntries(flds, context.FieldsLNGs, flds.FieldsLNGs);
                                        if (flds.FieldType == (byte)FieldTypes.ListSingle)
                                        {
                                            foreach (TopContractsEntities.FieldListItem fldListItem in context.FieldListItems.Where(lstItem => lstItem.FieldID == flds.FieldID))
                                            {
                                                fldListItem.DeleteLanguageEntries(fldListItem, context.FieldListItemsLNGs, fldListItem.FieldListItemsLNGs);
                                                context.FieldListItems.Remove(fldListItem);
                                            }
                                        }

                                        foreach (TopContractsEntities.ContractField ctFld in context.ContractFields.Where(f => f.CatalogFieldID == flds.FieldID).ToList())
                                        {
                                            context.ContractFields.Remove(ctFld);
                                        }
                                        context.Fields.Remove(flds);
                                    }
                                }
                                entityGroup.DeleteLanguageEntries(entityGroup, context.FieldGroupsLNGs, entityGroup.FieldGroupsLNGs);
                                context.FieldGroups.Remove(entityGroup);
                            }
                            context.FieldGroupsContractTypesMAPs.Remove(cTypeMap);
                        }
                        context.ContractTypes.Remove(efContractType);
                        //end code--deepak dhamija
                        //Commented by deepak dhamija because its not deleted the entity records and its not verify the LinkedEntityID Exist or not.  
                        //efContractType.DeleteLanguageEntries(efContractType, context.ContractTypesLNGs, efContractType.ContractTypesLNGs);
                        //FieldGroupsContractTypesMAP cTypeMap = context.FieldGroupsContractTypesMAPs.SingleOrDefault(map => map.ContractTypeID == efContractType.ContractTypeID);
                        //if (cTypeMap != null)
                        //{
                        //    TopContractsEntities.FieldGroup entityGroup = context.FieldGroups.SingleOrDefault(eg => eg.FieldGroupID == cTypeMap.FieldGroupID);
                        //    if (entityGroup != null)
                        //    {
                        //        entityGroup.DeleteLanguageEntries(entityGroup, context.FieldGroupsLNGs, entityGroup.FieldGroupsLNGs);
                        //        context.FieldGroups.Remove(entityGroup);
                        //    }
                        //    context.FieldGroupsContractTypesMAPs.Remove(cTypeMap);
                        //}
                        //context.ContractTypes.Remove(efContractType);


                        if (context.ContractTypesFieldsToCreateDefaults.Any(ct => ct.ContractTypeID == contractType.ID))
                        {
                            foreach (TopContractsEntities.ContractTypesFieldsToCreateDefault defEntFld in context.ContractTypesFieldsToCreateDefaults.Where(ct => ct.ContractTypeID == contractType.ID).ToList())
                            {
                                context.ContractTypesFieldsToCreateDefaults.Remove(defEntFld);
                            }
                        }
                    }
                }
            }
            return context.SaveChanges();
        }
        public int Save(ReportsInfoes Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.ReprotsInfo efReportInfo = null;
            foreach (TopContractsDAL10.SystemTables.ReportsInfo reportInfo in Data.Entries)
            {
                if (reportInfo.New)
                {
                    efReportInfo = new TopContractsEntities.ReprotsInfo();
                    efReportInfo.ReportID = new Guid();
                }
                else
                {
                    efReportInfo = context.ReprotsInfoes.Where(c => c.ReportID == new Guid(reportInfo.ReportID)).SingleOrDefault();
                }

                if (reportInfo.Deleted == false)
                {
                    efReportInfo.Data = reportInfo.Data;
                    efReportInfo.ExternalReport = reportInfo.ExternalReport;
                    efReportInfo.Locked = reportInfo.Locked;
                    efReportInfo.Name = reportInfo.Name;
                    efReportInfo.ReportID = new Guid(reportInfo.ReportID);
                    efReportInfo.Shared = reportInfo.Shared;
                    efReportInfo.Type = reportInfo.Type;
                    efReportInfo.UserId = reportInfo.UserId;
                    efReportInfo.OrganizationIdentifier = this.organizationIdentifier;
                }

                if (reportInfo.New)
                    context.ReprotsInfoes.Add(efReportInfo);
                else
                {
                    if (reportInfo.Deleted && efReportInfo != null)
                    {
                        context.ReprotsInfoes.Remove(efReportInfo);
                    }
                }
            }
            return context.SaveChanges();
        }
        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();
        }
        public void AddEntityRecordsForNewEntityFields()
        {
            //long ContractTypeContractsID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;
            long ContractTypeContractsID = Utilities.contractTypeContractsID; // Code implemented by Viplav on 17 june 2013 for remove webconfig concept
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            foreach (TopContractsEntities.FieldGroup efFldGrp in context.FieldGroups.Where(fgrp => fgrp.ContractTypeVisibility == (byte)FieldGroupContractTypeVisibility.VisibleToTypes && fgrp.FieldGroupsContractTypesMAPs.Any(cTypeMap => cTypeMap.ContractType.ParentContractTypeID != ContractTypeContractsID || cTypeMap.ContractType.ParentContractTypeID == null) == true))
            {
                List<long> contractIDs = new List<long>();

                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 || c.ContractType.ParentContractTypeID == null)).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 || c.ContractType.ParentContractTypeID == null)))
                        {
                            if (contractIDs.Contains(contract.ContractID))
                            {
                                //recordcounters = contract.ContractFields.Where(fg => fg.FieldGroupID == efFldGrp.FieldGroupID).Select(c => c.RecordCounter).Distinct().ToList();
                                //if (recordcounters.Count != 0)
                                //{
                                //    foreach (long? recordcounter in recordcounters)
                                //    {
                                //        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)
                                });
                                //}

                            }
                        }
                    }
            }
            context.SaveChanges();
        }
        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();
        }
        public int Save(DashboardCs Data, int UpdatingUserID)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.DashboardElement efdashboardElement = null;
            TopContractsEntities.DashboardElementDisplayValue efdashboardElementDisplayValue = null;
            foreach (TopContractsDAL10.DasboardTables.DashboardC dashboard in Data.Entries)
            {
                foreach (TopContractsDAL10.DasboardTables.DashboardElement dashboardElement in dashboard.DashboardElement.Entries)
                {
                    if (dashboardElement.New)
                    {
                        efdashboardElement = new TopContractsEntities.DashboardElement();
                    }
                    else
                    {
                        efdashboardElement = context.DashboardElements.Where(c => c.DashboardElementId == dashboardElement.ID).SingleOrDefault();
                    }

                    if (dashboardElement.Deleted == false)
                    {
                        efdashboardElement.OrganizationIdentifier = this.organizationIdentifier;
                        efdashboardElement.SearchId = dashboardElement.SearchId;
                        efdashboardElement.DashboardId = dashboardElement.DashboardId;
                        efdashboardElement.DisplaySeriesType = dashboardElement.DisplaySeriesType;
                        efdashboardElement.DisplaySizeHeight = dashboardElement.DisplaySizeHeight;
                        efdashboardElement.DisplaySizeWidth = dashboardElement.DisplaySizeWidth;
                        efdashboardElement.XAxis = dashboardElement.XAxis;
                        efdashboardElement.YAxis = dashboardElement.YAxis;
                        efdashboardElement.ElementType = dashboardElement.ElementType;
                        efdashboardElement.DisplayProperties = dashboardElement.DisplayProperties;
                        efdashboardElement.ScaleFormula = dashboardElement.ScaleFormula;
                        efdashboardElement.ScaleName = dashboardElement.ScaleName;
                        efdashboardElement.ChartTitle = dashboardElement.ChartTitle;
                        foreach (TopContractsDAL10.DasboardTables.DashboardElementDisplayValue DEValues in dashboardElement.DashboardElementDisplayValues.Entries)
                        {
                            if (DEValues.New)
                            {
                                efdashboardElementDisplayValue = new TopContractsEntities.DashboardElementDisplayValue();
                            }
                            else
                            {
                                efdashboardElementDisplayValue = context.DashboardElementDisplayValues.Where(c => c.DashboardElementDisplayId == DEValues.ID).SingleOrDefault();
                            }
                            if (DEValues.Deleted == false)
                            {
                                efdashboardElementDisplayValue.OrganizationIdentifier = this.organizationIdentifier;
                                efdashboardElementDisplayValue.DashboardElementId = DEValues.DashboardElementId;
                                efdashboardElementDisplayValue.Name = DEValues.Name;
                                efdashboardElementDisplayValue.Description = DEValues.Description;
                                efdashboardElementDisplayValue.Formula = DEValues.Formula;
                                efdashboardElementDisplayValue.DisplayOrder = DEValues.DisplayOrder;
                            }
                            if (DEValues.New)
                                efdashboardElement.DashboardElementDisplayValues.Add(efdashboardElementDisplayValue);
                            else
                            {
                                if (DEValues.Deleted && efdashboardElementDisplayValue != null)
                                {
                                    efdashboardElement.DashboardElementDisplayValues.Remove(efdashboardElementDisplayValue);
                                    context.DashboardElementDisplayValues.Remove(efdashboardElementDisplayValue);
                                }
                            }
                        }
                    }

                    if (dashboardElement.New)
                    {
                        efdashboardElement.UpdateDate = DateTime.Now;
                        efdashboardElement.UpdateUserID = UpdatingUserID;
                        context.DashboardElements.Add(efdashboardElement);
                    }
                    else
                    {
                        if (dashboardElement.Deleted && efdashboardElement != null)
                        {
                            foreach (TopContractsDAL10.DasboardTables.DashboardElementDisplayValue DEValues in dashboardElement.DashboardElementDisplayValues.Entries)
                            {
                                efdashboardElementDisplayValue = context.DashboardElementDisplayValues.Where(c => c.DashboardElementDisplayId == DEValues.ID).SingleOrDefault();
                                efdashboardElement.DashboardElementDisplayValues.Remove(efdashboardElementDisplayValue);
                                context.DashboardElementDisplayValues.Remove(efdashboardElementDisplayValue);
                            }
                            context.DashboardElements.Remove(efdashboardElement);
                        }
                    }
                }
            }

            bool updateDashBoardUpdateDetails = false;
            foreach (var entry in context.ChangeTracker.Entries())
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Modified)
                {
                    updateDashBoardUpdateDetails = true;
                }
            }
            if (updateDashBoardUpdateDetails)
                foreach (var entry in context.ChangeTracker.Entries())
                {
                    if (entry.Entity is TopContractsEntities.DashboardElement && (entry.State == EntityState.Added || entry.State == EntityState.Modified))
                    {
                        ((TopContractsEntities.DashboardElement)entry.Entity).UpdateDate = DateTime.Now;
                        ((TopContractsEntities.DashboardElement)entry.Entity).UpdateUserID = UpdatingUserID;
                    }
                }

            return context.SaveChanges();
        }
Ejemplo n.º 19
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();
        }
        public int Save(CurrencyRates Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            TopContractsEntities.CurrencyConversionRate efCurrency = null;
            foreach (TopContractsDAL10.SystemTables.CurrencyRate Currency in Data.Entries)
            {
                if (Currency.New)
                {
                    efCurrency = new TopContractsEntities.CurrencyConversionRate();
                }
                else
                {
                    efCurrency = context.CurrencyConversionRates.Where(c => c.CurrencyConversionRatesID == Currency.ID).SingleOrDefault();
                }
                if (Currency.Deleted == false)
                {
                    efCurrency.OrganizationIdentifier = this.organizationIdentifier;
                    efCurrency.CurrencyID = Currency.CurrencyID;
                    efCurrency.Rate = Currency.Rate;
                    efCurrency.RateDate = Convert.ToDateTime(Currency.RateDate);
                    efCurrency.DateCreated = DateTime.Now;
                }

                if (Currency.New)
                {
                    context.CurrencyConversionRates.Add(efCurrency);
                }
                else
                {
                    if (Currency.Deleted && efCurrency != null)
                    {
                        context.CurrencyConversionRates.Remove(efCurrency);
                    }
                }

            }
            return context.SaveChanges();
        }
Ejemplo 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();
 }
        /// <summary>
        /// This function will add the new dashboard
        /// </summary>
        /// <param action="objDashboardC"></param>
        /// <returns></returns>
        public int Save(TopContractsDAL10.DasboardTables.DashboardC objDashboardC)
        {
            int rtrnVal = 0;
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());

            Logger.WriteGeneralVerbose("DataHandler class - Save", "check for dashboard existance for userid {0}", objDashboardC.UserId);
            var dashBoardExist = context.Dashboards.FirstOrDefault(t => t.UserId == objDashboardC.UserId);
            if (dashBoardExist == null) //create new dashbaord over here
            {
                TopContractsEntities.Dashboard efDashBoard = new TopContractsEntities.Dashboard();

                efDashBoard.OrganizationIdentifier = this.organizationIdentifier;
                efDashBoard.Name = objDashboardC.Name;
                efDashBoard.Description = objDashboardC.Description;
                efDashBoard.UserId = objDashboardC.UserId;
                efDashBoard.DisplayOrder = objDashboardC.DisplayOrder;
                efDashBoard.UpdateDate = objDashboardC.UpdateDate;
                efDashBoard.UpdateUserID = objDashboardC.UpdateUserId;
                efDashBoard.Locked = objDashboardC.Locked;
                efDashBoard.Inactive = true;
                efDashBoard.ExternalID = objDashboardC.ExternalID;

                context.Dashboards.Add(efDashBoard);
                rtrnVal = context.SaveChanges();
                Logger.WriteGeneralVerbose("DataHandler class - Save", "dashboard is created for user with id {0}", rtrnVal);
            }
            return rtrnVal;
        }
Ejemplo 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();
 }
        public int Save(ContractTypes Data)
        {
            TopContractsV01Entities context = new TopContractsV01Entities(Utilities.getTestEnvName());
            TopContractsEntities.ContractType efContractType = null;
            foreach (TopContractsDAL10.SystemTables.ContractType contractType in Data.Entries)
            {
                if (contractType.New)
                {
                    efContractType = new TopContractsEntities.ContractType();
                    //efContractType.ParentContractTypeID = ConfigurationProvider.Default.ContractTypeContracts.ContractTypeContractsID;  //This parent value is used to save ContractParentID in database to make every new ContractType as Contracts
                    efContractType.ParentContractTypeID = Utilities.contractTypeContractsID;  // Code implemented by Viplav on 17 june 2013 for remove webconfig concept.
                }
                else
                {
                    efContractType = context.ContractTypes.Where(c => c.ContractTypeID == contractType.ID).SingleOrDefault();
                }

                if (contractType.Deleted == false)
                {
                    efContractType.InitCommonFields(efContractType, contractType, efContractType.ContractTypesLNGs, this.organizationIdentifier);
                    //efContractType.SelectorFieldID = contractType.SelectorField; // To be used in case of entities
                }

                if (contractType.New)
                    context.ContractTypes.Add(efContractType);
                else
                {
                    if (contractType.Deleted && efContractType != null)
                    {
                        efContractType.DeleteLanguageEntries(efContractType, context.ContractTypesLNGs, efContractType.ContractTypesLNGs);
                        context.ContractTypes.Remove(efContractType);
                    }
                }
            }
            return context.SaveChanges();
        }