Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.EntityType;
import javax.validation.constraints.Null;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -192,37 +193,6 @@ public static <E extends KapuaEntity> E create(@NonNull EntityManager em, @NonNu
return entity;
}

/**
* Finds a {@link KapuaEntity}.
*
* @param em The {@link EntityManager} that holds the transaction.
* @param clazz The {@link KapuaEntity} class. This must be the implementing {@code class}.
* @param scopeId The {@link KapuaEntity#getScopeId()} the entity to be found.
* @param entityId The {@link KapuaEntity#getId()} of the entity to be found.
* @since 1.0.0
*/
public static <E extends KapuaEntity> E find(@NonNull EntityManager em, @NonNull Class<E> clazz, @Nullable KapuaId scopeId, @NonNull KapuaId entityId) {
//
// Checking existence
E entityToFind = em.find(clazz, entityId);

//
// Return if not null and scopeIds matches
if (entityToFind != null) {
if (scopeId == null) {
return entityToFind;
} else if (entityToFind.getScopeId() == null) {
return entityToFind;
} else if (entityToFind.getScopeId().equals(scopeId)) {
return entityToFind;
} else {
return null;
}
} else {
return null;
}
}

/**
* Updates the {@link KapuaUpdatableEntity}.
*
Expand Down Expand Up @@ -256,31 +226,35 @@ public static <E extends KapuaUpdatableEntity> E update(@NonNull EntityManager e
}

/**
* Deletes a {@link KapuaEntity}.
* Finds a {@link KapuaEntity}.
*
* @param em The {@link EntityManager} that holds the transaction.
* @param clazz The {@link KapuaEntity} class. This must be the implementing {@code class}.
* @param scopeId The {@link KapuaEntity#getScopeId()} of the entity to be deleted.
* @param entityId The {@link KapuaEntity#getId()} of the entity to be deleted.
* @return The deleted {@link KapuaEntity}.
* @throws KapuaEntityNotFoundException If the {@link KapuaEntity} does not exists.
* @param scopeId The {@link KapuaEntity#getScopeId()} the entity to be found.
* @param entityId The {@link KapuaEntity#getId()} of the entity to be found.
* @since 1.0.0
*/
public static <E extends KapuaEntity> E delete(@NonNull EntityManager em, @NonNull Class<E> clazz, @NonNull KapuaId scopeId, @NonNull KapuaId entityId)
throws KapuaEntityNotFoundException {
public static <E extends KapuaEntity> E find(@NonNull EntityManager em, @NonNull Class<E> clazz, @Null KapuaId scopeId, @NonNull KapuaId entityId) {
//
// Checking existence
E entityToDelete = find(em, clazz, scopeId, entityId);
E entityToFind = em.find(clazz, entityId);

// If 'null' ScopeId has been requested, it means that we need to look for ANY ScopeId.
KapuaId scopeIdToMatch = scopeId != null ? scopeId : KapuaId.ANY;

//
// Deleting if not null and scopeIds matches
if (entityToDelete != null) {
em.remove(entityToDelete);
em.flush();
// Return if not null and ScopeIds matches
if (entityToFind != null) {
if (KapuaId.ANY.equals(scopeIdToMatch)) { // If requested ScopeId is ANY, return whatever Entity has been found
return entityToFind;
} else if (scopeIdToMatch.equals(entityToFind.getScopeId())) { // If a specific ScopeId is requested, return Entity if given ScopeId matches Entity.scopeId
return entityToFind;
} else { // If no match, return no result
return null;
}
} else {
throw new KapuaEntityNotFoundException(clazz.getSimpleName(), entityId);
return null;
}
return entityToDelete;
}

/**
Expand All @@ -297,7 +271,7 @@ public static <E extends KapuaEntity> E delete(@NonNull EntityManager em, @NonNu
public static <E extends KapuaNamedEntity> E findByName(@NonNull EntityManager em,
@NonNull Class<E> clazz,
@NonNull Object value) {
return findByName(em, clazz, null, value);
return findByName(em, clazz, KapuaId.ANY, value);
}

/**
Expand All @@ -314,7 +288,7 @@ public static <E extends KapuaNamedEntity> E findByName(@NonNull EntityManager e
@Nullable
public static <E extends KapuaNamedEntity> E findByName(@NonNull EntityManager em,
@NonNull Class<E> clazz,
@Nullable KapuaId scopeId,
@NonNull KapuaId scopeId,
@NonNull Object value) {
return findByField(em, clazz, scopeId, KapuaNamedEntityAttributes.NAME, value);
}
Expand All @@ -335,7 +309,7 @@ public static <E extends KapuaEntity> E findByField(@NonNull EntityManager em,
@NonNull Class<E> clazz,
@NonNull String name,
@NonNull Object value) {
return findByField(em, clazz, null, name, value);
return findByField(em, clazz, KapuaId.ANY, name, value);
}

/**
Expand All @@ -353,7 +327,7 @@ public static <E extends KapuaEntity> E findByField(@NonNull EntityManager em,
@Nullable
public static <E extends KapuaEntity> E findByField(@NonNull EntityManager em,
@NonNull Class<E> clazz,
@Nullable KapuaId scopeId,
@NonNull KapuaId scopeId,
@NonNull String name,
@NonNull Object value) {
CriteriaBuilder cb = em.getCriteriaBuilder();
Expand All @@ -373,7 +347,7 @@ public static <E extends KapuaEntity> E findByField(@NonNull EntityManager em,

ParameterExpression<KapuaId> pScopeId = null;

if (scopeId != null) {
if (!KapuaId.ANY.equals(scopeId)) {
pScopeId = cb.parameter(KapuaId.class, KapuaEntityAttributes.SCOPE_ID);
Predicate scopeIdPredicate = cb.equal(entityRoot.get(KapuaEntityAttributes.SCOPE_ID), pScopeId);

Expand Down Expand Up @@ -445,7 +419,9 @@ public static <I extends KapuaEntity, E extends I, L extends KapuaListResult<I>>
//
// WHERE
QueryPredicate kapuaPredicates = kapuaQuery.getPredicate();
if (kapuaQuery.getScopeId() != null) {
// Add ScopeId to query if has been defined one specific
if (kapuaQuery.getScopeId() != null && // Support for old method of querying for all ScopeIds (e.g.: query.setScopeId(null)
!kapuaQuery.getScopeId().equals(KapuaId.ANY)) {// Support for new method of querying for all ScopeIds (e.g.: query.setScopeId(KapuaId.ANY)

AndPredicate scopedAndPredicate = kapuaQuery.andPredicate(
kapuaQuery.attributePredicate(KapuaEntityAttributes.SCOPE_ID, kapuaQuery.getScopeId())
Expand Down Expand Up @@ -554,7 +530,9 @@ public static <I extends KapuaEntity, E extends I> long count(@NonNull EntityMan
//
// WHERE
QueryPredicate kapuaPredicates = kapuaQuery.getPredicate();
if (kapuaQuery.getScopeId() != null) {
// Add ScopeId to query if has been defined one specific
if (kapuaQuery.getScopeId() != null && // Support for old method of querying for all ScopeIds (e.g.: query.setScopeId(null)
!kapuaQuery.getScopeId().equals(KapuaId.ANY)) {// Support for new method of querying for all ScopeIds (e.g.: query.setScopeId(KapuaId.ANY)

AndPredicate scopedAndPredicate = kapuaQuery.andPredicate();

Expand Down Expand Up @@ -589,6 +567,41 @@ public static <I extends KapuaEntity, E extends I> long count(@NonNull EntityMan
return query.getSingleResult();
}

/**
* Deletes a {@link KapuaEntity}.
*
* @param em The {@link EntityManager} that holds the transaction.
* @param clazz The {@link KapuaEntity} class. This must be the implementing {@code class}.
* @param scopeId The {@link KapuaEntity#getScopeId()} of the entity to be deleted.
* @param entityId The {@link KapuaEntity#getId()} of the entity to be deleted.
* @return The deleted {@link KapuaEntity}.
* @throws KapuaEntityNotFoundException If the {@link KapuaEntity} does not exists.
* @since 1.0.0
*/
public static <E extends KapuaEntity> E delete(@NonNull EntityManager em, @NonNull Class<E> clazz, @NonNull KapuaId scopeId, @NonNull KapuaId entityId)
throws KapuaEntityNotFoundException {
//
// Checking existence
E entityToDelete = find(em, clazz, scopeId, entityId);

//
// Deleting if found
if (entityToDelete != null) {
em.remove(entityToDelete);
em.flush();
} else {
throw new KapuaEntityNotFoundException(clazz.getSimpleName(), entityId);
}

//
// Returning deleted entity
return entityToDelete;
}

//
// Private Methods
//

/**
* Handles {@link QueryPredicate} contained of a {@link KapuaQuery}.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class GwtJobStepDefinitionServiceImpl extends KapuaRemoteServiceServlet i
public ListLoadResult<GwtJobStepDefinition> findAll() throws GwtKapuaException {
List<GwtJobStepDefinition> gwtJobStepDefinitionList = new ArrayList<GwtJobStepDefinition>();
try {
JobStepDefinitionListResult result = JOB_STEP_DEFINITION_SERVICE.query(JOB_STEP_DEFINITION_FACTORY.newQuery(null));
JobStepDefinitionListResult result = JOB_STEP_DEFINITION_SERVICE.query(JOB_STEP_DEFINITION_FACTORY.newQuery(KapuaId.ANY));
for (JobStepDefinition jsd : result.getItems()) {

if (!Strings.isNullOrEmpty(JOB_STEP_DEFINITION_EXCLUDE_REGEX) && jsd.getName().matches(JOB_STEP_DEFINITION_EXCLUDE_REGEX)) {
Expand All @@ -76,7 +76,7 @@ public GwtJobStepDefinition find(String gwtJobStepDefinitionId) throws GwtKapuaE

GwtJobStepDefinition gwtJobStepDefinition = null;
try {
JobStepDefinition jobStepDefinition = JOB_STEP_DEFINITION_SERVICE.find(null, jobStepDefinitionId);
JobStepDefinition jobStepDefinition = JOB_STEP_DEFINITION_SERVICE.find(KapuaId.ANY, jobStepDefinitionId);
if (jobStepDefinition != null) {
gwtJobStepDefinition = KapuaGwtJobModelConverter.convertJobStepDefinition(jobStepDefinition);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public PagingLoadResult<GwtJobStep> query(PagingLoadConfig loadConfig, GwtJobSte
for (JobStep js : jobStepList.getItems()) {
GwtJobStep gwtJobStep = KapuaGwtJobModelConverter.convertJobStep(js);

JobStepDefinition jobStepDefinition = JOB_STEP_DEFINITION_SERVICE.find(GwtKapuaCommonsModelConverter.convertKapuaId(gwtJobStep.getScopeId()), GwtKapuaCommonsModelConverter.convertKapuaId(gwtJobStep.getJobStepDefinitionId()));
JobStepDefinition jobStepDefinition = JOB_STEP_DEFINITION_SERVICE.find(KapuaId.ANY, GwtKapuaCommonsModelConverter.convertKapuaId(gwtJobStep.getJobStepDefinitionId()));
gwtJobStep.setJobStepDefinitionName(jobStepDefinition.getName());

setEnumOnJobStepProperty(gwtJobStep.getStepProperties());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static void startJob(@NotNull KapuaId scopeId, @NotNull KapuaId jobId, @N
JobStep jobStep = jobStepIterator.next();

Step jslStep = new Step();
JobStepDefinition jobStepDefinition = STEP_DEFINITION_SERVICE.find(jobStep.getScopeId(), jobStep.getJobStepDefinitionId());
JobStepDefinition jobStepDefinition = STEP_DEFINITION_SERVICE.find(KapuaId.ANY, jobStep.getJobStepDefinitionId());
switch (jobStepDefinition.getStepType()) {
case GENERIC:
jslStep.setBatchlet(JobDefinitionBuildUtils.buildGenericStep(jobStepDefinition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand All @@ -49,7 +49,7 @@ public class AccessInfoDAOTest {

EntityManager entityManager;
AccessInfoCreator accessInfoCreator;
KapuaId scopeId, accessInfoId;
KapuaId scopeId, accessInfoId;
AccessInfoImpl entityToFindOrDelete;
KapuaQuery kapuaQuery;

Expand Down Expand Up @@ -154,7 +154,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessInfoImpl.class, accessInfoId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessInfoDAO.find(entityManager, scopeId, accessInfoId) instanceof AccessInfo);
Assert.assertTrue("True expected.", AccessInfoDAO.find(entityManager, KapuaId.ANY, accessInfoId) instanceof AccessInfo);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -266,6 +266,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessInfoImpl.class, accessInfoId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessInfoDAO.delete(entityManager, scopeId, accessInfoId) instanceof AccessInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -153,7 +153,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessPermissionImpl.class, accessPermissionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessPermissionDAO.find(entityManager, scopeId, accessPermissionId) instanceof AccessPermission);
Assert.assertTrue("True expected.", AccessPermissionDAO.find(entityManager, KapuaId.ANY, accessPermissionId) instanceof AccessPermission);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -265,6 +265,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessPermissionImpl.class, accessPermissionId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessPermissionDAO.delete(entityManager, scopeId, accessPermissionId) instanceof AccessPermission);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authorization.access.AccessRole;
import org.eclipse.kapua.service.authorization.access.AccessRoleCreator;
import org.eclipse.kapua.service.authorization.access.AccessRoleListResult;
import org.eclipse.kapua.service.authorization.access.AccessRole;
import org.eclipse.kapua.service.authorization.access.shiro.AccessRoleDAO;
import org.eclipse.kapua.service.authorization.access.shiro.AccessRoleImpl;
import org.junit.Assert;
Expand All @@ -33,10 +33,10 @@
import javax.persistence.EntityExistsException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.metamodel.EntityType;
import java.sql.SQLException;
Expand Down Expand Up @@ -162,7 +162,7 @@ public void findNullEntityToFindScopeIdTest() {
Mockito.when(entityManager.find(AccessRoleImpl.class, accessRoleId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(null);

Assert.assertTrue("True expected.", AccessRoleDAO.find(entityManager, scopeId, accessRoleId) instanceof AccessRole);
Assert.assertTrue("True expected.", AccessRoleDAO.find(entityManager, KapuaId.ANY, accessRoleId) instanceof AccessRole);
}

@Test(expected = NullPointerException.class)
Expand Down Expand Up @@ -274,6 +274,7 @@ public void countNullKapuaQueryTest() throws KapuaException {
@Test
public void deleteTest() throws KapuaEntityNotFoundException {
Mockito.when(entityManager.find(AccessRoleImpl.class, accessRoleId)).thenReturn(entityToFindOrDelete);
Mockito.when(entityToFindOrDelete.getScopeId()).thenReturn(KapuaId.ONE);

Assert.assertTrue("True expected.", AccessRoleDAO.delete(entityManager, scopeId, accessRoleId) instanceof AccessRole);
}
Expand Down
Loading