Search in sources :

Example 26 with EntityReference

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);
}
Also used : EntityRequest(org.xwiki.refactoring.job.EntityRequest) EntityReference(org.xwiki.model.reference.EntityReference) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 27 with EntityReference

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);
}
Also used : EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) EntityReference(org.xwiki.model.reference.EntityReference) JobContext(org.xwiki.job.JobContext) JobProgressManager(org.xwiki.job.event.status.JobProgressManager)

Example 28 with EntityReference

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);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference) SecureQuery(org.xwiki.query.SecureQuery) QueryException(org.xwiki.query.QueryException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 29 with EntityReference

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;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference)

Example 30 with EntityReference

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());
        }
    }
}
Also used : EntityType(org.xwiki.model.EntityType) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

EntityReference (org.xwiki.model.reference.EntityReference)338 Test (org.junit.Test)157 DocumentReference (org.xwiki.model.reference.DocumentReference)107 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)39 BaseObject (com.xpn.xwiki.objects.BaseObject)38 ArrayList (java.util.ArrayList)27 XWikiContext (com.xpn.xwiki.XWikiContext)24 WikiReference (org.xwiki.model.reference.WikiReference)24 SpaceReference (org.xwiki.model.reference.SpaceReference)23 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)18 XWikiException (com.xpn.xwiki.XWikiException)17 EntityType (org.xwiki.model.EntityType)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 XDOM (org.xwiki.rendering.block.XDOM)8 URL (java.net.URL)7 AttachmentReference (org.xwiki.model.reference.AttachmentReference)7 HashMap (java.util.HashMap)6 Before (org.junit.Before)6