<commit_before>package org.nasdanika.codegen.ecore.web.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jdt.core.IPackageFragment;
import org.nasdanika.codegen.GeneratorController;
import org.nasdanika.codegen.ecore.EcoreCodeGenerator;
import org.nasdanika.codegen.ecore.ModelElement;
import org.nasdanika.codegen.ecore.web.ui.model.EPackageConfiguration;
import org.nasdanika.codegen.java.PackageFragment;
import org.nasdanika.config.Context;
import org.nasdanika.config.MutableContext;

/**
 * Iterates over selected EPackages.
 * @author Pavel
 *
 */
public class RoutesPackageController implements GeneratorController<IPackageFragment, PackageFragment> {

	static final String ROUTE_BASE_EXTENDS = "route-base-extends";
	static final String RENDERER_BASE_EXTENDS = "renderer-base-extends";
	static final String RENDER_ANNOTATION_SOURCE = "renderer-annotation-source";

	@Override
	public Collection<Context> iterate(Context context, PackageFragment generator) throws Exception {
		Collection<Context> ret = new ArrayList<>();
		EcoreCodeGenerator eCoreCodeGenerator = context.get(EcoreCodeGenerator.class);
		for (EPackage ep: eCoreCodeGenerator.getEPackages()) {
			if (eCoreCodeGenerator.isSelected(ep)) {
				MutableContext mc = context.createSubContext();
				mc.set("epackage-name", ep.getName());
				mc.set(EPackage.class, ep);
				ret.add(mc);
				Set<String> packageTypes = new HashSet<>();
				packageTypes.add("RouteBase");
				packageTypes.add("RendererBase");
				
				mc.set(RENDERER_BASE_EXTENDS, "org.nasdanika.cdo.web.routes.app.Renderer");
				mc.set(ROUTE_BASE_EXTENDS, "org.nasdanika.cdo.web.routes.app.Route");
				mc.set(RENDER_ANNOTATION_SOURCE, "org.nasdanika.cdo.web.render");
				ModelElement me = eCoreCodeGenerator.find(ep, false);
				if (me != null) {
					EObject cfg = me.getConfiguration(WebUIGenerationTarget.CONFIG_ID);
					if (cfg instanceof EPackageConfiguration) {
						mc.set(RENDERER_BASE_EXTENDS, ((EPackageConfiguration) cfg).getRendererBaseExtends());
						mc.set(ROUTE_BASE_EXTENDS, ((EPackageConfiguration) cfg).getRouteBaseExtends());
						mc.set(RENDER_ANNOTATION_SOURCE, ((EPackageConfiguration) cfg).getRenderAnnotationSource());
					}
				}<commit_msg>Sub-packages are generated under their parent packages.<commit_after>package org.nasdanika.codegen.ecore.web.ui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.jdt.core.IPackageFragment;
import org.nasdanika.codegen.GeneratorController;
import org.nasdanika.codegen.ecore.EcoreCodeGenerator;
import org.nasdanika.codegen.ecore.ModelElement;
import org.nasdanika.codegen.ecore.web.ui.model.EPackageConfiguration;
import org.nasdanika.codegen.java.PackageFragment;
import org.nasdanika.config.Context;
import org.nasdanika.config.MutableContext;

/**
 * Iterates over selected EPackages.
 * @author Pavel
 *
 */
public class RoutesPackageController implements GeneratorController<IPackageFragment, PackageFragment> {

	static final String ROUTE_BASE_EXTENDS = "route-base-extends";
	static final String RENDERER_BASE_EXTENDS = "renderer-base-extends";
	static final String RENDER_ANNOTATION_SOURCE = "renderer-annotation-source";

	@Override
	public Collection<Context> iterate(Context context, PackageFragment generator) throws Exception {
		Collection<Context> ret = new ArrayList<>();
		EcoreCodeGenerator eCoreCodeGenerator = context.get(EcoreCodeGenerator.class);		
		EList<EPackage> ePackages = eCoreCodeGenerator.getEPackages();
		for (EPackage ep: ePackages) {
			if (eCoreCodeGenerator.isSelected(ep)) {
				MutableContext mc = context.createSubContext();
				String ePackageName = ep.getName();
				for (EObject container = ep.eContainer(); ePackages.contains(container); container = container.eContainer()) {
					ePackageName = ((EPackage) container).getName() + "." + ePackageName;
				}
				mc.set("epackage-name", ePackageName);
				mc.set(EPackage.class, ep);
				ret.add(mc);
				Set<String> packageTypes = new HashSet<>();
				packageTypes.add("RouteBase");
				packageTypes.add("RendererBase");
				
				mc.set(RENDERER_BASE_EXTENDS, "org.nasdanika.cdo.web.routes.app.Renderer");
				mc.set(ROUTE_BASE_EXTENDS, "org.nasdanika.cdo.web.routes.app.Route");
				mc.set(RENDER_ANNOTATION_SOURCE, "org.nasdanika.cdo.web.render");
				ModelElement me = eCoreCodeGenerator.find(ep, false);
				if (me != null) {
					EObject cfg = me.getConfiguration(WebUIGenerationTarget.CONFIG_ID);
					if (cfg instanceof EPackageConfiguration) {
						mc.set(RENDERER_BASE_EXTENDS, ((EPackageConfiguration) cfg).getRendererBaseExtends());
						mc.set(ROUTE_BASE_EXTENDS, ((EPackageConfiguration) cfg).getRouteBaseExtends());
						mc.set(RENDER_ANNOTATION_SOURCE, ((EPackageConfiguration) cfg).getRenderAnnotationSource());
					}
				}
{'fraction_non_alphanumeric': 0.11808328579577866, 'fraction_numerical': 0.0, 'mean_word_length': 5.469864698646987, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5658437', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1684, 'n_words': 277}