use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ElementNamesRenderCondition method doGet.
@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
// return false by default
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(false));
// get component path
Config config = new Config(request.getResource());
String componentPath = getParameter(config, request, PN_COMPONENT_PATH, String.class);
if (componentPath == null) {
return;
}
// get component resource
Resource component = request.getResourceResolver().getResource(componentPath);
if (component == null) {
return;
}
// override fragment path if set
ValueMap properties = component.getValueMap();
String displayMode = properties.get(PARAM_AND_PN_DISPLAY_MODE, String.class);
String displayModeParam = request.getParameter(PARAM_AND_PN_DISPLAY_MODE);
if (displayModeParam != null) {
displayMode = displayModeParam;
}
boolean shouldShow = displayMode != null && displayMode.equals(getParameter(config, request, PARAM_AND_PN_DISPLAY_MODE, String.class));
request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(shouldShow));
}
use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class AbstractContentFragmentDataSourceServlet method getContentFragment.
/**
* Returns the content fragment to use for this datasource, as configured by the datasource properties. If no
* content fragment is correctly configured, then {@code null} is returned.
*/
@Nullable
private ContentFragment getContentFragment(@NotNull SlingHttpServletRequest request) {
Config config = getConfig(request);
ValueMap map = getComponentValueMap(config, request);
if (config == null) {
return null;
}
// get fragment path
String fragmentPath = getParameter(config, PN_FRAGMENT_PATH, request);
// if not present or empty, get fragment path via component
if (StringUtils.isEmpty(fragmentPath)) {
// get fragment path from component resource
fragmentPath = map != null ? map.get(PN_PATH, String.class) : null;
}
// no fragment path available
if (StringUtils.isEmpty(fragmentPath)) {
return null;
}
// get content fragment resource
Resource fragmentResource = request.getResourceResolver().getResource(fragmentPath);
if (fragmentResource == null) {
return null;
}
// return content fragment
return fragmentResource.adaptTo(ContentFragment.class);
}
use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ModelElementsDataSourceServlet method doGet.
@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) {
// First try to get the model path from request parameters
// otherwise determine model path from component resource.
RequestParameter modelPathRequestParameter = request.getRequestParameter(PARAMETER_AND_PN_MODEL_PATH);
boolean isOrderBy = request.getResource().isResourceType(RESOURCE_TYPE_ORDER_BY_V1);
String modelPath;
if (modelPathRequestParameter != null) {
modelPath = modelPathRequestParameter.getString();
} else {
Config config = getConfig(request);
ValueMap componentValueMap = getComponentValueMap(config, request);
// get model path from component resource
modelPath = componentValueMap != null ? componentValueMap.get(ContentFragmentList.PN_MODEL_PATH, String.class) : null;
}
DataSource dataSource = EmptyDataSource.instance();
if (modelPath != null) {
ResourceResolver resourceResolver = request.getResourceResolver();
String pathToCFModelElements = String.format("%s/%s/model/cq:dialog/content/items", modelPath, JcrConstants.JCR_CONTENT);
Resource cfModelElementRoot = resourceResolver.getResource(pathToCFModelElements);
if (cfModelElementRoot != null) {
Iterator<Resource> resourceIterator = cfModelElementRoot.listChildren();
List<Resource> resourceList = new LinkedList<>();
if (isOrderBy) {
resourceList.add(createResource(resourceResolver, "Created", JcrConstants.JCR_CREATED));
resourceList.add(createResource(resourceResolver, "Last Modified", JcrConstants.JCR_CONTENT + "/" + JcrConstants.JCR_LASTMODIFIED));
}
while (resourceIterator.hasNext()) {
Resource elementResource = resourceIterator.next();
ValueMap valueMap = elementResource.getValueMap();
String valueValue = valueMap.get("name", "");
String textValue = valueMap.get("fieldLabel", valueValue);
if (isOrderBy && StringUtils.isNotEmpty(valueValue)) {
valueValue = "jcr:content/data/master/" + valueValue;
}
String metaType = valueMap.get("metaType", StringUtils.EMPTY);
if (!isOrderBy || StringUtils.startsWith(metaType, "text-")) {
Resource syntheticResource = createResource(resourceResolver, textValue, valueValue);
resourceList.add(syntheticResource);
}
}
dataSource = new SimpleDataSource(resourceList.iterator());
}
}
request.setAttribute(DataSource.class.getName(), dataSource);
}
use of com.adobe.granite.ui.components.Config in project aem-core-wcm-components by Adobe-Marketing-Cloud.
the class ElementsDataSourceServlet method getItems.
@NotNull
@Override
protected List<ContentElement> getItems(@NotNull ContentFragment fragment, @NotNull SlingHttpServletRequest request) {
Config config = getConfig(request);
ValueMap map = getComponentValueMap(config, request);
String textOnlyParam = request.getParameter(PARAM_AND_PN_DISPLAY_MODE);
boolean textOnly = map != null && map.containsKey(PARAM_AND_PN_DISPLAY_MODE) && map.get(PARAM_AND_PN_DISPLAY_MODE, "multi").equals(SINGLE_TEXT);
if (textOnlyParam != null) {
textOnly = textOnlyParam.equals(SINGLE_TEXT);
}
if (textOnly) {
Iterator<ContentElement> elementIterator = fragment.getElements();
List<ContentElement> elementList = new ArrayList<ContentElement>();
while (elementIterator.hasNext()) {
ContentElement element = elementIterator.next();
String contentType = element.getValue().getContentType();
if (contentType == null) {
contentType = element.getContentType();
}
if (contentType != null && contentType.startsWith("text/") && !element.getValue().getDataType().isMultiValue()) {
elementList.add(element);
}
}
return elementList;
}
return IteratorUtils.toList(fragment.getElements());
}
Aggregations