Search in sources :

Example 1 with LongCollection

use of org.opennms.web.rest.support.LongCollection in project opennms by OpenNMS.

the class AbstractDaoRestServiceWithDTO method getPropertyValues.

@GET
@Path("properties/{propertyId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getPropertyValues(@PathParam("propertyId") final String propertyId, @QueryParam("q") final String query, @QueryParam("limit") final Integer limit) {
    Set<SearchProperty> props = getQueryProperties();
    // Find the property with the matching ID
    Optional<SearchProperty> prop = props.stream().filter(p -> p.getId().equals(propertyId)).findAny();
    if (prop.isPresent()) {
        SearchProperty property = prop.get();
        if (property.values != null && property.values.size() > 0) {
            final Set<String> validValues;
            if (query != null && query.length() > 0) {
                validValues = property.values.keySet().stream().filter(v -> v.contains(query)).collect(Collectors.toSet());
            } else {
                validValues = property.values.keySet();
            }
            switch(property.type) {
                case FLOAT:
                    return Response.ok(new FloatCollection(validValues.stream().map(Float::parseFloat).collect(Collectors.toList()))).build();
                case INTEGER:
                    return Response.ok(new IntegerCollection(validValues.stream().map(Integer::parseInt).collect(Collectors.toList()))).build();
                case LONG:
                    return Response.ok(new LongCollection(validValues.stream().map(Long::parseLong).collect(Collectors.toList()))).build();
                case IP_ADDRESS:
                case STRING:
                    return Response.ok(new StringCollection(validValues)).build();
                case TIMESTAMP:
                    return Response.ok(new DateCollection(validValues.stream().map(v -> {
                        try {
                            return ISO8601DateEditor.stringToDate(v);
                        } catch (IllegalArgumentException | UnsupportedOperationException e) {
                            LOG.error("Invalid date in value list: " + v, e);
                            return null;
                        }
                    }).filter(Objects::nonNull).collect(Collectors.toList()))).build();
                default:
                    return Response.noContent().build();
            }
        }
        switch(property.type) {
            case FLOAT:
                List<Float> floats = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Float>(property, query, limit));
                return Response.ok(new FloatCollection(floats)).build();
            case INTEGER:
                List<Integer> ints = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Integer>(property, query, limit));
                return Response.ok(new IntegerCollection(ints)).build();
            case LONG:
                List<Long> longs = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Long>(property, query, limit));
                return Response.ok(new LongCollection(longs)).build();
            case IP_ADDRESS:
                List<InetAddress> addresses = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<InetAddress>(property, query, limit));
                return Response.ok(new StringCollection(addresses.stream().map(InetAddressUtils::str).collect(Collectors.toList()))).build();
            case STRING:
                List<String> strings = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<String>(property, query, limit));
                return Response.ok(new StringCollection(strings)).build();
            case TIMESTAMP:
                List<Date> dates = new HibernateTemplate(m_sessionFactory).execute(new HibernateListCallback<Date>(property, query, limit));
                return Response.ok(new DateCollection(dates)).build();
            default:
                return Response.noContent().build();
        }
    } else {
        // 404
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : QueryParameters(org.opennms.web.utils.QueryParameters) Produces(javax.ws.rs.Produces) Date(java.util.Date) SearchPropertyCollection(org.opennms.web.rest.support.SearchPropertyCollection) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CriteriaBuilderSearchVisitor(org.opennms.web.rest.support.CriteriaBuilderSearchVisitor) JaxbListWrapper(org.opennms.core.config.api.JaxbListWrapper) SearchConditionVisitor(org.apache.cxf.jaxrs.ext.search.SearchConditionVisitor) InetAddress(java.net.InetAddress) ReentrantReadWriteUpdateLock(com.googlecode.concurentlocks.ReentrantReadWriteUpdateLock) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Event(org.opennms.netmgt.xml.event.Event) Map(java.util.Map) ReadWriteUpdateLock(com.googlecode.concurentlocks.ReadWriteUpdateLock) Query(org.hibernate.Query) FloatCollection(org.opennms.web.rest.support.FloatCollection) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) QueryParametersBuilder(org.opennms.web.utils.QueryParametersBuilder) DELETE(javax.ws.rs.DELETE) PropertyNotFoundException(org.apache.cxf.jaxrs.ext.search.PropertyNotFoundException) MessageFormatter(org.slf4j.helpers.MessageFormatter) MultivaluedMapImpl(org.opennms.web.rest.support.MultivaluedMapImpl) Context(javax.ws.rs.core.Context) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) Collection(java.util.Collection) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) Criteria(org.opennms.core.criteria.Criteria) Serializable(java.io.Serializable) CriteriaBehavior(org.opennms.web.rest.support.CriteriaBehavior) Objects(java.util.Objects) List(java.util.List) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) HibernateException(org.hibernate.HibernateException) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) SearchContext(org.apache.cxf.jaxrs.ext.search.SearchContext) Session(org.hibernate.Session) OnmsDao(org.opennms.netmgt.dao.api.OnmsDao) ISO8601DateEditor(org.opennms.web.api.ISO8601DateEditor) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) IntegerCollection(org.opennms.web.rest.support.IntegerCollection) Qualifier(org.springframework.beans.factory.annotation.Qualifier) SearchBean(org.apache.cxf.jaxrs.ext.search.SearchBean) Status(javax.ws.rs.core.Response.Status) Order(org.opennms.core.criteria.Order) SearchProperty(org.opennms.web.rest.support.SearchProperty) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) StringCollection(org.opennms.web.rest.support.StringCollection) RestUtils(org.opennms.web.api.RestUtils) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) LongCollection(org.opennms.web.rest.support.LongCollection) Lock(java.util.concurrent.locks.Lock) PUT(javax.ws.rs.PUT) SearchCondition(org.apache.cxf.jaxrs.ext.search.SearchCondition) Collections(java.util.Collections) EventProxy(org.opennms.netmgt.events.api.EventProxy) DateCollection(org.opennms.web.rest.support.DateCollection) CriteriaBuilderUtils(org.opennms.web.utils.CriteriaBuilderUtils) Transactional(org.springframework.transaction.annotation.Transactional) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) InetAddressUtils(org.opennms.core.utils.InetAddressUtils) LongCollection(org.opennms.web.rest.support.LongCollection) DateCollection(org.opennms.web.rest.support.DateCollection) SearchProperty(org.opennms.web.rest.support.SearchProperty) FloatCollection(org.opennms.web.rest.support.FloatCollection) IntegerCollection(org.opennms.web.rest.support.IntegerCollection) Date(java.util.Date) StringCollection(org.opennms.web.rest.support.StringCollection) Objects(java.util.Objects) InetAddress(java.net.InetAddress) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ReadWriteUpdateLock (com.googlecode.concurentlocks.ReadWriteUpdateLock)1 ReentrantReadWriteUpdateLock (com.googlecode.concurentlocks.ReentrantReadWriteUpdateLock)1 Serializable (java.io.Serializable)1 InetAddress (java.net.InetAddress)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Lock (java.util.concurrent.locks.Lock)1 Collectors (java.util.stream.Collectors)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1