コード例 #1
0
        public static List <ThisEntity> GetAll(Guid parentUid, ref List <ContactPoint> orphanContacts)
        {
            ThisEntity        entity = new ThisEntity();
            List <ThisEntity> list   = new List <ThisEntity>();

            try
            {
                using (var context = new EntityContext())
                {
                    List <EM.Entity_Address> results = context.Entity_Address
                                                       .Where(s => s.Entity.EntityUid == parentUid)
                                                       .OrderByDescending(s => s.IsPrimaryAddress)
                                                       .ThenBy(s => s.Id)
                                                       .ToList();

                    if (results != null && results.Count > 0)
                    {
                        foreach (EM.Entity_Address item in results)
                        {
                            entity = new ThisEntity();
                            MapFromDB(item, entity);
                            if (entity.HasAddress() == false && entity.HasContactPoints())
                            {
                                orphanContacts.AddRange(entity.ContactPoint);
                            }
                            list.Add(entity);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + string.Format(".GetAll. Guid parentUid: {0}", parentUid));
            }
            return(list);
        }        //
コード例 #2
0
        public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status)
        {
            bool isValid = true;

            if (!IsValidGuid(parentUid))
            {
                status.AddError("Error: a valid parent identifier was not provided.");
                return(false);
            }

            int    count  = 0;
            Entity parent = EntityManager.GetEntity(parentUid);

            if (parent == null || parent.EntityBaseId == 0)
            {
                status.AddError("Error - the parent entity was not found.");
                return(false);
            }

            try
            {
                using (var context = new EntityContext())
                {
                    DBEntity efEntity = new DBEntity();
                    if (ValidateProfile(entity, parent, ref status) == false)
                    {
                        return(false);
                    }
                    bool resetIsPrimaryFlag = false;

                    if (entity.Id == 0)
                    {
                        //add
                        efEntity          = new DBEntity();
                        efEntity.EntityId = parent.Id;
                        entity.ParentId   = parent.Id;
                        MapToDB(entity, efEntity, ref resetIsPrimaryFlag);

                        //could just have contact points without address
                        if (entity.HasAddress())
                        {
                            efEntity.Created = efEntity.LastUpdated = DateTime.Now;
                            if (IsValidGuid(entity.RowId))
                            {
                                efEntity.RowId = entity.RowId;
                            }
                            else
                            {
                                efEntity.RowId = Guid.NewGuid();
                            }

                            context.Entity_Address.Add(efEntity);
                            count = context.SaveChanges();

                            //update profile record so doesn't get deleted
                            entity.Id       = efEntity.Id;
                            entity.ParentId = parent.Id;
                            entity.RowId    = efEntity.RowId;
                            if (count == 0)
                            {
                                status.AddError(string.Format(" Unable to add address. parentUid: {0}, City: {1}, Region: {2} <br\\> ", parentUid, efEntity.City, efEntity.Region));
                            }
                            else
                            {
                                if (resetIsPrimaryFlag)
                                {
                                    Reset_Prior_ISPrimaryFlags(efEntity.EntityId, entity.Id);
                                }
                            }
                        }
                        //handle contact points
                        //if address present, these need to be closely related
                        if (entity.Id > 0)
                        {
                            new Entity_ContactPointManager().SaveList(entity.ContactPoint, entity.RowId, ref status);
                        }
                        else
                        {
                            // put under parent
                            //should log this. If under parent should onlybe an org, and delete all has already been done.
                            new Entity_ContactPointManager().SaveList(entity.ContactPoint, parentUid, ref status, false);
                        }
                    }
                    else
                    {
                        entity.ParentId = parent.Id;

                        efEntity = context.Entity_Address.SingleOrDefault(s => s.Id == entity.Id);
                        if (efEntity != null && efEntity.Id > 0)
                        {
                            entity.RowId = efEntity.RowId;
                            //update
                            MapToDB(entity, efEntity, ref resetIsPrimaryFlag);
                            //has changed?
                            if (HasStateChanged(context))
                            {
                                efEntity.LastUpdated = System.DateTime.Now;

                                count = context.SaveChanges();
                            }
                            if (resetIsPrimaryFlag)
                            {
                                Reset_Prior_ISPrimaryFlags(entity.ParentId, entity.Id);
                            }

                            //handle contact points - very wierd approach, but shouldn't have updates
                            new Entity_ContactPointManager().SaveList(entity.ContactPoint, entity.RowId, ref status);
                        }
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
            {
                string message = HandleDBValidationError(dbex, thisClassName + ".Save() ", "Address Profile");
                status.AddError("Error - the save was not successful. " + message);
                LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId));
                isValid = false;
            }
            catch (Exception ex)
            {
                string message = FormatExceptions(ex);
                status.AddError("Error - the save was not successful. " + message);
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId));
                isValid = false;
            }
            return(isValid);
        }
コード例 #3
0
        public static void MapToDB(ThisEntity from, EM.Entity_Address to, ref bool resetIsPrimaryFlag)
        {
            resetIsPrimaryFlag = false;

            //NOTE: the parentId - currently orgId, is handled in the update code
            to.Id   = from.Id;
            to.Name = from.Name;
            //if this address is primary, and not previously primary, set indicator to reset existing settings
            //will need setting to default first address to primary if not entered
            if (from.IsMainAddress && ( bool )(!(to.IsPrimaryAddress ?? false)))
            {
                //initially attempt to only allow adding new primary,not unchecking
                resetIsPrimaryFlag = true;
            }
            to.IsPrimaryAddress = from.IsMainAddress;

            //bool hasChanged = false;
            //bool hasAddress = false;

            //if ( from.HasAddress() )
            //{
            //	hasAddress = true;
            //	if ( to.Latitude == null || to.Latitude == 0
            //	  || to.Longitude == null || to.Longitude == 0 )
            //		hasChanged = true;
            //}
            //if ( hasChanged == false )
            //{
            //	if ( to.Id == 0 )
            //		hasChanged = true;
            //	else
            //		hasChanged = HasAddressChanged( from, to );
            //}

            to.Address1            = from.Address1;
            to.Address2            = GetData(from.Address2, null);
            to.PostOfficeBoxNumber = GetData(from.PostOfficeBoxNumber, null);
            to.City       = from.City;
            to.PostalCode = from.PostalCode;
            to.Region     = from.AddressRegion ?? "";
            to.Country    = from.Country;
            //likely provided
            to.Latitude  = from.Latitude;
            to.Longitude = from.Longitude;

            if (from.HasAddress())
            {
                //check if lat/lng were not provided with address
                //may want to always do this to expand region!
                if (to.Latitude == null || to.Latitude == 0 ||
                    to.Longitude == null || to.Longitude == 0 ||
                    (to.Region ?? "").Length == 2
                    )
                {
                    UpdateGeo(from, to);
                }
            }

            //these will likely not be present?
            //If new, or address has changed, do the geo lookup
            //if ( hasAddress )
            //{
            //	if ( hasChanged )
            //	{
            //		UpdateGeo( from, to );
            //	}
            //}
            //else
            //{
            //	to.Latitude = 0;
            //	to.Longitude = 0;
            //}
        }