Skip to content

Commit

Permalink
Adds support for <groupBy> in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
davidandrewcope authored and semancik committed Oct 2, 2017
1 parent 1f77cf5 commit e52b4b6
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 21 deletions.
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.prism.query;

import com.evolveum.midpoint.prism.path.ItemPath;

import java.io.Serializable;

/**
* @author acope
*/
public class ObjectGrouping implements Serializable {

final private ItemPath groupBy;

ObjectGrouping(ItemPath groupBy) {
if (ItemPath.isNullOrEmpty(groupBy)) {
throw new IllegalArgumentException("Null or empty groupBy path is not supported.");
}
this.groupBy = groupBy;
}

public static ObjectGrouping createGrouping(ItemPath groupBy) {
return new ObjectGrouping(groupBy);
}

public ItemPath getGroupBy() {
return groupBy;
}


@Override
public String toString() {
return groupBy.toString();
}

@Override
public boolean equals(Object o) {
return equals(o, true);
}

public boolean equals(Object o, boolean exact) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

ObjectGrouping that = (ObjectGrouping) o;

if (groupBy != null ? !groupBy.equals(that.groupBy, exact) : that.groupBy != null)
return false;
return true;
}

@Override
public int hashCode() {
return groupBy.hashCode();
}
}
Expand Up @@ -33,6 +33,8 @@ public class ObjectPaging implements DebugDumpable, Serializable {
private Integer offset;
private Integer maxSize;
@NotNull private final List<ObjectOrdering> ordering = new ArrayList<>();
private List<ObjectGrouping> grouping = new ArrayList<>();

private String cookie;

protected ObjectPaging() {
Expand All @@ -43,15 +45,38 @@ protected ObjectPaging() {
this.maxSize = maxSize;
}

ObjectPaging(Integer offset, Integer maxSize, ItemPath groupBy) {
this.offset = offset;
this.maxSize = maxSize;
setGrouping(groupBy);
}

ObjectPaging(ItemPath orderBy, OrderDirection direction) {
setOrdering(orderBy, direction);
}

ObjectPaging(Integer offset, Integer maxSize, ItemPath orderBy, OrderDirection direction) {
ObjectPaging(ItemPath orderBy, OrderDirection direction, ItemPath groupBy) {
setOrdering(orderBy, direction);
setGrouping(groupBy);
}

ObjectPaging(Integer offset, Integer maxSize, ItemPath orderBy, OrderDirection direction) {
this.offset = offset;
this.maxSize = maxSize;
setOrdering(orderBy, direction);
}

ObjectPaging(Integer offset, Integer maxSize, ItemPath orderBy, OrderDirection direction, ItemPath groupBy) {
this.offset = offset;
this.maxSize = maxSize;
setOrdering(orderBy, direction);

setGrouping(groupBy);
}

ObjectPaging(ItemPath groupBy) {
setGrouping(groupBy);
}

public static ObjectPaging createPaging(Integer offset, Integer maxSize){
return new ObjectPaging(offset, maxSize);
Expand All @@ -64,20 +89,51 @@ public static ObjectPaging createPaging(Integer offset, Integer maxSize, QName o
public static ObjectPaging createPaging(Integer offset, Integer maxSize, ItemPath orderBy, OrderDirection direction) {
return new ObjectPaging(offset, maxSize, orderBy, direction);
}

public static ObjectPaging createPaging(Integer offset, Integer maxSize, List<ObjectOrdering> orderings) {

public static ObjectPaging createPaging(Integer offset, Integer maxSize, ItemPath groupBy) {
return new ObjectPaging(offset, maxSize, groupBy);
}

public static ObjectPaging createPaging(Integer offset, Integer maxSize, ItemPath orderBy, OrderDirection direction, ItemPath groupBy) {
return new ObjectPaging(offset, maxSize, orderBy, direction, groupBy);
}

public static ObjectPaging createPaging(Integer offset, Integer maxSize, List<ObjectOrdering> orderings) {
ObjectPaging paging = new ObjectPaging(offset, maxSize);
paging.setOrdering(orderings);
return paging;
}

public static ObjectPaging createPaging(Integer offset, Integer maxSize, List<ObjectOrdering> orderings, List<ObjectGrouping> groupings) {
ObjectPaging paging = new ObjectPaging(offset, maxSize);
paging.setOrdering(orderings);
paging.setGrouping(groupings);
return paging;
}

public static ObjectPaging createPaging(ItemPath orderBy, OrderDirection direction) {
return new ObjectPaging(orderBy, direction);
}

public static ObjectPaging createPaging(QName orderBy, OrderDirection direction) {
return new ObjectPaging(new ItemPath(orderBy), direction);
}

public static ObjectPaging createPaging(ItemPath orderBy, OrderDirection direction, ItemPath groupBy) {
return new ObjectPaging(orderBy, direction, groupBy);
}

public static ObjectPaging createPaging(QName orderBy, OrderDirection direction, QName groupBy) {
return new ObjectPaging(new ItemPath(orderBy), direction, new ItemPath(groupBy));
}

public static ObjectPaging createPaging(ItemPath groupBy) {
return new ObjectPaging(groupBy);
}

public static ObjectPaging createPaging(QName groupBy) {
return new ObjectPaging(new ItemPath(groupBy));
}

public static ObjectPaging createEmptyPaging(){
return new ObjectPaging();
Expand All @@ -103,11 +159,28 @@ public ObjectOrdering getPrimaryOrdering() {
}
}

public ItemPath getGroupBy(){
ObjectGrouping primary = getPrimaryGrouping();
return primary != null ? primary.getGroupBy() : null;
}

public ObjectGrouping getPrimaryGrouping() {
if (hasGrouping()) {
return grouping.get(0);
} else {
return null;
}
}

// TODO name?
public List<ObjectOrdering> getOrderingInstructions() {
return ordering;
}

public List<ObjectGrouping> getGroupingInstructions() {
return grouping;
}

public boolean hasOrdering() {
return !ordering.isEmpty();
}
Expand All @@ -117,6 +190,15 @@ public void setOrdering(ItemPath orderBy, OrderDirection direction) {
addOrderingInstruction(orderBy, direction);
}

public boolean hasGrouping() {
return !grouping.isEmpty();
}

public void setGrouping(ItemPath groupBy) {
this.grouping.clear();
addGroupingInstruction(groupBy);
}

public void addOrderingInstruction(ItemPath orderBy, OrderDirection direction) {
this.ordering.add(new ObjectOrdering(orderBy, direction));
}
Expand All @@ -140,6 +222,29 @@ public void setOrdering(Collection<ObjectOrdering> orderings) {
}
}


public void addGroupingInstruction(ItemPath groupBy) {
this.grouping.add(new ObjectGrouping(groupBy));
}

public void addGroupingInstruction(QName groupBy) {
addGroupingInstruction(new ItemPath(groupBy));
}

public void setGrouping(ObjectGrouping... groupings) {
this.grouping.clear();
if (groupings != null) {
this.grouping.addAll(Arrays.asList(groupings));
}
}

public void setGrouping(Collection<ObjectGrouping> groupings) {
this.grouping.clear();
if (groupings != null) {
this.grouping.addAll(groupings);
}
}

public Integer getOffset() {
return offset;
}
Expand Down Expand Up @@ -197,6 +302,13 @@ protected void copyTo(ObjectPaging clone) {
clone.maxSize = this.maxSize;
clone.ordering.clear();
clone.ordering.addAll(this.ordering);

if (this.grouping != null) {
clone.grouping = new ArrayList<>(this.grouping);
} else {
clone.grouping = null;
}

clone.cookie = this.cookie;
}

Expand Down Expand Up @@ -224,6 +336,11 @@ public String debugDump(int indent) {
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("Ordering: ").append(ordering);
}
if (hasGrouping()) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("Grouping: ").append(grouping);
}
if (getCookie() != null) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent + 1);
Expand Down Expand Up @@ -251,6 +368,11 @@ public String toString() {
sb.append(ordering);
sb.append(", ");
}
if (hasGrouping()) {
sb.append("GRP: ");
sb.append(grouping);
sb.append(", ");
}
if (getCookie() != null) {
sb.append("C:");
sb.append(getCookie());
Expand Down Expand Up @@ -287,6 +409,16 @@ public boolean equals(Object o, boolean exact) {
return false;
}
}
if (grouping.size() != that.grouping.size()) {
return false;
}
for (int i = 0; i < grouping.size(); i++) {
ObjectGrouping og1 = this.grouping.get(i);
ObjectGrouping og2 = that.grouping.get(i);
if (!og1.equals(og2, exact)) {
return false;
}
}
return cookie != null ? cookie.equals(that.cookie) : that.cookie == null;
}

Expand All @@ -295,6 +427,7 @@ public int hashCode() {
int result = offset != null ? offset.hashCode() : 0;
result = 31 * result + (maxSize != null ? maxSize.hashCode() : 0);
result = 31 * result + ordering.hashCode();
result = 31 * result + (grouping != null ? grouping.hashCode() : 0);
result = 31 * result + (cookie != null ? cookie.hashCode() : 0);
return result;
}
Expand Down
Expand Up @@ -28,10 +28,19 @@ public static ObjectPaging createObjectPaging(PagingType pagingType){
if (pagingType == null) {
return null;
}
if (pagingType.getOrderBy() != null && pagingType.getGroupBy() != null) {
return ObjectPaging.createPaging(pagingType.getOffset(), pagingType.getMaxSize(),
pagingType.getOrderBy().getItemPath(), toOrderDirection(pagingType.getOrderDirection()), pagingType.getGroupBy().getItemPath());
}

if (pagingType.getOrderBy() != null) {
return ObjectPaging.createPaging(pagingType.getOffset(), pagingType.getMaxSize(),
pagingType.getOrderBy().getItemPath(), toOrderDirection(pagingType.getOrderDirection()));
} else {

} if (pagingType.getGroupBy() != null) {
return ObjectPaging.createPaging(pagingType.getGroupBy().getItemPath());

} else {
return ObjectPaging.createPaging(pagingType.getOffset(), pagingType.getMaxSize());
}
}
Expand Down Expand Up @@ -62,7 +71,9 @@ public static PagingType createPagingType(ObjectPaging paging){
if (paging.getOrderBy() != null) {
pagingType.setOrderBy(new ItemPathType(paging.getOrderBy()));
}

if (paging.getGroupBy() != null) {
pagingType.setGroupBy(new ItemPathType(paging.getGroupBy()));
}
return pagingType;
}

Expand Down
Expand Up @@ -345,6 +345,16 @@ public S_FilterExit desc(ItemPath path) {
return finish().desc(path);
}

@Override
public S_FilterExit group(QName... names) {
return finish().group(names);
}

@Override
public S_FilterExit group(ItemPath path) {
return finish().group(path);
}

@Override
public S_FilterExit offset(Integer n) {
return finish().offset(n);
Expand Down

0 comments on commit e52b4b6

Please sign in to comment.