Search in sources :

Example 36 with QueryException

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

the class RepositoryManager method resolveAuthorIdOnWiki.

private String resolveAuthorIdOnWiki(String wiki, String authorName, String[] authorElements, XWikiContext xcontext) {
    Query query;
    try {
        query = this.queryManager.createQuery("from doc.object(XWiki.XWikiUsers) as user" + " where user.first_name like :userfirstname OR user.last_name like :userlastname", Query.XWQL);
        query.bindValue("userfirstname", '%' + authorElements[0] + '%');
        query.bindValue("userlastname", '%' + authorElements[authorElements.length - 1] + '%');
        query.setWiki(wiki);
        List<String> documentNames = query.execute();
        if (!documentNames.isEmpty()) {
            WikiReference wikiReference = new WikiReference(wiki);
            for (String documentName : documentNames) {
                DocumentReference documentReference = this.currentStringResolver.resolve(documentName, wikiReference);
                String userDisplayName = xcontext.getWiki().getPlainUserName(documentReference, xcontext);
                if (userDisplayName.equals(authorName)) {
                    return documentName;
                }
            }
        }
    } catch (QueryException e) {
        this.logger.error("Failed to resolve extension author [{}]", authorName, e);
    }
    return null;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 37 with QueryException

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

the class DefaultIconSetManagerTest method getIconSetNamesWhenException.

@Test
public void getIconSetNamesWhenException() throws Exception {
    // Mocks
    QueryException exception = new QueryException("exception in the query", null, null);
    when(queryManager.createQuery(any(), eq(Query.XWQL))).thenThrow(exception);
    // Test
    IconException caughtException = null;
    try {
        mocker.getComponentUnderTest().getIconSetNames();
    } catch (IconException e) {
        caughtException = e;
    }
    // Verify
    assertNotNull(caughtException);
    assertEquals("Failed to get the name of all icon sets.", caughtException.getMessage());
    assertEquals(exception, caughtException.getCause());
}
Also used : QueryException(org.xwiki.query.QueryException) IconException(org.xwiki.icon.IconException) Test(org.junit.Test)

Example 38 with QueryException

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

the class ScopeNotificationFilterClassMigrator method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    try {
        // Get every document having at least one NotificationPreferenceScopeClass XObject attached
        Query query = queryManager.createQuery("select distinct doc.fullName from Document doc, " + "doc.object(XWiki.Notifications.Code.NotificationPreferenceScopeClass) as obj", Query.XWQL);
        // Migrate each document to use the new XObject
        for (String result : query.<String>execute()) {
            DocumentReference userReference = resolver.resolve(result);
            XWikiDocument userDocument = xwiki.getDocument(userReference, context);
            logger.debug("Migrating document [{}]...", result);
            try {
                migrateDocument(userDocument);
            } catch (XWikiException e) {
                logger.warn("Failed to migrate document [{}].", result, e);
            }
        }
    // When every documents have been migrated, we used to delete the old XClass,
    // but it is not a good idea to delete a document in a migrator, when stores are not ready (such as
    // recycle bin) and when other modules are not ready (like the SOLR server).
    // See: https://jira.xwiki.org/browse/XWIKI-14749
    // So we don't do anything.
    } catch (QueryException e) {
        logger.error("Failed to perform a query on the current wiki.", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 39 with QueryException

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

the class DefaultInstanceModel method getSpaceReferences.

@Override
public EntityReferenceTreeNode getSpaceReferences(WikiReference wikiReference) throws FilterException {
    // Get the spaces
    List<String> spaceReferenceStrings;
    try {
        spaceReferenceStrings = this.queryManager.getNamedQuery("getSpaces").setWiki(wikiReference.getName()).execute();
    } catch (QueryException e) {
        throw new FilterException(String.format("Failed to get the list of spaces in wiki [%s]", wikiReference), e);
    }
    // Get references
    List<SpaceReference> spaceReferences = new ArrayList<>(spaceReferenceStrings.size());
    for (String spaceReferenceString : spaceReferenceStrings) {
        spaceReferences.add(this.spaceResolver.resolve(spaceReferenceString, wikiReference));
    }
    // Create the tree
    EntityReferenceTree tree = new EntityReferenceTree(spaceReferences);
    return tree.getChildren().iterator().next();
}
Also used : QueryException(org.xwiki.query.QueryException) EntityReferenceTree(org.xwiki.model.reference.EntityReferenceTree) SpaceReference(org.xwiki.model.reference.SpaceReference) ArrayList(java.util.ArrayList) FilterException(org.xwiki.filter.FilterException)

Example 40 with QueryException

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

the class XWikiGroupServiceImpl method getAllMatchedMembersNamesForGroup.

@Override
public Collection<String> getAllMatchedMembersNamesForGroup(String group, String matchField, int nb, int start, Boolean orderAsc, XWikiContext context) throws XWikiException {
    // TODO: add cache mechanism.
    XWikiDocument groupDocument = new XWikiDocument(this.currentMixedDocumentReferenceResolver.resolve(group));
    Map<String, Object> parameterValues = new HashMap<String, Object>();
    // //////////////////////////////////////
    // Create the query string
    StringBuilder queryString = new StringBuilder("SELECT field.value");
    queryString.append(' ').append(createMatchGroupMembersWhereClause(groupDocument.getFullName(), matchField, orderAsc, parameterValues));
    try {
        // //////////////////////////////////////
        // Create the query
        QueryManager qm = context.getWiki().getStore().getQueryManager();
        Query query = qm.createQuery(queryString.toString(), Query.HQL);
        for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
            query.bindValue(entry.getKey(), entry.getValue());
        }
        query.setOffset(start);
        query.setLimit(nb);
        query.setWiki(groupDocument.getDocumentReference().getWikiReference().getName());
        return query.execute();
    } catch (QueryException ex) {
        throw new XWikiException(0, 0, ex.getMessage(), ex);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) HashMap(java.util.HashMap) QueryManager(org.xwiki.query.QueryManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

QueryException (org.xwiki.query.QueryException)57 Query (org.xwiki.query.Query)32 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiException (com.xpn.xwiki.XWikiException)18 ArrayList (java.util.ArrayList)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)8 Test (org.junit.Test)8 QueryFilter (org.xwiki.query.QueryFilter)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 HashMap (java.util.HashMap)6 XWikiRestException (org.xwiki.rest.XWikiRestException)6 Event (org.xwiki.eventstream.Event)5 WikiReference (org.xwiki.model.reference.WikiReference)5 Date (java.util.Date)4 List (java.util.List)4 IconException (org.xwiki.icon.IconException)4 QueryManager (org.xwiki.query.QueryManager)4 SecureQuery (org.xwiki.query.SecureQuery)4 XWiki (com.xpn.xwiki.XWiki)3