Search in sources :

Example 21 with QueryException

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

the class DefaultInstanceModel method getDocumentReferences.

@Override
public List<DocumentReference> getDocumentReferences(SpaceReference spaceReference) throws FilterException {
    List<String> documentNames;
    try {
        Query query = this.queryManager.createQuery("select distinct doc.name from Document doc where doc.space = :space order by doc.name asc", Query.XWQL);
        query.bindValue("space", localSerializer.serialize(spaceReference));
        query.setWiki(spaceReference.getWikiReference().getName());
        documentNames = query.execute();
    } catch (QueryException e) {
        throw new FilterException(String.format("Failed to get the list of documents in space [%s]", spaceReference), e);
    }
    List<DocumentReference> documentReferences = new ArrayList<>(documentNames.size());
    for (String documentName : documentNames) {
        documentReferences.add(new DocumentReference(documentName, spaceReference));
    }
    return documentReferences;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) FilterException(org.xwiki.filter.FilterException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 22 with QueryException

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

the class HqlQueryExecutorTest method executeWhenStoreException.

@Test
public void executeWhenStoreException() throws Exception {
    XWikiException exception = mock(XWikiException.class);
    when(exception.getMessage()).thenReturn("nestedmessage");
    when(this.store.executeRead(any(XWikiContext.class), any(XWikiHibernateBaseStore.HibernateCallback.class))).thenThrow(exception);
    try {
        execute("statement", null);
        fail("Should have thrown an exception here");
    } catch (QueryException expected) {
        assertEquals("Exception while executing query. Query statement = [statement]", expected.getMessage());
        // Verify nested exception!
        assertEquals("nestedmessage", expected.getCause().getMessage());
    }
}
Also used : QueryException(org.xwiki.query.QueryException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) Test(org.junit.Test)

Example 23 with QueryException

use of org.xwiki.query.QueryException in project celements-blog by celements.

the class NewsletterReceivers method addNewsletterReceiver.

void addNewsletterReceiver(XWikiDocument blogDoc) throws XWikiException {
    String blogFN = getWebUtilsService().serializeRef(blogDoc.getDocumentReference(), true);
    String xwql = "from doc.object(Celements.NewsletterReceiverClass) as nr " + "where nr.isactive = '1' and nr.subscribed = :subscribed";
    // String hql = "select nr.email,doc.date from Celements.NewsletterReceiverClass as nr, "
    // + " XWikiDocument as doc "
    // + "where doc.fullName = "
    // + " nr.isactive='1' "
    // + "and subscribed='" + blogDoc.getFullName() + "'";
    // List<String> nlRegAddresses = context.getWiki().search(hql, context);
    DocumentReference receverClassRef = getBlogClasses().getNewsletterReceiverClassRef(getContext().getDatabase());
    try {
        List<String> nlRegReceiverList = Utils.getComponent(QueryManager.class).createQuery(xwql, Query.XWQL).bindValue("subscribed", blogFN).execute();
        if (nlRegReceiverList != null) {
            LOGGER.info("Found " + nlRegReceiverList.size() + " Celements.NewsletterReceiverClass" + " object-subscriptions for blog " + blogFN);
            String blogSpace = blogDoc.getXObject(getBlogClasses().getBlogConfigClassRef(getContext().getDatabase())).getStringValue("blogspace");
            for (String nlRegReceiverFN : nlRegReceiverList) {
                DocumentReference nlRegReceiverDocRef = getWebUtilsService().resolveDocumentReference(nlRegReceiverFN);
                XWikiDocument receiverDoc = getContext().getWiki().getDocument(nlRegReceiverDocRef, getContext());
                List<BaseObject> recieverObjs = receiverDoc.getXObjects(receverClassRef);
                for (BaseObject receiverObj : recieverObjs) {
                    String subscribedBlogs = receiverObj.getStringValue("subscribed");
                    if ((subscribedBlogs != null) && (("," + subscribedBlogs + ",").indexOf("," + blogFN + ",") >= 0)) {
                        String address = receiverObj.getStringValue("email");
                        address = address.toLowerCase();
                        if (!allAddresses.contains(address)) {
                            String language = receiverObj.getStringValue("language");
                            String firstname = "";
                            String name = "";
                            BaseObject contactObj = receiverDoc.getXObject(new DocumentReference(getContext().getWiki().getDatabase(), "Celements", "ContactClass"));
                            if (contactObj != null) {
                                firstname = contactObj.getStringValue("firstname");
                                name = contactObj.getStringValue("lastname");
                            }
                            if (getWebUtilsService().getAllowedLanguages(blogSpace).contains(language)) {
                                addrLangs.add(new String[] { "XWiki.XWikiGuest", address, language, firstname, name });
                            } else {
                                addresses.add(address);
                            }
                            allAddresses.add(address);
                            emailAddressDateList.add(new EmailAddressDate(address, receiverDoc.getDate(), language));
                            LOGGER.info("reveiver added: " + address);
                        }
                    }
                }
            }
        }
    } catch (QueryException exp) {
        LOGGER.error("Failed to execute newsletter receiver xwql [" + xwql + "].", exp);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) QueryManager(org.xwiki.query.QueryManager) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 24 with QueryException

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

the class DefaultMessageStream method getRecentDirectMessages.

@Override
public List<Event> getRecentDirectMessages(int limit, int offset) {
    List<Event> result = new ArrayList<Event>();
    try {
        Query q = this.qm.createQuery("where event.application = 'MessageStream' and event.type = 'directMessage'" + " and event.stream = :targetUser order by event.date desc", Query.XWQL);
        q.bindValue("targetUser", this.serializer.serialize(this.bridge.getCurrentUserReference()));
        q.setLimit(limit > 0 ? limit : 30).setOffset(offset >= 0 ? offset : 0);
        result = this.stream.searchEvents(q);
    } catch (QueryException ex) {
        LOG.warn("Failed to search direct messages: {}", ex.getMessage());
    }
    return result;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) Event(org.xwiki.eventstream.Event)

Example 25 with QueryException

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

the class DefaultMessageStream method getRecentMessagesForGroup.

@Override
public List<Event> getRecentMessagesForGroup(DocumentReference group, int limit, int offset) {
    List<Event> result = new ArrayList<Event>();
    try {
        Query q = this.qm.createQuery("where event.application = 'MessageStream' and event.type = 'groupMessage'" + " and event.stream = :group order by event.date desc", Query.XWQL);
        q.bindValue("group", this.serializer.serialize(group));
        q.setLimit(limit > 0 ? limit : 30).setOffset(offset >= 0 ? offset : 0);
        result = this.stream.searchEvents(q);
    } catch (QueryException ex) {
        LOG.warn("Failed to search group messages: {}", ex.getMessage());
    }
    return result;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) ArrayList(java.util.ArrayList) Event(org.xwiki.eventstream.Event)

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