Beispiel #1
0
        public override PlaceChangeRequest GetInstance(ISession session, ManagedSecurityContext sec)
        {
            PlaceChangeRequest instance = base.GetInstance(session, sec);

            instance.Name        = this.Name;
            instance.Type        = ManagedPlaceType.Find(session, this.Type);
            instance.Description = this.Description;
            instance.Street      = this.Street;
            instance.Zip         = this.Zip;
            instance.CrossStreet = this.CrossStreet;
            instance.Phone       = this.Phone;
            instance.Fax         = this.Fax;
            instance.Email       = this.Email;
            if (Id == 0)
            {
                instance.Account = GetOwner(session, AccountId, sec);
            }
            instance.City = (!string.IsNullOrEmpty(City))
                ? ManagedCity.FindOrCreate(session, City, State, Country)
                : null;
            instance.Neighborhood = (!string.IsNullOrEmpty(Neighborhood) && !string.IsNullOrEmpty(City))
                ? ManagedNeighborhood.FindOrCreate(session, Neighborhood, City, State, Country)
                : null;
            if (Id == 0)
            {
                instance.Place = session.Load <Place>(PlaceId);
            }
            return(instance);
        }
Beispiel #2
0
        public int Merge(ManagedSecurityContext sec, int id)
        {
            if (!sec.IsAdministrator())
            {
                throw new ManagedAccount.AccessDeniedException();
            }

            if (id == mInstance.Id)
            {
                throw new Exception("Cannot merge city into self");
            }

            int count = 0;

            City merge = Session.Load <City>(id);

            // update accounts
            IList accounts = Session.CreateCriteria(typeof(Account))
                             .Add(Expression.Eq("City", merge.Name))
                             .List();

            count += accounts.Count;
            foreach (Account account in accounts)
            {
                account.City = mInstance.Name;
                Session.Save(account);
            }

            // update account addresses
            IList accountaddresses = Session.CreateCriteria(typeof(AccountAddress))
                                     .Add(Expression.Eq("City", merge.Name))
                                     .List();

            count += accountaddresses.Count;
            foreach (AccountAddress accountaddress in accountaddresses)
            {
                accountaddress.City = mInstance.Name;
                Session.Save(accountaddress);
            }

            // merge neighborhoods
            foreach (Neighborhood nh in merge.Neighborhoods)
            {
                Neighborhood t_nh = Session.CreateCriteria(typeof(Neighborhood))
                                    .Add(Expression.Eq("City.Id", mInstance.Id))
                                    .Add(Expression.Eq("Name", nh.Name))
                                    .UniqueResult <Neighborhood>();

                if (t_nh != null)
                {
                    ManagedNeighborhood m_nh = new ManagedNeighborhood(Session, t_nh);
                    count += m_nh.Merge(sec, nh.Id);
                }
                else
                {
                    nh.City = mInstance;
                    Session.Save(nh);
                }
            }

            // merge places that don't have a neighborhood
            if (merge.Places != null)
            {
                count += merge.Places.Count;
                foreach (Place place in merge.Places)
                {
                    place.City = mInstance;
                    Session.Save(place);
                }
            }

            if (merge.PlaceChangeRequests != null)
            {
                count += merge.PlaceChangeRequests.Count;
                foreach (PlaceChangeRequest request in merge.PlaceChangeRequests)
                {
                    request.City = mInstance;
                    Session.Save(request);
                }
            }

            Session.Delete(merge);
            return(count);
        }