using System.Collections.Generic; using System.Linq; using BeWo.ServiceProxy; using BeWo.ViewModel; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.ClientPartials; namespace BeWo.Core.Service { public class GoalService { public static List GetGoalTree(IEnumerable goalCategories, List indGoalCategories, List pAllGoals, bool removeEmptyGoalCategories) { var allCategories = new List(); if (goalCategories != null && pAllGoals != null && pAllGoals.Count > 0) { foreach (var goalCat in goalCategories) { if (!pAllGoals.Contains(goalCat)) { allCategories.Add(goalCat); } } } if (indGoalCategories != null && pAllGoals != null) { if (ContainsAnyGoal(indGoalCategories, pAllGoals)) { foreach (var igoalCat in indGoalCategories) { if (!pAllGoals.Contains(igoalCat)) { allCategories.Add(igoalCat); } } } } var sortedCategories = allCategories.OrderBy(s => s.TypeDescription).ToList(); var sortedGoals = pAllGoals.OrderBy(s => s.TypeDescription).ToList(); var allCatList = new List(); var allGoalList = new List(); var rootGoalList = new List(); var goalCategoryDict = new Dictionary(); //foreach (var goal in sortedGoals) //{ // var childItem = new SupportConceptGoalDC { GoalEntryDC = goal }; // if (goal.ParentOid != null && goalCategoryDict.ContainsKey(goal.ParentOid.Value)) // { // var parentItem = goalCategoryDict[goal.ParentOid.Value]; // childItem.ParentGoal = parentItem; // parentItem.Children.Add(childItem); // } //} foreach (var goalCat in sortedCategories) { var treeItem = new SupportConceptGoalDC {GoalEntryDC = goalCat, IsExpanded = true}; goalCategoryDict.Add(goalCat.ValueListEntryOid.Value, treeItem); allCatList.Add(treeItem); } foreach (var goalCat in allCatList) { if (goalCat.GoalEntryDC.ParentOid.HasValue && goalCategoryDict.ContainsKey(goalCat.GoalEntryDC.ParentOid.Value)) { var parent = goalCategoryDict[goalCat.GoalEntryDC.ParentOid.Value]; goalCat.ParentGoal = parent; parent.Children.Add(goalCat); } } foreach (var goalCat in allCatList) { if (goalCat.ParentGoal == null) { rootGoalList.Add(goalCat); } } foreach (var goal in sortedGoals) { var childItem = new SupportConceptGoalDC { GoalEntryDC = goal, IsExpanded = true }; if (!goalCategoryDict.ContainsKey(goal.ValueListEntryOid.Value)) goalCategoryDict.Add(goal.ValueListEntryOid.Value, childItem); allGoalList.Add(childItem); } foreach (var goal in allGoalList) { if (goal.GoalEntryDC.ParentOid != null && goalCategoryDict.ContainsKey(goal.GoalEntryDC.ParentOid.Value)) { var parentItem = goalCategoryDict[goal.GoalEntryDC.ParentOid.Value]; goal.ParentGoal = parentItem; parentItem.Children.Add(goal); } } if (removeEmptyGoalCategories) { RemoveCategoriesWithoutSelectedGoals(rootGoalList, sortedGoals); } return rootGoalList; } private static void RemoveCategoriesWithoutSelectedGoals(ICollection goalList, List sortedGoals) { var cats2Remove = new List(); foreach (var cat in goalList) { RemoveCategoriesWithoutSelectedGoals(cat.Children, sortedGoals); if (!ContainsAnyGoal(cat, sortedGoals)) { cats2Remove.Add(cat); } } foreach (var cat in cats2Remove) { goalList.Remove(cat); } } private static bool ContainsAnyGoal(SupportConceptGoalDC cat, IEnumerable goals) { foreach (var goal in goals) { if (GoalIsPartOfCategory(cat, goal)) { return true; } } return false; } private static bool GoalIsPartOfCategory(SupportConceptGoalDC cat, ValueListEntryDC goal) { if (cat.GoalEntryDC.ValueListEntryOid == goal.ValueListEntryOid) { return true; } if (goal.ParentOid.HasValue) { if (cat.GoalEntryDC.ValueListEntryOid.Value == goal.ParentOid.Value) { return true; } foreach (var child in cat.Children) { bool ispart = GoalIsPartOfCategory(child, goal); if (ispart) { return true; } } } return false; } private static bool ContainsAnyGoal(IEnumerable indGoalCategories, IEnumerable goals) { var dict = indGoalCategories.ToDictionary((g => g.ValueListEntryOid.Value)); foreach (var goal in goals) { if (goal.ParentOid.HasValue && dict.ContainsKey(goal.ParentOid.Value)) { return true; } } return false; } public static IEnumerable GetZieleMassnahmenTree(List goalsAndCategories, bool removeEmptyGoalCategories) { var sortedCategories = goalsAndCategories.Where(w => w.Type.Equals(ValueListEntryType.SupportConceptGoalCategoryType) || w.Type.Equals(ValueListEntryType.SupportConceptIndividualGoalCategoryType)).OrderBy(s => s.DisplayName).ToList(); var sortedGoals = goalsAndCategories.Where(w => w.Type.Equals(ValueListEntryType.SupportConceptGoalType) || w.Type.Equals(ValueListEntryType.SupportConceptIndividualGoalType)).OrderBy(s => s.DisplayName).ToList(); var goalCategoryDict = new Dictionary(); var goalList = new List(); foreach (var goalCategory in sortedCategories) { var treeItem = new SupportConceptGoalDC { GoalEntryDC = goalCategory.DataContract, IsExpanded = true }; goalCategoryDict.Add(goalCategory.DataContract.ValueListEntryOid.Value, treeItem); goalList.Add(treeItem); } foreach (var goalCategory in sortedCategories.Where(w => w.DataContract.ParentOid != null)) { if (goalCategoryDict.ContainsKey(goalCategory.DataContract.ParentOid.Value)) { goalCategoryDict[goalCategory.DataContract.ValueListEntryOid.Value].ParentGoal = goalCategoryDict[goalCategory.DataContract.ParentOid.Value]; } } var categoriesWithParents = goalCategoryDict.Values.Where(w => w.GoalEntryDC.ParentOid != null).ToList(); foreach (var goalCategory in categoriesWithParents) { if (goalCategoryDict.ContainsKey(goalCategory.GoalEntryDC.ParentOid.Value)) { goalCategoryDict[goalCategory.GoalEntryDC.ParentOid.Value].Children.Add(goalCategory); } } foreach (var goal in sortedGoals) { var childItem = new SupportConceptGoalDC { GoalEntryDC = goal.DataContract }; if (goal.DataContract.ParentOid != null && goalCategoryDict.ContainsKey(goal.DataContract.ParentOid.Value)) { var parentItem = goalCategoryDict[goal.DataContract.ParentOid.Value]; childItem.ParentGoal = parentItem; parentItem.Children.Add(childItem); } } if (removeEmptyGoalCategories) { var cats2Remove = goalList.Where(cat => cat.Children.Count == 0).ToList(); foreach (var cat in cats2Remove) { goalList.Remove(cat); } } return goalList; } public static void RecursiveSynchronousValueListEntrySave(SupportConceptGoalTreeItem treeItem) { var neuerDC = ServiceFacade.DoValueListServiceSync(s => s.InsertNewValueListEntry(treeItem.GoalEntryVM.CommitToDataContract())); treeItem.GoalEntryVM = new ValueListEntryVM(neuerDC); treeItem.GoalEntryDC = neuerDC; foreach (var child in treeItem.Items) { child.ParentGoal = treeItem; child.GoalEntryVM.ParentEntry = neuerDC; child.GoalEntryDC.Parent = neuerDC; RecursiveSynchronousValueListEntrySave(child); } } private static List InsertedGoals; public static IEnumerable SaveIndividualGoalsAndMassnahmenRecursively(SupportConceptGoalTreeItem treeItem) { InsertedGoals = new List(); RecursiveSynchronousSCValueListEntrySave(treeItem); return InsertedGoals; } public static void RecursiveSynchronousSCValueListEntrySave(SupportConceptGoalTreeItem treeItem) { var neuerDC = ServiceFacade.DoValueListServiceSync(s => s.InsertNewValueListEntry(treeItem.GoalEntryVM.CommitToDataContract())); Cache.GetInstance().AddGoalEntry(neuerDC); InsertedGoals.Add(neuerDC); treeItem.GoalEntryVM = new ValueListEntryVM(neuerDC); treeItem.GoalEntryDC = neuerDC; foreach (var child in treeItem.Items) { child.ParentGoal = treeItem; child.GoalEntryVM.ParentEntry = neuerDC; child.GoalEntryDC.Parent = neuerDC; RecursiveSynchronousSCValueListEntrySave(child); } } public static ValueListEntryDC CopyGoal(ValueListEntryDC ve, bool copyRating) { ValueListEntryDC copyVe = new ValueListEntryDC(); copyVe.Abbreviation = ve.Abbreviation; copyVe.RatingTypeOid = copyRating ? ve.RatingTypeOid : null; copyVe.Notice = ve.Notice; copyVe.IsSystemEntry = ve.IsSystemEntry; copyVe.ParentOid = ve.ParentOid; copyVe.Type = ve.Type; copyVe.TypeDescription = ve.TypeDescription; copyVe.ValueListEntryOid = ve.ValueListEntryOid; copyVe.ValueListEntryVersion = ve.ValueListEntryVersion; if (ve.Parent != null) { copyVe.Parent = ve.Parent; } return copyVe; } public static List CopyGoals(List goalCats, bool copyRating) { var goalsCopy = new List(); if (goalCats != null) { foreach (var ve in goalCats) { var copyVe = GoalService.CopyGoal(ve, copyRating); goalsCopy.Add(copyVe); } } return goalsCopy; } } }