public ActionResult ChangeOwner(int id, ChangeOwnerModel model)
        {
            if (!User.IsInRole("buildingmanager") && !User.IsInRole("representative")) {
                return new HttpUnauthorizedResult();
            }

            var partitionSpace = partitionSpacesRepository.GetById(id);
            if (partitionSpace == null) {
                return HttpNotFound();
            }

            if (!partitionSpace.IsOwnedPartitionSpace) {
                return new HttpStatusCodeResult(405);
            }

            if(ModelState.IsValid) {
                var person = personsRepository.GetByOib(model.NewOwner.Oib);
                if(person != null) {
                    partitionSpace.Owner = person;
                } else {
                    Person newPerson = null;
                    if(string.IsNullOrEmpty(model.NewOwner.Surname)) {
                        newPerson = new LegalPerson(model.NewOwner.Oib, model.NewOwner.Name);
                    } else {
                        newPerson = new PhysicalPerson(model.NewOwner.Oib, model.NewOwner.Name, model.NewOwner.Surname);
                    }

                    var city = citiesRepository.GetById(model.NewOwner.Address.City.Id);
                    newPerson.Address = new Address(model.NewOwner.Address.StreetAddress, model.NewOwner.Address.StreetAddressNumber, city);

                    var ownerRole = rolesRepository.GetRole("owner");
                    var contractorRole = rolesRepository.GetRole("contractor");
                    var managerRole = rolesRepository.GetRole("buildingmanager");

                    var previousOwner = partitionSpace.Owner;
                    var previousOwnerUser = housingMgmtUsersRepository.GetUserByPerson(previousOwner);
                    if(previousOwnerUser.Roles.Contains(contractorRole) || previousOwnerUser.Roles.Contains(managerRole)) {
                        previousOwnerUser.RemoveRole(ownerRole);
                    }

                    partitionSpace.Owner = newPerson;
                    var newOwnerUser = housingMgmtUsersRepository.GetUserByPerson(newPerson);
                    if(newOwnerUser != null && !newOwnerUser.Roles.Contains(ownerRole)) {
                        newOwnerUser.AddRole(ownerRole);
                    }

                }

                return RedirectToAction("partitionspace", new {Id = id});
            }

            LinksModel links = new LinksModel();
            if (Session["lastPageId"] != null) {
                links.Id = (int)Session["lastPageId"];
            }

            var building = buildingsRepository.GetByPartitionSpace(partitionSpace);

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Zemljišna knjiga");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Zemljišna knjiga");
            }

            model.NewOwner.Address.Cities = new SelectList(citiesRepository.GetAll(), "Id", "Name", model.NewOwner.Address.City.Id);
            model.Roles = Roles.GetRolesForUser();
            model.CurrentRole = role;
            model.Links = links;

            return View(model);
        }
        public ActionResult ChangeOwner(int id)
        {
            if(!User.IsInRole("buildingmanager") && !User.IsInRole("representative")) {
                return new HttpUnauthorizedResult();
            }

            var partitionSpace = partitionSpacesRepository.GetById(id);
            if(partitionSpace == null) {
                return HttpNotFound();
            }

            if(!partitionSpace.IsOwnedPartitionSpace) {
                return new HttpStatusCodeResult(405);
            }

            LinksModel links = new LinksModel();
            if (Session["lastPageId"] != null) {
                links.Id = (int)Session["lastPageId"];
            }

            var building = buildingsRepository.GetByPartitionSpace(partitionSpace);

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Zemljišna knjiga");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Zemljišna knjiga");
            }

            var newOwner = new PhysicalPersonModel {
                Address = new AddressModel() {
                    Cities = new SelectList(citiesRepository.GetAll(), "Id", "Name")
                }
            };

            var model = new ChangeOwnerModel {
                NewOwner = newOwner,
                PartitionSpace = Mapper.Map<PartitionSpace, PartitionSpaceDetailModel>(partitionSpace),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(model);
        }