public static RecipePackage GetPackageCopy(this RecipePackage source)
 {
     RecipePackage package = new RecipePackage();
       package.Recipe = source.Recipe.GetPrimitiveCopy();
       foreach (RecipeComponentAndMaterialPackage recipeComponentAndMaterialPackage in source.RecipeComponentAndMaterialList)
       {
     package.RecipeComponentAndMaterialList.Add(recipeComponentAndMaterialPackage.GetPackageCopy());
       }
       return package;
 }
        public void CreateOrUpdateRecipePackage(RecipePackage recipePackage)
        {
            try
              {
            if (recipePackage != null && recipePackage.Recipe != null)
            {
              using (SmartWorkingEntities context = new SmartWorkingEntities())
              {
            Recipe existingObject = context.Recipes.Include("RecipeComponents").Where(x => x.Id == recipePackage.Recipe.Id).FirstOrDefault();

            //no record of this item in the DB, item being passed in has a PK
            if (existingObject == null && recipePackage.Recipe.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

            //Item has no PK value, must be new);
            if (recipePackage.Recipe.Id <= 0)
            {
              Recipe recipe = recipePackage.Recipe.GetEntity();
              context.Recipes.AddObject(recipe);
              foreach (RecipeComponentAndMaterialPackage recipeComponentAndMaterialPackage in recipePackage.RecipeComponentAndMaterialList)
              {
                  RecipeComponentPrimitive recipeComponentPrimitive =
                    recipeComponentAndMaterialPackage.GetRecipeComponentPrimitiveWithReference();
                  if (recipeComponentPrimitive != null)
                  {
                    recipeComponentPrimitive.Id = 0;
                    RecipeComponent recipeComponent= recipeComponentPrimitive.GetEntity();
                    recipeComponent.Recipe = recipe;
                    context.RecipeComponents.AddObject(recipeComponent);
                  }
              }
            }
            //Item was retrieved, and the item passed has a valid ID, do an update
            else
            {
              List<RecipeComponentPrimitive> existingRecipeComponents = existingObject.RecipeComponents.Select(x => x.GetPrimitive()).ToList();
              List<RecipeComponentPrimitive> newRecipeComponents = recipePackage.GetRecipeComponentListWithReference().ToList();
              List<RecipeComponentPrimitive> theSameElements = newRecipeComponents.Where(x => existingRecipeComponents.Select(y => y.Id).Contains(x.Id)).ToList();

              existingRecipeComponents.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id));
              newRecipeComponents.RemoveAll(x => theSameElements.Select(y => y.Id).Contains(x.Id));

              //remove
              if (existingRecipeComponents.Count() > 0)
              {
                List<int> existingRecipeComponentIds = existingRecipeComponents.Select(x => x.Id).ToList();
                List<RecipeComponent> recipeComponentListToDelete =
                  context.RecipeComponents.Where(x => existingRecipeComponentIds.Contains(x.Id)).ToList();

                foreach (RecipeComponent recipeComponent in recipeComponentListToDelete)
                {
                  context.RecipeComponents.DeleteObject(recipeComponent);
                }
              }

              //add
              foreach (RecipeComponentPrimitive newRecipeComponent in newRecipeComponents)
              {
                context.RecipeComponents.AddObject(newRecipeComponent.GetEntity());
              }

              context.Recipes.ApplyCurrentValues(recipePackage.Recipe.GetEntity());
            }

            context.SaveChanges();

            //TODO: FOR THE FUTURE
            //if (recipePackage.Recipe.Id > 0)
            //{
            //  existingObject.Deleted = DateTime.Now;
            //  context.Recipes.ApplyCurrentValues(existingObject);
            //}
            //Recipe recipe = recipePackage.Recipe.GetEntity();
            //recipe.Id = 0;
            //context.Recipes.AddObject(recipe);
            //foreach (
            //  RecipeComponentAndMaterialPackage recipeComponentAndMaterialPackage in
            //    recipePackage.RecipeComponentAndMaterialList)
            //{
            //  recipeComponentAndMaterialPackage.RecipeComponent.Id = 0;
            //  RecipeComponent recipeComponent =
            //    recipeComponentAndMaterialPackage.GetRecipeComponentPrimitiveWithReference().GetEntity();
            //  recipeComponent.Recipe = recipe;
            //  context.RecipeComponents.AddObject(recipeComponent);
            //}
            //context.SaveChanges();
              }
            }
            else
            {
              throw new Exception("str_Inpute parameter was wrong.");
            }
              }
              catch (Exception e)
              {
            throw new FaultException<ExceptionDetail>(new ExceptionDetail(e), e.Message);
              }
        }
 public RecipePackage EditRecipe(IModalDialogService modalDialogService, IServiceFactory serviceFactory,
     RecipePackage recipePackage)
 {
     var viewModel = new UpdateRecipeViewModel(modalDialogService, serviceFactory);
       viewModel.RecipePackage = recipePackage;
       viewModel.DialogMode = DialogMode.Update;
       ModalDialogHelper<UpdateRecipe>.ShowDialog(viewModel);
       return viewModel.RecipePackage;
 }
        public static RecipePackage GetRecipesPackage(this Recipe recipe)
        {
            RecipePackage result = new RecipePackage();
              if (recipe != null)
            result.Recipe = recipe.GetPrimitive();
              foreach (RecipeComponent recipeComponent in recipe.RecipeComponents)
              {
            if (recipeComponent != null)
              result.RecipeComponentAndMaterialList.Add(recipeComponent.GetRecipeComponentAndMaterialPackage());
              }

              return result;
        }