Skip to content

Commit

Permalink
Merge pull request #58 from EphyraSoftware/feature/57-assets
Browse files Browse the repository at this point in the history
Graphical assets
  • Loading branch information
Sub-Xaero authored Oct 25, 2018
2 parents 4f771aa + e74e873 commit ffdb2ae
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 2 deletions.
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]
}
}
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
}
}
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()
}
}
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>
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)
}
}
Binary file added acropolis-service/src/intTest/resources/test-img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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>
}
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
}
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
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.ephyra.acropolis.service.impl

import org.ephyra.acropolis.external.*
import org.ephyra.acropolis.external.RefType
import org.ephyra.acropolis.external.SystemSoftwareSpecialization
import org.ephyra.acropolis.external.YamlHelper
import org.ephyra.acropolis.external.model.ApplicationSoftware
import org.ephyra.acropolis.external.model.Project
import org.ephyra.acropolis.external.model.SoftwareContainer
import org.ephyra.acropolis.external.model.SystemSoftware
import org.ephyra.acropolis.external.packRef
import org.ephyra.acropolis.external.packSystemSpecialization
import org.ephyra.acropolis.persistence.api.ConnectionType
import org.ephyra.acropolis.persistence.api.IConnectable
import org.ephyra.acropolis.persistence.api.entity.ApplicationSoftwareEntity
Expand Down
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ExportCommand {
/**
* The export method for the export command
*/
@Suppress("ReturnCount")
@ShellMethod("Command for running exports")
fun export(filePath: String) {
val currentProject = appState.currentProject
Expand All @@ -47,4 +48,4 @@ class ExportCommand {

file.writeText(exportData)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ephyra.acropolis.shell

import org.ephyra.acropolis.persistence.api.GraphicalAssetType
import org.ephyra.acropolis.service.api.IGraphicalAssetService
import org.ephyra.acropolis.service.api.ImportType
import org.ephyra.acropolis.service.impl.ImportService
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -15,6 +17,9 @@ class ImportCommand {
@Autowired
lateinit var importService: ImportService

@Autowired
lateinit var graphicalAssetService: IGraphicalAssetService

/**
* The import method for the import command
*/
Expand All @@ -29,6 +34,7 @@ class ImportCommand {
when (file.extension) {
"yml" -> importService.importProject(file.readText(), ImportType.YAML)
"json" -> importService.importProject(file.readText(), ImportType.JSON)
"png" -> graphicalAssetService.create(file.name, file.readBytes(), GraphicalAssetType.PNG)
else -> println("Don't know how to import from files of type [${file.extension}]")
}
}
Expand Down

0 comments on commit ffdb2ae

Please sign in to comment.