001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.console.filter;
018
019import java.util.*;
020
021public class PropertiesViewFilter implements QueryFilter {
022    protected QueryFilter next;
023    protected Set viewFilter;
024
025    /**
026     * Creates a filter that will select the properties of a map object to view
027     * 
028     * @param next - the next query filter that will return a collection of maps
029     */
030    public PropertiesViewFilter(QueryFilter next) {
031        this(null, next);
032    }
033
034    /**
035     * Creates a filter that will select the properties of a map object to view
036     * 
037     * @param viewFilter - the properties to view
038     * @param next - the next query filter that will return a collection of maps
039     */
040    public PropertiesViewFilter(Set viewFilter, QueryFilter next) {
041        this.next = next;
042        this.viewFilter = viewFilter;
043    }
044
045    /**
046     * Filter the properties to view of the query result
047     * 
048     * @param query - the query string
049     * @return list of objects that has been view filtered
050     */
051    public List<Map<Object, Object>> query(String query) throws Exception {
052        return filterViewCollection(next.query(query), viewFilter);
053    }
054
055    /**
056     * Filter the properties to view of the query result
057     * 
058     * @param queries - the query map
059     * @return list of objects that has been view filtered
060     * @throws Exception
061     */
062    public List<Map<Object, Object>> query(List queries) throws Exception {
063        return filterViewCollection(next.query(queries), viewFilter);
064    }
065
066    /**
067     * Filter the view of each element in the collection
068     * 
069     * @param result - the lists to filter the view from
070     * @param viewFilter - the views to select
071     * @return list of objects whose view has been filtered
072     */
073    protected List<Map<Object, Object>> filterViewCollection(Collection<Map<Object, Object>> result, Set viewFilter) {
074        // Use a list to allow duplicate entries
075        List<Map<Object, Object>> newCollection = new ArrayList<Map<Object, Object>>();
076
077        for (Iterator<Map<Object, Object>> i = result.iterator(); i.hasNext();) {
078            newCollection.add(filterView(i.next()));
079        }
080
081        return newCollection;
082    }
083
084    /**
085     * Select only the attributes to view from the map data
086     * 
087     * @param data - data to filter the view from
088     * @return - data with the view filtered
089     */
090    protected Map<Object, Object> filterView(Map<Object, Object> data) {
091        // If no view specified, display all attributes
092        if (viewFilter == null || viewFilter.isEmpty()) {
093            return data;
094        }
095
096        Map<Object, Object> newData;
097        try {
098            // Lets try to use the same class as the original
099            newData = new LinkedHashMap(data.getClass().newInstance());
100        } catch (Exception e) {
101            // Lets use a default HashMap
102            newData = new LinkedHashMap<Object, Object>();
103        }
104
105        // Filter the keys to view
106        for (Iterator i = viewFilter.iterator(); i.hasNext();) {
107            Object key = i.next();
108            Object val = data.get(key);
109
110            if (val != null) {
111                newData.put(key, val);
112            }
113        }
114
115        return newData;
116    }
117
118}