2024-11-07 05:52:03 +01:00
|
|
|
import 'package:ambito/src/entity/_general/filter/item_filter_repository.dart';
|
2024-10-29 16:15:11 +01:00
|
|
|
import 'package:ambito/src/entity/entities.dart';
|
2024-11-25 16:00:03 +01:00
|
|
|
import 'package:ambito/src/entity/measure/measure_category.dart';
|
|
|
|
import 'package:ambito/src/entity/measure/measure_type.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-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-10-28 16:17:09 +01:00
|
|
|
IsarCollection collection = isar.measures;
|
2024-11-07 05:52:03 +01:00
|
|
|
|
2024-11-25 16:00:03 +01:00
|
|
|
Future<bool> getCategoriesAndTypes() async {
|
|
|
|
final categoriesMap = <String, MeasureCategory>{};
|
|
|
|
final typesMap = <String, MeasureType>{};
|
|
|
|
|
|
|
|
final autoIncrementCat = isar.measureCategorys.autoIncrement;
|
|
|
|
final autoIncrementType = isar.measureTypes.autoIncrement;
|
|
|
|
|
|
|
|
// Fetch all measures
|
|
|
|
final allMeasures = await isar.measures.where().findAll();
|
|
|
|
|
|
|
|
for (final measure in allMeasures) {
|
|
|
|
if (measure.actionGroup == null ||
|
|
|
|
measure.factsheetAreaType == null ||
|
|
|
|
measure.factsheetAreaType!.isEmpty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process categories
|
|
|
|
for (final cat in measure.factsheetAreaType!) {
|
|
|
|
final categoryName = cat.value;
|
|
|
|
if (categoryName == null) continue;
|
|
|
|
|
|
|
|
if (!categoriesMap.containsKey(categoryName)) {
|
|
|
|
categoriesMap[categoryName] = MeasureCategory()
|
|
|
|
..id = autoIncrementCat()
|
|
|
|
..name = categoryName
|
|
|
|
..image =
|
|
|
|
'images/actions/areatype/${categoryName.toLowerCase().replaceUmlauts()}.jpg'
|
|
|
|
..description = 'Lorem ipsum dolor sit amet.'
|
|
|
|
..children = 1;
|
|
|
|
} else {
|
|
|
|
categoriesMap[categoryName]!.children++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process types
|
|
|
|
final actionGroupName = measure.actionGroup!.value;
|
|
|
|
if (actionGroupName == null) continue;
|
|
|
|
|
|
|
|
if (!typesMap.containsKey(actionGroupName)) {
|
|
|
|
typesMap[actionGroupName] = MeasureType()
|
|
|
|
..id = autoIncrementType()
|
|
|
|
..name = actionGroupName
|
|
|
|
..description = measure.factsheetDefinition
|
|
|
|
..image = (measure.files != null && measure.files!.isNotEmpty)
|
|
|
|
? measure.files![0].url
|
|
|
|
: 'images/spacer.png' // Add the appropriate image URL if available
|
|
|
|
..children = 1;
|
|
|
|
} else {
|
|
|
|
typesMap[actionGroupName]!.children++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert maps to lists
|
|
|
|
isar.write((isar) {
|
|
|
|
isar.measureCategorys.putAll(categoriesMap.values.toList());
|
|
|
|
isar.measureTypes.putAll(typesMap.values.toList());
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-11-18 15:19:07 +01:00
|
|
|
Future<bool> buildLists() async {
|
|
|
|
var measures = getAll();
|
|
|
|
Map<String, List<Map<String, IdValueColor>>> lists = {};
|
|
|
|
|
|
|
|
List<IsarPropertySchema> props = MeasureSchema.schema.properties;
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
|
|
|
|
for (Measure measure in measures) {
|
|
|
|
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
|
|
|
|
..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 =
|
|
|
|
'images/actions/areatype/${entry.key.toLowerCase().replaceUmlauts()}.jpg'
|
|
|
|
..color = colors[entry.key]
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filtersMeasureGroup.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
|
|
|
..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 =
|
|
|
|
'images/actions/areatype/${entry.key.toLowerCase().replaceUmlauts()}.jpg'
|
|
|
|
..color = colors[entry.key]
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filterMonths.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
|
|
|
..type = 'month'
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
itemFilterRepo.putAll(filterFundingPrograms.entries
|
|
|
|
.map((entry) => ItemFilter()
|
|
|
|
..id = autoIncrement()
|
|
|
|
..name = entry.key
|
|
|
|
..type = 'fundingProgram'
|
|
|
|
..ids = entry.value)
|
|
|
|
.toList());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getMeasureCount() {
|
|
|
|
return isar.measures.where().count();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Measure?> getByIds(List<int> ids) {
|
|
|
|
return isar.measures.getAll(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Measure> getAllOrdered() {
|
|
|
|
return isar.measures.where().sortByName().findAll();
|
|
|
|
}
|
2024-11-09 22:03:03 +01:00
|
|
|
|
|
|
|
Future<bool> downloadImages() async {
|
|
|
|
List<Measure> measures = isar.measures.where().findAll();
|
|
|
|
List<String> files = [];
|
|
|
|
for (Measure measure in measures) {
|
|
|
|
if (measure.files != null) {
|
|
|
|
for (FilePart file in measure.files!) {
|
|
|
|
files.add(file.url!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-11-25 16:00:03 +01:00
|
|
|
|
|
|
|
List<MeasureCategory> getCategories() {
|
|
|
|
return isar.measureCategorys.where().findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<MeasureType> getTypes() {
|
|
|
|
return isar.measureTypes.where().findAll();
|
|
|
|
}
|
2024-10-21 15:01:20 +02:00
|
|
|
}
|