public HttpResponseMessage Get(int id)
		{
			using (var session = NHibernateHelper.CreateSessionFactory())
			{
				using (var transaction = session.BeginTransaction())
				{
					Renewal entity;

					if (id == 0)
					{
						entity = new Renewal();
					}
					else
					{
						// collect objects
						var data = new List<Renewal>(session.CreateCriteria(typeof(Renewal)).List<Renewal>());

						// collect single object
						entity = data.FirstOrDefault(x => x.Id == id);
					}

					// trim excess
					if (entity != null)
					{
						entity.Transactions.Clear();
						entity.RenewalType = null;
					}

					// return as HttpResponseMessage
					return WebApiHelper.ObjectToHttpResponseMessage(entity);
				}
			}
		}
 public virtual void AddRenewal(Renewal renewal)
 {
     renewal.Registration = this;
     Renewals.Add(renewal);
 }
		void DbInit()
		{
			NHibernateHelper.CreateSessionFactory(NHibernateHelper.Dialect.Sqlite, true);

			using (var session = NHibernateHelper.CreateSessionFactory())
			{
				using (var transaction = session.BeginTransaction())
				{
					// Add state
					var state01 = new State { Name = "Federal Capital Territory", Active = true };

					// Add locations
					var state01Location01 = new Location { Name = "Abaji" };
					var state01Location02 = new Location { Name = "Abuja" };
					var state01Location03 = new Location { Name = "Gwagwalada" };
					var state01Location04 = new Location { Name = "Kuje" };
					var state01Location05 = new Location { Name = "Bwari" };
					var state01Location06 = new Location { Name = "Kwali" };

					// logical add of locations
					AddLocationsToState(state01,
						state01Location01,
						state01Location02,
						state01Location03,
						state01Location04,
						state01Location05,
						state01Location06
					);


					// insert categories (3 categories)
					var category01 = new Category { Name = "Urban"};
					var category02 = new Category { Name = "Semi-Urban" };
					var category03 = new Category { Name = "Informal" };

					// add costing (insert some category prices for each)
					var costing01 = new Costing { EffectiveDate = DateTime.Now, RegistrationCost = 300.00M, RenewalCost = 200.00M, Category = category01, State = state01,};
					var costing02 = new Costing { EffectiveDate = DateTime.Now, RegistrationCost = 200.00M, RenewalCost = 100.00M, Category = category02, State = state01, };
					var costing03 = new Costing { EffectiveDate = DateTime.Now, RegistrationCost = 100.00M, RenewalCost = 50.00M, Category = category03, State = state01, };

					AddCostingToCategory(category01, costing01);
					AddCostingToCategory(category02, costing02);
					AddCostingToCategory(category03, costing03);

					// insert permissions
					var permission01 = new Permission { Name = "Search Registrations" };
					var permission02 = new Permission { Name = "Perform Registrations" };
					var permission03 = new Permission { Name = "Manage Payments" };
					var permission04 = new Permission { Name = "Manage Masterfiles" };

					session.SaveOrUpdate(permission01);
					session.SaveOrUpdate(permission02);
					session.SaveOrUpdate(permission03);
					session.SaveOrUpdate(permission04);

					// insert roles
					var role01 = new Role { Name = "Admin"};
					var role02 = new Role { Name = "Clerk" };

					session.SaveOrUpdate(role01);
					session.SaveOrUpdate(role02);

					var perms = new List<Permission>(session.CreateCriteria(typeof(Permission)).List<Permission>());
					foreach (var p in perms)
					{
						var rp = new RolePermission
						{
							Role = role01,
							Permission = p
						};
						session.SaveOrUpdate(rp);
					}


					// insert user (state, role, permission)
					var user01 = new User
					{
						FirstName = "FCT Admin",
						LastName = "",
						Username = "******",
						Password = "******",
						EmailAddress = "*****@*****.**",
						Mobile = "",
						Active = true,
						Created = DateTime.Now,
						Modified = DateTime.Now,
						Roles = new List<Role>
						{
							role01,
							role02
						},
						State = state01
					};

					var user02 = new User
					{
						FirstName = "Lyall",
						LastName = "van der Linde",
						Username = "******",
						Password = "******",
						EmailAddress = "*****@*****.**",
						Mobile = "0793874802",
						Active = true,
						Created = DateTime.Now,
						Modified = DateTime.Now,
						Roles = new List<Role>
						{
							role02
						},
						State = state01
					};

					// add services
					var service01 = new Service { Name = "Doctors" };
					var service02 = new Service { Name = "Radiology" };
					var service03 = new Service { Name = "Pathology" };
					var service04 = new Service { Name = "Theatre" };

					session.SaveOrUpdate(service01);
					session.SaveOrUpdate(service02);
					session.SaveOrUpdate(service03);
					session.SaveOrUpdate(service04);

					// add staffings
					var staffings01 = new Staffing { Name = "Medical Doctor" };
					var staffings02 = new Staffing { Name = "Nurse" };
					var staffings03 = new Staffing { Name = "Medical Lab Assistant" };
					var staffings04 = new Staffing { Name = "Pharmacist" };

					session.SaveOrUpdate(staffings01);
					session.SaveOrUpdate(staffings02);
					session.SaveOrUpdate(staffings03);
					session.SaveOrUpdate(staffings04);

					// register establishment (location, category, renewal)
					var categories = new List<Category>(session.CreateCriteria(typeof(Category)).List<Category>());
					var category = categories.FirstOrDefault(x => x.Name.Equals("Urban"));

					var locations = new List<Location>(session.CreateCriteria(typeof(Location)).List<Location>());
					var location = locations.FirstOrDefault(x => x.State.Id == 1);

					var estType = new TypeOfEstablishment ();
					estType.Name = "Hospital";

					estType.AddStaff (staffings01);
					estType.AddStaff (staffings02);
					estType.AddStaff (staffings03);
					estType.AddStaff (staffings04);

					session.Save (estType);

					var body = new ProfessionalBody ();
					body.Name = "MEDICAL AND DENTAL COUNCIL OF NIGERIA (MDCN)";
					session.Save (body);

					//Staffing

					var registration = new Registration
					{
						Category = category,
						Location = location,
						LastRenewalDate = null,
						ProfessionalBodyAttendance = "",
						ProfessionalBodyFirstName = "",
						ProfessionalBodyLastName = "",
						ProfessionalBodyRemarks = "",
						ProfessionalBodyPosition = "",
						MedicalDirectorFirstName = "",
						AddressLine1 = "",
						AddressLine2 = "",
						MedicalDirectorEmailAddress = "",
						AcceptanceDetailsReason = "",
						ProprietorGender = "",
						ProprietorFirstName = "",
						ProprietorNinNumber = "",
						ProfessionalBodyInvolvement = "",
						ProprietorEmailAddress = "",
						Lga = "",
						CacNumber = "",
						MedicalDirectorNinNumber = "",
						MedicalDirectorLastName = "",
						RegistrationDate = DateTime.Now,
						MedicalDirectorGender = "",
						EstablishmentName = "",
						PhermcRegistrationNumber = "",
						AdministratorMobile1 = "",
						ProprietorIsMedicalDirector = false,
						AcceptanceDetailsAccepted = true,
						ProprietorMobile1 = "",
						MedicalDirectorMobile1 = "",
						LandMark = "",
						AdministratorFirstName = "",
						AdministratorLastName = "",
						AdministratorMobile2 = "",
						AmountPaid = 200.00M,
						Created = DateTime.Now,
						Deleted = false,
						MedicalDirectorMobile2 = "",
						Modified = DateTime.Now,
						NoOfBeds = 10,
						PaymentMethod = null,
						ProfessionalBodySignatureDate = DateTime.Now,
						ProprietorLastName = "",
						ProprietorMobile2 = "",
						ReferenceNumber = "",
						RegistrationCosts = 0,

						TypeOfEstablishment = estType,
						ProfessionalBody = body
					};

					// add services
					var services = new List<Service>(session.CreateCriteria(typeof(Service)).List<Service>());
					var servicesToInsert = services.Where(x => x.Id >= 1);
					foreach (var s in servicesToInsert)
					{
						var rs = new RegistrationService
						{
							Registration = registration,
							Service = s,
							Selected = true
						};
						registration.AddRegistrationService(rs);
					}




					// add staffing
					var numbersToAdd = 0;
					foreach (var s in estType.Staffings) {
						
						var rts = new RegistrationTypeOfEstablishmentStaffing ();
						rts.TypeOfEstablishment = estType;
						rts.Staffing = s;
						rts.NumberOfStaff = ++numbersToAdd;

						registration.AddRegistrationTypeOfEstablishmentStaffing (rts);
					}



					// insert transactionTypes
					var renewalType01 = new RenewalType
					{
						Name = "Registration"
					};
					var renewalType02 = new RenewalType
					{
						Name = "Renewal"
					};
					session.SaveOrUpdate(renewalType01);
					session.SaveOrUpdate(renewalType02);

					// insert paymentTypes
					var paymentTypes01 = new PaymentType
					{
						Name = "Cash"
					};
					var paymentTypes02 = new PaymentType
					{
						Name = "Bank Deposit"
					};
					session.SaveOrUpdate(paymentTypes01);
					session.SaveOrUpdate(paymentTypes02);

					var renewalType = new List<RenewalType>(session.CreateCriteria(typeof(RenewalType)).List<RenewalType>());
					var renewalTypeToInsert = renewalType.FirstOrDefault(x => x.Id >= 1);

					// insert a renewal
					var renewal = new Renewal
					{
						Registration = registration,
						Date = DateTime.Now,
						Amount = 200.00M,
						Paid = false,
						RenewalType = renewalTypeToInsert
					};

					// insert transactionTypes
					var transactionType01 = new TransactionType
					{
						Name = "Registration"
					};
					var transactionType02 = new TransactionType
					{
						Name = "Renewal"
					};
					session.SaveOrUpdate(transactionType01);
					session.SaveOrUpdate(transactionType02);

					// insert a payment
					var transactionType = new List<TransactionType>(session.CreateCriteria(typeof(TransactionType)).List<TransactionType>());
					var transactionTypeToInsert = transactionType.FirstOrDefault(x => x.Id >= 1);

					var newTransaction = new Transaction
					{
						Amount = 200.00M,
						Date = DateTime.Now,
						Renewal = renewal,
						TransactionType = transactionTypeToInsert,
						PaymentType =  paymentTypes01,
						ReceivedFrom = "Alison"
					};

					session.SaveOrUpdate(state01);
					session.SaveOrUpdate(category01);
					session.SaveOrUpdate(category02);
					session.SaveOrUpdate(category03);

					session.SaveOrUpdate(user01);
					session.SaveOrUpdate(user02);

					session.SaveOrUpdate(registration);
					//session.SaveOrUpdate(renewal);
					//session.SaveOrUpdate(newTransaction);


                    // *************

                    var q1 = new Question();
                    q1.Query = "A";

                    var q2 = new Question();
                    q2.Query = "B";

                    session.SaveOrUpdate(q1);
                    session.SaveOrUpdate(q2);

                    var survey = new Survey();
                    survey.Name = "Lyall";
                    survey.Surname = "van der Linde";
                    survey.EmailAddress = "*****@*****.**";
                    survey.MobileNumber = "0716541254";
                    survey.Registration = registration;

                    session.SaveOrUpdate(survey);

                    var surveyQuestion = new SurveyQuestion();
                    surveyQuestion.Survey = survey;
                    surveyQuestion.Question = q1.Query;
                    surveyQuestion.Rating = 4.2;

                    session.SaveOrUpdate(surveyQuestion);

                    surveyQuestion = new SurveyQuestion();
                    surveyQuestion.Survey = survey;
                    surveyQuestion.Question = q2.Query;
                    surveyQuestion.Rating = 5.3;

                    session.SaveOrUpdate(surveyQuestion);

                    // *************

					transaction.Commit();
				}
			}

		}
        public HttpResponseMessage Post([FromBody]JToken body)
		{

		    using (var session = NHibernateHelper.CreateSessionFactory())
		    {
		        using (var transaction = session.BeginTransaction())
		        {
                    if (AuthHelper.JwtAuth() != 1)
                    {
                        return WebApiHelper.ObjectToHttpResponseMessage("No token was supplied", System.Net.HttpStatusCode.Unauthorized);
                    }


                    var stateId = Convert.ToInt32(AuthHelper.GetKey("stateId"));

                    var id = body.Value<decimal>("Id");

		            var r = PopulateRegistration(body);

                    var registration = new Registration();
                    Renewal renewal = null;

                    if (id != 0) 
                    {
                        // Existing Registration
                        var registrations = new List<Registration>(session.CreateCriteria(typeof(Registration)).List<Registration>());
                        registration = registrations.FirstOrDefault(x => x.Id == id);
                    }
                    else
                    {
                        // New Registration
                        var renewalType = new List<RenewalType>(session.CreateCriteria(typeof(RenewalType)).List<RenewalType>());
                        var renewalTypeToInsert = renewalType.FirstOrDefault(x => x.Id >= 1);

                        var costing = new List<Costing>(session.CreateCriteria(typeof(Costing)).List<Costing>());
                        var costingTypeToInsert = costing.FirstOrDefault(x => x.Category.Id == r.Category.Id && x.State.Id == stateId);

                        renewal = new Renewal
                        {
                            Date = DateTime.Now,
                            RenewalType = renewalTypeToInsert,
                            Registration = registration,
                            Amount = costingTypeToInsert.RenewalCost
                        };

                        registration.Renewals.Add(renewal);
                    }

                    if (registration != null)
                    {
                        //
                        registration.CacNumber = r.CacNumber ?? "";
                        var isRenewal = r.Renewals.FirstOrDefault(x => x.Id == 0) != null ? true : false;
                        if (isRenewal)
                        {
                            registration.LastRenewalDate = DateTime.Now;

                            var renewalType = new List<RenewalType>(session.CreateCriteria(typeof(RenewalType)).List<RenewalType>());
                            var renewalTypeToInsert = renewalType.FirstOrDefault(x => x.Id >= 2);

                            var costing = new List<Costing>(session.CreateCriteria(typeof(Costing)).List<Costing>());
                            var costingTypeToInsert = costing.FirstOrDefault(x => x.Category.Id == r.Category.Id && x.State.Id == stateId);

                            renewal = new Renewal
                            {
                                Date = DateTime.Now,
                                RenewalType = renewalTypeToInsert,
                                Registration = registration,
                                Amount = costingTypeToInsert.RenewalCost
                            };
                            registration.Renewals.Add(renewal);
                        }
                        else
                        {
                            registration.LastRenewalDate = r.LastRenewalDate;
                        }

                        registration.RegistrationDate = r.RegistrationDate;
                        registration.ProprietorFirstName = r.ProprietorFirstName ?? "";
                        registration.ProprietorLastName = r.ProprietorLastName ?? "";
                        registration.ProprietorGender = r.ProprietorGender ?? "";
                        registration.ProprietorNinNumber = r.ProprietorNinNumber ?? "";
                        registration.ProprietorIsMedicalDirector = r.ProprietorIsMedicalDirector;
                        registration.ProprietorMobile1 = r.ProprietorMobile1 ?? "";
                        registration.ProprietorMobile2 = r.ProprietorMobile2 ?? "";
                        registration.ProprietorEmailAddress = r.ProprietorEmailAddress ?? "";

                        registration.MedicalDirectorFirstName = r.MedicalDirectorFirstName ?? "";
                        registration.MedicalDirectorLastName = r.MedicalDirectorLastName ?? "";
                        registration.MedicalDirectorGender = r.MedicalDirectorGender;
                        registration.MedicalDirectorNinNumber = r.MedicalDirectorNinNumber ?? "";
                        registration.MedicalDirectorMobile1 = r.MedicalDirectorMobile1 ?? "";
                        registration.MedicalDirectorMobile2 = r.MedicalDirectorMobile2 ?? "";
                        registration.MedicalDirectorEmailAddress = r.MedicalDirectorEmailAddress ?? "";

                        registration.AdministratorFirstName = r.AdministratorFirstName ?? "";
                        registration.AdministratorLastName = r.AdministratorLastName ?? "";
                        registration.AdministratorMobile1 = r.AdministratorMobile1 ?? "";
                        registration.AdministratorMobile2 = r.AdministratorMobile2 ?? "";

                        registration.EstablishmentName = r.EstablishmentName ?? "";

                        registration.TypeOfEstablishment = r.TypeOfEstablishment;

                        registration.ProfessionalBody = r.ProfessionalBody;

                        registration.NoOfBeds = r.NoOfBeds;

                        registration.AddressLine1 = r.AddressLine1 ?? "";
                        registration.AddressLine2 = r.AddressLine2 ?? "";
                        registration.LandMark = r.LandMark ?? "";

                        registration.Latitude = r.Latitude;
                        registration.Longitude = r.Longitude;

                        // Add 
                        foreach (var rs in r.RegistrationTypeOfEstablishmentStaffing)
                        {
                            rs.Registration = registration;
                        }
                        registration.RegistrationTypeOfEstablishmentStaffing = r.RegistrationTypeOfEstablishmentStaffing;
						

                        foreach (var rs in r.RegistrationServices)
                        {
                            var registrationService = registration.RegistrationServices.FirstOrDefault(x => x.Service.Name == rs.Service.Name);

                            if (registrationService != null)
                            {
                                registrationService.Selected = rs.Selected;
                            }
                            else
                            {
                                var serviceToAdd = session.CreateCriteria(typeof(Service)).List<Service>().FirstOrDefault(x => x.Name == rs.Service.Name);

                                var regService = new RegistrationService
                                {
                                    Registration = registration,
                                    Service = serviceToAdd,
                                    Selected = rs.Selected
                                };
                                // if not in db, add it
                                registration.AddRegistrationService(regService);
                            }

                        }

                        registration.ProfessionalBodyAttendance = r.ProfessionalBodyAttendance ?? "";
                        registration.ProfessionalBodyInvolvement = r.ProfessionalBodyInvolvement ?? "";
                        registration.ProfessionalBodyRemarks = r.ProfessionalBodyRemarks ?? "";
                        registration.ProfessionalBodyFirstName = r.ProfessionalBodyFirstName ?? "";
                        registration.ProfessionalBodyLastName = r.ProfessionalBodyLastName ?? "";
                        registration.ProfessionalBodyPosition = r.ProfessionalBodyPosition ?? "";
                        registration.ProfessionalBodySignatureDate = r.ProfessionalBodySignatureDate;

                        registration.AcceptanceDetailsAccepted = r.AcceptanceDetailsAccepted;
                        registration.AcceptanceDetailsReason = r.AcceptanceDetailsReason ?? "";

                        registration.Category =
                            (from x in session.CreateCriteria(typeof(Category)).List<Category>()
                             where x.Id == r.Category.Id
                             select x).FirstOrDefault();

                        registration.Location =
                            (from x in session.CreateCriteria(typeof(Location)).List<Location>()
                             where x.Id == r.Location.Id
                             select x).FirstOrDefault();

                        registration.Created = DateTime.Now;
                        registration.Modified = DateTime.Now;
                        registration.Deleted = false;
                    }

                    session.SaveOrUpdate(registration);
                    if (renewal != null)
                    {
                        session.SaveOrUpdate(renewal);
                    }
                    transaction.Commit();

                    return Get(registration.Id);
		        }
		    }
		}
 public virtual void AddRenewal(Renewal renewal)
 {
     renewal.Registration = this;
     Renewals.Add(renewal);
 }