2024-10-29 16:15:11 +01:00
|
|
|
import 'package:ambito/src/entity/entities.dart';
|
2024-12-04 16:21:50 +01:00
|
|
|
import 'package:ambito/src/entity/lists/list_display.dart';
|
|
|
|
import 'package:ambito/src/entity/lists/list_repository.dart';
|
|
|
|
import 'package:ambito/src/entity/measure/category/measure_category.dart';
|
|
|
|
import 'package:ambito/src/entity/measure/measure_grouping.dart';
|
2024-11-07 05:52:03 +01:00
|
|
|
import 'package:ambito/src/extensions/extensions.dart';
|
2024-10-21 15:01:20 +02:00
|
|
|
import 'package:ambito/src/packages/ambito_db/base_db.dart';
|
|
|
|
import 'package:isar/isar.dart';
|
|
|
|
|
2024-11-20 17:09:53 +01:00
|
|
|
import '../../consts/consts.dart';
|
2024-12-04 16:21:50 +01:00
|
|
|
import '../../packages/ambito_notifier/notifier/filter_notifier.dart';
|
|
|
|
import '../../widgets/form/fields/field_dropdown.dart';
|
|
|
|
import 'group/measure_group.dart';
|
2024-10-21 15:01:20 +02:00
|
|
|
|
2024-10-28 16:17:09 +01:00
|
|
|
class MeasureRepository extends BaseDB {
|
2024-10-29 16:15:11 +01:00
|
|
|
@override
|
2024-12-04 16:21:50 +01:00
|
|
|
IsarCollection collection = isar.measureTypes;
|
2024-11-07 05:52:03 +01:00
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
putMeasureGroups(MeasureGrouping mg) {
|
|
|
|
isar.write((isar) {
|
|
|
|
isar.measureGroupings.put(mg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, int> getTypesCounterByCategory(String category) {
|
|
|
|
final counter = <String, int>{};
|
|
|
|
|
|
|
|
/*final allMeasures = isar.measureGroups.where().findAll();
|
|
|
|
|
|
|
|
for (final measure in allMeasures) {
|
|
|
|
if (measure.categories?.isEmpty ?? true || measure.type == null) continue;
|
|
|
|
|
|
|
|
final typeName = measure.type!.value;
|
|
|
|
if (typeName == null) continue;
|
|
|
|
|
|
|
|
final hasCategory = measure.categories!.any((cat) =>
|
|
|
|
cat.value?.toLowerCase().replaceUmlauts() == category.toLowerCase());
|
|
|
|
|
|
|
|
if (hasCategory) {
|
|
|
|
counter[typeName] = (counter[typeName] ?? 0) + 1;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> getCategoriesGroupsAndTypes() async {
|
2024-11-25 16:00:03 +01:00
|
|
|
final categoriesMap = <String, MeasureCategory>{};
|
2024-12-04 16:21:50 +01:00
|
|
|
final groupsMap = <String, MeasureGroup>{};
|
2024-11-25 16:00:03 +01:00
|
|
|
|
|
|
|
final autoIncrementCat = isar.measureCategorys.autoIncrement;
|
2024-12-04 16:21:50 +01:00
|
|
|
final autoIncrementType = isar.measureGroups.autoIncrement;
|
2024-11-25 16:00:03 +01:00
|
|
|
|
|
|
|
// Fetch all measures
|
2024-12-04 16:21:50 +01:00
|
|
|
final List<MeasureTypes> allMeasures = isar.measureTypes.where().findAll();
|
2024-11-25 16:00:03 +01:00
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
for (final MeasureTypes measure in allMeasures) {
|
|
|
|
if (measure.measureCategory == null ||
|
|
|
|
measure.measureCategory == null ||
|
|
|
|
measure.measureCategory!.isEmpty) {
|
2024-11-25 16:00:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process categories
|
2024-12-04 16:21:50 +01:00
|
|
|
for (final cat in measure.measureCategory!) {
|
2024-11-25 16:00:03 +01:00
|
|
|
final categoryName = cat.value;
|
|
|
|
if (categoryName == null) continue;
|
|
|
|
|
|
|
|
if (!categoriesMap.containsKey(categoryName)) {
|
|
|
|
categoriesMap[categoryName] = MeasureCategory()
|
|
|
|
..id = autoIncrementCat()
|
|
|
|
..name = categoryName
|
2024-12-04 16:21:50 +01:00
|
|
|
..url = categoryName.toLowerCase().replaceUmlauts()
|
2024-11-25 16:00:03 +01:00
|
|
|
..image =
|
2024-12-04 16:21:50 +01:00
|
|
|
'images/measure/category/${categoryName.toLowerCase().replaceUmlauts()}.jpg'
|
2024-11-25 16:00:03 +01:00
|
|
|
..description = 'Lorem ipsum dolor sit amet.'
|
|
|
|
..children = 1;
|
|
|
|
} else {
|
|
|
|
categoriesMap[categoryName]!.children++;
|
|
|
|
}
|
2024-12-04 16:21:50 +01:00
|
|
|
categoriesMap[categoryName]!.ids.add(measure.id);
|
2024-11-25 16:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process types
|
2024-12-04 16:21:50 +01:00
|
|
|
final groupName = measure.measureGroup!.value;
|
|
|
|
if (groupName == null) continue;
|
2024-11-25 16:00:03 +01:00
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
if (!groupsMap.containsKey(groupName)) {
|
|
|
|
groupsMap[groupName] = MeasureGroup()
|
2024-11-25 16:00:03 +01:00
|
|
|
..id = autoIncrementType()
|
2024-12-04 16:21:50 +01:00
|
|
|
..name = groupName
|
|
|
|
..url = groupName.toLowerCase().replaceUmlauts()
|
|
|
|
..description = measure.description ?? ''
|
|
|
|
..image =
|
|
|
|
'images/measure/group/${groupName.toLowerCase().replaceUmlauts()}.jpg' // Add the appropriate image URL if available
|
|
|
|
..children = 1
|
|
|
|
..categories = measure.measureCategory!
|
|
|
|
.map((item) =>
|
|
|
|
item.value!.toLowerCase().replaceUmlauts().toString())
|
|
|
|
.toList();
|
2024-11-25 16:00:03 +01:00
|
|
|
} else {
|
2024-12-04 16:21:50 +01:00
|
|
|
groupsMap[groupName]!.categories.addAll(measure.measureCategory!
|
|
|
|
.map(
|
|
|
|
(item) => item.value!.toLowerCase().replaceUmlauts().toString())
|
|
|
|
.toList());
|
|
|
|
groupsMap[groupName]!.children++;
|
2024-11-25 16:00:03 +01:00
|
|
|
}
|
2024-12-04 16:21:50 +01:00
|
|
|
groupsMap[groupName]!.ids.add(measure.id);
|
2024-11-25 16:00:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isar.write((isar) {
|
|
|
|
isar.measureCategorys.putAll(categoriesMap.values.toList());
|
2024-12-04 16:21:50 +01:00
|
|
|
isar.measureGroups.putAll(groupsMap.values.toList());
|
2024-11-25 16:00:03 +01:00
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-18 15:19:07 +01:00
|
|
|
Future<bool> buildLists() async {
|
|
|
|
var measures = getAll();
|
|
|
|
Map<String, List<Map<String, IdValueColor>>> lists = {};
|
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
/*List<IsarPropertySchema> props = MeasureSchema.schema.properties;
|
2024-11-18 15:19:07 +01:00
|
|
|
for (var prop in props) {
|
|
|
|
if (prop.type == IsarType.objectList) {
|
|
|
|
lists[prop.name] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.d(lists);
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
for (Measure measure in measures) {
|
|
|
|
var json = measure.toJson();
|
|
|
|
if (counter == 0) {
|
|
|
|
logger.d(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
lists.forEach((key, value) {
|
|
|
|
if (counter == 0) {
|
|
|
|
logger.d(key);
|
|
|
|
}
|
|
|
|
if (json[key] != null) {
|
|
|
|
logger.d(json[key]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
counter++;
|
2024-12-04 16:21:50 +01:00
|
|
|
}*/
|
2024-11-18 15:19:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-07 05:52:03 +01:00
|
|
|
Future<bool> buildMeasureFilters() async {
|
|
|
|
Map<String, List<int>> filtersAreaType = {};
|
|
|
|
Map<String, List<int>> filtersMeasureGroup = {};
|
|
|
|
Map<String, List<int>> filterMonths = {};
|
|
|
|
Map<String, List<int>> filterFundingPrograms = {};
|
|
|
|
Map<String, String> colors = {};
|
|
|
|
|
|
|
|
var measures = getAll();
|
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
/*for (Measure measure in measures) {
|
2024-11-07 05:52:03 +01:00
|
|
|
measure.factsheetAreaType?.forEach((ivc) {
|
|
|
|
filtersAreaType.putIfAbsent(ivc.value!, () => []).add(measure.id);
|
|
|
|
colors[ivc.value!] = ivc.color!;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (measure.actionGroup != null) {
|
|
|
|
filtersMeasureGroup
|
|
|
|
.putIfAbsent(measure.actionGroup!.value!, () => [])
|
|
|
|
.add(measure.id);
|
|
|
|
colors[measure.actionGroup!.value!] = measure.actionGroup!.color!;
|
|
|
|
}
|
|
|
|
|
|
|
|
measure.timeFrame?.forEach((ivc) {
|
|
|
|
filterMonths.putIfAbsent(ivc.value!, () => []).add(measure.id);
|
|
|
|
colors[ivc.value!] = ivc.color!;
|
|
|
|
});
|
|
|
|
|
|
|
|
measure.fundingPrograms?.forEach((ivc) {
|
|
|
|
filterFundingPrograms.putIfAbsent(ivc.value!, () => []).add(measure.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
final itemFilterRepo = ItemFilterRepository();
|
|
|
|
final autoIncrement = isar.itemFilters.autoIncrement;
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filtersAreaType.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
2024-12-04 16:21:50 +01:00
|
|
|
..url = entry.key.toLowerCase().replaceUmlauts()
|
2024-11-07 05:52:03 +01:00
|
|
|
..type = 'areaType'
|
|
|
|
..description =
|
2024-11-09 22:03:03 +01:00
|
|
|
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy.'
|
2024-11-07 05:52:03 +01:00
|
|
|
..image =
|
2024-12-04 16:21:50 +01:00
|
|
|
'images/measure/category/${entry.key.toLowerCase().replaceUmlauts()}.jpg'
|
2024-11-07 05:52:03 +01:00
|
|
|
..color = colors[entry.key]
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filtersMeasureGroup.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
2024-12-04 16:21:50 +01:00
|
|
|
..url = entry.key.toLowerCase().replaceUmlauts()
|
2024-11-07 05:52:03 +01:00
|
|
|
..type = 'group'
|
2024-11-09 22:03:03 +01:00
|
|
|
..description = 'Lorem ipsum dolor sit amet, consetetur sadipscing.'
|
2024-11-07 05:52:03 +01:00
|
|
|
..image =
|
2024-12-04 16:21:50 +01:00
|
|
|
'images/measure/category/${entry.key.toLowerCase().replaceUmlauts()}.jpg'
|
2024-11-07 05:52:03 +01:00
|
|
|
..color = colors[entry.key]
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filterMonths.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
2024-12-04 16:21:50 +01:00
|
|
|
..url = entry.key.toLowerCase().replaceUmlauts()
|
2024-11-07 05:52:03 +01:00
|
|
|
..type = 'month'
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filterFundingPrograms.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
2024-12-04 16:21:50 +01:00
|
|
|
..url = entry.key.toLowerCase().replaceUmlauts()
|
2024-11-07 05:52:03 +01:00
|
|
|
..type = 'fundingProgram'
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
2024-12-04 16:21:50 +01:00
|
|
|
*/
|
2024-11-07 05:52:03 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getMeasureCount() {
|
2024-12-04 16:21:50 +01:00
|
|
|
return isar.measureTypes.where().count();
|
2024-11-07 05:52:03 +01:00
|
|
|
}
|
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
List<MeasureTypes?> getByIds(List<int> ids) {
|
|
|
|
return isar.measureTypes.getAll(ids);
|
2024-11-07 05:52:03 +01:00
|
|
|
}
|
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
List<MeasureTypes> getAllOrdered() {
|
|
|
|
return isar.measureTypes.where().sortByMeasureType().findAll();
|
2024-11-07 05:52:03 +01:00
|
|
|
}
|
2024-11-09 22:03:03 +01:00
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
List<MeasureTypes> getAllOrderedByCategory(String category) {
|
|
|
|
final lowerCaseCategory = category.toLowerCase().replaceUmlauts();
|
|
|
|
final all = isar.measureTypes.where().sortByMeasureType().findAll();
|
|
|
|
|
|
|
|
return all.where((mt) {
|
|
|
|
return mt.measureCategory?.any((idcv) =>
|
|
|
|
idcv.value?.toLowerCase().replaceUmlauts() ==
|
|
|
|
lowerCaseCategory) ??
|
|
|
|
false;
|
|
|
|
}).toList();
|
2024-11-09 22:03:03 +01:00
|
|
|
}
|
2024-11-25 16:00:03 +01:00
|
|
|
|
|
|
|
List<MeasureCategory> getCategories() {
|
|
|
|
return isar.measureCategorys.where().findAll();
|
|
|
|
}
|
|
|
|
|
2024-12-04 16:21:50 +01:00
|
|
|
List<int>? getIdsByCategory(String category) {
|
|
|
|
if (category == '') {
|
|
|
|
return isar.measureCategorys
|
|
|
|
.where()
|
|
|
|
.idsProperty()
|
|
|
|
.findAll()
|
|
|
|
.expand((x) => x)
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
return isar.measureCategorys
|
|
|
|
.where()
|
|
|
|
.urlEqualTo(category)
|
|
|
|
.idsProperty()
|
|
|
|
.findFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int>? getIdsByGroup(String group) {
|
|
|
|
if (group == '') {
|
|
|
|
return isar.measureCategorys
|
|
|
|
.where()
|
|
|
|
.idsProperty()
|
|
|
|
.findAll()
|
|
|
|
.expand((x) => x)
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
return isar.measureGroups
|
|
|
|
.where()
|
|
|
|
.urlEqualTo(group)
|
|
|
|
.idsProperty()
|
|
|
|
.findFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int>? getIdsByCategoryAndGroup(String category, String group) {
|
|
|
|
Set<int> byCat = Set.from(getIdsByCategory(category) ?? []);
|
|
|
|
Set<int> byGroup = Set.from(getIdsByGroup(group) ?? []);
|
|
|
|
return byCat.intersection(byGroup).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MeasureGroup> getGroupsByCategory(String category) {
|
|
|
|
return isar.measureGroups
|
|
|
|
.where()
|
|
|
|
.categoriesElementContains(category)
|
|
|
|
.findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldDropdown getGroupDropDownByCategory(String category) {
|
|
|
|
List<MeasureGroup> filterItems = getGroupsByCategory(category);
|
|
|
|
|
|
|
|
return FieldDropdown(
|
|
|
|
name: 'group',
|
|
|
|
label: 'Maßnahmengruppe',
|
|
|
|
filterValue: ambitoFilterNotifier.activeFilters['group'],
|
|
|
|
onClear: () {
|
|
|
|
ambitoFilterNotifier.removeFilter('group');
|
|
|
|
ambitoFilterNotifier.removeFilter('type');
|
|
|
|
},
|
|
|
|
onSelected: (value) {
|
|
|
|
ambitoFilterNotifier.removeFilter('type');
|
|
|
|
ambitoFilterNotifier.setFilter(
|
|
|
|
'group',
|
|
|
|
value ?? '',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
entries: filterItems.map((item) => item.name!).toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldDropdown getTypeDropDownByCategoryAndGroup(
|
|
|
|
String category, String group) {
|
|
|
|
List<ListDisplay> filterItemsToShow =
|
|
|
|
ListRepository().getTypesToDisplay(category, group);
|
|
|
|
|
|
|
|
List<ListDisplay> filterItems = [];
|
|
|
|
|
|
|
|
for (var filterItem in filterItemsToShow) {
|
|
|
|
int childs = ListRepository()
|
|
|
|
.getMeasuresToDisplay(
|
|
|
|
category, group, filterItem.title!.toLowerCase().replaceUmlauts())
|
|
|
|
.length;
|
|
|
|
if (childs > 0) {
|
|
|
|
filterItems.add(filterItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldDropdown(
|
|
|
|
name: 'type',
|
|
|
|
label: 'Maßnahmentyp',
|
|
|
|
filterValue: ambitoFilterNotifier.activeFilters['type'],
|
|
|
|
onClear: () {
|
|
|
|
ambitoFilterNotifier.removeFilter('type');
|
|
|
|
},
|
|
|
|
onSelected: (value) {
|
|
|
|
ambitoFilterNotifier.setFilter(
|
|
|
|
'type',
|
|
|
|
value ?? '',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
entries: filterItems.map((item) => item.title!).toList(),
|
|
|
|
);
|
2024-11-25 16:00:03 +01:00
|
|
|
}
|
2024-10-21 15:01:20 +02:00
|
|
|
}
|