movieWithUserDataToTurtle static method
Converts a single movie with user's personal rating and comment to TTL format. This creates a unified file containing both movie metadata and user's personal data.
Implementation
static String movieWithUserDataToTurtle(
Movie movie, double? rating, String? comment) {
final triples = <URIRef, Map<URIRef, dynamic>>{};
// Create the movie resource with all movie metadata.
final movieResource = localNS.withAttr('movie${movie.id}');
triples[movieResource] = {
rdfType: movieType,
identifier: Literal('${movie.id}', datatype: XSD.int),
name: Literal(_escapeString(movie.title)),
description: Literal(_escapeString(movie.overview)),
image: Literal(_escapeString(movie.posterUrl)),
thumbnailUrl: Literal(_escapeString(movie.backdropUrl)),
aggregateRating: Literal('${movie.voteAverage}', datatype: XSD.double),
datePublished:
Literal(movie.releaseDate.toIso8601String(), datatype: XSD.dateTime),
genre: Literal(movie.genreIds.join(',')),
};
// Add user's personal rating if it exists.
if (rating != null) {
final userRatingResource = localNS.withAttr('userRating${movie.id}');
triples[userRatingResource] = {
rdfType: ratingType,
movieId: Literal('${movie.id}', datatype: XSD.int),
value: Literal('$rating', datatype: XSD.double),
};
// Link the movie to the user rating.
triples[movieResource]![localNS.withAttr('hasUserRating')] =
userRatingResource;
}
// Add user's personal comment if it exists.
if (comment != null && comment.isNotEmpty) {
final userCommentResource = localNS.withAttr('userComment${movie.id}');
triples[userCommentResource] = {
rdfType: commentType,
movieId: Literal('${movie.id}', datatype: XSD.int),
text: Literal(_escapeString(comment)),
};
// Link the movie to the user comment.
triples[movieResource]![localNS.withAttr('hasUserComment')] =
userCommentResource;
}
// Define namespace bindings.
final bindNamespaces = {'': localNS, 'schema': movieNS};
// Add JSON backup for compatibility.
final movieJson = jsonEncode(movie.toJson());
final userDataJson = jsonEncode({
'rating': rating,
'comment': comment,
});
final ttlContent =
tripleMapToTurtle(triples, bindNamespaces: bindNamespaces);
final withJsonBackup =
'$ttlContent\n\n# JSON_MOVIE_DATA: $movieJson\n# JSON_USER_DATA: $userDataJson';
return withJsonBackup;
}