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 */
017 package org.apache.activemq.console.filter;
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.HashMap;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025 import java.util.Set;
026
027 public class PropertiesViewFilter implements QueryFilter {
028 protected QueryFilter next;
029 protected Set viewFilter;
030
031 /**
032 * Creates a filter that will select the properties of a map object to view
033 *
034 * @param next - the next query filter that will return a collection of maps
035 */
036 public PropertiesViewFilter(QueryFilter next) {
037 this(null, next);
038 }
039
040 /**
041 * Creates a filter that will select the properties of a map object to view
042 *
043 * @param viewFilter - the properties to view
044 * @param next - the next query filter that will return a collection of maps
045 */
046 public PropertiesViewFilter(Set viewFilter, QueryFilter next) {
047 this.next = next;
048 this.viewFilter = viewFilter;
049 }
050
051 /**
052 * Filter the properties to view of the query result
053 *
054 * @param query - the query string
055 * @return list of objects that has been view filtered
056 */
057 public List<Map<Object, Object>> query(String query) throws Exception {
058 return filterViewCollection(next.query(query), viewFilter);
059 }
060
061 /**
062 * Filter the properties to view of the query result
063 *
064 * @param queries - the query map
065 * @return list of objects that has been view filtered
066 * @throws Exception
067 */
068 public List<Map<Object, Object>> query(List queries) throws Exception {
069 return filterViewCollection(next.query(queries), viewFilter);
070 }
071
072 /**
073 * Filter the view of each element in the collection
074 *
075 * @param result - the lists to filter the view from
076 * @param viewFilter - the views to select
077 * @return list of objects whose view has been filtered
078 */
079 protected List<Map<Object, Object>> filterViewCollection(Collection<Map<Object, Object>> result, Set viewFilter) {
080 // Use a list to allow duplicate entries
081 List<Map<Object, Object>> newCollection = new ArrayList<Map<Object, Object>>();
082
083 for (Iterator<Map<Object, Object>> i = result.iterator(); i.hasNext();) {
084 newCollection.add(filterView(i.next()));
085 }
086
087 return newCollection;
088 }
089
090 /**
091 * Select only the attributes to view from the map data
092 *
093 * @param data - data to filter the view from
094 * @return - data with the view filtered
095 */
096 protected Map<Object, Object> filterView(Map<Object, Object> data) {
097 // If no view specified, display all attributes
098 if (viewFilter == null || viewFilter.isEmpty()) {
099 return data;
100 }
101
102 Map<Object, Object> newData;
103 try {
104 // Lets try to use the same class as the original
105 newData = data.getClass().newInstance();
106 } catch (Exception e) {
107 // Lets use a default HashMap
108 newData = new HashMap<Object, Object>();
109 }
110
111 // Filter the keys to view
112 for (Iterator i = viewFilter.iterator(); i.hasNext();) {
113 Object key = i.next();
114 Object val = data.get(key);
115
116 if (val != null) {
117 newData.put(key, val);
118 }
119 }
120
121 return newData;
122 }
123
124 }