|
| 1 | +const { log } = require("node:console"); |
| 2 | + |
| 3 | +/// Ensure collection has mandatory fields |
| 4 | +function fixCollection(collection) { |
| 5 | + let coll = collection |
| 6 | + if (!('type' in coll)) { coll.type = []; } |
| 7 | + if (!('db' in coll)) { coll.db = ''; } |
| 8 | + if (!('url' in coll)) { coll.url = ''; } |
| 9 | + if (!('access' in coll)) { coll.access = []; } |
| 10 | + return coll; |
| 11 | +}; |
| 12 | + |
| 13 | +/// Returns an URL for collections |
| 14 | +function collection_url(s="") { |
| 15 | + return `/importer/collection/${s}`; |
| 16 | +} |
| 17 | + |
| 18 | +/// Get list of collections from the KG |
| 19 | +function getCollections() { |
| 20 | + const req = { method: 'GET', headers: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' }}; |
| 21 | + fetch(collection_url(), req) |
| 22 | + .then(response => response.json()) |
| 23 | + .then(js => { |
| 24 | + app.collections = js.map(d => fixCollection(d)); |
| 25 | + app.collections.sort((a, b) => a.id - b.id); |
| 26 | + }) |
| 27 | + .catch(err => console.error(err)) |
| 28 | + .finally(() => { console.log('fetch collections done'); }); |
| 29 | +} |
| 30 | + |
| 31 | +function makeCollection(id_str, collection_tpl) { |
| 32 | + const collection = collection_tpl ? { ...collection_tpl } : { |
| 33 | + type: [], |
| 34 | + access: [], |
| 35 | + partOf: [], |
| 36 | + url: '', |
| 37 | + db: '', |
| 38 | + license: '', |
| 39 | + }; |
| 40 | + if (collection_tpl) { |
| 41 | + collection.id = id_str; |
| 42 | + collection.name = `New Collection ${id_str}`; |
| 43 | + collection.uri = `https://graph.nfdi4objects.net/collection/${id_str}`; |
| 44 | + } |
| 45 | + return collection |
| 46 | +} |
| 47 | + |
| 48 | +function showProgess(on = false) { |
| 49 | + document.getElementById("SP1").style.display = on ? "inline-block" : "none"; |
| 50 | +} |
| 51 | + |
| 52 | +/// Import TTL data into the KG |
| 53 | +function postCollectionData(uri, data, suffix) { |
| 54 | + showProgess(true); |
| 55 | + var req = { |
| 56 | + method: 'POST', |
| 57 | + headers: { |
| 58 | + 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json' |
| 59 | + }, |
| 60 | + body: JSON.stringify({ "data": data, "uri": uri, suffix: suffix }) |
| 61 | + } |
| 62 | + fetch('/postCollectionData', req) |
| 63 | + .then(response => response.json()) |
| 64 | + .then(json => console.log(json)) |
| 65 | + .catch(err => console.error(err)) |
| 66 | + .finally(() => { |
| 67 | + showProgess(false); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +function makeAppData() { |
| 72 | + return { |
| 73 | + data() { |
| 74 | + return { |
| 75 | + collections: [], |
| 76 | + displayCollection: null, |
| 77 | + collectionInfo: 'No info.', |
| 78 | + rdfFiles: [], |
| 79 | + } |
| 80 | + }, |
| 81 | + delimiters: ["${", "}$"], // for global |
| 82 | + compilerOptions: { delimiters: ["${", "}$"] }, // for standalone |
| 83 | + methods: { |
| 84 | + selectCollection(c_id) { |
| 85 | + this.displayCollection = this.collections.find(c => c.id === c_id); |
| 86 | + }, |
| 87 | + |
| 88 | + delCollection() { |
| 89 | + /// Remove a type from the collection |
| 90 | + let N = this.displayCollection ? this.displayCollection.id :this.collections.length |
| 91 | + let id = prompt('Enter the index of the collection to delete:', N); |
| 92 | + id = parseInt(id) - 1; |
| 93 | + if (!isNaN(id) && id >= 0 || id < N) { |
| 94 | + let q = this.collections[id]; |
| 95 | + //console.log(q.id) |
| 96 | + fetch(collection_url(q.id), { |
| 97 | + method: 'DELETE', |
| 98 | + headers: { "Content-Type": "application/json", } |
| 99 | + }) |
| 100 | + .then(response => response.json()) |
| 101 | + .then(data => { |
| 102 | + console.log(data); |
| 103 | + this.collections.splice(id, 1) |
| 104 | + this.displayCollection = null |
| 105 | + }) |
| 106 | + .catch(err => alert(err)) |
| 107 | + .finally(() => { console.log(`collection ${id} deleted`); }); |
| 108 | + } |
| 109 | + |
| 110 | + }, |
| 111 | + |
| 112 | + addCollection() { |
| 113 | + /// Add a new collection |
| 114 | + let new_id = 1; |
| 115 | + let N = this.collections.length; |
| 116 | + if (N > 0) { |
| 117 | + new_id = Math.max(...this.collections.map(c => c.id)) + 1; |
| 118 | + } |
| 119 | + let lastCollection = N > 0 ? this.collections[N - 1] : null; |
| 120 | + const new_collection = makeCollection(new_id.toString(), lastCollection) |
| 121 | + |
| 122 | + fetch(collection_url(), { |
| 123 | + method: 'POST', |
| 124 | + headers: { "Content-Type": "application/json", }, |
| 125 | + body: JSON.stringify(new_collection) |
| 126 | + }) |
| 127 | + .then(response => response.json()) |
| 128 | + .then(data => { |
| 129 | + //console.log(data); |
| 130 | + this.collections.push(fixCollection(data)); |
| 131 | + this.selectCollection(new_id.toString()); |
| 132 | + }) |
| 133 | + .catch(err => alert(err)) |
| 134 | + .finally(() => { console.log('collection added'); }); |
| 135 | + }, |
| 136 | + showDetails(uri) { |
| 137 | + /// Show number of triples in collection |
| 138 | + getNumTriples(uri, num => { |
| 139 | + this.collectionInfo = `The collection <${uri}> contains ${num} triples.`; |
| 140 | + $('#collectionInfoMDialog').modal('show'); |
| 141 | + }); |
| 142 | + }, |
| 143 | + deleteCollData(id) { |
| 144 | + if (this.displayCollection == null) { |
| 145 | + alert('No collection selected for data deletion.'); |
| 146 | + return; |
| 147 | + } |
| 148 | + if (!confirm(`Are you sure you want to delete all data for collection ${id}? This action cannot be undone.`)) { |
| 149 | + return; |
| 150 | + } |
| 151 | + showProgess(true); |
| 152 | + var req = { method: 'POST', headers: { 'Accept': 'application/json, text/plain, */*' } } |
| 153 | + fetch(collection_url(`${id}/remove`), req) |
| 154 | + .then(response => response.json()) |
| 155 | + .then(json => console.log(json)) |
| 156 | + .catch(err => console.error(err)) |
| 157 | + .finally(() => { |
| 158 | + showProgess(false); |
| 159 | + alert('Data deletion completed.'); |
| 160 | + }); |
| 161 | + }, |
| 162 | + saveRDFUrl(event) { |
| 163 | + // Saves x3ml event data |
| 164 | + this.rdfFiles = event.target.files; |
| 165 | + }, |
| 166 | + uploadRDF() { |
| 167 | + /// Upload RDF data from selected file |
| 168 | + if (this.rdfFiles.length ==0) { |
| 169 | + alert('No RDF file selected for upload.'); |
| 170 | + } |
| 171 | + else { |
| 172 | + const file0 = this.rdfFiles[0] |
| 173 | + const textPromise = file0.text(); |
| 174 | + textPromise.then((data) => { |
| 175 | + let suffix = file0.name.split('.').pop().toLowerCase(); |
| 176 | + postCollectionData(this.displayCollection.uri, data, suffix); |
| 177 | + this.rdfFiles = []; |
| 178 | + }); |
| 179 | + } |
| 180 | + }, |
| 181 | + putCollection(collection) { |
| 182 | + /// Update collection metadata |
| 183 | + if (collection == null) { |
| 184 | + alert('No collection selected for update.'); |
| 185 | + return; |
| 186 | + } |
| 187 | + const headers = { "Content-Type": "application/json", } |
| 188 | + fetch(collection_url(collection.id), { |
| 189 | + method: 'PUT', |
| 190 | + headers: headers, |
| 191 | + body: JSON.stringify(collection) |
| 192 | + }) |
| 193 | + .then(response => response.json()) |
| 194 | + .then(data => { console.log(data); }) |
| 195 | + .catch(err => alert(err)) |
| 196 | + .finally(() => { |
| 197 | + this.getCollection(collection.id); |
| 198 | + alert('collection changed'); |
| 199 | + }); |
| 200 | + }, |
| 201 | + getCollection(id) { |
| 202 | + /// Retrieve collection metadata |
| 203 | + if (!id) { |
| 204 | + alert('No collection selected for retrieval.'); |
| 205 | + return; |
| 206 | + } |
| 207 | + const headers = { "Accept": "application/json, text/plain, */*", } |
| 208 | + fetch(collection_url(id), { method: 'GET', headers: headers }) |
| 209 | + .then(response => response.json()) |
| 210 | + .then(data => { this.displayCollection = fixCollection(data); }) |
| 211 | + .catch(err => alert(err)) |
| 212 | + .finally(() => { console.log('collection fetched'); }); |
| 213 | + }, |
| 214 | + addType() { |
| 215 | + /// Add a new type to the collection |
| 216 | + if (this.displayCollection == null) { |
| 217 | + alert('No collection selected for adding type.'); |
| 218 | + return; |
| 219 | + } |
| 220 | + this.displayCollection.type.push('x:y'); |
| 221 | + }, |
| 222 | + delType() { |
| 223 | + /// Remove a type from the collection |
| 224 | + if (this.displayCollection == null) { |
| 225 | + alert('No collection selected for deleting type.'); |
| 226 | + return; |
| 227 | + } |
| 228 | + let N = this.displayCollection.type.length |
| 229 | + let id = prompt('Enter the index of the type to delete:', N); |
| 230 | + id = parseInt(id) - 1; |
| 231 | + if (!isNaN(id) && id >= 0 || id < N) { |
| 232 | + this.displayCollection.type.splice(id, 1); |
| 233 | + } |
| 234 | + }, |
| 235 | + }, |
| 236 | + mounted() { |
| 237 | + document.onreadystatechange = () => { |
| 238 | + if (document.readyState == "complete") { |
| 239 | + getCollections(); |
| 240 | + } |
| 241 | + } |
| 242 | + }, |
| 243 | + created() { |
| 244 | + console.log('created'); |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments