com.xlrit.gears.base.repository.AbstractRepository Maven / Gradle / Ivy
package com.xlrit.gears.base.repository;
import java.util.Collection;
import java.util.List;
import com.xlrit.gears.base.collection.FixedSizeList;
import com.xlrit.gears.base.event.EntityEventManager;
import com.xlrit.gears.base.execution.Execution;
import com.xlrit.gears.base.function.DefaultFunctions;
import com.xlrit.gears.base.model.Publishable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
public abstract class AbstractRepository implements Repository {
protected final Class entityType;
@Autowired
protected EntityEventManager entityEventManager;
protected AbstractRepository(Class entityType) {
this.entityType = entityType;
}
public Class getEntityType() {
return entityType;
}
public T findById(String id) { throw new UnsupportedOperationException(); }
public List findAll() { throw new UnsupportedOperationException(); }
public List findMany(Specification spec) { throw new UnsupportedOperationException(); }
public T create(Execution execution) {
return doCreate(execution);
}
public List createCollection(Execution execution, List> linkedElements) {
return new FixedSizeList<>(entityType, linkedElements.size());
}
public void remove(T instance, Execution execution) {
doRemove(instance, execution);
}
public void removeAll(Collection instances, Execution execution) {
for (T instance : instances) {
doRemove(instance, execution);
}
}
public void publish(T instance, Execution execution) {
doPublish(instance, execution);
}
protected abstract T doCreate(Execution execution);
protected abstract void doRemove(T instance, Execution execution);
protected void doPublish(T instance, Execution execution) {
if (instance instanceof Publishable publishable) {
publishable.setPublishedAt(DefaultFunctions.currentDatetime());
publishable.setPublisher(execution.getCurrentUser());
}
entityEventManager.fireElementPublished(entityType, instance);
}
}