Search in sources :

Example 11 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class QueryGeneratorTest method setUp.

@Before
public void setUp() throws Exception {
    queryManager = mocker.getInstance(QueryManager.class);
    notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
    serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    userPreferencesSource = mocker.getInstance(ConfigurationSource.class, "user");
    wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
    notificationFilterManager = mocker.getInstance(NotificationFilterManager.class);
    startDate = new Date(10);
    query = mock(Query.class);
    when(queryManager.createQuery(anyString(), anyString())).thenReturn(query);
    pref1StartDate = new Date(100000000);
    NotificationPreference pref1 = mock(NotificationPreference.class);
    when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
    when(pref1.getFormat()).thenReturn(NotificationFormat.ALERT);
    when(pref1.getStartDate()).thenReturn(pref1StartDate);
    when(pref1.isNotificationEnabled()).thenReturn(true);
    when(notificationPreferenceManager.getPreferences(userReference, true, NotificationFormat.ALERT)).thenReturn(Arrays.asList(pref1));
    NotificationFilterPreference fakeFilterPreference = mock(NotificationFilterPreference.class);
    when(fakeFilterPreference.isActive()).thenReturn(true);
    when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(fakeFilterPreference));
    when(userPreferencesSource.getProperty("displayHiddenDocuments", 0)).thenReturn(0);
    when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
}
Also used : NotificationFilterManager(org.xwiki.notifications.filters.NotificationFilterManager) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) Query(org.xwiki.query.Query) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) QueryManager(org.xwiki.query.QueryManager) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 12 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class DefaultNotificationManager method getEvents.

private List<CompositeEvent> getEvents(List<CompositeEvent> results, Parameters parameters) throws NotificationException {
    // Because the user might not be able to see all notifications because of the rights, we take from the database
    // more events than expected and we will filter afterwards.
    final int batchSize = parameters.expectedCount * 2;
    try {
        // Create the query
        Query query = queryGenerator.generateQuery(parameters.userReference, parameters.format, parameters.endDate, parameters.fromDate, parameters.blackList);
        if (query == null) {
            return Collections.emptyList();
        }
        query.setLimit(batchSize);
        // Get a batch of events
        List<Event> batch = eventStream.searchEvents(query);
        // Add to the results the events the user has the right to see
        for (Event event : batch) {
            DocumentReference document = event.getDocument();
            // Don't record events concerning a doc the user cannot see
            if (document != null && !authorizationManager.hasAccess(Right.VIEW, parameters.userReference, document)) {
                continue;
            }
            if (filterEvent(event, parameters)) {
                continue;
            }
            // Record this event
            recordEvent(results, event);
            // If the expected count is reached, stop now
            if (results.size() >= parameters.expectedCount) {
                return results;
            }
        }
        // If we haven't get the expected number of events, perform a new batch
        if (results.size() < parameters.expectedCount && batch.size() == batchSize) {
            parameters.blackList.addAll(getEventsIds(batch));
            getEvents(results, parameters);
        }
        return results;
    } catch (Exception e) {
        throw new NotificationException("Fail to get the list of notifications.", e);
    }
}
Also used : Query(org.xwiki.query.Query) NotificationException(org.xwiki.notifications.NotificationException) CompositeEvent(org.xwiki.notifications.CompositeEvent) Event(org.xwiki.eventstream.Event) DocumentReference(org.xwiki.model.reference.DocumentReference) NotificationException(org.xwiki.notifications.NotificationException)

Example 13 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class ExplicitlyAllowedValuesDBListQueryBuilder method build.

@Override
public Query build(DBListClass dbListClass) throws QueryException {
    String statement = dbListClass.getSql();
    DocumentReference authorReference = dbListClass.getOwnerDocument().getAuthorReference();
    if (this.authorizationManager.hasAccess(Right.SCRIPT, authorReference, dbListClass.getReference())) {
        String namespace = this.entityReferenceSerializer.serialize(dbListClass.getDocumentReference());
        try {
            statement = this.authorExecutor.call(() -> {
                return evaluateVelocityCode(dbListClass.getSql(), namespace);
            }, authorReference);
        } catch (Exception e) {
            this.logger.warn("Failed to evaluate the Velocity code from the query [{}]." + " Root cause is [{}]. Continuing with the raw query.", statement, ExceptionUtils.getRootCauseMessage(e));
        }
    }
    Query query = this.secureQueryManager.createQuery(statement, Query.HQL);
    query.setWiki(dbListClass.getOwnerDocument().getDocumentReference().getWikiReference().getName());
    return query;
}
Also used : Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) QueryException(org.xwiki.query.QueryException)

