All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openlca.validation.ProjectCheck Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package org.openlca.validation;

import org.openlca.core.database.NativeSql;
import org.openlca.core.model.ModelType;

class ProjectCheck implements Runnable {

	private final Validation v;
	private boolean foundErrors = false;

	ProjectCheck(Validation v) {
		this.v = v;
	}

	@Override
	public void run() {
		try {
			checkProjectRefs();
			checkVariantRefs();
			if (!foundErrors && !v.wasCanceled()) {
				v.ok("checked projects");
			}
		} catch (Exception e) {
			v.error("error in project validation", e);
		} finally {
			v.workerFinished();
		}
	}

	private void checkProjectRefs() {
		if (v.wasCanceled())
			return;
		var sql = "select " +
			/* 1 */ "id, " +
			/* 2 */ "f_impact_method, " +
			/* 3 */ "f_nwset from tbl_projects";
		NativeSql.on(v.db).query(sql, r -> {
			var projectId = r.getLong(1);

			var methodId = r.getLong(2);
			if (methodId != 0 && !v.ids.contains(ModelType.IMPACT_METHOD, methodId)) {
				v.error(projectId, ModelType.PROJECT,
					"invalid reference to impact method @" + methodId);
				foundErrors = true;
			}

			var nwSetId = r.getLong(3);
			if (nwSetId != 0 && !v.ids.nwSets().contains(nwSetId)) {
				v.error(projectId, ModelType.PROJECT,
					"invalid reference to nw-set @" + nwSetId);
				foundErrors = true;
			}
			return !v.wasCanceled();
		});
	}

	private void checkVariantRefs() {
		if (v.wasCanceled())
			return;
		var sql = "select " +
			/* 1 */ "id, " +
			/* 2 */ "f_project, " +
			/* 3 */ "f_product_system, " +
			/* 4 */ "f_unit, " +
			/* 5 */ "f_flow_property_factor from tbl_project_variants";
		NativeSql.on(v.db).query(sql, r -> {
			long variantId = r.getLong(1);
			long projectId = r.getLong(2);

			if (!v.ids.contains(ModelType.PROJECT, projectId)) {
				v.warning("unlinked project variant @" + variantId);
				foundErrors = true;
				return !v.wasCanceled();
			}

			long systemId = r.getLong(3);
			if (!v.ids.contains(ModelType.PRODUCT_SYSTEM, systemId)) {
				v.error(projectId, ModelType.PROJECT,
					"invalid reference to product system @" + systemId
						+ " in variant @" + variantId);
				foundErrors = true;
				return !v.wasCanceled();
			}

			long propId = r.getLong(5);
			long unitId = r.getLong(4);
			if (!v.ids.units().isFactorUnit(propId, unitId)) {
				v.error(projectId, ModelType.PROJECT,
						"invalid reference to unit=" + unitId + " property=" +propId
								+ " in variant @" + variantId);
				foundErrors = true;
			}

			return !v.wasCanceled();
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy