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;
}
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();
}
}
Aggregations