package org.openlca.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "tbl_flows")
public class Flow extends CategorizedEntity {

	@Column(name = "flow_type")
	@Enumerated(EnumType.STRING)
	public FlowType flowType;

	@Column(name = "cas_number")
	public String casNumber;

	@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
	@JoinColumn(name = "f_flow")
	public final List<FlowPropertyFactor> flowPropertyFactors = new ArrayList<>();

	@Column(name = "formula")
	public String formula;

	@Column(name = "infrastructure_flow")
	public boolean infrastructureFlow;

	@Column(name = "synonyms")
	public String synonyms;

	@OneToOne
	@JoinColumn(name = "f_location")
	public Location location;

	@OneToOne
	@JoinColumn(name = "f_reference_flow_property")
	public FlowProperty referenceFlowProperty;

	@Override
	public Flow clone() {
		Flow clone = new Flow();
		Util.cloneRootFields(this, clone);
		clone.category = category;
		clone.flowType = flowType;
		clone.casNumber = casNumber;
		clone.formula = formula;
		clone.infrastructureFlow = infrastructureFlow;
		clone.location = location;
		clone.referenceFlowProperty = referenceFlowProperty;
		for (FlowPropertyFactor factor : flowPropertyFactors) {
			clone.flowPropertyFactors.add(factor.clone());
		}
		clone.synonyms = synonyms;
		return clone;
	}

	public FlowPropertyFactor getReferenceFactor() {
		if (referenceFlowProperty == null)
			return null;
		for (FlowPropertyFactor f : flowPropertyFactors) {
			if (Objects.equals(referenceFlowProperty, f.flowProperty))
				return f;
		}
		return null;
	}

	public FlowPropertyFactor getFactor(FlowProperty property) {
		for (FlowPropertyFactor f : flowPropertyFactors)
			if (Objects.equals(f.flowProperty, property))
				return f;
		return null;
	}

}

@@ -1687,24 +1687,112 @@
  clone;%0A%09%7D%0A%0A
+%09/**%0A%09 * Get the flow property factor of the reference flow property of this flow.%0A%09 */%0A
 %09public Flow
@@ -2034,16 +2034,476 @@
 ll;%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns the reference unit of this flow. More specifically, it returns the%0A%09 * reference unit of the unit group of the reference flow property of this flow.%0A%09 * In openLCA, results of a flow are always calculated in its reference unit.%0A%09 */%0A%09public Unit getReferenceUnit() %7B%0A%09%09if (referenceFlowProperty == null)%0A%09%09%09return null;%0A%09%09if (referenceFlowProperty.unitGroup == null)%0A%09%09%09return null;%0A%09%09return referenceFlowProperty.unitGroup.referenceUnit;%0A%09%7D%0A%0A
 %09public 

add a convenience function to get the reference unit of a flow