6666
6767import static org .opensilex .sparql .service .SPARQLQueryHelper .makeVar ;
6868
69+ //TODO MAX change name at end?
6970public class UpdateSOWithLocationObservationCollectionModel implements OpenSilexModuleUpdate {
7071
7172 private OpenSilex opensilex ;
@@ -99,22 +100,30 @@ public void execute() throws OpensilexModuleUpdateException {
99100 mongodb = opensilex .getServiceInstance (MongoDBService .DEFAULT_SERVICE , MongoDBService .class );
100101
101102 try {
103+ //Before starting the migration, check if any ScientificObject is already a featureOfInterest of a LocationObservationCollection.
104+ //If it is, then it means this migration has already been run so we leave immediately.
105+ if (wasMigrationPreviouslyRun ()){
106+ logger .warn ("It looks like this migration has already been performed. If this is not the case contact the Opensilex Team!" );
107+ return ;
108+ }
109+
102110 sparql .startTransaction ();
103111 mongodb .startTransaction ();
104112
105113 //1 - For every existing ScientificObject URI fetch all old GeospatialModels, create new LocationObservationModels,
106114 //placed in a Map of Format ScientificObject URI -> List(LocationObservationModels)
107115 StringUriMap <List <LocationObservationModel >> soLocationGeospatialListMap = makeNewLocationObservationsFromGeospatialModels ();
108- //2 - Fetch all old Moves for every ScientificObject
109- Stream <SPARQLResult > moveDetailsList = sparqlGetMoveDetails ();
110- //3 - Create a move in RDF4J for each location from geospatial Collection
116+ //2 - Create a move in RDF4J for each location from geospatial Collection
111117 List <MoveModel > newMoves = sparqlCreateSoMoves (soLocationGeospatialListMap );
118+
119+ //3 - Now Fetch all old Moves for every ScientificObject but also other stuff, normally it should just be Devices
120+ Stream <SPARQLResult > moveDetailsList = sparqlGetMoveDetails ();
112121 //4 - Make new Location Observations for old existing Moves, add them to the soLocationGeospatialListMap Map
113122 StringUriMap <List <LocationObservationModel >> soLocationListMap = makeLocationObservationsFromExistingMoves (soLocationGeospatialListMap , moveDetailsList );
114123 //5 - Add collections for each OS that has at least one LocationObservation. Return in a Map of format OS URI -> ObservationCOLLECTION URI
115124 StringUriMap <URI > soCollectionMap = sparqlAddLocationCollection (soLocationListMap );
116- //6 - Complete location models for each SO and insert to Location collection
117- mongoSoToLocationCollection (soLocationListMap , soCollectionMap , newMoves );
125+ //6 - Complete location observation models for each SO and insert to Location collection
126+ setObservationCollectionAndMoveUrisAndInsertLocationObservations (soLocationListMap , soCollectionMap , newMoves );
118127
119128 sparql .commitTransaction ();
120129 mongodb .commitTransaction ();
@@ -131,25 +140,47 @@ public void execute() throws OpensilexModuleUpdateException {
131140 }
132141 }
133142
143+ /**
144+ * Checks if this migration was most likely already run. Does this by looking to see if any OS is already a feature of interest of a LocationObservationCollection.
145+ * @return true if any OS's are feature of interest, false if nay
146+ */
147+ private boolean wasMigrationPreviouslyRun () throws SPARQLException {
148+ //TODO MAX add for generic moves
149+ SelectBuilder select = new SelectBuilder ();
150+ Var uriVar = makeVar (SPARQLResourceModel .URI_FIELD );
151+ Var collectionVar = makeVar ("collection" );
152+ Var typeVar = makeVar ("type" );
153+
154+ select .addVar (uriVar );
155+ select .addWhere (typeVar , Ontology .subClassAny , Oeso .ScientificObject );
156+ select .addWhere (uriVar , RDF .type , typeVar );
157+ select .addWhere (collectionVar , RDF .type , SOSA .ObservationCollection );
158+ select .addWhere (collectionVar , SOSA .hasFeatureOfInterest , uriVar );
159+
160+ return !sparql .executeSelectQueryAsStream (select ).toList ().isEmpty ();
161+ }
162+
134163 /**
135164 *
136165 * @return a Map of format ScientificObjectURI -> List(LocationObservationModel), corresponding to the list of found
137166 * locations (in correct new format) for every existing ScientificObject.
167+ *
138168 */
139169 private StringUriMap <List <LocationObservationModel >> makeNewLocationObservationsFromGeospatialModels () throws SPARQLException {
140- //Get OS subClass
170+ //Get OS subClasses
141171 SelectBuilder select = new SelectBuilder ().addWhere (new TriplePath (makeVar (ScientificObjectModel .TYPE_FIELD ), Ontology .subClassAny , Oeso .ScientificObject .asNode ()));
142172 List <URI > soRdfType = sparql .executeSelectQueryAsStream (select )
143173 .map (sparqlResult -> URI .create (SPARQLDeserializers .getExpandedURI (sparqlResult .getStringValue (ScientificObjectModel .TYPE_FIELD ))))
144174 .collect (Collectors .toList ());
145175
146- StringUriMap <List <LocationObservationModel >> soLocationListMap = new StringUriMap <>();
147176 MongoDatabase db = mongodb .getDatabase ();
148177
149178 //Get SO from geospatial collection
150179 MongoCollection <GeospatialModel > geospatialCollection = db .getCollection (GeospatialDAO .GEOSPATIAL_COLLECTION_NAME , GeospatialModel .class );
151180 List <GeospatialModel > soFromGeospatial = geospatialCollection .find (Filters .in (SPARQLResourceModel .TYPE_FIELD , soRdfType )).into (new ArrayList <>());
152181
182+ StringUriMap <List <LocationObservationModel >> soLocationListMap = new StringUriMap <>();
183+
153184 // Mapping FeatureOfInterest and locations
154185 // from geospatial collection
155186 soFromGeospatial .forEach (geo ->{
@@ -219,12 +250,12 @@ private Stream<SPARQLResult> sparqlGetMoveDetails() throws SPARQLException {
219250 Var toVar = makeVar (LocationModel .TO_FIELD );
220251 Var fromVar = makeVar (LocationModel .FROM_FIELD );
221252 Var featureVar = makeVar (LocationObservationModel .FEATURE_OF_INTEREST_FIELD );
222- Var soTypeVar = makeVar (ScientificObjectModel .TYPE_FIELD );
253+ // Var soTypeVar = makeVar(ScientificObjectModel.TYPE_FIELD);
223254
224255 //where SO clause
225- WhereBuilder whereSO = new WhereBuilder ()
256+ /* WhereBuilder whereSO = new WhereBuilder()
226257 .addWhere(featureVar, RDF.type, soTypeVar)
227- .addWhere (new TriplePath (soTypeVar , Ontology .subClassAny , Oeso .ScientificObject .asNode ()));
258+ .addWhere(new TriplePath(soTypeVar, Ontology.subClassAny, Oeso.ScientificObject.asNode()));*/
228259
229260 //where event clause
230261 //start optional clause
@@ -242,16 +273,16 @@ private Stream<SPARQLResult> sparqlGetMoveDetails() throws SPARQLException {
242273
243274 WhereBuilder where = new WhereBuilder ().addGraph (eventGraph ,whereEvent );
244275 SelectBuilder select = new SelectBuilder ()
245- .addWhere (whereSO )
276+ // .addWhere(whereSO)
246277 .addWhere (where )
247278 .addVar (eventVar )
248279 .addVar (endVar )
249280 .addVar (endDateVar )
250281 .addVar (startVar )
251282 .addVar (startDateVar )
252283 .addVar (toVar )
253- .addVar (fromVar )
254- .addVar (soTypeVar );
284+ .addVar (fromVar );
285+ // .addVar(soTypeVar);
255286
256287 SPARQLQueryHelper .appendGroupConcatAggregator (select , featureVar , true );
257288 select .addGroupBy (eventVar );
@@ -261,7 +292,7 @@ private Stream<SPARQLResult> sparqlGetMoveDetails() throws SPARQLException {
261292 select .addGroupBy (startDateVar );
262293 select .addGroupBy (toVar );
263294 select .addGroupBy (fromVar );
264- select .addGroupBy (soTypeVar );
295+ // select.addGroupBy(soTypeVar);
265296
266297 return sparql .executeSelectQueryAsStream (select );
267298 }
@@ -467,7 +498,7 @@ private StringUriMap<URI> sparqlAddLocationCollection(StringUriMap<List<Location
467498 return soCollectionMap ;
468499 }
469500
470- private void mongoSoToLocationCollection (StringUriMap <List <LocationObservationModel >> soLocationListMap , StringUriMap <URI > soCollectionMap , List <MoveModel > newMoves ) throws NoSQLAlreadyExistingUriException , URISyntaxException {
501+ private void setObservationCollectionAndMoveUrisAndInsertLocationObservations (StringUriMap <List <LocationObservationModel >> soLocationListMap , StringUriMap <URI > soCollectionMap , List <MoveModel > newMoves ) throws NoSQLAlreadyExistingUriException , URISyntaxException {
471502 MongoCollection <LocationObservationModel > locationCollection = mongodb .getDatabase ().getCollection (LocationObservationDAO .LOCATION_COLLECTION_NAME , LocationObservationModel .class );
472503
473504 soLocationListMap .forEach ((feature , locationList ) -> locationList .forEach (location -> {
0 commit comments