package org.openlca.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * An impact assessment method.
 */
@Entity
@Table(name = "tbl_impact_methods")
public class ImpactMethod extends CategorizedEntity {

	@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
	@JoinColumn(name = "f_impact_method")
	private final List<ImpactCategory> impactCategories = new ArrayList<>();

	@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
	@JoinColumn(name = "f_impact_method")
	private final List<NwSet> nwSets = new ArrayList<>();

	@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true)
	@JoinColumn(name = "f_owner")
	private final List<Parameter> parameters = new ArrayList<>();

	@Override
	public ImpactMethod clone() {
		ImpactMethod lciaMethod = new ImpactMethod();
		lciaMethod.setRefId(UUID.randomUUID().toString());
		lciaMethod.setName(getName());
		lciaMethod.setCategory(getCategory());
		lciaMethod.setDescription(getDescription());
		for (ImpactCategory lciaCategory : getImpactCategories()) {
			lciaMethod.getImpactCategories().add(lciaCategory.clone());
		}
		// TODO: clone parameters and nw-sets!
		return lciaMethod;
	}

	public List<ImpactCategory> getImpactCategories() {
		return impactCategories;
	}

	public List<NwSet> getNwSets() {
		return nwSets;
	}

	public List<Parameter> getParameters() {
		return parameters;
	}

}
@@ -50,24 +50,50 @@
 .ArrayList;%0A
+import java.util.HashMap;%0A
 import java.
@@ -308,48 +308,8 @@
 e;%0A%0A
-/**%0A * An impact assessment method.%0A */%0A
 @Ent
@@ -746,168 +746,8 @@
 );%0A%0A
-%09@OneToMany(cascade = %7B CascadeType.ALL %7D, orphanRemoval = true)%0A%09@JoinColumn(name = %22f_owner%22)%0A%09private final List%3CParameter%3E parameters = new ArrayList%3C%3E();%0A%0A
 %09@Ov
@@ -799,26 +799,21 @@
 tMethod 
-lciaMethod
+clone
  = new I
@@ -825,34 +825,29 @@
 Method();%0A%09%09
-lciaMethod
+clone
 .setRefId(UU
@@ -873,34 +873,29 @@
 tring());%0A%09%09
-lciaMethod
+clone
 .setName(get
@@ -901,34 +901,29 @@
 tName());%0A%09%09
-lciaMethod
+clone
 .setCategory
@@ -941,26 +941,21 @@
 y());%0A%09%09
-lciaMethod
+clone
 .setDesc
@@ -987,32 +987,90 @@
 ;%0A%09%09
-for (ImpactCategory lcia
+HashMap%3CImpactCategory, ImpactCategory%3E impactMap = new HashMap%3C%3E();%0A%09%09for (Impact
 Cate
@@ -1074,16 +1074,24 @@
 ategory 
+origCat 
 : getImp
@@ -1116,133 +1116,604 @@
 %0A%09%09%09
-lciaMethod.getImpactCategories().add(lciaCategory.clone());%0A%09%09%7D%0A%09%09// TODO: clone parameters and nw-sets!%0A%09%09return lciaMethod;
+ImpactCategory clonedCat = origCat.clone();%0A%09%09%09impactMap.put(origCat, clonedCat);%0A%09%09%09clone.getImpactCategories().add(clonedCat);%0A%09%09%7D%0A%09%09cloneNwSets(clone, impactMap);%0A%09%09return clone;%0A%09%7D%0A%0A%09private void cloneNwSets(ImpactMethod clone,%0A%09                         HashMap%3CImpactCategory, ImpactCategory%3E impactMap) %7B%0A%09%09for(NwSet nwSet : getNwSets()) %7B%0A%09%09%09NwSet clonedSet = nwSet.clone();%0A%09%09%09clone.getNwSets().add(clonedSet);%0A%09%09%09for(NwFactor factor : nwSet.getFactors()) %7B%0A%09%09%09%09ImpactCategory clonedCat = impactMap.get(factor%0A%09%09%09%09%09%09.getImpactCategory());%0A%09%09%09%09factor.setImpactCategory(clonedCat);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A
@@ -1856,72 +1856,5 @@
 %09%7D%0A%0A
-%09public List%3CParameter%3E getParameters() %7B%0A%09%09return parameters;%0A%09%7D%0A%0A
 %7D

copy normalisation and weighting sets in clone method of LCIA methods; removed parameters from LCIA methods (we need more specific LCIA parameters)