Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

explain component exporter, container exporter

Avatar

Level 7

I am looking at below code to reuse on one of the component to export child nodes/components as json.

@notnull
	@Override
	public Map<String, ? extends ComponentExporter> getExportedItems() {
		if (childModels == null) {
			childModels = getChildModels(request, ComponentExporter.class);
		}

		return childModels;
	}

	@NotNull
	@Override
	public String[] getExportedItemsOrder() {
		Map<String, ? extends ComponentExporter> models = getExportedItems();

		if (models.isEmpty()) {
			return ArrayUtils.EMPTY_STRING_ARRAY;
		}

		return models.keySet().toArray(ArrayUtils.EMPTY_STRING_ARRAY);

	}

	/**
	 * Returns a map (resource name => Sling Model class) of the given resource
	 * children's Sling Models that can be adapted to {@link T}.
	 *
	 * @Param slingRequest the current request
	 * @Param modelClass   the Sling Model class to be adapted to
	 * @Return a map (resource name => Sling Model class) of the given resource
	 *         children's Sling Models that can be adapted to {@link T}
	 */
	@NotNull
	private <T> Map<String, T> getChildModels(@NotNull SlingHttpServletRequest slingRequest,
											  @notnull Class<T> modelClass) {
		Map<String, T> itemWrappers = new LinkedHashMap<>();

		for (final Resource child : slingModelFilter.filterChildResources(request.getResource().getChildren())) {
			itemWrappers.put(child.getName(), modelFactory.getModelFromWrappedRequest(slingRequest, child, modelClass));
		}

		return itemWrappers;
	}

 

 getChildModels() : how does this method adapting to specific component adapter type? I see from ModelFactory class that this method passes "ComponentExporter.class" as modelClass / adapterType. I would like to understand how ModelFactory using ComponentExporter.class as adapterType to fetch model json for child components/nodes.

 

Doesn't adapterType needs to be Button.class if button is a child component or Teaser.class if Teaser is a child component?

 

Thanks for any insights.

 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

 

In the given code, the getChildModels() method uses the modelFactory to adapt child resources to the ComponentExporter model class. The ComponentExporter.class is used as the adapter type because it represents a common interface for exporting components. It doesn't have to match the child component classes (e.g., Button.class or Teaser.class). The Sling Model adaptation mechanism leverages annotations and configurations to map the child resources to the appropriate Sling Model implementation. This approach simplifies the process of exporting child components as JSON.

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

Hi,

 

In the given code, the getChildModels() method uses the modelFactory to adapt child resources to the ComponentExporter model class. The ComponentExporter.class is used as the adapter type because it represents a common interface for exporting components. It doesn't have to match the child component classes (e.g., Button.class or Teaser.class). The Sling Model adaptation mechanism leverages annotations and configurations to map the child resources to the appropriate Sling Model implementation. This approach simplifies the process of exporting child components as JSON.