/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.widget.searchandfilter.command.searchandfilter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.geomajas.command.Command;
import org.geomajas.global.ExceptionCode;
import org.geomajas.global.GeomajasException;
import org.geomajas.layer.VectorLayer;
import org.geomajas.layer.VectorLayerService;
import org.geomajas.layer.feature.Feature;
import org.geomajas.layer.feature.InternalFeature;
import org.geomajas.security.SecurityContext;
import org.geomajas.service.DtoConverterService;
import org.geomajas.service.FilterService;
import org.geomajas.service.GeoService;
import org.geomajas.widget.searchandfilter.command.dto.FeatureSearchRequest;
import org.geomajas.widget.searchandfilter.command.dto.FeatureSearchResponse;
import org.geomajas.widget.searchandfilter.service.DtoSearchConverterService;
import org.opengis.filter.Filter;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Command to retrieve features using a FeatureSearch Criterion.
 * <p>
 * criteria are combined to one filter (per layer).
 * <p>
 * Please note that AND does not (automatically) work over layers, eg. there are
 * separate filters, one for every layer (geographic filters work on multiple
 * layers however, so no need to define those multiple times).
 * 
 * @author Kristof Heirwegh
 */
@Component
public class FeatureSearchCommand implements Command<FeatureSearchRequest, FeatureSearchResponse> {

	@SuppressWarnings("unused")
	private final Logger log = LoggerFactory.getLogger(FeatureSearchCommand.class);

	@Autowired
	private GeoService geoService;

	@Autowired
	private VectorLayerService layerService;

	@Autowired
	private DtoConverterService dtoConverterService;

	@Autowired
	private DtoSearchConverterService dtoSearchConverterService;

	@Autowired
	private FilterService filterService;

	@Autowired
	private SecurityContext securityContext;

	public void execute(final FeatureSearchRequest request, final FeatureSearchResponse response) throws Exception {
		if (request.getCriterion() == null) {
			throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "criterion");
		}
		if (null == request.getMapCrs()) {
			throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "mapCrs");
		}
		if (!request.getCriterion().isValid()) {
			throw new GeomajasException(ExceptionCode.UNEXPECTED_PROBLEM, "criterion is not valid");
		}

		String mapCrsCode = request.getMapCrs();
		CoordinateReferenceSystem mapCrs = geoService.getCrs2(request.getMapCrs());

		Map<VectorLayer, Filter> filters = dtoSearchConverterService.dtoCriterionToFilters(request.getCriterion(),
				mapCrs);

		Map<String, String> layerFilters = request.getLayerFilters();

		for (Entry<VectorLayer, Filter> entry : filters.entrySet()) {
			String layerId = entry.getKey().getId();
			if (securityContext.isLayerVisible(layerId)) {
				Filter f = entry.getValue();
				if (layerFilters.containsKey(layerId)) {
					f = filterService.createAndFilter(filterService.parseFilter(layerFilters.get(layerId)), f);
				}

				List<InternalFeature> temp = layerService.getFeatures(layerId, mapCrs, f, null,
						request.getFeatureIncludes(), 0, request.getMax());
				if (temp.size() > 0) {
					List<Feature> features = new ArrayList<Feature>();
					for (InternalFeature feature : temp) {
						Feature dto = dtoConverterService.toDto(feature);
						dto.setCrs(mapCrsCode);
						features.add(dto);
					}
					response.addLayer(layerId, features);
				}
			}
		}
	}

	public FeatureSearchResponse getEmptyCommandResponse() {
		return new FeatureSearchResponse();
	}
}

@@ -3656,24 +3656,78 @@
 ayerId)) %7B%0D%0A
+%09%09%09%09%09String layerFilter = layerFilters.get(layerId);%0D%0A
 %09%09%09%09%09f = fil
@@ -3790,30 +3790,16 @@
 erFilter
-s.get(layerId)
 ), f);%0D%0A

Create filter widget, that allows combined filters on the map