Search in sources :

Example 6 with Property

use of org.motechproject.mds.query.Property in project motech by motech.

the class AbstractSearchExecution method execute.

@Override
public T execute(Query query, InstanceSecurityRestriction restriction) {
    List<Property> properties = new ArrayList<>();
    Range<DateTime> deliveryTimeRange = criteria.getDeliveryTimeRange();
    boolean dateRangeUsed = isUsableRange(deliveryTimeRange);
    if (dateRangeUsed) {
        properties.add(new RangeProperty<>("deliveryTime", deliveryTimeRange, DateTime.class.getName()));
    }
    Set<DeliveryStatus> deliveryStatuses = criteria.getDeliveryStatuses();
    if (deliveryStatuses.isEmpty()) {
        deliveryStatuses = new HashSet<>(Arrays.asList(DeliveryStatus.values()));
    }
    properties.add(new SetProperty<>("deliveryStatus", deliveryStatuses, DeliveryStatus.class.getName()));
    StringBuilder queryBuilder = new StringBuilder(initialQuery(dateRangeUsed));
    int initialLength = queryBuilder.length();
    if (StringUtils.isNotEmpty(criteria.getToAddress())) {
        properties.add(new MatchesProperty("toAddress", criteria.getToAddress()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getFromAddress())) {
        properties.add(new MatchesProperty("fromAddress", criteria.getFromAddress()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getMessage())) {
        properties.add(new MatchesProperty("message", criteria.getMessage()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getSubject())) {
        properties.add(new MatchesProperty("subject", criteria.getSubject()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    closeQuery(queryBuilder, initialLength);
    if (restriction != null && !restriction.isEmpty()) {
        properties.add(new RestrictionProperty(restriction, SecurityUtil.getUsername()));
        queryBuilder.append(" && %s");
    }
    QueryUtil.useFilterFromPattern(query, queryBuilder.toString(), properties);
    return execute(query, properties);
}
Also used : ArrayList(java.util.ArrayList) MatchesProperty(org.motechproject.mds.query.MatchesProperty) DateTime(org.joda.time.DateTime) RestrictionProperty(org.motechproject.mds.query.RestrictionProperty) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) SetProperty(org.motechproject.mds.query.SetProperty) Property(org.motechproject.mds.query.Property) MatchesProperty(org.motechproject.mds.query.MatchesProperty) RestrictionProperty(org.motechproject.mds.query.RestrictionProperty) RangeProperty(org.motechproject.mds.query.RangeProperty)

Aggregations

ArrayList (java.util.ArrayList)6 Property (org.motechproject.mds.query.Property)6 Query (javax.jdo.Query)5 PersistenceManager (javax.jdo.PersistenceManager)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Collection (java.util.Collection)1 DateTime (org.joda.time.DateTime)1 DeliveryStatus (org.motechproject.email.domain.DeliveryStatus)1 MatchesProperty (org.motechproject.mds.query.MatchesProperty)1 RangeProperty (org.motechproject.mds.query.RangeProperty)1 RestrictionProperty (org.motechproject.mds.query.RestrictionProperty)1 SetProperty (org.motechproject.mds.query.SetProperty)1