Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added KapuaQuery.defaultSortCriteria #3287

Merged
merged 4 commits into from
Apr 19, 2021
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 @@ -15,10 +15,10 @@
import org.eclipse.kapua.model.query.KapuaQuery;

/**
* Service configuration query.
*
* @since 1.0
* {@link ServiceConfig} {@link KapuaQuery} definition.
*
* @see KapuaQuery
* @since 1.0.0
*/
public interface ServiceConfigQuery extends KapuaQuery {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@
import org.eclipse.kapua.model.id.KapuaId;

/**
* Service configuration query reference implementation.
* {@link ServiceConfigQuery} implementation.
*
* @since 1.0
* @since 1.0.0
*/
public class ServiceConfigQueryImpl extends AbstractKapuaQuery implements ServiceConfigQuery {

/**
* Constructor
* Constructor.
*
* @since 1.0.0
*/
private ServiceConfigQueryImpl() {
super();
}

/**
* Constructor
* Constructor.
*
* @param scopeId
* @param scopeId The {@link #getScopeId()}.
* @since 1.0.0
*/
public ServiceConfigQueryImpl(KapuaId scopeId) {
this();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2016, 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.model.query;

import org.eclipse.kapua.model.KapuaNamedEntity;
import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.model.query.KapuaSortCriteria;
import org.eclipse.kapua.model.query.SortOrder;

/**
* {@link KapuaQuery} {@code abstract} implementation for {@link KapuaNamedEntity}.
* <p>
* It default the {@link #getSortCriteria()} on the {@link KapuaNamedEntityAttributes#NAME}
*
* @since 1.5.0
*/
public abstract class AbstractKapuaNamedQuery extends AbstractKapuaQuery implements KapuaQuery {

/**
* Constructor.
*
* @since 1.5.0
*/
public AbstractKapuaNamedQuery() {
super();
}

/**
* Constructor.
*
* @param scopeId The {@link #getScopeId()}.
* @since 1.5.0
*/
public AbstractKapuaNamedQuery(KapuaId scopeId) {
super(scopeId);
}

/**
* Clone constructor.
*
* @param query The {@link KapuaQuery} to clone.
* @since 1.5.0
*/
public AbstractKapuaNamedQuery(KapuaQuery query) {
super(query);
}

@Override
public KapuaSortCriteria getDefaultSortCriteria() {
return fieldSortCriteria(KapuaNamedEntityAttributes.NAME, SortOrder.ASCENDING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.kapua.commons.model.query.predicate.AndPredicateImpl;
import org.eclipse.kapua.commons.model.query.predicate.AttributePredicateImpl;
import org.eclipse.kapua.commons.model.query.predicate.OrPredicateImpl;
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.FieldSortCriteria;
Expand Down Expand Up @@ -51,13 +50,10 @@ public abstract class AbstractKapuaQuery implements KapuaQuery {

/**
* Constructor.
* <p>
* It defaults the {@link #sortCriteria} to order by the {@link KapuaEntity#getCreatedOn()} {@link SortOrder#ASCENDING}.
*
* @since 1.0.0
*/
public AbstractKapuaQuery() {
setSortCriteria(fieldSortCriteria(KapuaEntityAttributes.CREATED_ON, SortOrder.ASCENDING));
}

/**
Expand All @@ -80,11 +76,12 @@ public AbstractKapuaQuery(KapuaId scopeId) {
* @param query the query to clone.
*/
public AbstractKapuaQuery(@NotNull KapuaQuery query) {
this.setFetchAttributes(query.getFetchAttributes());
this.setPredicate(query.getPredicate());
this.setLimit(query.getLimit());
this.setOffset(query.getOffset());
this.setSortCriteria(query.getSortCriteria());
setFetchAttributes(query.getFetchAttributes());
setPredicate(query.getPredicate());
setLimit(query.getLimit());
setOffset(query.getOffset());
setSortCriteria(query.getSortCriteria());
setAskTotalCount(query.getAskTotalCount());
}

@Override
Expand Down Expand Up @@ -136,6 +133,11 @@ public void setSortCriteria(KapuaSortCriteria sortCriteria) {
this.sortCriteria = sortCriteria;
}

@Override
public KapuaSortCriteria getDefaultSortCriteria() {
return fieldSortCriteria(KapuaEntityAttributes.ENTITY_ID, SortOrder.ASCENDING);
}

@Override
public Integer getOffset() {
return offset;
Expand All @@ -156,6 +158,16 @@ public void setLimit(Integer limit) {
this.limit = limit;
}

@Override
public Boolean getAskTotalCount() {
return askTotalCount;
}

@Override
public void setAskTotalCount(Boolean askTotalCount) {
this.askTotalCount = askTotalCount;
}

//
// Predicate factory
@Override
Expand Down Expand Up @@ -192,14 +204,4 @@ public OrPredicate orPredicate(QueryPredicate... queryPredicates) {
public FieldSortCriteria fieldSortCriteria(String attributeName, SortOrder sortOrder) {
return new FieldSortCriteriaImpl(attributeName, sortOrder);
}

@Override
public Boolean getAskTotalCount() {
return askTotalCount;
}

public void setAskTotalCount(Boolean askTotalCount) {
this.askTotalCount = askTotalCount;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
*******************************************************************************/
package org.eclipse.kapua.commons.service.event.store.api;

import org.eclipse.kapua.model.query.KapuaQuery;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.kapua.model.query.KapuaQuery;

/**
* KapuaEvent query definition.
*
* @since 1.0
* {@link EventStoreRecord} {@link KapuaQuery} definition.
*
* @see KapuaQuery
* @since 1.0.0
*/
@XmlRootElement(name = "query")
@XmlAccessorType(XmlAccessType.PROPERTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordQuery;
import org.eclipse.kapua.model.id.KapuaId;

/**
* {@link EventStoreRecordQuery} implementation.
*
* @since 1.0.0
*/
public class EventStoreQueryImpl extends AbstractKapuaQuery implements EventStoreRecordQuery {

/**
* Constructor
* Constructor.
*
* @since 1.0.0
*/
public EventStoreQueryImpl() {
super();
}

/**
* Constructor
* Constructor.
*
* @param scopeId
* @param scopeId The {@link #getScopeId()}.
* @since 1.0.0
*/
public EventStoreQueryImpl(KapuaId scopeId) {
this();
setScopeId(scopeId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.kapua.commons.service.internal;

import com.google.common.base.MoreObjects;
import org.apache.commons.lang.ArrayUtils;
import org.eclipse.kapua.KapuaEntityExistsException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
Expand Down Expand Up @@ -433,15 +434,14 @@ public static <I extends KapuaEntity, E extends I, L extends KapuaListResult<I>>
// ORDER BY
// Default to the KapuaEntity id if no ordering is specified.
Order order;
if (kapuaQuery.getSortCriteria() != null) {
FieldSortCriteria sortCriteria = (FieldSortCriteria) kapuaQuery.getSortCriteria();
if (kapuaQuery.getSortCriteria() != null || kapuaQuery.getDefaultSortCriteria() != null) {
FieldSortCriteria sortCriteria = (FieldSortCriteria) MoreObjects.firstNonNull(kapuaQuery.getSortCriteria(), kapuaQuery.getDefaultSortCriteria());

if (SortOrder.DESCENDING.equals(sortCriteria.getSortOrder())) {
order = cb.desc(extractAttribute(entityRoot, sortCriteria.getAttributeName()));
} else {
order = cb.asc(extractAttribute(entityRoot, sortCriteria.getAttributeName()));
}

} else {
order = cb.asc(entityRoot.get(entityType.getSingularAttribute(KapuaEntityAttributes.ENTITY_ID)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ public class EventStoreQueryImplTest extends Assert {
@Test
public void eventStoreQueryImplTest1() {
EventStoreQueryImpl eventStoreQueryImpl = new EventStoreQueryImpl();
assertNotNull("Null not expected.", eventStoreQueryImpl.getSortCriteria());
assertNull("query.sortCriteria", eventStoreQueryImpl.getSortCriteria());
assertNotNull("query.defaultSortCriteria", eventStoreQueryImpl.getDefaultSortCriteria());
}

@Test
public void eventStoreQueryImplTest2() {
KapuaId scopeId = new KapuaIdStatic(BigInteger.ONE);

EventStoreQueryImpl eventStoreQueryImpl1 = new EventStoreQueryImpl(null);
assertNotNull("Null not expected.", eventStoreQueryImpl1.getSortCriteria());
assertNull("query.sortCriteria", eventStoreQueryImpl1.getSortCriteria());
assertNotNull("query.defaultSortCriteria", eventStoreQueryImpl1.getDefaultSortCriteria());
assertNull("Null expected.", eventStoreQueryImpl1.getScopeId());

EventStoreQueryImpl eventStoreQueryImpl2 = new EventStoreQueryImpl(scopeId);
assertNotNull("Null not expected.", eventStoreQueryImpl2.getSortCriteria());
assertNull("query.sortCriteria", eventStoreQueryImpl2.getSortCriteria());
assertNotNull("query.defaultSortCriteria", eventStoreQueryImpl2.getDefaultSortCriteria());
assertEquals("Expected and actual values should be the same.", scopeId, eventStoreQueryImpl2.getScopeId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import javax.xml.bind.annotation.XmlType;

/**
* {@link QueuedJobExecutionQuery} definition.
* {@link QueuedJobExecution} {@link KapuaQuery} definition.
*
* @see KapuaQuery
* @since 1.1.0
*/
@XmlRootElement(name = "query")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
import org.eclipse.kapua.model.id.KapuaId;

/**
* {@link QueuedJobExecutionQuery} implementation
* {@link QueuedJobExecutionQuery} implementation.
*
* @since 1.1.0
*/
public class QueuedJobExecutionQueryImpl extends AbstractKapuaQuery implements QueuedJobExecutionQuery {

/**
* Constructor
* Constructor.
*
* @param scopeId
* @param scopeId The {@link #getScopeId()}.
* @since 1.1.0
*/
public QueuedJobExecutionQueryImpl(KapuaId scopeId) {
super(scopeId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
*******************************************************************************/
package org.eclipse.kapua.service.account;

import org.eclipse.kapua.model.query.KapuaQuery;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.kapua.model.query.KapuaQuery;

/**
* Account query definition.
*
* @since 1.0
* {@link Account} {@link KapuaQuery} definition.
*
* @see KapuaQuery
* @since 1.0.0
*/
@XmlRootElement(name = "query")
@XmlAccessorType(XmlAccessType.PROPERTY)
Expand Down
Loading