mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Calendar and Blogs canned query refactoring of common property comparisons
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29437 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -19,7 +19,6 @@
|
||||
package org.alfresco.repo.blog.cannedqueries;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.BlogIntegrationModel;
|
||||
@@ -55,57 +54,32 @@ public abstract class AbstractBlogPostsCannedQueryFactory extends AbstractQNameA
|
||||
* Note that it is the responsibility of the calling code to ensure that the specified
|
||||
* property values actually implement Comparable themselves.
|
||||
*/
|
||||
protected static class PropertyBasedComparator implements Comparator<BlogEntity>
|
||||
protected static class BlogEntityComparator extends PropertyBasedComparator<BlogEntity>
|
||||
{
|
||||
private QName comparableProperty;
|
||||
|
||||
public PropertyBasedComparator(QName comparableProperty)
|
||||
public BlogEntityComparator(QName comparableProperty)
|
||||
{
|
||||
this.comparableProperty = comparableProperty;
|
||||
super(comparableProperty);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public int compare(BlogEntity nr1, BlogEntity nr2)
|
||||
{
|
||||
Comparable prop1 = null;
|
||||
Comparable prop2 = null;
|
||||
if (comparableProperty.equals(ContentModel.PROP_PUBLISHED))
|
||||
{
|
||||
prop1 = nr1.getPublishedDate();
|
||||
prop2 = nr2.getPublishedDate();
|
||||
}
|
||||
else if (comparableProperty.equals(ContentModel.PROP_CREATED))
|
||||
{
|
||||
prop1 = nr1.getCreatedDate();
|
||||
prop2 = nr2.getCreatedDate();
|
||||
}
|
||||
else if (comparableProperty.equals(BlogIntegrationModel.PROP_POSTED))
|
||||
{
|
||||
prop1 = nr1.getPostedDate();
|
||||
prop2 = nr2.getPostedDate();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Unsupported blog sort property: "+comparableProperty);
|
||||
}
|
||||
|
||||
if (prop1 == null && prop2 == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (prop1 == null && prop2 != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (prop1 != null && prop2 == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return prop1.compareTo(prop2);
|
||||
}
|
||||
protected Comparable getProperty(BlogEntity entity) {
|
||||
if (comparableProperty.equals(ContentModel.PROP_PUBLISHED))
|
||||
{
|
||||
return entity.getPublishedDate();
|
||||
}
|
||||
else if (comparableProperty.equals(ContentModel.PROP_CREATED))
|
||||
{
|
||||
return entity.getCreatedDate();
|
||||
}
|
||||
else if (comparableProperty.equals(BlogIntegrationModel.PROP_POSTED))
|
||||
{
|
||||
return entity.getPostedDate();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Unsupported blog sort property: "+comparableProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -20,13 +20,14 @@ package org.alfresco.repo.blog.cannedqueries;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.query.CannedQuery;
|
||||
import org.alfresco.query.CannedQueryParameters;
|
||||
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
||||
import org.alfresco.repo.blog.cannedqueries.AbstractBlogPostsCannedQueryFactory.PropertyBasedComparator;
|
||||
import org.alfresco.repo.blog.cannedqueries.AbstractBlogPostsCannedQueryFactory.BlogEntityComparator;
|
||||
import org.alfresco.repo.domain.query.CannedQueryDAO;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.AbstractCannedQueryPermissions;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityBean;
|
||||
@@ -142,12 +143,13 @@ public class DraftsAndPublishedBlogPostsCannedQuery extends AbstractCannedQueryP
|
||||
for (Pair<? extends Object, SortOrder> sortPair : sortPairs)
|
||||
{
|
||||
QName sortProperty = (QName) sortPair.getFirst();
|
||||
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
||||
Comparator<BlogEntity> comparator = new BlogEntityComparator(sortProperty);
|
||||
|
||||
if (sortPair.getSecond() == SortOrder.DESCENDING)
|
||||
{
|
||||
Collections.sort(filtered, Collections.reverseOrder(comparator));
|
||||
comparator = Collections.reverseOrder(comparator);
|
||||
}
|
||||
Collections.sort(filtered, comparator);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -20,13 +20,14 @@ package org.alfresco.repo.blog.cannedqueries;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.query.CannedQuery;
|
||||
import org.alfresco.query.CannedQueryParameters;
|
||||
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
||||
import org.alfresco.repo.blog.cannedqueries.AbstractBlogPostsCannedQueryFactory.PropertyBasedComparator;
|
||||
import org.alfresco.repo.blog.cannedqueries.AbstractBlogPostsCannedQueryFactory.BlogEntityComparator;
|
||||
import org.alfresco.repo.domain.node.AuditablePropertiesEntity;
|
||||
import org.alfresco.repo.domain.query.CannedQueryDAO;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.AbstractCannedQueryPermissions;
|
||||
@@ -140,12 +141,13 @@ public class GetBlogPostsCannedQuery extends AbstractCannedQueryPermissions<Blog
|
||||
Pair<? extends Object, SortOrder> sortPair = sortPairs.get(0);
|
||||
|
||||
QName sortProperty = (QName) sortPair.getFirst();
|
||||
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
||||
Comparator<BlogEntity> comparator = new BlogEntityComparator(sortProperty);
|
||||
|
||||
if (sortPair.getSecond() == SortOrder.DESCENDING)
|
||||
{
|
||||
Collections.sort(filtered, Collections.reverseOrder(comparator));
|
||||
comparator = Collections.reverseOrder(comparator);
|
||||
}
|
||||
Collections.sort(filtered, comparator);
|
||||
}
|
||||
|
||||
List<BlogPostInfo> blogPostInfos = new ArrayList<BlogPostInfo>(filtered.size());
|
||||
|
@@ -30,6 +30,8 @@ import org.alfresco.query.CannedQueryParameters;
|
||||
import org.alfresco.query.CannedQuerySortDetails.SortOrder;
|
||||
import org.alfresco.repo.calendar.CalendarModel;
|
||||
import org.alfresco.repo.domain.query.CannedQueryDAO;
|
||||
import org.alfresco.repo.query.AbstractQNameAwareCannedQueryFactory.NestedComparator;
|
||||
import org.alfresco.repo.query.AbstractQNameAwareCannedQueryFactory.PropertyBasedComparator;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.AbstractCannedQueryPermissions;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityBean;
|
||||
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||
@@ -198,11 +200,11 @@ public class GetCalendarEntriesCannedQuery extends AbstractCannedQueryPermission
|
||||
new ArrayList<Pair<Comparator<CalendarEntity>,SortOrder>>();
|
||||
for(Pair<? extends Object, SortOrder> sortPair : sortPairs)
|
||||
{
|
||||
QName sortProperty = (QName) sortPair.getFirst();
|
||||
final PropertyBasedComparator comparator = new PropertyBasedComparator(sortProperty);
|
||||
final QName sortProperty = (QName)sortPair.getFirst();
|
||||
final CalendarEntityComparator comparator = new CalendarEntityComparator(sortProperty);
|
||||
comparators.add(new Pair<Comparator<CalendarEntity>, SortOrder>(comparator, sortPair.getSecond()));
|
||||
}
|
||||
NestedComparator comparator = new NestedComparator(comparators);
|
||||
NestedComparator<CalendarEntity> comparator = new NestedComparator<CalendarEntity>(comparators);
|
||||
|
||||
// Sort
|
||||
Collections.sort(filtered, comparator);
|
||||
@@ -264,93 +266,32 @@ public class GetCalendarEntriesCannedQuery extends AbstractCannedQueryPermission
|
||||
* Note that it is the responsibility of the calling code to ensure that the specified
|
||||
* property values actually implement Comparable themselves.
|
||||
*/
|
||||
protected static class PropertyBasedComparator implements Comparator<CalendarEntity>
|
||||
protected static class CalendarEntityComparator extends PropertyBasedComparator<CalendarEntity>
|
||||
{
|
||||
private QName comparableProperty;
|
||||
|
||||
public PropertyBasedComparator(QName comparableProperty)
|
||||
protected CalendarEntityComparator(QName property)
|
||||
{
|
||||
this.comparableProperty = comparableProperty;
|
||||
super(property);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public int compare(CalendarEntity nr1, CalendarEntity nr2)
|
||||
{
|
||||
Comparable prop1 = null;
|
||||
Comparable prop2 = null;
|
||||
protected Comparable getProperty(CalendarEntity entity) {
|
||||
if (comparableProperty.equals(CalendarModel.PROP_FROM_DATE))
|
||||
{
|
||||
prop1 = nr1.getFromDate();
|
||||
prop2 = nr2.getFromDate();
|
||||
return entity.getFromDate();
|
||||
}
|
||||
else if (comparableProperty.equals(CalendarModel.PROP_TO_DATE))
|
||||
{
|
||||
prop1 = nr1.getToDate();
|
||||
prop2 = nr2.getToDate();
|
||||
return entity.getToDate();
|
||||
}
|
||||
else if (comparableProperty.equals(ContentModel.PROP_CREATED))
|
||||
{
|
||||
prop1 = nr1.getCreatedDate();
|
||||
prop2 = nr2.getCreatedDate();
|
||||
return entity.getCreatedDate();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Unsupported calendar sort property: "+comparableProperty);
|
||||
}
|
||||
|
||||
if (prop1 == null && prop2 == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (prop1 == null && prop2 != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (prop1 != null && prop2 == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return prop1.compareTo(prop2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static class NestedComparator implements Comparator<CalendarEntity>
|
||||
{
|
||||
private List<Pair<Comparator<CalendarEntity>, SortOrder>> comparators;
|
||||
|
||||
private NestedComparator(List<Pair<Comparator<CalendarEntity>, SortOrder>> comparators)
|
||||
{
|
||||
this.comparators = comparators;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(CalendarEntity entry1, CalendarEntity entry2) {
|
||||
for(Pair<Comparator<CalendarEntity>, SortOrder> pc : comparators)
|
||||
{
|
||||
int result = pc.getFirst().compare(entry1, entry2);
|
||||
if(result != 0)
|
||||
{
|
||||
// Sorts differ, return
|
||||
if(pc.getSecond() == SortOrder.ASCENDING)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0 - result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Sorts are the same, try the next along
|
||||
}
|
||||
}
|
||||
// No difference on any
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,7 @@
|
||||
package org.alfresco.repo.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.query.AbstractCannedQueryFactory;
|
||||
@@ -31,6 +32,7 @@ import org.alfresco.repo.domain.qname.QNameDAO;
|
||||
import org.alfresco.repo.domain.query.CannedQueryDAO;
|
||||
import org.alfresco.repo.security.permissions.impl.acegi.MethodSecurityBean;
|
||||
import org.alfresco.repo.tenant.TenantService;
|
||||
import org.alfresco.service.cmr.calendar.CalendarEntry;
|
||||
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
@@ -152,4 +154,87 @@ public abstract class AbstractQNameAwareCannedQueryFactory<R> extends AbstractCa
|
||||
}
|
||||
return nodePair.getFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility class to sort Entities on the basis of a Comparable property.
|
||||
* Comparisons of two null properties are considered 'equal' by this comparator.
|
||||
* Comparisons involving one null and one non-null property will return the null property as
|
||||
* being 'before' the non-null property.
|
||||
*
|
||||
* Note that it is the responsibility of the calling code to ensure that the specified
|
||||
* property values actually implement Comparable themselves.
|
||||
*/
|
||||
public static abstract class PropertyBasedComparator<R> implements Comparator<R>
|
||||
{
|
||||
protected QName comparableProperty;
|
||||
|
||||
public PropertyBasedComparator(QName comparableProperty)
|
||||
{
|
||||
this.comparableProperty = comparableProperty;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected abstract Comparable getProperty(R entity);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public int compare(R r1, R r2)
|
||||
{
|
||||
Comparable prop1 = getProperty(r1);
|
||||
Comparable prop2 = getProperty(r2);
|
||||
|
||||
if (prop1 == null && prop2 == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (prop1 == null && prop2 != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (prop1 != null && prop2 == null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return prop1.compareTo(prop2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class NestedComparator<R> implements Comparator<R>
|
||||
{
|
||||
private List<Pair<Comparator<R>, SortOrder>> comparators;
|
||||
|
||||
public NestedComparator(List<Pair<Comparator<R>, SortOrder>> comparators)
|
||||
{
|
||||
this.comparators = comparators;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(R entry1, R entry2) {
|
||||
for(Pair<Comparator<R>, SortOrder> pc : comparators)
|
||||
{
|
||||
int result = pc.getFirst().compare(entry1, entry2);
|
||||
if(result != 0)
|
||||
{
|
||||
// Sorts differ, return
|
||||
if(pc.getSecond() == SortOrder.ASCENDING)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0 - result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Sorts are the same, try the next along
|
||||
}
|
||||
}
|
||||
// No difference on any
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user