/// <summary>
        /// Updates the contractor.
        /// </summary>
        /// <param name="contractorPrimitive">The contractor primitive.</param>
        public void CreateOrUpdateContractor(ContractorPrimitive contractorPrimitive)
        {
            try
              {
            using (SmartWorkingEntities context = new SmartWorkingEntities())
            {
              Contractor contractor = contractorPrimitive.GetEntity();

              Contractor existingObject = context.Contractors.Where(x => x.Id == contractor.Id).FirstOrDefault();

              //no record of this item in the DB, item being passed in has a PK
              if (existingObject == null && contractor.Id > 0)
              {
            throw new FaultException<ExceptionDetail>(new ExceptionDetail(new Exception("Błąd zapisu do bazy")),
                                                        "Obiekt nie istniał w bazie, a jego Id jest większe od 0.");
              }
              //Item has no PK value, must be new
              else if (contractor.Id <= 0)
              {
            context.Contractors.AddObject(contractor);
              }
              //Item was retrieved, and the item passed has a valid ID, do an update
              else
              {
            context.Contractors.ApplyCurrentValues(contractor);
              }

              context.SaveChanges();
            }
              }
              catch (Exception e)
              {
            throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message);
              }
        }
 /// <summary>
 /// Deletes the contractor.
 /// </summary>
 /// <param name="contractorPrimitive">The contractor primitive.</param>
 public void DeleteContractor(ContractorPrimitive contractorPrimitive)
 {
     try
       {
     //TODO: if is not used in any DeliveryNotes than delete.
     using (SmartWorkingEntities context = new SmartWorkingEntities())
     {
       Contractor contractor = context.Contractors.Where(x => x.Id == contractorPrimitive.Id).FirstOrDefault();
       if (contractor != null)
       {
     contractor.Deleted = DateTime.Now;
     context.SaveChanges();
       }
       else
       {
     throw new Exception("This contractor does not exist in db.");
       }
     }
       }
       catch (Exception e)
       {
     throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message);
       }
 }
        public static ContractorPrimitive GetPrimitive(this Contractor entity)
        {
            ContractorPrimitive primitive = new ContractorPrimitive();

                primitive.Id = entity.Id;

                primitive.InternalName = entity.InternalName;

                primitive.Name = entity.Name;

                primitive.ZIPCode = entity.ZIPCode;

                primitive.City = entity.City;

                primitive.Street = entity.Street;

                primitive.HouseNo = entity.HouseNo;

                primitive.Deleted = entity.Deleted;

                primitive.Phone = entity.Phone;

                primitive.Deactivated = entity.Deactivated;

                return primitive;
        }
 public ContractorPrimitive EditContractor(IModalDialogService modalDialogService, IServiceFactory serviceFactory,
     ContractorPrimitive contractorToEdit)
 {
     var viewModel = new UpdateContractorViewModel(modalDialogService, serviceFactory);
       viewModel.Contractor = contractorToEdit;
       viewModel.DialogMode = DialogMode.Update;
       ModalDialogHelper<UpdateContractor>.ShowDialog(viewModel);
       return viewModel.Contractor;
 }
        public static ContractorPrimitive GetPrimitiveCopy(this ContractorPrimitive primitiveToCopy)
        {
            if (primitiveToCopy == null) return null;

                ContractorPrimitive primitive = new ContractorPrimitive();

                primitive.Id = primitiveToCopy.Id;

                primitive.InternalName = primitiveToCopy.InternalName;

                primitive.Name = primitiveToCopy.Name;

                primitive.ZIPCode = primitiveToCopy.ZIPCode;

                primitive.City = primitiveToCopy.City;

                primitive.Street = primitiveToCopy.Street;

                primitive.HouseNo = primitiveToCopy.HouseNo;

                primitive.Deleted = primitiveToCopy.Deleted;

                primitive.Phone = primitiveToCopy.Phone;

                primitive.Deactivated = primitiveToCopy.Deactivated;

                return primitive;
        }