anchela commented on code in PR #25:
URL:
https://github.com/apache/sling-org-apache-sling-jcr-resource/pull/25#discussion_r885722435
##########
src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/BasicQueryLanguageProvider.java:
##########
@@ -98,85 +99,91 @@ public Iterator<ValueMap> queryResources(final
ResolveContext<JcrProviderState>
try {
final QueryResult result =
JcrResourceUtil.query(ctx.getProviderState().getSession(), query,
queryLanguage);
- final String[] colNames = result.getColumnNames();
- final RowIterator rows = result.getRows();
+ return new ValueMapIterator(result.getColumnNames(),
result.getRows());
+ } catch (final javax.jcr.query.InvalidQueryException iqe) {
+ throw new QuerySyntaxException(iqe.getMessage(), query, language,
iqe);
+ } catch (final RepositoryException re) {
+ throw new SlingException(re.getMessage(), re);
+ }
+ }
- return new Iterator<ValueMap>() {
Review Comment:
yeah... because the original code is imho extra hard to read:
original:
```
@Override
public Iterator<ValueMap> queryResources(final @NotNull
ResolveContext<JcrProviderState> ctx,
final String query,
final String language) {
final String queryLanguage =
ArrayUtils.contains(getSupportedLanguages(ctx), language) ? language :
DEFAULT_QUERY_LANGUAGE;
try {
final QueryResult result =
JcrResourceUtil.query(ctx.getProviderState().getSession(), query,
queryLanguage);
final String[] colNames = result.getColumnNames();
final RowIterator rows = result.getRows();
return new Iterator<ValueMap>() {
private ValueMap next;
{
next = seek();
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public ValueMap next() {
if (next == null) {
throw new NoSuchElementException();
}
final ValueMap result = next;
next = seek();
return result;
}
private ValueMap seek() {
ValueMap result = null;
while (result == null && rows.hasNext()) {
try {
final Row jcrRow = rows.nextRow();
final String resourcePath = jcrRow.getPath();
if (resourcePath != null &&
providerContext.getExcludedPaths().matches(resourcePath) == null) {
final Map<String, Object> row = new
HashMap<>();
boolean didPath = false;
boolean didScore = false;
final Value[] values = jcrRow.getValues();
for (int i = 0; i < values.length; i++) {
Value v = values[i];
if (v != null) {
String colName = colNames[i];
row.put(colName,
JcrResourceUtil.toJavaObject(values[i]));
if
(colName.equals(QUERY_COLUMN_PATH)) {
didPath = true;
row.put(colName,
JcrResourceUtil.toJavaObject(values[i]).toString());
}
if
(colName.equals(QUERY_COLUMN_SCORE)) {
didScore = true;
}
}
}
if (!didPath) {
row.put(QUERY_COLUMN_PATH,
jcrRow.getPath());
}
if (!didScore) {
row.put(QUERY_COLUMN_SCORE,
jcrRow.getScore());
}
result = new ValueMapDecorator(row);
}
} catch (final RepositoryException re) {
logger.error("queryResources$next: Problem
accessing row values", re);
}
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (final javax.jcr.query.InvalidQueryException iqe) {
throw new QuerySyntaxException(iqe.getMessage(), query,
language, iqe);
} catch (final RepositoryException re) {
throw new SlingException(re.getMessage(), re);
}
}
```
new code:
```
@Override
public Iterator<ValueMap> queryResources(final
ResolveContext<JcrProviderState> ctx,
final String query,
final String language) {
final String queryLanguage =
ArrayUtils.contains(getSupportedLanguages(ctx), language) ? language :
DEFAULT_QUERY_LANGUAGE;
try {
final QueryResult result =
JcrResourceUtil.query(ctx.getProviderState().getSession(), query,
queryLanguage);
return new ValueMapIterator(result.getColumnNames(),
result.getRows());
} catch (final javax.jcr.query.InvalidQueryException iqe) {
throw new QuerySyntaxException(iqe.getMessage(), query,
language, iqe);
} catch (final RepositoryException re) {
throw new SlingException(re.getMessage(), re);
}
}
private class ValueMapIterator implements Iterator<ValueMap> {
private final String[] colNames;
private final RowIterator rows;
private ValueMap next;
private ValueMapIterator(@NotNull String[] colNames, @NotNull
RowIterator rows) {
this.colNames = colNames;
this.rows = rows;
next = seek();
}
@Override
public boolean hasNext() {
return next != null;
}
@Override
public ValueMap next() {
if ( next == null ) {
throw new NoSuchElementException();
}
final ValueMap result = next;
next = seek();
return result;
}
private ValueMap seek() {
ValueMap result = null;
while (result == null && rows.hasNext()) {
try {
final Row jcrRow = rows.nextRow();
final String resourcePath = jcrRow.getPath();
if (resourcePath != null &&
providerContext.getExcludedPaths().matches(resourcePath) == null) {
result = new ValueMapDecorator(getRow(jcrRow));
}
} catch (final RepositoryException re) {
logger.error("queryResources$next: Problem accessing row
values", re);
}
}
return result;
}
private Map<String, Object> getRow(@NotNull Row jcrRow) throws
RepositoryException {
final Map<String, Object> row = new HashMap<>();
boolean didPath = false;
boolean didScore = false;
final Value[] values = jcrRow.getValues();
for (int i = 0; i < values.length; i++) {
Value v = values[i];
if (v != null) {
String colName = colNames[i];
row.put(colName,
JcrResourceUtil.toJavaObject(values[i]));
if (colName.equals(QUERY_COLUMN_PATH)) {
didPath = true;
row.put(colName,
JcrResourceUtil.toJavaObject(values[i]).toString());
}
if (colName.equals(QUERY_COLUMN_SCORE)) {
didScore = true;
}
}
}
if (!didPath) {
row.put(QUERY_COLUMN_PATH, jcrRow.getPath());
}
if (!didScore) {
row.put(QUERY_COLUMN_SCORE, jcrRow.getScore());
}
return row;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]