public SavePostureConstraintResponse SavePostureConstraint(SavePostureConstraintRequest request)
        {
            try
            {
                var postureConstraint = request.MapTo<PostureConstraint>();

                if (request.Id == 0)
                {
                    var posture = new Posture { Id = request.PostureId };
                    DataContext.Postures.Attach(posture);
                    postureConstraint.Posture = posture;
                    foreach (var id in request.RelationIds)
                    {
                        var desiredState = new DesiredState { Id = id };
                        DataContext.DesiredStates.Attach(desiredState);
                        postureConstraint.DesiredStates.Add(desiredState);
                    }
                    DataContext.PostureConstraints.Add(postureConstraint);
                }
                else
                {
                    postureConstraint = DataContext.PostureConstraints.Include(x => x.DesiredStates).First(x => x.Id == request.Id);
                    request.MapPropertiesToInstance<PostureConstraint>(postureConstraint);
                    postureConstraint.DesiredStates = new List<DesiredState>();
                    foreach (var id in request.RelationIds)
                    {
                        var desiredState = DataContext.DesiredStates.Local.FirstOrDefault(x => x.Id == id);
                        if (desiredState == null)
                        {
                            desiredState = new DesiredState { Id = id };
                            DataContext.DesiredStates.Attach(desiredState);
                        }
                        postureConstraint.DesiredStates.Add(desiredState);
                    }

                }
                DataContext.SaveChanges();
                return new SavePostureConstraintResponse
                {
                    IsSuccess = true,
                    Message = "The item has been saved successfully",
                    Id = postureConstraint.Id,
                    Definition = postureConstraint.Definition,
                    RelationIds = postureConstraint.DesiredStates.Select(x => x.Id).ToArray()
                };
            }
            catch
            {
                return new SavePostureConstraintResponse
                {
                    IsSuccess = false,
                    Message = "An error occured, please contact the administrator for further information"
                };
            }
        }
 public SavePlanningBlueprintResponse SavePlanningBlueprint(SavePlanningBlueprintRequest request)
 {
     try
     {
         if (request.Id == 0)
         {
             var planningBluePrint = request.MapTo<PlanningBlueprint>();
             var environmentsScanning = new EnvironmentsScanning();
             var businessPostureIdentification = new BusinessPostureIdentification { IsLocked = true };
             var midtermPhaseFormulation = new MidtermPhaseFormulation { IsLocked = true };
             var midtermStrategyPlanning = new MidtermStrategyPlanning { IsLocked = true };
             var constructionPosture = new Posture { Type = PostureType.Construction };
             var operationPosture = new Posture { Type = PostureType.Operation };
             var decommissioningPosture = new Posture { Type = PostureType.Decommissioning };
             businessPostureIdentification.Postures.Add(constructionPosture);
             businessPostureIdentification.Postures.Add(operationPosture);
             businessPostureIdentification.Postures.Add(decommissioningPosture);
             planningBluePrint.EnvironmentsScanning = environmentsScanning;
             planningBluePrint.BusinessPostureIdentification = businessPostureIdentification;
             planningBluePrint.MidtermPhaseFormulation = midtermPhaseFormulation;
             planningBluePrint.MidtermStragetyPlanning = midtermStrategyPlanning;
             foreach (var keyOutputId in request.KeyOutputIds)
             {
                 var keyOutputConfig = new KeyOutputConfiguration { Id = keyOutputId };
                 DataContext.KeyOutputConfigs.Attach(keyOutputConfig);
                 planningBluePrint.KeyOutput.Add(keyOutputConfig);
             }
             DataContext.PlanningBlueprints.Add(planningBluePrint);
         }
         else
         {
             var planningBlueprint = DataContext.PlanningBlueprints
                 .Include(x => x.KeyOutput).First(x => x.Id == request.Id);
             planningBlueprint.KeyOutput = new List<KeyOutputConfiguration>();
             foreach (var keyOutputId in request.KeyOutputIds)
             {
                 var keyOutputConfig = DataContext.KeyOutputConfigs.Local.FirstOrDefault(x => x.Id == keyOutputId);
                 if (keyOutputConfig == null)
                 {
                     keyOutputConfig = new KeyOutputConfiguration { Id = keyOutputId };
                     DataContext.KeyOutputConfigs.Attach(keyOutputConfig);
                 }
                 planningBlueprint.KeyOutput.Add(keyOutputConfig);
             }
             request.MapPropertiesToInstance<PlanningBlueprint>(planningBlueprint);
         }
         DataContext.SaveChanges();
         return new SavePlanningBlueprintResponse
         {
             IsSuccess = true,
             Message = "The item has been successfully saved"
         };
     }
     catch
     {
         return new SavePlanningBlueprintResponse
         {
             IsSuccess = false,
             Message = "An error occured, please contact administrator for further information"
         };
     }
 }
        public SaveDesiredStateResponse SaveDesiredState(SaveDesiredStateRequest request)
        {
            try
            {
               var desiredState = request.MapTo<DesiredState>();

                if (request.Id == 0)
                {
                    var posture = new Posture { Id = request.PostureId };
                    DataContext.Postures.Attach(posture);
                    desiredState.Posture = posture;
                    DataContext.DesiredStates.Add(desiredState);
                }
                else
                {
                    desiredState = DataContext.DesiredStates.First(x => x.Id == request.Id);
                    request.MapPropertiesToInstance<DesiredState>(desiredState);
                }
                DataContext.SaveChanges();
                return new SaveDesiredStateResponse
                {
                    IsSuccess = true,
                    Message = "The item has been saved successfully",
                    Id = desiredState.Id,
                    Description = desiredState.Description
                };
            }
            catch {
                return new SaveDesiredStateResponse
                {
                    IsSuccess = false,
                    Message = "An error occured, please contact the administrator for further information"
                };
            }
        }