use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class EntityJobTest method processEachEntity.
@Test
public void processEachEntity() {
final List<EntityReference> references = new ArrayList<>();
NoopEntityJob job = new NoopEntityJob() {
@Override
protected void process(EntityReference entityReference) {
references.add(entityReference);
}
};
DocumentReference documentReference = new DocumentReference("chess", Arrays.asList("Path", "To"), "Success");
EntityRequest request = new EntityRequest();
request.setEntityReferences(documentReference.getReversedReferenceChain());
initialize(job, request);
job.run();
assertEquals(request.getEntityReferences(), references);
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class EntityJobTest method initialize.
private void initialize(NoopEntityJob job, EntityRequest request) {
ReflectionUtils.setFieldValue(job, "jobContext", mock(JobContext.class));
ReflectionUtils.setFieldValue(job, "progressManager", mock(JobProgressManager.class));
ReflectionUtils.setFieldValue(job, "authorization", this.authorization);
ReflectionUtils.setFieldValue(job, "modelBridge", this.modelBridge);
EntityReferenceProvider defaultEntityReferenceProvider = mock(EntityReferenceProvider.class);
ReflectionUtils.setFieldValue(job, "defaultEntityReferenceProvider", defaultEntityReferenceProvider);
when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("WebHome", EntityType.DOCUMENT, null));
job.initialize(request);
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class XWQLQueryExecutor method execute.
@Override
public <T> List<T> execute(Query query) throws QueryException {
EntityReference currentEntityReference = this.context.getCurrentEntityReference();
Query nativeQuery;
try {
this.progress.startStep(query, "query.xwql.progress.execute", "Execute XWQL query [{}]", query);
if (query.getWiki() != null) {
if (currentEntityReference.getType() == EntityType.WIKI) {
this.context.setCurrentEntityReference(new WikiReference(query.getWiki()));
} else {
this.context.setCurrentEntityReference(currentEntityReference.replaceParent(currentEntityReference.extractReference(EntityType.WIKI), new WikiReference(query.getWiki())));
}
}
nativeQuery = getQueryManager().createQuery(this.translator.translate(query.getStatement()), this.translator.getOutputLanguage());
nativeQuery.setLimit(query.getLimit());
nativeQuery.setOffset(query.getOffset());
nativeQuery.setWiki(query.getWiki());
if (query.getFilters() != null) {
for (QueryFilter filter : query.getFilters()) {
nativeQuery.addFilter(filter);
}
}
for (Entry<String, Object> e : query.getNamedParameters().entrySet()) {
nativeQuery.bindValue(e.getKey(), e.getValue());
}
for (Entry<Integer, Object> e : query.getPositionalParameters().entrySet()) {
nativeQuery.bindValue(e.getKey(), e.getValue());
}
if (nativeQuery instanceof SecureQuery && query instanceof SecureQuery) {
// No need to validate the HQL query for short XWQL queries
if (((SecureQuery) query).isCurrentAuthorChecked() && !isShortFormStatement(query.getStatement())) {
((SecureQuery) nativeQuery).checkCurrentAuthor(true);
}
// Let HQL module take care of that is supported
((SecureQuery) nativeQuery).checkCurrentUser(((SecureQuery) query).isCurrentUserChecked());
}
return nativeQuery.execute();
} catch (Exception e) {
if (e instanceof QueryException) {
throw (QueryException) e;
}
throw new QueryException("Exception while translating [" + query.getStatement() + "] XWQL query to the [" + this.translator.getOutputLanguage() + "] language", query, e);
} finally {
this.context.setCurrentEntityReference(currentEntityReference);
this.progress.endStep(query);
}
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class AbstractEntityJob method getCommonParent.
/**
* Return the deepest common parents shared by all concerned entities. For example, if we have:
* <ul>
* <li>A.B.C</li>
* <li>A.B.D</li>
* </ul>
* the results will be A.B.
*
* @param entityReferences a collection of entities
* @return the deepest common parent or null if there is no common ancestor
* @since 9.1RC1
*/
protected EntityReference getCommonParent(Collection<EntityReference> entityReferences) {
if (entityReferences == null || entityReferences.isEmpty()) {
return null;
}
Iterator<EntityReference> iterator = entityReferences.iterator();
EntityReference commonParent = iterator.next();
while (iterator.hasNext()) {
EntityReference entity = iterator.next();
while (!entity.hasParent(commonParent)) {
commonParent = commonParent.getParent();
if (commonParent == null) {
return null;
}
}
}
return commonParent;
}
use of org.xwiki.model.reference.EntityReference in project xwiki-platform by xwiki.
the class AbstractEntityJob method visitDocumentNodes.
private void visitDocumentNodes(EntityReferenceTreeNode node, Visitor<DocumentReference> visitor) {
if (node != null) {
EntityReference nodeReference = node.getReference();
EntityType nodeType = nodeReference != null ? nodeReference.getType() : null;
if (nodeType == EntityType.SPACE || nodeType == EntityType.WIKI || nodeType == null) {
// A node that corresponds to an entity that can contain documents.
visitDocumentAncestor(node, visitor);
} else if (nodeType == EntityType.DOCUMENT) {
visitor.visit((DocumentReference) node.getReference());
}
}
}
Aggregations