Skip to content

Commit

Permalink
fix(#3388): Add new module for asset management
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe committed Dec 30, 2024
1 parent 4777236 commit 179ed94
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 67 deletions.
87 changes: 87 additions & 0 deletions asset-model-management/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.streampipes</groupId>
<artifactId>streampipes-parent</artifactId>
<version>0.97.0-SNAPSHOT</version>
</parent>

<artifactId>asset-model-management</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>

<!-- StreamPipes dependencies -->
<dependency>
<groupId>org.apache.streampipes</groupId>
<artifactId>streampipes-storage-api</artifactId>
<version>0.97.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.streampipes</groupId>
<artifactId>streampipes-storage-management</artifactId>
<version>0.97.0-SNAPSHOT</version>
</dependency>

<!-- External dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<propertyExpansion>
checkstyle.config.base.path=${project.parent.basedir}/tools/maven
</propertyExpansion>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.assetmodel.management;

import org.apache.streampipes.model.assets.SpAssetModel;
import org.apache.streampipes.storage.api.IGenericStorage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
* This class provides convinience methods to work with asset models
*/
public class AssetModelManagement {

private final IGenericStorage genericStorage;
private final ObjectMapper objectMapper;

public AssetModelManagement(IGenericStorage genericStorage) {
this.genericStorage = genericStorage;
this.objectMapper = new ObjectMapper();
}

/**
* Retrieves all asset models from generic storage and converts them to a list of asset models.
*
* @return a list of asset models
*/
public List<SpAssetModel> findAll() throws IOException {

try {
return genericStorage.findAll(SpAssetModel.APP_DOC_TYPE)
.stream()
.map(this::convertMapToAssetModel)
.toList();
} catch (IOException e) {
throw new IOException("Error while fetching all asset models from generic storage.", e);
}
}


/**
* Retrieves a single asset model by its ID.
*
* @param assetId the ID of the asset model to retrieve
* @return the asset model
* @throws IOException if an I/O error occurs
*/
public SpAssetModel findOne(String assetId) throws NoSuchElementException, IOException {
var assetModelData = genericStorage.findOne(assetId);

if (assetModelData == null) {
throw new NoSuchElementException("Asset model with ID " + assetId + " not found.");
}

return this.convertMapToAssetModel(assetModelData);
}

/**
* Creates a new asset model.
*
* @param asset the asset model to create
* @return the created asset model
* @throws IOException if an I/O error occurs
*/
public SpAssetModel create(String asset) throws IOException {
var assetModelInstanceInDatabase = genericStorage.create(asset);

return this.convertMapToAssetModel(assetModelInstanceInDatabase);
}

/**
*
* @param assetId the ID of the asset model to update
* @param assetModel the updated asset model
* @return the updated asset model
* @throws IOException if an I/O error occurs
*/
public SpAssetModel update(String assetId, SpAssetModel assetModel) throws IOException {
var assetModelAsJson = this.convertAssetModelToJson(assetModel);
return update(assetId, assetModelAsJson);
}

/**
* Updates an existing asset model.
*
* @param assetId the ID of the asset model to update
* @param assetModelJson the updated asset model as a JSON string
* @return the updated asset model
* @throws IOException if an I/O error occurs
*/
public SpAssetModel update(String assetId, String assetModelJson) throws IOException {
var updatedAssetModelAsMap = genericStorage.update(assetId, assetModelJson);
return this.convertMapToAssetModel(updatedAssetModelAsMap);
}

/**
* Deletes an asset model by its ID and revision.
*
* @param assetId the ID of the asset model to delete
* @param rev the revision of the asset model to delete
* @throws IOException if an I/O error occurs
*/
public void delete(String assetId, String rev) throws IOException {
genericStorage.delete(assetId, rev);
}

private SpAssetModel convertMapToAssetModel(Map<String, Object> assetModelMap) {
return objectMapper.convertValue(assetModelMap, SpAssetModel.class);
}

private String convertAssetModelToJson(SpAssetModel assetModel) throws JsonProcessingException {
return objectMapper.writeValueAsString(assetModel);
}

}
Loading

0 comments on commit 179ed94

Please sign in to comment.