<commit_before>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 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.plugin.deskmanager.service.common;

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

import javax.annotation.Resource;

import org.geomajas.configuration.NamedStyleInfo;
import org.geomajas.configuration.Parameter;
import org.geomajas.configuration.VectorLayerInfo;
import org.geomajas.configuration.client.ClientApplicationInfo;
import org.geomajas.plugin.deskmanager.domain.LayerModel;
import org.geomajas.plugin.deskmanager.domain.dto.DynamicLayerConfiguration;
import org.geomajas.plugin.deskmanager.service.manager.DiscoveryService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionDtoConverterService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionDtoConverterService.NamedObject;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionWriterService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionWriterServiceImpl;
import org.geomajas.plugin.runtimeconfig.service.BeanFactoryService;
import org.geomajas.plugin.runtimeconfig.service.ContextConfiguratorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */
@Component
@Transactional(readOnly = true)
public class DynamicLayerLoadServiceImpl implements DynamicLayerLoadService {

	private final Logger log = LoggerFactory.getLogger(DynamicLayerLoadServiceImpl.class);

	@Autowired
	private LayerModelService layerModelService;

	@Autowired
	private DiscoveryService discoService;

	@Autowired
	private ContextConfiguratorService configService;

	@Autowired
	private BeanDefinitionDtoConverterService converterService;

	@Autowired
	private BeanFactoryService beanFactoryService;

	@Resource(name = "postGisDatastoreParams")
	private Map<String, String> postgisDataStoreParams;

	@Resource(name = "dynamicLayersApplication")
	private ClientApplicationInfo applicationInfo;

	@Autowired
	private BeanDefinitionWriterService bser;

	// -------------------------------------------------

	public void loadDynamicLayers() {
		log.info("Loading dynamic layers");
		try {

			// -- clientside / namedstyleinfo --
			List<BeanDefinitionHolder> holders = new ArrayList<BeanDefinitionHolder>();
			List<NamedObject> objects = new ArrayList<NamedObject>();
			List<String> clientLayerIds = new ArrayList<String>();
			for (LayerModel lm : layerModelService.getDynamicLayerModelsInternal()) {
				log.info(" - creating a dynamic layer for: " + lm.getName());
				clientLayerIds.add(lm.getClientLayerId());

				updateLayerProperties(lm.getDynamicLayerConfiguration());
				objects.addAll(getClientLayerInfoObject(lm.getDynamicLayerConfiguration()));

				// -- serverside layer bean has to be processed separately --
				Map<String, Object> params = discoService.createBeanLayerDefinitionParameters(lm
						.getDynamicLayerConfiguration());
				holders.addAll(beanFactoryService.createBeans(params));

				// Add layer to the dynamicLayersApplication for dto postprocessing
				applicationInfo.getMaps().get(0).getLayers().add(
						lm.getDynamicLayerConfiguration().getClientLayerInfo());
			}

			NamedObjectImpl applicationInfoNamedObject = new NamedObjectImpl(applicationInfo,
					"dynamicLayersApplication");
			objects.add(applicationInfoNamedObject);

			holders.addAll(converterService.createBeanDefinitionsByIntrospection(objects));
			activateBeans(holders, clientLayerIds);

			log.info(" - finished loading dynamic layers into context.");
		} catch (Exception e) {
			log.warn("Error loading dynamic layers: " + e.getMessage());
		}
	}

	// ---- Load -----------------------------------------------

	/**
	 * replaces params if necessary, removes styleinfos (use names)
	 */
	private void updateLayerProperties(DynamicLayerConfiguration lc) throws Exception {
		if (lc == null) {
			throw new IllegalArgumentException("Need a DynamicLayerConfiguration");
		}

		if (lc.getParameter(DynamicLayerConfiguration.PARAM_SOURCE_TYPE) != null
				&& DynamicLayerConfiguration.SOURCE_TYPE_SHAPE.equals(
						lc.getParameter(DynamicLayerConfiguration.PARAM_SOURCE_TYPE).getValue())) {
			lc.getParameters().clear();

			// inject private properties for shapelayers
			for (Entry<String, String> entry : postgisDataStoreParams.entrySet()) {
				Parameter p = new Parameter();
				p.setName(entry.getKey());
				p.setValue(entry.getValue());
				lc.getParameters().add(p);
			}
		}
	}

