blob: dea5fcb84d6718deaa9d544754081cf889c80869 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.graphql.samples.website.datafetchers;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.graphql.samples.website.models.SlingWrappers;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
/** Find articles which contain specific text */
class ArticlesWithTextFetcher implements DataFetcher<Object> {
public static final String NAME = "articlesWithText";
public static final String P_WITH_TEXT = "withText";
private final Resource resource;
ArticlesWithTextFetcher(Resource resource) {
this.resource = resource;
}
@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
final String expectedText = environment.getArgument(P_WITH_TEXT);
final String jcrQuery = String.format(
"/jcr:root%s//*[jcr:contains(@text, '%s') or jcr:contains(@title, '%s')]",
Constants.ARTICLES_ROOT, expectedText, expectedText);
final List<Map<String, Object>> result = new ArrayList<>();
final Iterator<Resource> it = resource.getResourceResolver().findResources(jcrQuery, "xpath");
// TODO should use pagination
int counter = 451;
while(it.hasNext()) {
if(--counter <= 0) {
break;
}
result.add(SlingWrappers.resourceWrapper(it.next()));
}
return result;
}
}