Skip to content

Commit

Permalink
feat: add crud rest
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Dec 1, 2022
1 parent 1c42c97 commit 7b3a82f
Show file tree
Hide file tree
Showing 27 changed files with 876 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties(CrudFrameworkProperties.CONFIGURATION_PREFIX)
class CrudFrameworkProperties {
companion object {
const val CONFIGURATION_PREFIX = "crudframework"
const val CONFIGURATION_PREFIX = "crud"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public interface CrudDao {

<ID extends Serializable, Entity extends BaseCrudEntity<ID>, E extends DynamicModelFilter> long indexCount(E filter, Class<Entity> clazz);

<ID extends Serializable, Entity extends BaseCrudEntity<ID>> void softDeleteById(ID id, String deleteColumn, Class<Entity> clazz);

<ID extends Serializable, Entity extends BaseCrudEntity<ID>> void hardDeleteById(ID id, Class<Entity> clazz);

<ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity saveOrUpdate(Entity entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import studio.crud.crudframework.modelfilter.DynamicModelFilter;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;

public interface CrudDeleteHandler {
Expand All @@ -17,5 +18,5 @@ <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void deleteInternal

<ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity deleteHardTransactional(DynamicModelFilter filter, Class<Entity> clazz, List<CRUDOnDeleteHook<ID, Entity>> onHooks, boolean applyPolicies);

<ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity deleteSoftTransactional(DynamicModelFilter filter, String deleteField, Class<Entity> clazz, List<CRUDOnDeleteHook<ID, Entity>> onHooks, boolean applyPolicies);
<ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity deleteSoftTransactional(DynamicModelFilter filter, Field deleteField, Class<Entity> clazz, List<CRUDOnDeleteHook<ID, Entity>> onHooks, boolean applyPolicies);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import javax.annotation.Resource;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;

@WrapException(CrudDeleteException.class)
public class CrudDeleteHandlerImpl implements CrudDeleteHandler {
Expand Down Expand Up @@ -67,7 +69,7 @@ public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void deleteI
if(metadataDTO.getDeleteableType() == EntityMetadataDTO.DeleteableType.Hard) {
entity = crudDeleteHandlerProxy.deleteHardTransactional(filter, clazz, hooks.getOnHooks(), applyPolicies);
} else {
entity = crudDeleteHandlerProxy.deleteSoftTransactional(filter, metadataDTO.getDeleteField().getName(), clazz, hooks.getOnHooks(), applyPolicies);
entity = crudDeleteHandlerProxy.deleteSoftTransactional(filter, metadataDTO.getDeleteField(), clazz, hooks.getOnHooks(), applyPolicies);
}

crudHelper.evictEntityFromCache(entity);
Expand All @@ -92,13 +94,20 @@ public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity delet

@Override
@Transactional(readOnly = false)
public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity deleteSoftTransactional(DynamicModelFilter filter, String deleteField, Class<Entity> clazz, List<CRUDOnDeleteHook<ID, Entity>> onHooks, boolean applyPolicies) {
public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> Entity deleteSoftTransactional(DynamicModelFilter filter, Field deleteField, Class<Entity> clazz, List<CRUDOnDeleteHook<ID, Entity>> onHooks, boolean applyPolicies) {
Entity entity = getEntityForDeletion(filter, clazz, applyPolicies);

for(CRUDOnDeleteHook<ID, Entity> onHook : onHooks) {
onHook.run(entity);
}
crudHelper.getCrudDaoForEntity(clazz).softDeleteById(entity.getId(), deleteField, clazz);
try {
deleteField.setAccessible(true);
deleteField.set(entity, true);
} catch (IllegalAccessException e) {
CrudDeleteException exception = new CrudDeleteException("Error deleting entity " + clazz.getName());
exception.initCause(e);
throw exception;
}

return entity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package studio.crud.crudframework.crud.handler

import org.springframework.beans.factory.InitializingBean
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import studio.crud.crudframework.crud.policy.Policy
import studio.crud.crudframework.crud.policy.PolicyRuleType
import studio.crud.crudframework.crud.security.PrincipalProvider
Expand All @@ -15,6 +17,8 @@ internal class CrudSecurityHandlerImpl(
private val principalProvider: ObjectProvider<PrincipalProvider>
) : CrudSecurityHandler, InitializingBean {
private val policyMap = mutableMapOf<Class<*>, MutableList<Policy<PersistentEntity>>>()
@Autowired
private lateinit var applicationContext: ApplicationContext

override fun afterPropertiesSet() {
for (policy in policies.orderedStream()) {
Expand All @@ -24,7 +28,7 @@ internal class CrudSecurityHandlerImpl(
}

override fun getPolicies(clazz: Class<out PersistentEntity>): List<Policy<PersistentEntity>> {
return policyMap[clazz] ?: emptyList()
return applicationContext.getBeansOfType(Policy::class.java).values.toList() as List<Policy<PersistentEntity>>
}

override fun decorateFilter(clazz: Class<out PersistentEntity>, filter: DynamicModelFilter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ fun <RootType : PersistentEntity> not(setup: FilterFieldsBuilder<RootType>.() ->
operation = FilterFieldOperation.Not
children = filter.filterFields
}
}
}

fun <RootType : PersistentEntity> emptyFilter(): DynamicModelFilter {
return DynamicModelFilter()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package studio.crud.crudframework.ro

import java.io.Serializable
import java.util.*

/**
* Date: 10.01.13 Time: 20:27
*
* @author Shani Holdengreber
* @author thewizkid@gmail.com
*/
abstract class BaseRO<ID> : Serializable {
abstract var id: ID?

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as BaseRO<*>

if (id != other.id) return false

return true
}

override fun hashCode(): Int {
return id?.hashCode() ?: 0
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ package studio.crud.crudframework.util
import studio.crud.crudframework.modelfilter.DynamicModelFilter
import studio.crud.crudframework.modelfilter.FilterField
import studio.crud.crudframework.modelfilter.enums.FilterFieldOperation
import java.lang.reflect.Field

private fun Class<*>.getDeclaredFieldRecursive(name: String): Field {
var clazz: Class<*>? = this
while (clazz != null) {
try {
return clazz.getDeclaredField(name)
} catch (e: NoSuchFieldException) {
clazz = clazz.superclass
}
}
throw NoSuchFieldException(name)
}

private fun getPropertyValue(target: Any, string: String): Any? {
var item: Any? = target
Expand All @@ -14,7 +27,7 @@ private fun getPropertyValue(target: Any, string: String): Any? {
break
}
val clazz: Class<*> = item.javaClass
val field = clazz.getDeclaredField(part)
val field = clazz.getDeclaredFieldRecursive(part)
field.isAccessible = true
item = field.get(item)
}
Expand All @@ -26,14 +39,18 @@ private fun getPropertyValue(target: Any, string: String): Any? {
*/
fun DynamicModelFilter.filtersMatch(target: Any): Boolean {
return this.filterFields.all { filterField ->
return@all filterField.matches(target)
return@all filterField.filtersMatch(target)
}
}

/**
* Check if [target] matches the given filter field
*/
fun FilterField.matches(target: Any): Boolean {
fun FilterField.filtersMatch(target: Any): Boolean {
if (this.operation == FilterFieldOperation.Noop) {
return false
}

val actualValue: Any? = if (this.operation.junction) {
null
} else {
Expand Down Expand Up @@ -98,13 +115,13 @@ fun FilterField.matches(target: Any): Boolean {
return value.isNotEmpty()
}
FilterFieldOperation.And -> {
return children.all { it.matches(target) }
return children.all { it.filtersMatch(target) }
}
FilterFieldOperation.Or -> {
return children.any { it.matches(target) }
return children.any { it.filtersMatch(target) }
}
FilterFieldOperation.Not -> {
return children.none { it.matches(target) }
return children.none { it.filtersMatch(target) }
}
FilterFieldOperation.Noop -> return false
null -> return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class TestCrudDao : CrudDao {
TODO("Not yet implemented")
}

override fun <ID : Serializable?, Entity : BaseCrudEntity<ID>?> softDeleteById(id: ID, deleteColumn: String?, clazz: Class<Entity>?) {
TODO("Not yet implemented")
}

override fun <ID : Serializable?, Entity : BaseCrudEntity<ID>?> hardDeleteById(id: ID, clazz: Class<Entity>?) {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ public <ID extends Serializable, Entity extends BaseCrudEntity<ID>, E extends Dy
return getCriteriaCount(criteria);
}

@Override
public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void softDeleteById(ID id, String deleteColumn, Class<Entity> clazz) {
getCurrentSession().createQuery("UPDATE " + clazz.getSimpleName() + " SET " + deleteColumn + " = true where id = :id")
.setParameter("id", id)
.executeUpdate();
}

@Override
public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void hardDeleteById(ID id, Class<Entity> clazz) {
deleteObject(clazz, id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package studio.crud.crudframework.mongo.dao

import org.springframework.data.mongodb.core.query.Update
import studio.crud.crudframework.crud.handler.CrudDao
import studio.crud.crudframework.model.BaseCrudEntity
import studio.crud.crudframework.modelfilter.DynamicModelFilter
import studio.crud.crudframework.modelfilter.dsl.where
import java.io.Serializable

class MongoCrudDaoImpl : CrudDao, AbstractMongoBaseDao() {
Expand All @@ -19,18 +17,6 @@ class MongoCrudDaoImpl : CrudDao, AbstractMongoBaseDao() {
return mongoTemplate.count(buildQuery(filter), clazz)
}

override fun <ID : Serializable?, Entity : BaseCrudEntity<ID>?> softDeleteById(id: ID, deleteColumn: String?, clazz: Class<Entity>?) {
mongoTemplate.updateFirst(
buildQuery(
where<BaseCrudEntity<String>> {
BaseCrudEntity<String>::id Equal id as String
}
),
Update.update(deleteColumn, true),
clazz
)
}

override fun <ID : Serializable?, Entity : BaseCrudEntity<ID>?> hardDeleteById(id: ID, clazz: Class<Entity>?) {
deleteObject(clazz, id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class TestCrudDaoImpl : CrudDao {
}.size.toLong()
}

override fun <ID : Serializable, Entity : BaseCrudEntity<ID>> softDeleteById(id: ID, deleteColumn: String?, clazz: Class<Entity>?) {
entities.removeIf {
it.id == id
}
}

override fun <ID : Serializable, Entity : BaseCrudEntity<ID>> hardDeleteById(id: ID, clazz: Class<Entity>?) {
entities.removeIf {
it.id == id
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions crud-framework-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@
<artifactId>crud-framework-core</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>studio.crud.crudframework</groupId>
<artifactId>crud-framework-test</artifactId>
<version>${revision}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit 7b3a82f

Please sign in to comment.