|
| 1 | +Data Modeling |
| 2 | + |
| 3 | + The high-level <<<SolidClient>>> and <<<SolidSyncClient>>> make it easy to bind your Java objects to RDF-based Solid resources. This guide will help you structure your class definitions to make this possible. |
| 4 | + |
| 5 | +* What you will need |
| 6 | + |
| 7 | + * About 15 minutes |
| 8 | + |
| 9 | + * Your favorite text editor or IDE |
| 10 | + |
| 11 | + * Java 11 or later |
| 12 | + |
| 13 | + * Maven 3.5 or later |
| 14 | + |
| 15 | +* How to complete |
| 16 | + |
| 17 | + The solution involves creating a wrapper class so that your class can act not only as an RDF Graph |
| 18 | + but also as a domain-specific type. |
| 19 | + |
| 20 | +* 1. Subclass the Resource type |
| 21 | + |
| 22 | + The first step involves ensuring that your class extends the <<<com.inrupt.client.Resource>>> class. |
| 23 | + Afterwards, consider the methods you expect in your class. |
| 24 | + |
| 25 | ++--- |
| 26 | +public class Playlist extends Resource { |
| 27 | + |
| 28 | + public Playlist(URI identifier, Dataset dataset) { |
| 29 | + super(identifier, dataset); |
| 30 | + } |
| 31 | + |
| 32 | + public String getTitle() { |
| 33 | + ... |
| 34 | + } |
| 35 | + |
| 36 | + public void setTitle(String title) { |
| 37 | + ... |
| 38 | + } |
| 39 | + |
| 40 | + public Set<URI> getSongs() { |
| 41 | + ... |
| 42 | + } |
| 43 | + |
| 44 | + public void addSong(URI song) { |
| 45 | + ... |
| 46 | + } |
| 47 | +} |
| 48 | ++--- |
| 49 | + |
| 50 | + |
| 51 | +* 2. Create the wrapper node |
| 52 | + |
| 53 | + Next, you will create an inner class that binds the data between RDF and your class. |
| 54 | + |
| 55 | ++--- |
| 56 | +public class Playlist extends Resource { |
| 57 | + |
| 58 | + ... |
| 59 | + |
| 60 | + static IRI DC_TITLE = rdf.createIRI("http://purl.org/dc/terms/title"); |
| 61 | + static IRI EX_SONG = rdf.createIRI("http://music.example/song"); |
| 62 | + |
| 63 | + static class Node extends WrapperIRI { |
| 64 | + Node(RDFTerm original, Graph graph) { |
| 65 | + super(original, graph); |
| 66 | + } |
| 67 | + |
| 68 | + String getTitle() { |
| 69 | + return anyOrNull(DC_TITLE, ValueMappings::literalAsString); |
| 70 | + } |
| 71 | + |
| 72 | + void setTitle(String value) { |
| 73 | + overwriteNullable(DC_TITLE, value, TermMappings::asStringLiteral); |
| 74 | + } |
| 75 | + |
| 76 | + Set<URI> getSongs() { |
| 77 | + return objects(EX_SONG, TermMappings::asIri, ValueMappings::iriAsUri); |
| 78 | + } |
| 79 | + |
| 80 | + void addSong(URI song) { |
| 81 | + add(EX_SONG, song, TermMappings::asIri); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | ++--- |
| 86 | + |
| 87 | + Your inner class (<<<Node>>> in this example) will have access to a variety of functions |
| 88 | + to aid in mapping between the underlying RDF Graph and your Java type. {{{https://javadoc.io/doc/com.inrupt/inrupt-rdf-wrapping-commons/latest/com/inrupt/rdf/wrapping/commons/WrapperBlankNodeOrIRI.html}These methods}} are summarized in the <<<inrupt-rdf-wrapping-commons>>> Javadocs. |
| 89 | + |
| 90 | + Similarly, the built-in {{{https://javadoc.io/doc/com.inrupt/inrupt-rdf-wrapping-commons/latest/com/inrupt/rdf/wrapping/commons/TermMappings.html}TermMappings}} |
| 91 | + and {{{https://javadoc.io/doc/com.inrupt/inrupt-rdf-wrapping-commons/latest/com/inrupt/rdf/wrapping/commons/ValueMappings.html}ValueMappings}} |
| 92 | + are described in the {{{https://javadoc.io/doc/com.inrupt/inrupt-rdf-wrapping-commons/latest/com/inrupt/rdf/wrapping/commons/package-summary.html}Javadocs}} for the <<<inrupt-rdf-wrapping-commons>>> library. |
| 93 | + |
| 94 | +* 3. Connect your public methods to the wrapper node |
| 95 | + |
| 96 | + Now you are ready to link your public methods to the methods from your inner class. |
| 97 | + |
| 98 | ++--- |
| 99 | +public class Playlist extends Resource { |
| 100 | + |
| 101 | + private Node subject; |
| 102 | + |
| 103 | + public Playlist(URI identifier, Dataset dataset) { |
| 104 | + super(identifier, dataset); |
| 105 | + |
| 106 | + subject = new Node(rdf.createIRI(identifier.toString()), dataset.getGraph()); |
| 107 | + } |
| 108 | + |
| 109 | + public String getTitle() { |
| 110 | + return subject.getTitle(); |
| 111 | + } |
| 112 | + |
| 113 | + public void setTitle(String title) { |
| 114 | + subject.setTitle(title); |
| 115 | + } |
| 116 | + |
| 117 | + public Set<URI> getSongs() { |
| 118 | + return subject.getSongs(); |
| 119 | + } |
| 120 | + |
| 121 | + public void addSong(URI song) { |
| 122 | + subject.addSong(song); |
| 123 | + } |
| 124 | + |
| 125 | + ... |
| 126 | +} |
| 127 | ++--- |
| 128 | + |
| 129 | +* 4. Use your class in the high-level client |
| 130 | + |
| 131 | + Now, you can use your class with the high-level Solid client, mapping data seamlessly between your |
| 132 | + Java application and an RDF resource. |
| 133 | + |
| 134 | ++--- |
| 135 | +public class MyApplication { |
| 136 | + |
| 137 | + public void run() { |
| 138 | + Session session = OpenIdConnect.ofClientCredentials(issuer, clientId, clientSecret, "client_secret_basic"); |
| 139 | + SolidSyncClient client = SolidSyncClient.getClient().session(session); |
| 140 | + |
| 141 | + URI uri = URI.create("https://storage.example/playlists/910b509c-1ca5-4d6a-a093-377e8e1b390e"); |
| 142 | + try (Playlist playlist = client.read(uri, Playlist.class)) { |
| 143 | + System.out.println(playlist.getTitle()); |
| 144 | + |
| 145 | + // Add a new song to the playlist |
| 146 | + playlist.addSong(song); |
| 147 | + |
| 148 | + // Update the playlist in your storage |
| 149 | + client.update(playlist); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | ++--- |
| 154 | + |
0 commit comments