diff --git a/lib/d2_touch.dart b/lib/d2_touch.dart index 06e7736..b0391ec 100644 --- a/lib/d2_touch.dart +++ b/lib/d2_touch.dart @@ -11,6 +11,7 @@ import 'package:d2_touch/modules/engine/engine.module.dart'; import 'package:d2_touch/modules/engine/http/http.module.dart'; import 'package:d2_touch/modules/file_resource/file_resource.module.dart'; import 'package:d2_touch/modules/metadata/dataset/data_set.module.dart'; +import 'package:d2_touch/modules/metadata/option_group/option_group.module.dart'; import 'package:d2_touch/modules/metadata/option_set/option_set.module.dart'; import 'package:d2_touch/modules/metadata/organisation_unit/organisation_unit.module.dart'; import 'package:d2_touch/modules/metadata/program/program.module.dart'; @@ -31,7 +32,7 @@ import 'modules/metadata/dashboard/dashboard.module.dart'; import 'modules/metadata/data_element/data_element.module.dart'; class D2Touch implements D2TouchModel { - int? instanceVersion ; + int? instanceVersion; String locale = 'en'; Database? _database; bool? inMemory; @@ -48,6 +49,8 @@ class D2Touch implements D2TouchModel { DataElementModule(database: _database); ProgramModule get programModule => ProgramModule(database: _database); + OptionGroupModule get optionGroupModule => + OptionGroupModule(database: _database); DataSetModule get dataSetModule => DataSetModule(database: _database); @@ -84,7 +87,6 @@ class D2Touch implements D2TouchModel { DatabaseFactory? databaseFactory, SharedPreferences? sharedPreferenceInstance, }) async { - if (_d2Instance == null) { _d2Instance = D2Touch._internal(); @@ -151,6 +153,7 @@ class D2Touch implements D2TouchModel { await DataElementModule.createTables(database: database); await DataSetModule.createTables(database: database); await ProgramModule.createTables(database: database); + await OptionGroupModule.createTables(database: database); await DashboardModule.createTables(database: database); await TrackedEntityInstanceModule.createTables(database: database); await AggregateModule.createTables(database: database); @@ -184,6 +187,7 @@ class D2Touch implements D2TouchModel { await DataElementModule.createTables(database: database); await DataSetModule.createTables(database: database); await ProgramModule.createTables(database: database); + await OptionGroupModule.createTables(database: database); await DashboardModule.createTables(database: database); await TrackedEntityInstanceModule.createTables(database: database); await AggregateModule.createTables(database: database); diff --git a/lib/modules/metadata/option_group/entities/option_group.entity.dart b/lib/modules/metadata/option_group/entities/option_group.entity.dart new file mode 100644 index 0000000..19ed16d --- /dev/null +++ b/lib/modules/metadata/option_group/entities/option_group.entity.dart @@ -0,0 +1,36 @@ +import 'dart:convert'; + +import 'package:d2_touch/core/annotations/index.dart'; +import 'package:d2_touch/shared/entities/identifiable.entity.dart'; + +@AnnotationReflectable +@Entity(tableName: 'optiongroup', apiResourceName: 'optionGroups') +class OptionGroup extends IdentifiableEntity { + @Column(type: ColumnType.TEXT) + String options; + + OptionGroup({ + required String id, + required String name, + required bool dirty, + required this.options, + }) : super(id: id, name: name, dirty: dirty); + + factory OptionGroup.fromJson(Map jsonData) { + return OptionGroup( + id: jsonData['id'], + name: jsonData['name'], + dirty: jsonData['dirty'], + options: jsonEncode(jsonData['options']), + ); + } + + Map toJson() { + final Map data = new Map(); + data['id'] = this.id; + data['name'] = this.name; + data['dirty'] = this.dirty; + data['options'] = this.options; + return data; + } +} diff --git a/lib/modules/metadata/option_group/option_group.module.dart b/lib/modules/metadata/option_group/option_group.module.dart new file mode 100644 index 0000000..dea383a --- /dev/null +++ b/lib/modules/metadata/option_group/option_group.module.dart @@ -0,0 +1,12 @@ +import 'package:d2_touch/modules/metadata/option_group/queries/option_group.query.dart'; +import 'package:sqflite/sqflite.dart'; + +class OptionGroupModule { + Database? database; + OptionGroupModule({this.database, String? locale}); + static createTables({required Database database}) async { + await OptionGroupQuery(database: database).createTable(); + } + + OptionGroupQuery get optionGroup => OptionGroupQuery(database: database); +} diff --git a/lib/modules/metadata/option_group/queries/option_group.query.dart b/lib/modules/metadata/option_group/queries/option_group.query.dart new file mode 100644 index 0000000..7c829dc --- /dev/null +++ b/lib/modules/metadata/option_group/queries/option_group.query.dart @@ -0,0 +1,22 @@ +import 'package:d2_touch/modules/metadata/option_group/entities/option_group.entity.dart'; +import 'package:d2_touch/shared/queries/base.query.dart'; +import 'package:d2_touch/shared/utilities/query_filter.util.dart'; +import 'package:sqflite/sqflite.dart'; + +class OptionGroupQuery extends BaseQuery { + OptionGroupQuery({Database? database}) : super(database: database); + + @override + Future dhisUrl() { + final apiFilter = + QueryFilter.getApiFilters(this.repository.columns, this.filters); + + if ((this.selected).isNotEmpty) { + return Future.value( + 'optionGroups.json${apiFilter != null ? '?$apiFilter&' : '?'}fields=${this.selected.join(',')}&paging=false'); + } + + return Future.value( + 'optionGroups.json${apiFilter != null ? '?$apiFilter&' : '?'}fields=id,name,shortName,options[code,name]&paging=false'); + } +}