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.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.Set;
023
024public class GroupPropertiesViewFilter extends PropertiesViewFilter {
025
026    /**
027     * Creates a group properties filter that is able to filter the display
028     * result based on a group prefix
029     * 
030     * @param next - the next query filter
031     */
032    public GroupPropertiesViewFilter(QueryFilter next) {
033        super(next);
034    }
035
036    /**
037     * Creates a group properties filter that is able to filter the display
038     * result based on a group prefix
039     * 
040     * @param groupView - the group filter to use
041     * @param next - the next query filter
042     */
043    public GroupPropertiesViewFilter(Set groupView, QueryFilter next) {
044        super(groupView, next);
045    }
046
047    /**
048     * Filter the properties that matches the group prefix only.
049     * 
050     * @param data - map data to filter
051     * @return - filtered map data
052     */
053    protected Map filterView(Map data) {
054        // If no view specified, display all attributes
055        if (viewFilter == null || viewFilter.isEmpty()) {
056            return data;
057        }
058
059        Map newData;
060        try {
061            // Lets try to use the same class as the original
062            newData = data.getClass().newInstance();
063        } catch (Exception e) {
064            // Lets use a default HashMap
065            newData = new HashMap();
066        }
067
068        // Filter the keys to view
069        for (Iterator<String> i = data.keySet().iterator(); i.hasNext();) {
070            String key = i.next();
071
072            // Checks if key matches any of the group filter
073            for (Iterator j = viewFilter.iterator(); j.hasNext();) {
074                String group = (String)j.next();
075                if (key.startsWith(group)) {
076                    newData.put(key, data.get(key));
077                    break;
078                }
079            }
080        }
081
082        return newData;
083    }
084}