-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements custom mongodb mapping converter (#81)
Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
- Loading branch information
Showing
12 changed files
with
418 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
basyx.aasrepository/basyx.aasrepository-backend-mongodb/src/test/resources/DummyAas.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"modelType": "AssetAdministrationShell", | ||
"assetInformation": null, | ||
"id": "dummyAAS", | ||
"idShort": "dummyAASIdShort" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...g/eclipse/digitaltwin/basyx/common/mongocore/CustomIdentifiableMappingMongoConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2023 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.common.mongocore; | ||
|
||
import java.io.IOException; | ||
|
||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Identifiable; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.data.mongodb.MongoDatabaseFactory; | ||
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; | ||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; | ||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
/** | ||
* Custom abstract mongodb mapping converter for applying mixins to the | ||
* metamodels for conformance to the specification. | ||
* | ||
* @author danish | ||
* | ||
*/ | ||
@Component | ||
@ConditionalOnProperty(prefix = "basyx", name = "backend", havingValue = "MongoDB") | ||
public class CustomIdentifiableMappingMongoConverter extends MappingMongoConverter { | ||
|
||
private static final String MONGODB_ID_KEY = "_id"; | ||
|
||
private ObjectMapper mapper; | ||
|
||
public CustomIdentifiableMappingMongoConverter(MongoDatabaseFactory databaseFactory, MongoMappingContext mappingContext, | ||
ObjectMapper mapper) { | ||
super(new DefaultDbRefResolver(databaseFactory), mappingContext); | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public <S> S read(Class<S> clazz, Bson source) { | ||
Document doc = (Document) source; | ||
|
||
doc.remove(MONGODB_ID_KEY); | ||
|
||
String metamodelJson = doc.toJson(); | ||
|
||
return deserializeMetamodel(clazz, metamodelJson); | ||
} | ||
|
||
@Override | ||
public void write(Object source, Bson target) { | ||
Document document = (Document) target; | ||
|
||
String id = ((Identifiable) source).getId(); | ||
|
||
String json = serializeMetamodel(source); | ||
|
||
document.put(MONGODB_ID_KEY, id); | ||
document.putAll(Document.parse(json)); | ||
} | ||
|
||
private String serializeMetamodel(Object source) { | ||
try { | ||
return mapper.writeValueAsString(source); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private <S> S deserializeMetamodel(Class<S> clazz, String string) { | ||
try { | ||
return mapper.readValue(string, clazz); | ||
} catch (IOException e) { | ||
throw new RuntimeException(string, e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.