ModelElementsDataSourceServlet extends AbstractDataSourceServlet { @Override protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) { RequestParameter modelPathRequestParameter = request.getRequestParameter(PARAMETER_AND_PN_MODEL_PATH); boolean isOrderBy = request.getResource().isResourceType(RESOURCE_TYPE_ORDER_BY); String modelPath; if (modelPathRequestParameter != null) { modelPath = modelPathRequestParameter.getString(); } else { Config config = getConfig(request); ValueMap componentValueMap = getComponentValueMap(config, request); 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); }   static final String RESOURCE_TYPE; static final String RESOURCE_TYPE_ORDER_BY; }
@Test public void verifyDataSourceWhenNoParameterIsGiven() { context.request().setResource(Mockito.mock(Resource.class)); modelElementsDataSourceServlet.doGet(context.request(), context.response()); assertThat(context.request().getAttribute(DataSource.class.getName()), instanceOf(EmptyDataSource.class)); }
@Test public void verifyDataSourceWhenOrderByIsGiven() { InputStream jsonResourceAsStream = getClass().getResourceAsStream("test-content.json"); context.load().json(jsonResourceAsStream, "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel"); Resource mockResource = Mockito.mock(Resource.class); when(mockResource.isResourceType(ModelElementsDataSourceServlet.RESOURCE_TYPE_ORDER_BY)).thenReturn(true); context.request().setResource(mockResource); context.request().setParameterMap(ImmutableMap.of( PARAMETER_AND_PN_MODEL_PATH, "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel")); modelElementsDataSourceServlet.doGet(context.request(), context.response()); SimpleDataSource simpleDataSource = (SimpleDataSource) context.request().getAttribute(DataSource.class.getName()); List<Resource> resourceList = IteratorUtils.toList(simpleDataSource.iterator()); assertThat(resourceList, not(empty())); assertThat(resourceList, allOf( hasItem(resourceWithPropertiesTextAndValue("Created", "jcr:created")), hasItem(resourceWithPropertiesTextAndValue("Last Modified", "jcr:content/jcr:lastModified")), hasItem(resourceWithPropertiesTextAndValue("textFieldLabel", "jcr:content/data/master/textField")), hasItem(resourceWithPropertiesTextAndValue("multiTextField", "jcr:content/data/master/multiTextField")))); }
@Test public void verifyDataSourceWhenModelParameterIsGiven() { InputStream jsonResourceAsStream = getClass().getResourceAsStream("test-content.json"); context.load().json(jsonResourceAsStream, "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel"); context.currentResource(Mockito.mock(Resource.class)); context.request().setParameterMap(ImmutableMap.of( PARAMETER_AND_PN_MODEL_PATH, "/conf/foobar/settings/dam/cfm/models/yetanothercfmodel")); modelElementsDataSourceServlet.doGet(context.request(), context.response()); SimpleDataSource simpleDataSource = (SimpleDataSource) context.request().getAttribute(DataSource.class.getName()); List<Resource> resourceList = IteratorUtils.toList(simpleDataSource.iterator()); assertThat(resourceList, not(empty())); assertThat(resourceList, allOf( hasItem(resourceWithPropertiesTextAndValue("textFieldLabel", "textField")), hasItem(resourceWithPropertiesTextAndValue("multiTextField", "multiTextField")), hasItem(resourceWithPropertiesTextAndValue("numberFieldLabel", "numberField")), hasItem(resourceWithPropertiesTextAndValue("booleanField", "booleanField")), hasItem(resourceWithPropertiesTextAndValue("dateAndTimeFieldLabel", "dateAndTimeField")), hasItem(resourceWithPropertiesTextAndValue("enumerationFieldLabel", "enumerationField")))); }