public ActionResult Bill(int id)
        {
            if (User.IsInRole("contractor")) { return new HttpUnauthorizedResult(); }

            var bill = billsRepository.GetById(id);
            if (bill == null) { return HttpNotFound(); }

            var building = bill.Reserve.Building;

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

            var person = personsRepository.GetPersonByUsername(User.Identity.Name);
            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                if (!building.RepresentativeOfPartOwners.Equals(person)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Pričuva");
            } else if (role == "buildingmanager") {
                if (!building.BuildingManager.LegalPerson.Equals(person as LegalPerson)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetManagerLinks(building, "Pričuva");
            } else if (role == "owner") {
                if (!building.GetOwners().Any(o => o.Oib == person.Oib)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Pričuva");
            }

            var model = new BillMethodModel {
                Bill = Mapper.Map<Bill, BillModel>(bill),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

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

            var legalPerson = personsRepository.GetLegalPersonByUsername(User.Identity.Name);
            if (legalPerson == null) { return new HttpUnauthorizedResult(); }

            IList<Bill> bills = null;
            string role = string.Empty;
            LinksModel links = null;
            if(id > 0) {
                var building = buildingsRepository.GetById(id);

                bills = billsRepository.GetBillsFrom(legalPerson, building.Reserve);
                role = "buildingmanager";
                links = new LinksModel{
                    Id = building.Id,
                    Links = NavLinksGenerator.GetManagerLinks(building, "Upraviteljevi računi")
                };
            } else {
                bills = billsRepository.GetBillsFrom(legalPerson);
            }

            var unpaidBills = bills.Where(b => b.IsPaid == false);
            var paidBills = bills.Where(b => b.IsPaid== true);

            var model = new Models.BuildingManager.BillsModel {
                PaidBills = Mapper.Map<IEnumerable<Bill>, IEnumerable<Models.Finances.BillModel>>(paidBills),
                UnpaidBills = Mapper.Map<IEnumerable<Bill>, IEnumerable<Models.Finances.BillModel>>(unpaidBills),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links,
                BuildingId = id
            };

            return View(model);
        }
        public ActionResult Bills(int id = 0)
        {
            if (!User.IsInRole("owner")) { return new HttpUnauthorizedResult(); }

            var person = personsRepository.GetPersonByUsername(User.Identity.Name);

            IList<Bill> bills = null;
            string role = string.Empty;
            LinksModel links = null;
            if (id > 0) {
                var building = buildingsRepository.GetById(id);
                bills = billsRepository.GetBillsIssuedFor(person, building.Reserve);
                role = "owner";
                links = new LinksModel {
                    Id = building.Id,
                    Links = NavLinksGenerator.GetOwnerLinks(building, "Moji računi")
                };
            } else {
                bills = billsRepository.GetBillsIssuedFor(person);
            }

            var paidBills = bills.Where(b => b.IsPaid).ToList();
            var unpaidBills = bills.Where(b => b.IsPaid == false).ToList();

            var model = new BillsModel {
                PaidBills = Mapper.Map<IList<Bill>, IList<BillModel>>(paidBills),
                UnpaidBills = Mapper.Map<IList<Bill>, IList<BillModel>>(unpaidBills),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(model);
        }
        public ActionResult Votings(int id)
        {
            var building = buildingsRepository.GetById(id);
            if(building == null) {
                return HttpNotFound();
            }

            var votings = adminJobsVotingsRepository.GetByBuilding(building);
            foreach (var voting in votings) {
                if (!voting.IsFinished) {
                    voting.Evaluate();
                    if (voting.IsFinished) {
                        var url = Url.Action("voting", "buildingmanagement", new { Id = voting.Id }, "http");
                        emailNotifier.NotifyOfAdminJobsVotingCompletition(voting, url);
                    }
                }
            }

            var votingsMapped = Mapper.Map<IList<AdministrationJobsVoting>, IList<AdminJobsVotingListModel>>(votings);

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

            var isRepresentative = false;
            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Rad uprave");
                isRepresentative = true;
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Rad uprave");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Rad uprave");
            }

            var model = new VotingsModel() {
                Building = building.Address.ToString(),
                BuidlingId = building.Id,
                Votings = votingsMapped,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links,
                IsRepresentative = isRepresentative
            };

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

            var voting = adminJobsVotingsRepository.GetById(id);
            if(voting == null) {
                return HttpNotFound();
            }

            if(!voting.IsFinished) {
                voting.Evaluate();
                if(voting.IsFinished) {
                    var url = Url.Action("voting", "buildingmanagement", new { Id = voting.Id }, "http");
                    emailNotifier.NotifyOfAdminJobsVotingCompletition(voting, url);
                }
            }

            var currentPerson = personsRepository.GetPersonByUsername(User.Identity.Name);

            var adminJobsVotingDetail = Mapper.Map<AdministrationJobsVoting, AdminJobsVotingDetailModel>(voting);

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

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(voting.Building, "Rad uprave");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(voting.Building, "Rad uprave");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(voting.Building, "Rad uprave");
            }

            var model = new VotingModel() {
                Voting = adminJobsVotingDetail,
                IsUserVoted = voting.IsOwnerVoted(currentPerson),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(model);
        }
        public ActionResult Vote(int id, VoteModel voteModel)
        {
            if (!User.IsInRole("owner")) { return new HttpUnauthorizedResult(); }

            var administrationJobsVoting = adminJobsVotingsRepository.GetById(id);
            if (administrationJobsVoting == null) {
                return HttpNotFound();
            }

            if (ModelState.IsValid) {
                var person = personsRepository.GetPersonByUsername(User.Identity.Name);
                var partitionSpace = partitionSpacesRepository.GetPartitionSpace(person, administrationJobsVoting.Building.LandRegistry);
                if(partitionSpace != null && partitionSpace.IsOwnedPartitionSpace) {
                    try {
                        var votingVoteModel = voteModel.Vote;
                        var ownerVote = new OwnerVote(votingVoteModel.Vote, partitionSpace);
                        administrationJobsVoting.AddVote(ownerVote);

                        return RedirectToAction("voting", new { Id = administrationJobsVoting.Id });

                    } catch (BusinessRulesException ex) {
                        ex.CopyTo(ModelState);
                    }
                } else {
                    ModelState.AddModelError("", "Etaža ne postoji ili niste vlasnik etaže za nevedenu zgradu, stoga ne možete glasati.");
                }

            }

            voteModel.Roles = Roles.GetRolesForUser();
            voteModel.CurrentRole = "owner";

            LinksModel links = new LinksModel();
            if (Session["lastPageId"] != null) {
                links.Id = (int)Session["lastPageId"];
                links.Links = NavLinksGenerator.GetOwnerLinks(administrationJobsVoting.Building, "Rad uprave");
            }

            voteModel.Links = links;

            return View(voteModel);
        }
        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 Index(int id)
        {
            if (User.IsInRole("contractor")) { return new HttpUnauthorizedResult(); }

            var building = buildingsRepository.GetById(id);
            if(building == null) {
                return HttpNotFound();
            }

            var maintenances = maintenancesRepository.GetAllMaintenancesByBuilding(building.Id);
            var newMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.NotStarted);
            var activeMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.InProgress);
            var inConfirmationMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.InConfirmation);
            var completedMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.Completed);

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

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Kvarovi");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Kvarovi");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Kvarovi");
            }

            var model = new IndexModel() {
                NewMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(newMaintenances),
                ActiveMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(activeMaintenances),
                InConfirmationMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(inConfirmationMaintenances),
                CompletedMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(completedMaintenances),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links,
                Building = Mapper.Map<Building, BuildingListModel>(building)
            };

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

            var building = buildingsRepository.GetById(id);
            if (building == null) { return HttpNotFound(); }

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

            var person = personsRepository.GetPersonByUsername(User.Identity.Name);
            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                if (!building.RepresentativeOfPartOwners.Equals(person)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Izdani računi pričuve");
            } else if (role == "buildingmanager") {
                if (!building.BuildingManager.LegalPerson.Equals(person as LegalPerson)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetManagerLinks(building, "Izdani računi pričuve");
            }

            var reserve = building.Reserve;
            var reserveBills = billsRepository.GetIssuedReserveBillsFor(reserve, date.Month, date.Year);
            if(reserveBills == null) {
                return HttpNotFound();
            }

            var unpaidBills = reserveBills.Where(b => b.IsPaid == false).ToList();
            var paidBills = reserveBills.Where(b => b.IsPaid).ToList();

            var model = new ReserveBillsModel {
                Date = date,
                Reserve = Mapper.Map<Reserve, ReserveModel>(reserve),
                UnpaidBills = Mapper.Map<IList<Bill>, IList<BillModel>>(unpaidBills),
                PaidBills = Mapper.Map<IList<Bill>, IList<BillModel>>(paidBills),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

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

            var building = buildingsRepository.GetById(id);
            if (building == null) { return HttpNotFound(); }

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

            var person = personsRepository.GetPersonByUsername(User.Identity.Name);
            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                if (!building.RepresentativeOfPartOwners.Equals(person)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Izdani računi pričuve");
            } else if (role == "buildingmanager") {
                if (!building.BuildingManager.LegalPerson.Equals(person as LegalPerson)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetManagerLinks(building, "Izdani računi pričuve");
            }

            var reserve = building.Reserve;

            var periods = billsRepository.GetReservePeriods(reserve);

            var model = new ReservePeriodsModel {
                Reserve = Mapper.Map<Reserve, ReserveModel>(reserve),
                Periods = periods,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(model);
        }
        public ActionResult Reserve(int id)
        {
            if (User.IsInRole("contractor")) { return new HttpUnauthorizedResult(); }

            var building = buildingsRepository.GetById(id);
            if (building == null) { return HttpNotFound(); }

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

            var person = personsRepository.GetPersonByUsername(User.Identity.Name);
            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                if(!building.RepresentativeOfPartOwners.Equals(person)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Pričuva");
            } else if (role == "buildingmanager") {
                if (!building.BuildingManager.LegalPerson.Equals(person as LegalPerson)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetManagerLinks(building, "Pričuva");
            } else if( role == "owner") {
                if (!building.GetOwners().Any(o => o.Oib == person.Oib)) {
                    return new HttpUnauthorizedResult();
                }

                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Pričuva");
            }

            var reserve = building.Reserve;

            // pokusaj izdati racune
            if(ReserveBilling.IssueMonthlyReserveBills(reserve, billsRepository)) {
                var bills = billsRepository.GetIssuedReserveBillsFor(reserve, DateTime.Now.Month, DateTime.Now.Year);
                foreach (var bill in bills) {
                    var url = Url.Action("bill", "owner", new {Id = bill.Id}, "http");
                    emailNotifier.NotifyOfBilling(bill, url);
                }
            }

            var model = new ReserveMModel {
                Reserve = Mapper.Map<Reserve, ReserveModel>(reserve),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(model);
        }
        public ActionResult PartitionSpace(int id)
        {
            IPartitionSpace partitionSpace = partitionSpacesRepository.GetById(id);
            if(partitionSpace == null) {
                return HttpNotFound();
            }

            PartitionSpaceDetailModel detailModel = Mapper.Map<IPartitionSpace, PartitionSpaceDetailModel>(partitionSpace);

            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");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Zemljišna knjiga");
            }

            var partitionSpaceModel = new PartitionSpaceModel() {
                PartitionSpace = detailModel,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

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

            var landRegistry = landRegistriesRepository.GetById(id);
            if(landRegistry == null) {
                return HttpNotFound();
            }

            var building = buildingsRepository.GetByLandRegistry(landRegistry);

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

            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");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Zemljišna knjiga");
            }

            var landRegistryDetailModel = Mapper.Map<LandRegistry, LandRegistryDetailModel>(landRegistry);
            var landRegistryModel = new LandRegistryModel() {
                LandRegistry = landRegistryDetailModel,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = role,
                Links = links
            };

            return View(landRegistryModel);
        }
        public ActionResult Create(int id)
        {
            var building = buildingsRepository.GetById(id);
            if(building == null) {
                return HttpNotFound();
            }

            var repairServicesModel = Mapper.Map<IEnumerable<RepairService>, IEnumerable<RepairServiceModel>>(repairServicesRepository.GetAll());

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

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, "Kvarovi");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, "Kvarovi");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Kvarovi");
            }

            var model = new Models.BuildingMaintenance.CreateModel() {
                RepairServices = new SelectList(repairServicesModel, "Id", "Name"),
                Urgencies = new SelectList(new[] {
                    new { Id = (int)Urgency.Low, Name = "Niska" },
                    new { Id = (int)Urgency.Normal, Name = "Normalna"},
                    new { Id = (int)Urgency.High, Name = "Visoka"}
                }, "Id", "Name"),
                MaintenanceRequest = new MaintenanceRequestModel(),
                Roles = Roles.GetRolesForUser(),
                Links = links,
                CurrentRole = role,
                Building = building.Address.ToString()
            };

            return View(model);
        }
        public ActionResult Building(int id)
        {
            if (!User.IsInRole("buildingmanager") && !User.IsInRole("representative")) { return new HttpUnauthorizedResult(); }
            string currentRole = string.Empty;
            if (User.IsInRole("representative")) {
                currentRole = "representative";
            } else {
                currentRole = "buildingmanager";
            }

            var building = buildingsRepository.GetById(id);
            if(building == null) {
                return HttpNotFound();
            }

            var votings = adminJobsVotingsRepository.GetByBuilding(building, 5);

            var buildingDetailModel = Mapper.Map<Building, BuildingDetailModel>(building);
            var votingsMapped = Mapper.Map<IList<AdministrationJobsVoting>, IList<AdminJobsVotingListModel>>(votings);

            LinksModel links = new LinksModel() {
                Id = building.Id
            };

            if (currentRole == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(building, string.Empty);
            } else {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(building, string.Empty);
            }

            var buildingModel = new BuildingModel() {
                Building = buildingDetailModel,
                Votings = votingsMapped,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = currentRole,
                Links = links
            };

            Session.Add("role", currentRole);
            Session.Add("lastPageId", building.Id);

            return View(buildingModel);
        }
        public ActionResult Details(int id)
        {
            var maintenance = maintenancesRepository.GetById(id);

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

            string role = (string)Session["role"] ?? string.Empty;
            if (role == "representative") {
                links.Links = NavLinksGenerator.GetRepresentativeLinks(maintenance.Building, "Kvarovi");
            } else if (role == "buildingmanager") {
                links.Links = NavLinksGenerator.GetManagerLinks(maintenance.Building, "Kvarovi");
            } else if (role == "owner") {
                links.Links = NavLinksGenerator.GetOwnerLinks(maintenance.Building, "Kvarovi");
            }

            var model = new DetailsModel() {
                Maintenance = Mapper.Map<Maintenance, MaintenanceDetailModel>(maintenance),
                Roles = Roles.GetRolesForUser(),
                Links = links,
                CurrentRole = role
            };

            return View(model);
        }
        public ActionResult Vote(int id)
        {
            if (!User.IsInRole("owner")) { return new HttpUnauthorizedResult(); }

            var administrationJobsVoting = adminJobsVotingsRepository.GetById(id);
            if(administrationJobsVoting == null) {
                return HttpNotFound();
            }

            var currentPerson = personsRepository.GetPersonByUsername(User.Identity.Name);
            var isCurrentPersonOwner = administrationJobsVoting.Building.GetOwners().Contains(currentPerson);
            if(!isCurrentPersonOwner) {
                return new HttpUnauthorizedResult();
            }

            var votingVoteModel = Mapper.Map<AdministrationJobsVoting, VotingVoteModel>(administrationJobsVoting);

            LinksModel links = new LinksModel();
            if (Session["lastPageId"] != null) {
                links.Id = (int)Session["lastPageId"];
                links.Links = NavLinksGenerator.GetOwnerLinks(administrationJobsVoting.Building, "Rad uprave");
            }

            var model = new VoteModel() {
                Vote = votingVoteModel,
                Roles = Roles.GetRolesForUser(),
                CurrentRole = "owner",
                Links = links
            };

            return View(model);
        }
        public ActionResult My(int id)
        {
            var building = buildingsRepository.GetById(id);
            if (building == null) {
                return HttpNotFound();
            }

            var submitter = personsRepository.GetPersonByUsername(User.Identity.Name);
            var maintenances = maintenancesRepository.GetAllMaintenancesBySubmitter(submitter, building.Id);
            var newMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.NotStarted);
            var activeMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.InProgress);
            var inConfirmationMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.InConfirmation);
            var completedMaintenances = maintenances.Where(m => m.StatusOfMaintenance == StatusOfMaintenance.Completed);

            LinksModel links = new LinksModel();
            if(Session["lastPageId"] != null) {
                links.Id = (int) Session["lastPageId"];
                links.Links = NavLinksGenerator.GetOwnerLinks(building, "Moji kvarovi");
            }

            var model = new IndexModel() {
                NewMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(newMaintenances),
                ActiveMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(activeMaintenances),
                InConfirmationMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(inConfirmationMaintenances),
                CompletedMaintenances = Mapper.Map<IEnumerable<Maintenance>, IEnumerable<MaintenanceDetailModel>>(completedMaintenances),
                Roles = Roles.GetRolesForUser(),
                CurrentRole = "owner",
                Links = links,
                Building = Mapper.Map<Building, BuildingListModel>(building)
            };

            return View("Index", 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);
        }