toControllersCode method

String toControllersCode()

Implementation

String toControllersCode() {
  final String modelName = table.modelName ?? toCamelCase(table.tableName!);

  final StringBuffer subItemsCodeList = StringBuffer();
  final StringBuffer subMenuCodeList = StringBuffer();
  String subControllers() {
    final retVal = StringBuffer();
    for (final collection in table.collections!) {
      String objName = table.modelName!;
      objName += 'To${collection.childTable.modelName}';
      //print('subControllers: ${table.tableName} -> ${collection.childTableField.fieldName} -> objName:$objName');
      if (!Controllers.controllersub.contains(objName) && formTables.contains(collection.childTable)) {
        retVal.writeln('''class ${objName}ControllerSub extends ${collection.childTable.modelName}Controller {
          static String relationshipFieldName='${collection.childTableField.fieldName}';
          static String primaryKeyName= '${collection.childTable.primaryKeyNames[0]}';
          static bool useSoftDeleting = ${collection.childTable.useSoftDeleting.toString()};
          //static String formListTitleField = '${getformListTitleField(table)}';
          //static String formListSubTitleField = '${getformListSubTitleField(table)}';
          }''');
        Controllers.controllersub.add(objName);
        // PlaylistId IN (SELECT PlaylistId FROM PlaylistTrack WHERE TrackId=$id)
        final filterExpression = collection.relationType == RelationType.MANY_TO_MANY ? '${collection.childTable.primaryKeyNames[0]} IN (SELECT ${collection.childTable.primaryKeyNames[0]} FROM ${collection.childTableField.manyToManyTableName} WHERE ${table.primaryKeyNames[0]}=?)' : '\${${objName}ControllerSub.relationshipFieldName}=?';

        subItemsCodeList.writeln('''
          case '$objName':
          return SQFViewList(${objName}ControllerSub(),
            primaryKeyName: ${objName}ControllerSub.primaryKeyName,
            useSoftDeleting: ${objName}ControllerSub.useSoftDeleting,
            //formListTitleField: '${getformListTitleField(table)}',
            //formListSubTitleField: '${getformListSubTitleField(table)}',
            filterExpression: '$filterExpression',filterParameter: id,);''');
        subMenuCodeList.writeln('''
            menu['$objName'] = '${table.modelName} To ${collection.childTable.modelName}(${collection.childTableField.fieldName})';''');
      }
    }
    return retVal.toString();
  }

  String subItemsCode() => subItemsCodeList.isNotEmpty
      ? '''SQFViewList? subList(${table.primaryKeyTypes[0]} id, String controllerName) {
           switch (controllerName) {
          ${subItemsCodeList.toString()}
           default:
          return null;
    }
  }'''
      : '';

  String subMenuCode() => '''
        Map<String, String> subMenu() {
  final menu = <String, String>{};
  ${subMenuCodeList.toString()}
  return menu;
}
        ''';

  return '''
// BEGIN CONTROLLER (${table.modelName})
  ${subControllers().toString()}
  class ${modelName}Controller extends $modelName{
  String formListTitleField  = '${getformListTitleField(table)}';
  String formListSubTitleField  = '${getformListSubTitleField(table)}';
   static SQFViewList getController = SQFViewList(
    ${table.modelName}Controller(),
    primaryKeyName: '${table.primaryKeyNames[0]}',
    useSoftDeleting: ${table.useSoftDeleting.toString()},
  );
  ${subMenuCode().toString()}
  ${subItemsCode().toString()}
  Future<Widget> gotoEdit(dynamic obj) async{
     return ${modelName}Add(obj==null ? $modelName() : await $modelName().getById(${__gotoEditgetByIdParameters(table)}) ?? $modelName());
}
}
// END CONTROLLER (${table.modelName})
''';
}