Search in sources :

Example 1 with XWikiLink

use of com.xpn.xwiki.doc.XWikiLink in project xwiki-platform by xwiki.

the class XWikiHibernateStore method loadLinks.

// ---------------------------------------
// Links
// ---------------------------------------
@Override
public List<XWikiLink> loadLinks(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    List<XWikiLink> links = new ArrayList<XWikiLink>();
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(false, context);
        }
        Session session = getSession(context);
        Query query = session.createQuery(" from XWikiLink as link where link.id.docId = :docId");
        query.setLong("docId", docId);
        links = query.list();
        if (bTransaction) {
            endTransaction(context, false, false);
            bTransaction = false;
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_LINKS, "Exception while loading links", e);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
    return links;
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) XWikiLink(com.xpn.xwiki.doc.XWikiLink) Session(org.hibernate.Session)

Example 2 with XWikiLink

use of com.xpn.xwiki.doc.XWikiLink in project xwiki-platform by xwiki.

the class XWikiHibernateStore method saveLinks.

@Override
public void saveLinks(XWikiDocument doc, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // need to delete existing links before saving the page's one
        deleteLinks(doc.getId(), context, bTransaction);
        // necessary to blank links from doc
        context.remove("links");
        // Extract the links.
        Set<XWikiLink> links = new LinkedHashSet<>();
        // Add wiki syntax links.
        // FIXME: replace with doc.getUniqueWikiLinkedPages(context) when OldRendering is dropped.
        links.addAll(this.oldRenderingProvider.get().extractLinks(doc, context));
        // Add included pages.
        List<String> includedPages = doc.getIncludedPages(context);
        for (String includedPage : includedPages) {
            XWikiLink wikiLink = new XWikiLink();
            wikiLink.setDocId(doc.getId());
            wikiLink.setFullName(this.localEntityReferenceSerializer.serialize(doc.getDocumentReference()));
            wikiLink.setLink(includedPage);
            links.add(wikiLink);
        }
        // Save the links.
        for (XWikiLink wikiLink : links) {
            // Verify that the link reference isn't larger than 255 characters (and truncate it if that's the case)
            // since otherwise that would lead to a DB error that would result in a fatal error, and the user would
            // have a hard time understanding why his page failed to be saved.
            wikiLink.setLink(StringUtils.substring(wikiLink.getLink(), 0, 255));
            session.save(wikiLink);
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_LINKS, "Exception while saving links", e);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session) XWikiLink(com.xpn.xwiki.doc.XWikiLink)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiLink (com.xpn.xwiki.doc.XWikiLink)2 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)2 SQLException (java.sql.SQLException)2 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)2 Session (org.hibernate.Session)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 InitializationException (org.xwiki.component.phase.InitializationException)2 QueryException (org.xwiki.query.QueryException)2 UnexpectedException (org.xwiki.store.UnexpectedException)2 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Query (org.hibernate.Query)1