<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.layer.geotools;

import java.util.HashMap;
import java.util.Map;

import org.geomajas.configuration.AbstractAttributeInfo;
import org.geomajas.global.ExceptionCode;
import org.geomajas.global.GeomajasException;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.FeatureModel;
import org.geomajas.layer.shapeinmem.FeatureSourceRetriever;
import org.geomajas.service.DtoConverterService;
import org.geotools.data.DataStore;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.opengis.feature.simple.SimpleFeature;

import com.vividsolutions.jts.geom.Geometry;

/**
 * GeoTools feature model. Should be able to use any GeoTools data source.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class GeoToolsFeatureModel extends FeatureSourceRetriever implements FeatureModel {

	private final SimpleFeatureBuilder builder;

	private final int srid;

	private final DtoConverterService converterService;

	// Constructor:

	/**
	 * Create a GeoTools feature model.
	 *
	 * @param dataStore data store
	 * @param featureSourceName feature source name
	 * @param srid srid
	 * @param converterService converter service
	 * @throws LayerException feature model could not be constructed
	 */
	public GeoToolsFeatureModel(DataStore dataStore, String featureSourceName, int srid,
			DtoConverterService converterService) throws LayerException {
		super();
		setDataStore(dataStore);
		setFeatureSourceName(featureSourceName);
		this.srid = srid;
		builder = new SimpleFeatureBuilder(getSchema());
		this.converterService = converterService;
	}

	// FeatureModel implementation:

	@Override
	public Attribute getAttribute(Object feature, String name) throws LayerException {
		return convertAttribute(asFeature(feature).getAttribute(name), name);
	}

	@Override
	public Map<String, Attribute> getAttributes(Object feature) throws LayerException {
		SimpleFeature f = asFeature(feature);
		HashMap<String, Attribute> attribs = new HashMap<String, Attribute>();
		for (AbstractAttributeInfo attributeInfo : getAttributeInfoMap().values()) {
			String name = attributeInfo.getName();
			attribs.put(name, convertAttribute(f.getAttribute(name), name));
		}
		return attribs;
	}

	private Attribute convertAttribute(Object object, String name) throws LayerException {
		AbstractAttributeInfo attributeInfo = getAttributeInfoMap().get(name);
		if (null == attributeInfo) {
			throw new LayerException(ExceptionCode.ATTRIBUTE_UNKNOWN, name, getAttributeInfoMap().keySet());<commit_msg>#1259 - catch exception if feature type is not available to avoid abort of startup<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.layer.geotools;

import java.util.HashMap;
import java.util.Map;

import org.geomajas.configuration.AbstractAttributeInfo;
import org.geomajas.global.ExceptionCode;
import org.geomajas.global.GeomajasException;
import org.geomajas.layer.LayerException;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.FeatureModel;
import org.geomajas.layer.shapeinmem.FeatureSourceRetriever;
import org.geomajas.service.DtoConverterService;
import org.geotools.data.DataStore;
import org.geotools.data.SchemaNotFoundException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.opengis.feature.simple.SimpleFeature;

import com.vividsolutions.jts.geom.Geometry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * GeoTools feature model. Should be able to use any GeoTools data source.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class GeoToolsFeatureModel extends FeatureSourceRetriever implements FeatureModel {

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

	private SimpleFeatureBuilder builder;

	private final int srid;

	private final DtoConverterService converterService;

	// Constructor:

	/**
	 * Create a GeoTools feature model.
	 *
	 * @param dataStore data store
	 * @param featureSourceName feature source name
	 * @param srid srid
	 * @param converterService converter service
	 * @throws LayerException feature model could not be constructed
	 */
	public GeoToolsFeatureModel(DataStore dataStore, String featureSourceName, int srid,
			DtoConverterService converterService) throws LayerException {
		super();
		setDataStore(dataStore);
		setFeatureSourceName(featureSourceName);
		this.srid = srid;
		try {
			builder = new SimpleFeatureBuilder( getSchema() );
		} catch ( LayerException e ) {
			log.warn(" The layer could not be correctly initialized: " + featureSourceName );
			builder = null;
		}
		this.converterService = converterService;
	}

	// FeatureModel implementation:

	@Override
	public Attribute getAttribute(Object feature, String name) throws LayerException {
		return convertAttribute(asFeature(feature).getAttribute(name), name);
	}

	@Override
	public Map<String, Attribute> getAttributes(Object feature) throws LayerException {
		SimpleFeature f = asFeature(feature);
		HashMap<String, Attribute> attribs = new HashMap<String, Attribute>();
		for (AbstractAttributeInfo attributeInfo : getAttributeInfoMap().values()) {
			String name = attributeInfo.getName();
			attribs.put(name, convertAttribute(f.getAttribute(name), name));
		}
		return attribs;
	}

	private Attribute convertAttribute(Object object, String name) throws LayerException {
		AbstractAttributeInfo attributeInfo = getAttributeInfoMap().get(name);
		if (null == attributeInfo) {
			throw new LayerException(ExceptionCode.ATTRIBUTE_UNKNOWN, name, getAttributeInfoMap().keySet());
{'fraction_non_alphanumeric': 0.08848730331827387, 'fraction_numerical': 0.0034273251285246924, 'mean_word_length': 5.356435643564357, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3878802', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1756, 'n_words': 503}