Search in sources :

Example 56 with QueryException

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

the class ArticleNullDatesMigrator method migrate.

@Override
public void migrate(SubSystemHibernateMigrationManager manager, XWikiContext context) throws XWikiException {
    try {
        List<String> results = queryManager.createQuery(getXWQL(), Query.XWQL).execute();
        LOGGER.debug("Got results for xwql '" + getXWQL() + "': " + results);
        if (results != null) {
            for (String fullName : results) {
                DocumentReference docRef = webUtilsService.resolveDocumentReference(fullName);
                context.getWiki().saveDocument(context.getWiki().getDocument(docRef, context), "article null dates migration", context);
            }
        }
    } catch (QueryException qexc) {
        LOGGER.error("Exception executing query: " + getXWQL(), qexc);
    }
}
Also used : QueryException(org.xwiki.query.QueryException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 57 with QueryException

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

the class NewsletterReceiversRemoveDoublesMigrator method migrate.

@Override
public void migrate(SubSystemHibernateMigrationManager manager, XWikiContext context) throws XWikiException {
    DocumentReference recObjRef = new DocumentReference(context.getDatabase(), "Celements", "NewsletterReceiverClass");
    String xwql = "from doc.object(Celements.NewsletterReceiverClass) as obj";
    try {
        List<String> receivers = queryManager.createQuery(xwql, Query.XWQL).execute();
        Map<String, Object[]> receiversMap = new HashMap<>();
        if (receivers != null) {
            for (String docName : receivers) {
                if (docName != null) {
                    XWikiDocument recDoc = context.getWiki().getDocument(getWebUtils().resolveDocumentReference(docName), context);
                    BaseObject recObj = recDoc.getXObject(recObjRef);
                    String recMail = recObj.getStringValue("email");
                    int isActive = recObj.getIntValue("isactive", 0);
                    if (!"".equals(recMail)) {
                        String recSubscr = recObj.getStringValue("subscribed");
                        String recKey = recMail + "," + recSubscr;
                        if (receiversMap.containsKey(recKey)) {
                            int duplActive = (Integer) receiversMap.get(recKey)[0];
                            XWikiDocument duplDoc = (XWikiDocument) receiversMap.get(recKey)[1];
                            if (!recDoc.getDocumentReference().equals(duplDoc.getDocumentReference())) {
                                if ((isActive == 1) || (duplActive != 1)) {
                                    recDoc.removeXObject(recObj);
                                    context.getWiki().saveDocument(recDoc, "Removed douplicate", context);
                                } else {
                                    duplDoc.removeXObject((BaseObject) receiversMap.get(recKey)[2]);
                                    context.getWiki().saveDocument(duplDoc, "Removed douplicate", context);
                                    receiversMap.put(recMail + "," + recSubscr, new Object[] { isActive, recDoc, recObj });
                                }
                            }
                            LOGGER.info("Remove duplicate of " + recMail + " - " + recSubscr);
                        } else {
                            receiversMap.put(recMail + "," + recSubscr, new Object[] { isActive, recDoc, recObj });
                        }
                    } else {
                        LOGGER.info("Empty NewsletterReceiver found on doc " + recDoc.getDocumentReference());
                    }
                }
            }
        }
    } catch (QueryException qe) {
        LOGGER.error("Exception cleaning duplicate newsletter receivers", qe);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

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