Example 14 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class XClassMigratorListener method migrate.

private void migrate(PropertyClass newPropertyClass) throws QueryException {
    ClassPropertyReference propertyReference = newPropertyClass.getReference();
    EntityReference classReference = propertyReference.extractReference(EntityType.DOCUMENT);
    EntityReference wikiReference = propertyReference.extractReference(EntityType.WIKI);
    // Get all document containing object of modified class
    Query query = this.queryManager.createQuery("from doc.object(" + this.localSerializer.serialize(classReference) + ") as obj", Query.XWQL);
    query.setWiki(wikiReference.getName());
    List<String> documents = query.execute();
    if (!documents.isEmpty()) {
        XWikiContext xcontext = this.xcontextProvider.get();
        String currentWikiId = xcontext.getWikiId();
        try {
            // Switch to class wiki to be safer
            xcontext.setWikiId(wikiReference.getName());
            for (String documentName : documents) {
                try {
                    migrate(newPropertyClass, documentName, xcontext);
                } catch (XWikiException e) {
                    this.logger.error("Failed to migrate property [{}] in document [{}]", propertyReference, documentName, xcontext);
                }
            }
        } finally {
            // Restore context wiki
            xcontext.setWikiId(currentWikiId);
        }
    }
}
Also used : Query(org.xwiki.query.Query) EntityReference(org.xwiki.model.reference.EntityReference) XWikiContext(com.xpn.xwiki.XWikiContext) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 15 with Query

use of org.xwiki.query.Query in project xwiki-platform by xwiki.

the class DefaultGroupsQueryBuilder method build.

@Override
public Query build(GroupsClass groupsClass) throws QueryException {
    // We select distinct results because the document that defines the group has multiple XWiki.XWikiGroups
    // objects, one for each group member. We order by document full name because the group document title is often
    // empty. We could use coalesce and nullif functions to fall back on the document name when the title is empty
    // but it complicates too much the query (and we can have a lot of XWiki.XWikiGroups objects, more than the
    // number of users). We have to select the lower case version of the group reference in order to be able to use
    // it in the order by clause (otherwise we get "PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions
    // must appear in select list").
    String statement = new StringBuilder("select distinct doc.fullName as groupReference, doc.title as groupName,").append(" lower(doc.fullName) as lowerGroupReference ").append("from XWikiDocument doc, BaseObject obj ").append("where doc.fullName = obj.name and obj.className = 'XWiki.XWikiGroups'").append(" and doc.space = 'XWiki' and doc.name <> 'XWikiGroupTemplate' ").append("order by lowerGroupReference, groupReference").toString();
    Query query = this.queryManager.createQuery(statement, Query.HQL);
    // Resolve the document full name from the first column into a DocumentReference (the group profile reference).
    query.addFilter(this.documentFilter);
    // Remove the groups whose profile page is not viewable by the current user.
    query.addFilter(this.viewableFilter);
    query.setWiki(groupsClass.getOwnerDocument().getDocumentReference().getWikiReference().getName());
    return query;
}
Also used : Query(org.xwiki.query.Query)

Aggregations

Query (org.xwiki.query.Query)129 DocumentReference (org.xwiki.model.reference.DocumentReference)41 Test (org.junit.Test)39 QueryException (org.xwiki.query.QueryException)36 ArrayList (java.util.ArrayList)29 XWikiException (com.xpn.xwiki.XWikiException)18 QueryFilter (org.xwiki.query.QueryFilter)18 QueryManager (org.xwiki.query.QueryManager)18 XWikiContext (com.xpn.xwiki.XWikiContext)15 HashMap (java.util.HashMap)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 Map (java.util.Map)11 List (java.util.List)9 Before (org.junit.Before)9 SecureQuery (org.xwiki.query.SecureQuery)9 XWiki (com.xpn.xwiki.XWiki)8 BaseObject (com.xpn.xwiki.objects.BaseObject)8 SQLQuery (org.hibernate.SQLQuery)8 WikiReference (org.xwiki.model.reference.WikiReference)8 WrappingQuery (org.xwiki.query.WrappingQuery)8