<commit_before>/*
 * 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.plugin.reporting.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRewindableDataSource;

import org.geomajas.configuration.LabelStyleInfo;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.InternalFeature;

/**
 * Jasper Reports data source for a collection of internal features. Report fields should have the attribute names as
 * their name.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class InternalFeatureDataSource implements JRRewindableDataSource {

	private List<InternalFeature> features;

	private Iterator<InternalFeature> iterator;

	private InternalFeature currentFeature;

	public InternalFeatureDataSource(Collection<InternalFeature> features) {
		this.features = new ArrayList<InternalFeature>(features);
	}

	public boolean next() {
		if (iterator == null) {
			iterator = features.iterator();
		}
		if (iterator.hasNext()) {
			currentFeature = iterator.next();
			return true;
		} else {
			return false;
		}
	}

	public Object getFieldValue(JRField field) throws JRException {
		if (currentFeature != null) {
			String fieldName = field.getName();
			if (LabelStyleInfo.ATTRIBUTE_NAME_ID.equals(fieldName)) {
				return currentFeature.getId();
			}
			Map<String, Attribute> attributes = currentFeature.getAttributes();
			if (null != attributes) {
				Attribute attribute = attributes.get(field.getName());
				if (null != attribute) {
					return attribute.getValue();
				}
			}
		}
		return null;
	}

	/**
	 * Moves to the position before the first element. Calling {@link #next()} after this will move to the first
	 * element.<commit_msg>REP-1 allow feature itself to be queried<commit_after>/*
 * 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.plugin.reporting.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRRewindableDataSource;

import org.geomajas.configuration.LabelStyleInfo;
import org.geomajas.layer.feature.Attribute;
import org.geomajas.layer.feature.InternalFeature;

/**
 * Jasper Reports data source for a collection of internal features. Report fields should have the attribute names as
 * their name.
 *
 * @author <NAME>
 * @author <NAME>
 */
public class InternalFeatureDataSource implements JRRewindableDataSource {

	private List<InternalFeature> features;

	private Iterator<InternalFeature> iterator;

	private InternalFeature currentFeature;

	public InternalFeatureDataSource(Collection<InternalFeature> features) {
		this.features = new ArrayList<InternalFeature>(features);
	}

	public boolean next() {
		if (iterator == null) {
			iterator = features.iterator();
		}
		if (iterator.hasNext()) {
			currentFeature = iterator.next();
			return true;
		} else {
			return false;
		}
	}

	public Object getFieldValue(JRField field) throws JRException {
		if (currentFeature != null) {
			String fieldName = field.getName();
			if ("@".equals(fieldName)) {
				return currentFeature;
			}
			if (LabelStyleInfo.ATTRIBUTE_NAME_ID.equals(fieldName)) {
				return currentFeature.getId();
			}
			Map<String, Attribute> attributes = currentFeature.getAttributes();
			if (null != attributes) {
				Attribute attribute = attributes.get(field.getName());
				if (null != attribute) {
					return attribute.getValue();
				}
			}
		}
		return null;
	}

	/**
	 * Moves to the position before the first element. Calling {@link #next()} after this will move to the first
	 * element.
{'fraction_non_alphanumeric': 0.10230951867040794, 'fraction_numerical': 0.003669328728685517, 'mean_word_length': 4.308132875143184, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6352221', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1300, 'n_words': 391}