1
1
package com .herminiogarcia .dmaog .dataAccess
2
2
3
- import com .herminiogarcia .dmaog .common .{DataLocalFileWriter , IRIValue , ModelLoader , PrefixedNameConverter , ResourceLoader }
3
+ import com .herminiogarcia .dmaog .common .{DataLocalFileWriter , IRIValue , ModelLoader , MultilingualString , PrefixedNameConverter , ResourceLoader }
4
4
import org .apache .jena .datatypes .xsd .XSDDatatype
5
5
import org .apache .jena .query .{Dataset , DatasetFactory , QueryExecutionFactory , QueryFactory , QuerySolution , ResultSet , ResultSetFactory }
6
6
import org .apache .jena .rdf .model .{Model , ModelFactory , ResourceFactory }
@@ -77,19 +77,22 @@ class DataAccess(fileNameForGeneratedContent: String,
77
77
78
78
private def getGroupedStatements (sparql : String ,
79
79
getSubjectFunction : QuerySolution => String ,
80
- getPredicateFunction : QuerySolution => String ): Map [String , List [(String , String )]] = {
80
+ getPredicateFunction : QuerySolution => String ): Map [String , List [(String , ObjectResult )]] = {
81
81
val resultSet = QueryExecutorFactory .getQueryExecutor(sparql, sparqlEndpoint, () => getModel).execute()
82
- val groupedBySubjectResults = mutable.Map [String , List [(String , String )]]()
82
+ val groupedBySubjectResults = mutable.Map [String , List [(String , ObjectResult )]]()
83
83
while (resultSet.hasNext) {
84
84
val result = resultSet.next()
85
85
val subject = getSubjectFunction(result)
86
86
val predicate = getPredicateFunction(result)
87
87
val theObject = result.get(" object" )
88
88
val predicateURI = predicate
89
89
val objectValue = {
90
- if (theObject == null ) " "
91
- else if (theObject.isLiteral) theObject.asLiteral().getString
92
- else theObject.asResource().getURI
90
+ if (theObject == null ) LiteralResult (" " )
91
+ else if (theObject.isLiteral &&
92
+ (theObject.asLiteral().getLanguage.nonEmpty || theObject.asLiteral().getDatatypeURI == " http://www.w3.org/1999/02/22-rdf-syntax-ns#langString" ))
93
+ MultilingualStringResult (theObject.asLiteral().getString, theObject.asLiteral().getLanguage)
94
+ else if (theObject.isLiteral) LiteralResult (theObject.asLiteral().getString)
95
+ else IRIResult (theObject.asResource().getURI)
93
96
}
94
97
groupedBySubjectResults.get(subject) match {
95
98
case Some (value) =>
@@ -199,7 +202,7 @@ class DataAccess(fileNameForGeneratedContent: String,
199
202
val sparql = loadFromResources(" insertData.sparql" )
200
203
.replaceFirst(" \\ $prefixes" , " " )
201
204
.replaceFirst(" \\ $triples" , triples)
202
- updateExecution(instance, sparql)
205
+ updateExecution(sparql)
203
206
}
204
207
205
208
def delete [T ](instance : T ): Unit = {
@@ -208,11 +211,10 @@ class DataAccess(fileNameForGeneratedContent: String,
208
211
.replaceFirst(" \\ $prefixes" , " " )
209
212
.replaceAll(" \\ $triples" , triples)
210
213
.replaceFirst(" \\ $value" , " <" + getIdValue(instance) + " >" )
211
- updateExecution(instance, sparql)
212
-
214
+ updateExecution(sparql)
213
215
}
214
216
215
- private def updateExecution [ T ]( instance : T , sparql : String ): Unit = {
217
+ private def updateExecution ( sparql : String ): Unit = {
216
218
val dataset = DatasetFactory .create(getModel)
217
219
val updateQuery = UpdateFactory .create(sparql)
218
220
createUpdateProcessor(updateQuery, dataset).execute()
@@ -252,6 +254,8 @@ class DataAccess(fileNameForGeneratedContent: String,
252
254
val values2 = values.map(v => {
253
255
val objectTriple = {
254
256
if (v.isInstanceOf [IRIValue ]) Option (" <" + v.asInstanceOf [IRIValue ].iri + " >" )
257
+ else if (v.isInstanceOf [MultilingualString ])
258
+ Option (" \" " + v.asInstanceOf [MultilingualString ].value + " \" " + " @" + v.asInstanceOf [MultilingualString ].langTag)
255
259
else if (v != null ) Option (" \" " + v.toString + " \" " + " ^^<" + getXSDType(v).getURI + " >" )
256
260
else Option .empty
257
261
}
@@ -321,7 +325,7 @@ class DataAccess(fileNameForGeneratedContent: String,
321
325
}).map(_._2 + capitalizedFieldName)
322
326
}
323
327
324
- private def populateObjects [T ](groupedBySubjectResults : Map [String , List [(String , String )]], theClass : Class [T ]): List [T ] = {
328
+ private def populateObjects [T ](groupedBySubjectResults : Map [String , List [(String , ObjectResult )]], theClass : Class [T ]): List [T ] = {
325
329
val model = getModel
326
330
val prefixedNameConverterFunc = convertPrefixedName(nsPrefixes)_
327
331
val prefixedValueConverterFunc = convertPrefixedNameToValue(nsPrefixes)_
@@ -334,15 +338,18 @@ class DataAccess(fileNameForGeneratedContent: String,
334
338
val attributeName = prefixedNameConverterFunc(attribute._1).capitalize
335
339
val setterName = " set" + attributeName
336
340
val getterName = " get" + attributeName
337
- val value = prefixedValueConverterFunc(attribute._2)
341
+ val value = prefixedValueConverterFunc(attribute._2.value )
338
342
methods.find(_.getName == setterName).foreach(m => {
339
343
val getterMethod = methods.find(_.getName == getterName).headOption
340
344
val isList = m.getParameterTypes.head == classOf [util.List [Object ]]
341
345
val setterParameterType =
342
346
if (! isList) m.getParameterTypes.head
343
347
else m.getGenericParameterTypes.head.asInstanceOf [ParameterizedType ].getActualTypeArguments.headOption.map(_.asInstanceOf [Class [_]]).get
344
348
if (setterParameterType == classOf [IRIValue ]) {
345
- invokeSetterOrAddToList(m, getterMethod, instance, createIRIValue(attribute._2, model), isList)
349
+ invokeSetterOrAddToList(m, getterMethod, instance, createIRIValue(attribute._2.value, model), isList)
350
+ } else if (setterParameterType == classOf [MultilingualString ]) {
351
+ invokeSetterOrAddToList(m, getterMethod, instance,
352
+ new MultilingualString (attribute._2.value, attribute._2.asInstanceOf [MultilingualStringResult ].langtag), isList)
346
353
} else {
347
354
if (setterParameterType.getMethods.exists(_.getName == " valueOf" ) && setterParameterType != classOf [String ]) {
348
355
val numericConversion = setterParameterType.getMethod(" valueOf" , classOf [String ])
@@ -407,7 +414,7 @@ class DataAccess(fileNameForGeneratedContent: String,
407
414
new IRIValue (iri, namespace, localPart)
408
415
}
409
416
410
- private def generateRDF [T ](groupedBySubjectResults : Map [String , List [(String , String )]], rdfFormat : String ): String = {
417
+ private def generateRDF [T ](groupedBySubjectResults : Map [String , List [(String , ObjectResult )]], rdfFormat : String ): String = {
411
418
// new model and add prefixes
412
419
val inputModel = getModel
413
420
val modelToReturn = ModelFactory .createDefaultModel()
@@ -416,8 +423,11 @@ class DataAccess(fileNameForGeneratedContent: String,
416
423
groupedBySubjectResults.foreach({ case (subject, predicateObjects) => predicateObjects.foreach({
417
424
case (predicate, theObject) =>
418
425
inputModel.getResource(subject).listProperties(ResourceFactory .createProperty(predicate)).asScala.filter(s => {
419
- if (s.getObject.isLiteral) s.getObject.asLiteral().getString == theObject
420
- else s.getObject.asResource().getURI == theObject
426
+ if (s.getObject.isLiteral && s.getObject.asLiteral().getLanguage.nonEmpty)
427
+ s.getObject.asLiteral().getString == theObject.value &&
428
+ s.getObject.asLiteral().getLanguage == theObject.asInstanceOf [MultilingualStringResult ].langtag
429
+ else if (s.getObject.isLiteral) s.getObject.asLiteral().getString == theObject.value
430
+ else s.getObject.asResource().getURI == theObject.value
421
431
}).foreach(modelToReturn.add)
422
432
}) })
423
433
// serialize in the appropriate format
@@ -460,3 +470,11 @@ case class SparqlEndpointQueryExecutor(sparql: String, endpoint: String) extends
460
470
resultSet
461
471
}
462
472
}
473
+
474
+ sealed trait ObjectResult {
475
+ val value : String
476
+ }
477
+
478
+ case class LiteralResult (value : String ) extends ObjectResult
479
+ case class IRIResult (value : String ) extends ObjectResult
480
+ case class MultilingualStringResult (value : String , langtag : String ) extends ObjectResult
0 commit comments