Skip to content

Commit

Permalink
💥 first attempt to break entity factories, centered around Tags as a …
Browse files Browse the repository at this point in the history
…peripheral use case

Signed-off-by: dseurotech <[email protected]>
  • Loading branch information
dseurotech committed Feb 4, 2025
1 parent 81b71f1 commit 987dc69
Show file tree
Hide file tree
Showing 77 changed files with 622 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
@Deprecated
public abstract class AbstractKapuaConfigurableResourceLimitedService<
E extends KapuaEntity,
C extends KapuaEntityCreator<E>,
C extends KapuaEntityCreator,
S extends KapuaEntityService<E, C>,
F extends KapuaEntityFactory<E>
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @since 1.0
*/
public class ServiceConfigCreator extends KapuaUpdatableEntityCreator<ServiceConfig> {
public class ServiceConfigCreator extends KapuaUpdatableEntityCreator {

private static final long serialVersionUID = 7508550960304732465L;

Expand All @@ -42,7 +42,7 @@ public ServiceConfigCreator(KapuaId scopeId) {
super(scopeId);
}

public ServiceConfigCreator(KapuaEntityCreator<ServiceConfig> entityCreator) {
public ServiceConfigCreator(KapuaEntityCreator entityCreator) {
super(entityCreator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
*******************************************************************************/
package org.eclipse.kapua.commons.jpa;

import org.eclipse.kapua.KapuaEntityExistsException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.KapuaExceptionUtils;
import org.eclipse.kapua.storage.TxContext;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Predicate;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
Expand All @@ -27,11 +23,17 @@
import javax.persistence.OptimisticLockException;
import javax.persistence.PessimisticLockException;
import javax.persistence.RollbackException;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Predicate;

import org.eclipse.kapua.KapuaEntityExistsException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.KapuaExceptionUtils;
import org.eclipse.kapua.storage.TxContext;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JpaTxContext implements JpaAwareTxContext, TxContext {

private final Logger logger = LoggerFactory.getLogger(this.getClass());
public final EntityManagerFactory entityManagerFactory;
Optional<EntityManager> entityManager = Optional.empty();
Expand Down Expand Up @@ -97,7 +99,7 @@ public KapuaException convertPersistenceException(Exception ex) {
public boolean isRecoverableException(Exception ex) {
if (ex instanceof KapuaEntityExistsException || ex instanceof EntityExistsException) {
/*
* Most KapuaEntities inherit from AbstractKapuaEntity, which auto-generates ids via a method marked with @PrePersist and the use of
* Most KapuaEntities inherit from KapuaEntityBase, which auto-generates ids via a method marked with @PrePersist and the use of
* a org.eclipse.kapua.commons.model.id.IdGenerator. Ids are pseudo-randomic. To deal with potential conflicts, a number of retries
* is allowed. The entity needs to be detached in order for the @PrePersist method to be invoked once more, generating a new id
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ protected AbstractKapuaUpdatableEntity(KapuaUpdatableEntity entity) {
setModifiedOn(entity.getModifiedOn());
setModifiedBy(entity.getModifiedBy());
setOptlock(entity.getOptlock());
setEntityAttributes(entity.getEntityAttributes());
setEntityProperties(entity.getEntityProperties());
// setEntityAttributes(entity.getEntityAttributes());
// setEntityProperties(entity.getEntityProperties());
}

@Override
Expand Down Expand Up @@ -156,7 +156,7 @@ public void setOptlock(int optlock) {
// Attributes APIs
// -------------------------------------------------

@Override
// @Override
public Properties getEntityAttributes() {
try {
return PropertiesUtils.readPropertiesFromString(attributes);
Expand All @@ -165,7 +165,7 @@ public Properties getEntityAttributes() {
}
}

@Override
// @Override
public void setEntityAttributes(Properties entityAttributes) {
try {
this.attributes = PropertiesUtils.writePropertiesToString(entityAttributes);
Expand All @@ -178,7 +178,7 @@ public void setEntityAttributes(Properties entityAttributes) {
// Properties APIs
// -------------------------------------------------

@Override
// @Override
public Properties getEntityProperties() {
try {
return PropertiesUtils.readPropertiesFromString(properties);
Expand All @@ -187,7 +187,7 @@ public Properties getEntityProperties() {
}
}

@Override
// @Override
public void setEntityProperties(Properties properties) {
try {
this.properties = PropertiesUtils.writePropertiesToString(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface KapuaBaseMapper {
@Mapping(target = "modifiedOn", ignore = true)
@Mapping(target = "modifiedBy", ignore = true)
@Mapping(target = "optlock", ignore = true)
@Mapping(target = "entityProperties", ignore = true)
@IgnoreKapuaEntityReadonlyFields
public @interface IgnoreKapuaUpdatableEntityReadonlyFields {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.eclipse.kapua.model.KapuaEntityCreator;
import org.eclipse.kapua.model.id.KapuaId;

public class EventStoreRecordCreator extends KapuaEntityCreator<EventStoreRecord> {
public class EventStoreRecordCreator extends KapuaEntityCreator {

private static final long serialVersionUID = 1048699703033893534L;

Expand All @@ -26,7 +26,7 @@ public EventStoreRecordCreator(KapuaId scopeId) {
super(scopeId);
}

public EventStoreRecordCreator(KapuaEntityCreator<EventStoreRecord> entityCreator) {
public EventStoreRecordCreator(KapuaEntityCreator entityCreator) {
super(entityCreator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private KapuaNamedEntityServiceUtils() {
* @deprecated since 2.0.0 - use {@link org.eclipse.kapua.storage.KapuaNamedEntityRepository} instead
*/
@Deprecated
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator<E>> void checkEntityNameUniqueness(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService, @NotNull C creator)
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator> void checkEntityNameUniqueness(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService, @NotNull C creator)
throws KapuaException {
checkEntityNameUniqueness(kapuaNamedEntityService, creator, Collections.emptyList());
}
Expand All @@ -98,7 +98,7 @@ public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator<E>>
* @deprecated since 2.0.0 - use {@link org.eclipse.kapua.storage.KapuaNamedEntityRepository} instead
*/
@Deprecated
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator<E>> void checkEntityNameUniqueness(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService, @NotNull C creator,
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator> void checkEntityNameUniqueness(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService, @NotNull C creator,
@NotNull List<QueryPredicate> additionalPredicates) throws KapuaException {
KapuaQuery query = new KapuaQuery();
query.setScopeId(creator.getScopeId());
Expand Down Expand Up @@ -196,7 +196,7 @@ public static <E extends KapuaNamedEntity> void checkEntityNameUniqueness(@NotNu
* @deprecated since 2.0.0 - use {@link org.eclipse.kapua.storage.KapuaNamedEntityRepository} instead
*/
@Deprecated
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator<E>> void checkEntityNameUniquenessInAllScopes(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService,
public static <E extends KapuaNamedEntity, C extends KapuaNamedEntityCreator> void checkEntityNameUniquenessInAllScopes(@NotNull KapuaEntityService<E, C> kapuaNamedEntityService,
@NotNull C creator) throws KapuaException {
KapuaQuery query = new KapuaQuery();
query.setPredicate(query.attributePredicate(KapuaNamedEntityAttributes.NAME, creator.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*******************************************************************************/
package org.eclipse.kapua.commons.model;

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;

import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.security.KapuaSession;
Expand All @@ -24,12 +28,6 @@
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

import java.math.BigInteger;
import java.util.Date;
import java.util.Properties;
import java.util.Random;


@Category(JUnitTests.class)
public class AbstractKapuaUpdatableEntityTest {

Expand Down Expand Up @@ -65,8 +63,8 @@ public void abstractKapuaUpdatableEntityScopeIdTest() {
@Test
public void abstractKapuaUpdatableEntityEntityIdTest() {
KapuaUpdatableEntity entity = new ActualKapuaUpdatableEntity();
entity.setEntityAttributes(new Properties());
entity.setEntityProperties(new Properties());
// entity.setEntityAttributes(new Properties());
// entity.setEntityProperties(new Properties());
entity.setOptlock(10);
AbstractKapuaUpdatableEntity updatableEntity = new ActualKapuaUpdatableEntity(entity);
updatableEntity.setModifiedOn(new Date());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

@Category(JUnitTests.class)
@RunWith(value = Parameterized.class)
public class AbstractKapuaEntityCreatorTest {
public class KapuaEntityBaseCreatorTest {

private final static Random RANDOM = RandomUtils.getInstance();

Expand All @@ -42,17 +42,17 @@ public static Iterable<Object[]> eids() {
return Arrays.asList(new Object[] { new BigInteger(64, RANDOM) }, new Object[] { null });
}

public AbstractKapuaEntityCreatorTest(BigInteger eid) {
public KapuaEntityBaseCreatorTest(BigInteger eid) {
this.eid = eid;
}

private class ActualKapuaEntityCreator<E extends KapuaEntity> extends KapuaEntityCreator<E> {
private class ActualKapuaEntityCreator<E extends KapuaEntity> extends KapuaEntityCreator {

public ActualKapuaEntityCreator(KapuaId scopeId) {
super(scopeId);
}

public ActualKapuaEntityCreator(KapuaEntityCreator<E> abstractEntityCreator) {
public ActualKapuaEntityCreator(KapuaEntityCreator abstractEntityCreator) {
super(abstractEntityCreator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*******************************************************************************/
package org.eclipse.kapua.commons.model;

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;

import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.security.KapuaSession;
Expand All @@ -24,13 +28,8 @@
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;


@Category(JUnitTests.class)
public class AbstractKapuaEntityTest {
public class KapuaEntityBaseTest {

private final static Random RANDOM = RandomUtils.getInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static Collection<Object[]> strings() {
});
}

private class ActualKapuaNamedEntityCreator<E extends KapuaEntity> extends KapuaNamedEntityCreator<E> {
private class ActualKapuaNamedEntityCreator<E extends KapuaEntity> extends KapuaNamedEntityCreator {

protected ActualKapuaNamedEntityCreator(KapuaId scopeId, String name) {
super(scopeId, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Properties;

import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.KapuaUpdatableEntityCreator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
Expand All @@ -26,7 +25,7 @@
@Category(JUnitTests.class)
public class KapuaUpdatableEntityCreatorTest {

private class ActualKapuaUpdatableEntityCreator<E extends KapuaEntity> extends KapuaUpdatableEntityCreator<E> {
private class ActualKapuaUpdatableEntityCreator extends KapuaUpdatableEntityCreator {

public ActualKapuaUpdatableEntityCreator(KapuaId scopeId) {
super(scopeId);
Expand All @@ -36,14 +35,14 @@ public ActualKapuaUpdatableEntityCreator(KapuaId scopeId) {
@Test
public void abstractKapuaUpdatableEntityCreatorScopeId() {
KapuaId scopeId = new KapuaEid();
KapuaUpdatableEntityCreator<AbstractKapuaEntity> kapuaUpdatableEntityCreator = new ActualKapuaUpdatableEntityCreator<>(scopeId);
KapuaUpdatableEntityCreator kapuaUpdatableEntityCreator = new ActualKapuaUpdatableEntityCreator(scopeId);
Assert.assertEquals("Actual and expected values are not the same!", scopeId, kapuaUpdatableEntityCreator.getScopeId());
}

@Test
public void getEntityAttributesTest() {
KapuaId scopeId = new KapuaEid();
KapuaUpdatableEntityCreator<AbstractKapuaEntity> kapuaUpdatableEntityCreator = new ActualKapuaUpdatableEntityCreator<>(scopeId);
KapuaUpdatableEntityCreator kapuaUpdatableEntityCreator = new ActualKapuaUpdatableEntityCreator(scopeId);
Properties properties = new Properties();
kapuaUpdatableEntityCreator.setEntityAttributes(properties);
Assert.assertEquals("Actual and expected values are not the same!", properties, kapuaUpdatableEntityCreator.getEntityAttributes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@XmlRootElement(name = "queuedJobExecutionCreator")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType
public class QueuedJobExecutionCreator extends KapuaUpdatableEntityCreator<QueuedJobExecution> {
public class QueuedJobExecutionCreator extends KapuaUpdatableEntityCreator {

private static final long serialVersionUID = 3119071638220738358L;

Expand All @@ -45,7 +45,7 @@ public QueuedJobExecutionCreator(KapuaId scopeId) {
super(scopeId);
}

public QueuedJobExecutionCreator(KapuaEntityCreator<QueuedJobExecution> entityCreator) {
public QueuedJobExecutionCreator(KapuaEntityCreator entityCreator) {
super(entityCreator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.tag.Tag;
import org.eclipse.kapua.service.tag.TagCreator;
import org.eclipse.kapua.service.tag.TagFactory;
import org.eclipse.kapua.service.tag.TagListResult;
import org.eclipse.kapua.service.tag.TagQuery;
import org.eclipse.kapua.service.tag.TagService;
Expand All @@ -49,8 +48,6 @@ public class Tags extends AbstractKapuaResource {

@Inject
public TagService tagService;
@Inject
public TagFactory tagFactory;

/**
* Gets the {@link Tag} list in the scope.
Expand Down Expand Up @@ -196,7 +193,7 @@ public Tag find(
@PathParam("tagId") EntityId tagId) throws KapuaException {
Tag tag = tagService.find(scopeId, tagId);

return returnNotNullEntity(tag, Tag.TYPE, tagId);
return returnNotNullEntity(tag, "tag", tagId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"organizationCountry",
"expirationDate"
})
public class AccountCreator extends KapuaNamedEntityCreator<Account> {
public class AccountCreator extends KapuaNamedEntityCreator {

private static final long serialVersionUID = -2460883485294616032L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ default Organization createOrganisation() {
void merge(@MappingTarget Organization account, Organization request);

//For backward compatibility only
@Mapping(target = "entityAttributes", ignore = true)
@Mapping(target = "entityProperties", ignore = true)
AccountUpdateRequest mapChildUpdate(Account account);

@Mapping(target = "entityAttributes", ignore = true)
@Mapping(target = "entityProperties", ignore = true)
CurrentAccountUpdateRequest mapCurrentUpdate(Account account);
}
Loading

0 comments on commit 987dc69

Please sign in to comment.