package com.axelor.apps.production.service;

import com.axelor.apps.production.db.Configurator;
import com.axelor.apps.production.db.ConfiguratorCreator;
import com.axelor.apps.production.db.repo.ConfiguratorRepository;
import com.axelor.meta.db.MetaJsonField;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class ConfiguratorCreatorServiceImpl implements ConfiguratorCreatorService {
	
	@Inject
	private ConfiguratorRepository configuratorRepo;
	
	
	@Override
	@Transactional
	public Configurator generateConfigurator(ConfiguratorCreator creator) {
		
		if (creator == null) {
			return null;
		}
		
		Configurator configurator =  configuratorRepo.all().filter("self.configuratorCreator = ?1", creator).fetchOne();
		
		for (MetaJsonField field : creator.getAttributes()) {
			String condition = "configuratorCreator.id == " + creator.getId();
			String showIf = field.getShowIf();
			if (!Strings.isNullOrEmpty(showIf)) {
				if (!showIf.contains(condition)) {
					field.setShowIf(condition + " && (" + showIf + ")");
				}
			}
			else {
				field.setShowIf(condition);
			}
		}
		
		if (configurator == null) {
			configurator = new Configurator();
			configurator.setConfiguratorCreator(creator);
			configuratorRepo.save(configurator);
		}
		
		
		return configurator;
	}

}

@@ -679,126 +679,8 @@
 %0A%09%09%0A
-%09%09Configurator configurator =  configuratorRepo.all().filter(%22self.configuratorCreator = ?1%22, creator).fetchOne();%0A%09%09%0A
 %09%09fo
@@ -754,16 +754,24 @@
 tion = %22
+$record.
 configur
@@ -1046,24 +1046,142 @@
 %09%09%09%7D%0A%09%09%7D%0A%09%09%0A
+%09%09Configurator configurator =  configuratorRepo.all().filter(%22self.configuratorCreator = ?1%22, creator).fetchOne();%0A%09%09%0A
 %09%09if (config

Fix configurator