	private List<NamedObject> getClientLayerInfoObject(DynamicLayerConfiguration lc) throws Exception {
		if (lc == null) {
			throw new IllegalArgumentException("Need a DynamicLayerConfiguration");
		}
		List<NamedObject> objects = new ArrayList<BeanDefinitionDtoConverterService.NamedObject>();

		if (lc.getServerLayerInfo() instanceof VectorLayerInfo) {
			NamedStyleInfo nsi = ((VectorLayerInfo) lc.getServerLayerInfo()).getNamedStyleInfos().get(0);
			objects.add(new NamedObjectImpl(nsi, nsi.getName()));
		}

		objects.add(new NamedObjectImpl(lc.getClientLayerInfo(), lc.getClientLayerInfo().getId()));

		return objects;
	}

	private void activateBeans(List<BeanDefinitionHolder> defs, List<String> clientLayerIds) throws Exception {
		if (defs.size() > 0) {
			if (log.isDebugEnabled()) {
				// -- write config out to XML --
				((BeanDefinitionWriterServiceImpl) bser).setBaseResource(new FileSystemResource("."));
				bser.persist("_dynamicConfig_", defs);
			}

			configService.configureBeanDefinitions(defs);
		}
	}

	/**
	 * TODO.
	 * 
	 * @author <NAME>
	 * 
	 */
	private static class NamedObjectImpl implements NamedObject {

		private final Object o;

		private final String name;

		public NamedObjectImpl(Object o, String name) {
			this.o = o;
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public Object getObject() {
			return o;
		}
	}

}
<commit_msg>GDM-38 Adding dynamic wms layer with username ans password causes error<commit_after>/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2013 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.plugin.deskmanager.service.common;

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

import javax.annotation.Resource;

import org.geomajas.configuration.NamedStyleInfo;
import org.geomajas.configuration.Parameter;
import org.geomajas.configuration.VectorLayerInfo;
import org.geomajas.configuration.client.ClientApplicationInfo;
import org.geomajas.plugin.deskmanager.domain.LayerModel;
import org.geomajas.plugin.deskmanager.domain.dto.DynamicLayerConfiguration;
import org.geomajas.plugin.deskmanager.service.manager.DiscoveryService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionDtoConverterService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionDtoConverterService.NamedObject;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionWriterService;
import org.geomajas.plugin.runtimeconfig.service.BeanDefinitionWriterServiceImpl;
import org.geomajas.plugin.runtimeconfig.service.BeanFactoryService;
import org.geomajas.plugin.runtimeconfig.service.ContextConfiguratorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author <NAME>
 */
@Component
@Transactional(readOnly = true)
public class DynamicLayerLoadServiceImpl implements DynamicLayerLoadService {

	private final Logger log = LoggerFactory.getLogger(DynamicLayerLoadServiceImpl.class);

	@Autowired
	private LayerModelService layerModelService;

	@Autowired
	private DiscoveryService discoService;

	@Autowired
	private ContextConfiguratorService configService;

	@Autowired
	private BeanDefinitionDtoConverterService converterService;

	@Autowired
	private BeanFactoryService beanFactoryService;

	@Resource(name = "postGisDatastoreParams")
	private Map<String, String> postgisDataStoreParams;

	@Resource(name = "dynamicLayersApplication")
	private ClientApplicationInfo applicationInfo;

	@Autowired
	private BeanDefinitionWriterService bser;

	// -------------------------------------------------

	public void loadDynamicLayers() {
		log.info("Loading dynamic layers");

		try {
			// -- clientside / namedstyleinfo --
			List<BeanDefinitionHolder> holders = new ArrayList<BeanDefinitionHolder>();
			List<NamedObject> objects = new ArrayList<NamedObject>();
			List<String> clientLayerIds = new ArrayList<String>();
			for (LayerModel lm : layerModelService.getDynamicLayerModelsInternal()) {
				log.info(" - creating a dynamic layer for: " + lm.getName());
				try {
					clientLayerIds.add(lm.getClientLayerId());

					updateLayerProperties(lm.getDynamicLayerConfiguration());
					objects.addAll(getClientLayerInfoObject(lm.getDynamicLayerConfiguration()));

					// -- serverside layer bean has to be processed separately --
					Map<String, Object> params = discoService.createBeanLayerDefinitionParameters(lm
							.getDynamicLayerConfiguration());
					holders.addAll(beanFactoryService.createBeans(params));

					// Add layer to the dynamicLayersApplication for dto postprocessing
					applicationInfo.getMaps().get(0).getLayers()
							.add(lm.getDynamicLayerConfiguration().getClientLayerInfo());
				} catch (Exception e) {
					log.warn("Error loading dynamic layers: " + e.getMessage());
				}
			}

			NamedObjectImpl applicationInfoNamedObject = new NamedObjectImpl(applicationInfo,
					"dynamicLayersApplication");
			objects.add(applicationInfoNamedObject);

			holders.addAll(converterService.createBeanDefinitionsByIntrospection(objects));
			activateBeans(holders, clientLayerIds);

			log.info(" - finished loading dynamic layers into context.");
		} catch (Exception e) {
			log.warn("Error activating dynamic layers: " + e.getMessage());
		}
	}

	// ---- Load -----------------------------------------------

	/**
	 * replaces params if necessary, removes styleinfos (use names)
	 */
	private void updateLayerProperties(DynamicLayerConfiguration lc) throws Exception {
		if (lc == null) {
			throw new IllegalArgumentException("Need a DynamicLayerConfiguration");
		}

		if (lc.getParameter(DynamicLayerConfiguration.PARAM_SOURCE_TYPE) != null
				&& DynamicLayerConfiguration.SOURCE_TYPE_SHAPE.equals(lc.getParameter(
						DynamicLayerConfiguration.PARAM_SOURCE_TYPE).getValue())) {
			lc.getParameters().clear();

			// inject private properties for shapelayers
			for (Entry<String, String> entry : postgisDataStoreParams.entrySet()) {
				Parameter p = new Parameter();
				p.setName(entry.getKey());
				p.setValue(entry.getValue());
				lc.getParameters().add(p);
			}
		}
	}

	private List<NamedObject> getClientLayerInfoObject(DynamicLayerConfiguration lc) throws Exception {
		if (lc == null) {
			throw new IllegalArgumentException("Need a DynamicLayerConfiguration");
		}
		List<NamedObject> objects = new ArrayList<BeanDefinitionDtoConverterService.NamedObject>();

		if (lc.getServerLayerInfo() instanceof VectorLayerInfo) {
			NamedStyleInfo nsi = ((VectorLayerInfo) lc.getServerLayerInfo()).getNamedStyleInfos().get(0);
			objects.add(new NamedObjectImpl(nsi, nsi.getName()));
		}

		objects.add(new NamedObjectImpl(lc.getClientLayerInfo(), lc.getClientLayerInfo().getId()));

		return objects;
	}

	private void activateBeans(List<BeanDefinitionHolder> defs, List<String> clientLayerIds) throws Exception {
		if (defs.size() > 0) {
			if (log.isDebugEnabled()) {
				// -- write config out to XML --
				((BeanDefinitionWriterServiceImpl) bser).setBaseResource(new FileSystemResource("."));
				bser.persist("_dynamicConfig_", defs);
			}

			configService.configureBeanDefinitions(defs);
		}
	}

	/**
	 * TODO.
	 * 
	 * @author <NAME>
	 * 
	 */
	private static class NamedObjectImpl implements NamedObject {

		private final Object o;

		private final String name;

		public NamedObjectImpl(Object o, String name) {
			this.o = o;
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public Object getObject() {
			return o;
		}
	}

}

{'fraction_non_alphanumeric': 0.11532373558453306, 'fraction_numerical': 0.002110499736187533, 'mean_word_length': 4.773716275021758, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6547507', 'n_tokens_mistral': 4075, 'n_tokens_neox': 3615, 'n_words': 777}