-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from EphyraSoftware/feature/57-assets
Graphical assets
- Loading branch information
Showing
13 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...is-persistence/src/main/kotlin/org/ephyra/acropolis/persistence/api/GraphicalAssetType.kt
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,17 @@ | ||
package org.ephyra.acropolis.persistence.api | ||
|
||
/** | ||
* Types of data which can be stored as graphical assets | ||
*/ | ||
enum class GraphicalAssetType(val type: Int) { | ||
PNG(0); | ||
|
||
companion object { | ||
private val map = GraphicalAssetType.values().associateBy(GraphicalAssetType::type) | ||
|
||
/** | ||
* Lookup a graphical asset type enumeration value from its integer key | ||
*/ | ||
fun fromInt(type: Int) = map[type] | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...tence/src/main/kotlin/org/ephyra/acropolis/persistence/api/entity/GraphicalAssetEntity.kt
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,53 @@ | ||
package org.ephyra.acropolis.persistence.api.entity | ||
|
||
import org.ephyra.acropolis.persistence.api.GraphicalAssetType | ||
import java.util.Arrays | ||
import javax.persistence.Column | ||
import javax.persistence.Entity | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.GenerationType | ||
import javax.persistence.Id | ||
import javax.persistence.Lob | ||
|
||
/** | ||
* Entity model to represent a graphical asset (e.g. an icon) | ||
*/ | ||
@Entity | ||
data class GraphicalAssetEntity @JvmOverloads constructor( | ||
@Column(nullable = false) | ||
val name: String, | ||
|
||
@Column(nullable = false) | ||
@Lob | ||
val source: ByteArray, | ||
|
||
@Column(nullable = false) | ||
val assetType: Int, | ||
|
||
@Column | ||
var description: String? = null | ||
) { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
val id: Long? = null | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as GraphicalAssetEntity | ||
|
||
if (!Arrays.equals(source, other.source)) return false | ||
if (description != other.description) return false | ||
if (id != other.id) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = Arrays.hashCode(source) | ||
result = 31 * result + (description?.hashCode() ?: 0) | ||
result = 31 * result + (id?.hashCode() ?: 0) | ||
return result | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...main/kotlin/org/ephyra/acropolis/persistence/api/persistence/GraphicalAssetPersistence.kt
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,33 @@ | ||
package org.ephyra.acropolis.persistence.api.persistence | ||
|
||
import org.ephyra.acropolis.persistence.api.entity.GraphicalAssetEntity | ||
import org.ephyra.acropolis.persistence.impl.GraphicalAssetRepository | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* Wrapper around GraphicalAssetRepository operations. | ||
*/ | ||
@Component | ||
class GraphicalAssetPersistence { | ||
@Autowired | ||
private lateinit var repo: GraphicalAssetRepository | ||
|
||
/** | ||
* Stores a new graphical asset in the asset repository | ||
* | ||
* @param graphicalAsset The asset to store | ||
*/ | ||
fun create(graphicalAsset: GraphicalAssetEntity) { | ||
repo.save(graphicalAsset) | ||
} | ||
|
||
/** | ||
* Finds all graphical assets in the asset repository | ||
* | ||
* @return Iterator for each graphical asset entity found in the repository | ||
*/ | ||
fun findAll(): MutableIterable<GraphicalAssetEntity> { | ||
return repo.findAll() | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...istence/src/main/kotlin/org/ephyra/acropolis/persistence/impl/GraphicalAssetRepository.kt
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,9 @@ | ||
package org.ephyra.acropolis.persistence.impl | ||
|
||
import org.ephyra.acropolis.persistence.api.entity.GraphicalAssetEntity | ||
import org.springframework.data.repository.CrudRepository | ||
|
||
/** | ||
* Repository for storing graphical assets | ||
*/ | ||
interface GraphicalAssetRepository : CrudRepository<GraphicalAssetEntity, Long> |
35 changes: 35 additions & 0 deletions
35
...s-service/src/intTest/kotlin/org/ephyra/acropolis/service/GraphicalAssetServiceIntTest.kt
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,35 @@ | ||
package org.ephyra.acropolis.service | ||
|
||
import org.ephyra.acropolis.persistence.api.GraphicalAssetType | ||
import org.ephyra.acropolis.service.api.IGraphicalAssetService | ||
import org.ephyra.acropolis.service.config.ServiceConfiguration | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.test.context.junit.jupiter.SpringExtension | ||
|
||
@ExtendWith(SpringExtension::class) | ||
@SpringBootTest(classes = [IntegrationTestConfiguration::class, ServiceConfiguration::class]) | ||
class GraphicalAssetServiceIntTest { | ||
@Autowired | ||
private lateinit var graphicalAssetService: IGraphicalAssetService | ||
|
||
/** | ||
* Test that a PNG file can be used to create a graphical asset | ||
*/ | ||
@Test | ||
@DisplayName("Test that a PNG file can be used to create a graphical asset") | ||
fun createGraphicalAssetFromFile() { | ||
val data = javaClass.getResourceAsStream("/test-img.png").readAllBytes() | ||
graphicalAssetService.create("test-asset", data, GraphicalAssetType.PNG) | ||
|
||
val allAssets = graphicalAssetService.findAll() | ||
assertEquals(1, allAssets.size) | ||
|
||
val asset = allAssets[0] | ||
assertEquals(data.size, asset.data.size) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions
25
acropolis-service/src/main/kotlin/org/ephyra/acropolis/service/api/IGraphicalAssetService.kt
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,25 @@ | ||
package org.ephyra.acropolis.service.api | ||
|
||
import org.ephyra.acropolis.persistence.api.GraphicalAssetType | ||
import org.ephyra.acropolis.service.api.model.GraphicalAsset | ||
|
||
/** | ||
* Service for managing graphical assets. These assets are intended for use in diagram rendering. | ||
*/ | ||
interface IGraphicalAssetService { | ||
/** | ||
* Create a new asset record for later use. | ||
* | ||
* @param name The name which will be used to identify the asset | ||
* @param type Enum identifier for the format of the data to expect in the data field | ||
* @param data The raw data for the asset | ||
*/ | ||
fun create(name: String, data: ByteArray, type: GraphicalAssetType) | ||
|
||
/** | ||
* Finds all available graphical assets | ||
* | ||
* @return list of graphical assets | ||
*/ | ||
fun findAll(): List<GraphicalAsset> | ||
} |
8 changes: 8 additions & 0 deletions
8
acropolis-service/src/main/kotlin/org/ephyra/acropolis/service/api/model/AssetType.kt
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,8 @@ | ||
package org.ephyra.acropolis.service.api.model | ||
|
||
/** | ||
* Enumeration to represent types of asset supported by the service interface | ||
*/ | ||
enum class AssetType { | ||
PNG | ||
} |
31 changes: 31 additions & 0 deletions
31
acropolis-service/src/main/kotlin/org/ephyra/acropolis/service/api/model/GraphicalAsset.kt
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,31 @@ | ||
package org.ephyra.acropolis.service.api.model | ||
|
||
import org.ephyra.acropolis.persistence.api.GraphicalAssetType | ||
import java.util.Arrays | ||
|
||
/** | ||
* Model to represent a graphical asset. | ||
*/ | ||
data class GraphicalAsset( | ||
val type: AssetType, | ||
|
||
val data: ByteArray | ||
) { | ||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as GraphicalAsset | ||
|
||
if (type != other.type) return false | ||
if (!Arrays.equals(data, other.data)) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = type.hashCode() | ||
result = 31 * result + Arrays.hashCode(data) | ||
return result | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
acropolis-service/src/main/kotlin/org/ephyra/acropolis/service/impl/ExportService.kt
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
35 changes: 35 additions & 0 deletions
35
acropolis-service/src/main/kotlin/org/ephyra/acropolis/service/impl/GraphicalAssetService.kt
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,35 @@ | ||
package org.ephyra.acropolis.service.impl | ||
|
||
import org.ephyra.acropolis.persistence.api.GraphicalAssetType | ||
import org.ephyra.acropolis.persistence.api.entity.GraphicalAssetEntity | ||
import org.ephyra.acropolis.persistence.api.persistence.GraphicalAssetPersistence | ||
import org.ephyra.acropolis.service.api.IGraphicalAssetService | ||
import org.ephyra.acropolis.service.api.model.AssetType | ||
import org.ephyra.acropolis.service.api.model.GraphicalAsset | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Service | ||
import java.lang.IllegalStateException | ||
|
||
/** | ||
* Service implementation for working with graphical assets | ||
*/ | ||
@Service | ||
class GraphicalAssetService : IGraphicalAssetService { | ||
@Autowired | ||
private lateinit var persistence: GraphicalAssetPersistence | ||
|
||
override fun create(name: String, data: ByteArray, type: GraphicalAssetType) { | ||
persistence.create(GraphicalAssetEntity(name, data, type.type)) | ||
} | ||
|
||
override fun findAll(): List<GraphicalAsset> { | ||
return persistence.findAll().map { assetEntity -> | ||
val type = when (GraphicalAssetType.fromInt(assetEntity.assetType)) { | ||
GraphicalAssetType.PNG -> AssetType.PNG | ||
else -> throw IllegalStateException("Unknown asset type") | ||
} | ||
|
||
GraphicalAsset(type, assetEntity.source) | ||
} | ||
} | ||
} |
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