use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class CriteriaBuilderSearchVisitor method visit.
@Override
public void visit(SearchCondition<Q> sc) {
PrimitiveStatement statement = sc.getStatement();
if (statement != null) {
if (statement.getProperty() != null) {
String name = getRealPropertyName(statement.getProperty());
// TODO: Figure out how to use validators at some point
// validatePropertyValue(name, originalValue);
// Introspect the property type
ClassValue clsValue = getPrimitiveFieldClass(statement, name, statement.getValue().getClass(), statement.getValueType(), statement.getValue());
// If the property value is a String
boolean isWildcard = false;
if (String.class.equals(clsValue.getCls())) {
// And if it's a FIQL wildcard
if (SearchUtils.containsWildcard((String) clsValue.getValue())) {
// Then mark it as a wildcard and replace the * wildcards with % wildcards
isWildcard = true;
clsValue.setValue(SearchUtils.toSqlWildcardString((String) clsValue.getValue(), false));
}
}
final Object value;
// Check to see if we have any criteria behaviors for this search term
if (m_criteriaBehaviors != null && m_criteriaBehaviors.containsKey(name)) {
// TODO: Change CriteriaBehaviors so that they can remap prefixes
// so that we don't have to put every joined property into m_criteriaMapping
CriteriaBehavior<?> behavior = m_criteriaBehaviors.get(name);
// Convert the query bean property name to the Criteria property name
// if necessary
name = behavior.getPropertyName() == null ? name : behavior.getPropertyName();
// If we're using CriteriaBehaviors, assume that the value is a String
// and convert it to the value that will be used in the Criteria
value = NULL_VALUE.equals((String) clsValue.getValue()) ? null : behavior.convert((String) clsValue.getValue());
// Execute any beforeVisit() actions for this query term such as adding
// additional JOIN aliases
behavior.beforeVisit(m_criteriaBuilder, value, sc.getConditionType(), isWildcard);
// If the behavior indicates that we should skip this search term, then return
if (behavior.shouldSkipProperty(sc.getConditionType(), isWildcard)) {
return;
}
} else {
value = clsValue.getValue();
}
switch(sc.getConditionType()) {
case EQUALS:
if (isWildcard) {
m_criteriaBuilder.like(name, value);
} else {
if (value == null || NULL_VALUE.equals(value) || NULL_DATE_VALUE.equals(value)) {
m_criteriaBuilder.isNull(name);
} else {
m_criteriaBuilder.eq(name, value);
}
}
break;
case NOT_EQUALS:
if (isWildcard) {
m_criteriaBuilder.not().like(name, value);
} else {
if (value == null || NULL_VALUE.equals(value) || NULL_DATE_VALUE.equals(value)) {
m_criteriaBuilder.isNotNull(name);
} else {
// Match any rows that do not match the value or are null
m_criteriaBuilder.or(Restrictions.ne(name, value), Restrictions.isNull(name));
}
}
break;
case LESS_THAN:
// TODO: Check for null?
m_criteriaBuilder.lt(name, value);
break;
case GREATER_THAN:
// TODO: Check for null?
m_criteriaBuilder.gt(name, value);
break;
case LESS_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.le(name, value);
break;
case GREATER_OR_EQUALS:
// TODO: Check for null?
m_criteriaBuilder.ge(name, value);
break;
case OR:
case AND:
case CUSTOM:
default:
}
}
} else {
List<Restriction> subRestrictions = new ArrayList<>();
for (SearchCondition<Q> condition : sc.getSearchConditions()) {
// Create a new CriteriaBuilder
CriteriaBuilder builder = null;
try {
// Try to use the same class as the outside CriteriaBuilder
builder = m_criteriaBuilder.getClass().getConstructor(Class.class).newInstance(m_class);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
LOG.warn("Could not create " + m_criteriaBuilder.getClass().getSimpleName() + "; falling back to CriteriaBuilder: " + e.getClass().getSimpleName() + ": " + e.getMessage());
builder = new CriteriaBuilder(m_class);
}
// Create a new visitor for the SearchCondition
CriteriaBuilderSearchVisitor<T, Q> newVisitor = new CriteriaBuilderSearchVisitor<T, Q>(builder, m_class, m_criteriaBehaviors);
// Visit the children
condition.accept(newVisitor);
Criteria newCriteria = newVisitor.getQuery().toCriteria();
// Add any aliases from the subcriteria
Collection<Alias> aliases = newCriteria.getAliases();
if (aliases != null) {
for (Alias alias : aliases) {
m_criteriaBuilder.alias(alias);
}
}
// Fetch the rendered restrictions
Collection<Restriction> restrictions = newCriteria.getRestrictions();
// If there are restrictions...
if (restrictions != null && restrictions.size() > 0) {
final Restriction subRestriction;
// If there are multiple restrictions...
if (restrictions.size() > 1) {
// Wrap them in an AND restriction
subRestriction = Restrictions.all(restrictions);
} else {
subRestriction = restrictions.iterator().next();
}
LOG.info(subRestriction.toString());
subRestrictions.add(subRestriction);
}
}
switch(sc.getConditionType()) {
case OR:
LOG.info("OR criteria");
// .or() with current Criteria
m_criteriaBuilder.or(subRestrictions.toArray(new Restriction[0]));
break;
case AND:
LOG.info("AND criteria");
// .and() with current Criteria
m_criteriaBuilder.and(subRestrictions.toArray(new Restriction[0]));
break;
default:
}
}
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class AlarmRestService method getAlarms.
/**
* <p>
* getAlarms
* </p>
*
* @return a {@link org.opennms.netmgt.model.OnmsAlarmCollection} object.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional
public OnmsAlarmCollection getAlarms(@Context final SecurityContext securityContext, @Context final UriInfo uriInfo) {
SecurityHelper.assertUserReadCredentials(securityContext);
final CriteriaBuilder builder = getCriteriaBuilder(uriInfo.getQueryParameters(), false);
builder.distinct();
final OnmsAlarmCollection coll = new OnmsAlarmCollection(m_alarmDao.findMatching(builder.toCriteria()));
// For getting totalCount
coll.setTotalCount(m_alarmDao.countMatching(builder.count().toCriteria()));
return coll;
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class AlarmRestService method updateAlarms.
/**
* <p>
* updateAlarms
* </p>
*
* @param formProperties
* a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
*/
@PUT
@Transactional
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateAlarms(@Context final SecurityContext securityContext, final MultivaluedMapImpl formProperties) {
writeLock();
try {
final String ackValue = formProperties.getFirst("ack");
formProperties.remove("ack");
final String escalateValue = formProperties.getFirst("escalate");
formProperties.remove("escalate");
final String clearValue = formProperties.getFirst("clear");
formProperties.remove("clear");
final CriteriaBuilder builder = getCriteriaBuilder(formProperties, false);
builder.distinct();
builder.limit(0);
builder.offset(0);
final String ackUser = formProperties.containsKey("ackUser") ? formProperties.getFirst("ackUser") : securityContext.getUserPrincipal().getName();
formProperties.remove("ackUser");
SecurityHelper.assertUserEditCredentials(securityContext, ackUser);
final List<OnmsAlarm> alarms = m_alarmDao.findMatching(builder.toCriteria());
for (final OnmsAlarm alarm : alarms) {
final OnmsAcknowledgment acknowledgement = new OnmsAcknowledgment(alarm, ackUser);
acknowledgement.setAckAction(AckAction.UNSPECIFIED);
if (ackValue != null) {
if (Boolean.parseBoolean(ackValue)) {
acknowledgement.setAckAction(AckAction.ACKNOWLEDGE);
} else {
acknowledgement.setAckAction(AckAction.UNACKNOWLEDGE);
}
} else if (escalateValue != null) {
if (Boolean.parseBoolean(escalateValue)) {
acknowledgement.setAckAction(AckAction.ESCALATE);
}
} else if (clearValue != null) {
if (Boolean.parseBoolean(clearValue)) {
acknowledgement.setAckAction(AckAction.CLEAR);
}
} else {
throw getException(Status.BAD_REQUEST, "Must supply one of the 'ack', 'escalate', or 'clear' parameters, set to either 'true' or 'false'.");
}
m_ackDao.processAck(acknowledgement);
}
return alarms == null || alarms.isEmpty() ? Response.notModified().build() : Response.noContent().build();
} finally {
writeUnlock();
}
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class EventRestService method getEventsBetween.
/**
* Returns all the events which match the filter/query in the query
* parameters
*
* @return Collection of OnmsEventCollection (ready to be XML-ified)
* @throws java.text.ParseException
* if any.
*/
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Path("between")
@Transactional
public OnmsEventCollection getEventsBetween(@Context final UriInfo uriInfo) throws ParseException {
final MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
final String column;
if (params.containsKey("column")) {
column = params.getFirst("column");
params.remove("column");
} else {
column = "eventTime";
}
Date begin;
if (params.containsKey("begin")) {
try {
begin = ISO8601_FORMATTER.parseLocalDateTime(params.getFirst("begin")).toDate();
} catch (final Throwable t1) {
try {
begin = ISO8601_FORMATTER_MILLIS.parseDateTime(params.getFirst("begin")).toDate();
} catch (final Throwable t2) {
throw getException(Status.BAD_REQUEST, "Can't parse start date");
}
}
params.remove("begin");
} else {
begin = new Date(0);
}
Date end;
if (params.containsKey("end")) {
try {
end = ISO8601_FORMATTER.parseLocalDateTime(params.getFirst("end")).toDate();
} catch (final Throwable t1) {
try {
end = ISO8601_FORMATTER_MILLIS.parseLocalDateTime(params.getFirst("end")).toDate();
} catch (final Throwable t2) {
throw getException(Status.BAD_REQUEST, "Can't parse end date");
}
}
params.remove("end");
} else {
end = new Date();
}
final CriteriaBuilder builder = getCriteriaBuilder(params);
builder.match("all");
try {
builder.between(column, begin, end);
} catch (final Throwable t) {
throw getException(Status.BAD_REQUEST, "Unable to parse " + begin + " and " + end + " as dates!");
}
final OnmsEventCollection coll = new OnmsEventCollection(m_eventDao.findMatching(builder.toCriteria()));
coll.setTotalCount(m_eventDao.countMatching(builder.count().toCriteria()));
return coll;
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class NotificationRestService method updateNotifications.
/**
* <p>updateNotifications</p>
*
* @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
*/
@PUT
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateNotifications(@Context final SecurityContext securityContext, final MultivaluedMapImpl params) {
writeLock();
try {
Boolean ack = false;
if (params.containsKey("ack")) {
ack = "true".equals(params.getFirst("ack"));
params.remove("ack");
}
final CriteriaBuilder builder = getCriteriaBuilder(params);
for (final OnmsNotification notif : m_notifDao.findMatching(builder.toCriteria())) {
processNotifAck(securityContext, notif, ack);
}
return Response.noContent().build();
} finally {
writeUnlock();
}
}
Aggregations