package org.openlca.core.database;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.openlca.core.model.Category;
import org.openlca.core.model.Parameter;
import org.openlca.core.model.ParameterScope;
import org.openlca.core.model.descriptors.ParameterDescriptor;
import org.openlca.util.Strings;

public class ParameterDao extends
        RootEntityDao<Parameter, ParameterDescriptor> {

	public ParameterDao(IDatabase database) {
		super(Parameter.class, ParameterDescriptor.class, database);
	}

	public List<Parameter> getGlobalParameters() {
		String jpql = "select p from Parameter p where p.scope = :scope";
		Map<String, Object> args = new HashMap<>();
		args.put("scope", ParameterScope.GLOBAL);
		return getAll(jpql, args);
	}

	public List<ParameterDescriptor> getGlobalDescriptors() {
		String sql = getDescriptorQuery();
		List<Object> parameters = new ArrayList<>();
		sql += " where scope = ?";
		parameters.add(ParameterScope.GLOBAL.name());
		List<Object[]> results = selectAll(sql, getDescriptorFields(),
				parameters);
		return createDescriptors(results);
	}

	@Override
	public List<ParameterDescriptor> getDescriptors(
			Optional<Category> category) {
		String sql = getDescriptorQuery();
		List<Object> parameters = new ArrayList<>();
		if (category.isPresent()) {
			sql += " where f_category = ?";
			parameters.add(category.get().id);
		} else {
			sql += " where f_category is null";
		}
		sql += " and scope = ?";
		parameters.add(ParameterScope.GLOBAL.name());
		List<Object[]> results = selectAll(sql, getDescriptorFields(),
				parameters);
		return createDescriptors(results);
	}

	public List<ParameterDescriptor> getDescriptors(String[] names) {
		return getDescriptors(names, null);
	}

	public List<ParameterDescriptor> getDescriptors(String[] names,
			ParameterScope scope) {
		if (names == null || names.length == 0)
			return Collections.emptyList();
		StringBuilder sql = new StringBuilder(getDescriptorQuery());
		List<Object> parameters = new ArrayList<>();
		String[] list = new String[names.length];
		for (int i = 0; i < names.length; i++)
			list[i] = "'" + names[i].toLowerCase() + "'";
		sql.append(" WHERE lower(name) IN (" + Strings.join(list, ',') + ")");
		if (scope != null) {
			sql.append(" AND scope = ?");
			parameters.add(scope.name());
		}
		List<Object[]> results = selectAll(sql.toString(),
				getDescriptorFields(), parameters);
		return createDescriptors(results);
	}

	public boolean existsGlobal(String name) {
		if (name == null)
			return false;
		String jpql = "SELECT count(param) FROM Parameter param "
				+ "WHERE lower(param.name) = :name "
				+ "AND param.scope = :scope";
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("name", name.toLowerCase());
		parameters.put("scope", ParameterScope.GLOBAL);
		return getCount(jpql, parameters) > 0;
	}

}

@@ -33,66 +33,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 impo
@@ -321,41 +321,8 @@
 tor;
-%0Aimport org.openlca.util.Strings;
 %0A%0Apu
@@ -824,115 +824,54 @@
 %7B%0A%09%09
-String sql = getDescriptorQuery();%0A%09%09List%3CObject%3E parameters = new ArrayList%3C%3E();%0A%09%09sql += %22 
+return queryDescriptors(%0A%09%09%09%09%22
 where 
+d.
 scope = 
 ?%22;%0A
@@ -866,37 +866,20 @@
 scope = 
-?%22;%0A%09%09parameters.add(
+%22 + 
 Paramete
@@ -902,127 +902,19 @@
 me()
-);%0A%09%09List%3CObject%5B%5D%3E results = selectAll(sql, getDescriptorFields(),%0A%09%09%09%09parameters);%0A%09%09return createDescriptors(results
+, List.of()
 );%0A%09
@@ -976,20 +976,16 @@
 riptors(
-%0A%09%09%09
 Optional
@@ -1013,96 +1013,19 @@
 %7B%0A%09%09
-String sql = getDescriptorQuery();%0A%09%09List%3CObject%3E parameters = new ArrayList%3C%3E();%0A%09%09if (
+var cond = 
 cate
@@ -1044,30 +1044,23 @@
 nt()
-) %7B
 %0A%09%09%09
-sql +=
+?
  %22
- 
 where 
+d.
 f_ca
@@ -1072,30 +1072,12 @@
 y = 
-?%22;%0A%09%09%09parameters.add(
+%22 + 
 cate
@@ -1093,40 +1093,23 @@
 ).id
-);
 %0A%09%09
-%7D else %7B%0A%09%09%09sql +=
+%09:
  %22
- 
 where 
+d.
 f_ca
@@ -1131,15 +1131,12 @@
 ;%0A%09%09
-%7D%0A%09%09sql
+cond
  += 
@@ -1141,24 +1141,26 @@
 = %22 and 
+d.
 scope = 
 ?%22;%0A%09%09pa
@@ -1155,29 +1155,12 @@
 e = 
-?%22;%0A%09%09parameters.add(
+%22 + 
 Para
@@ -1187,949 +1187,51 @@
 me()
-);%0A%09%09List%3CObject%5B%5D%3E results = selectAll(sql, getDescriptorFields(),%0A%09%09%09%09parameters);%0A%09%09return createDescriptors(results);%0A%09%7D%0A%0A%09public List%3CParameterDescriptor%3E getDescriptors(String%5B%5D names) %7B%0A%09%09return getDescriptors(names, null);%0A%09%7D%0A%0A%09public List%3CParameterDescriptor%3E getDescriptors(String%5B%5D names,%0A%09%09%09ParameterScope scope) %7B%0A%09%09if (names == null %7C%7C names.length == 0)%0A%09%09%09return Collections.emptyList();%0A%09%09StringBuilder sql = new StringBuilder(getDescriptorQuery());%0A%09%09List%3CObject%3E parameters = new ArrayList%3C%3E();%0A%09%09String%5B%5D list = new String%5Bnames.length%5D;%0A%09%09for (int i = 0; i %3C names.length; i++)%0A%09%09%09list%5Bi%5D = %22'%22 + names%5Bi%5D.toLowerCase() + %22'%22;%0A%09%09sql.append(%22 WHERE lower(name) IN (%22 + Strings.join(list, ',') + %22)%22);%0A%09%09if (scope != null) %7B%0A%09%09%09sql.append(%22 AND scope = ?%22);%0A%09%09%09parameters.add(scope.name());%0A%09%09%7D%0A%09%09List%3CObject%5B%5D%3E results = selectAll(sql.toString(),%0A%09%09%09%09getDescriptorFields(), parameters);%0A%09%09return createDescriptors(results
+;%0A%09%09return queryDescriptors(cond, List.of()
 );%0A%09

fix more daos for new descriptor queries III