@Override public Object clone() { final ListAttachmentArchive out = new ListAttachmentArchive(cloneAttachment(this.attachment)); out.attachment.setAttachment_archive(out); for (XWikiAttachment revision : this.revisions) { final XWikiAttachment revClone = cloneAttachment(revision); revClone.setAttachment_archive(out); out.revisions.add(revClone); } return out; }
/** * Clone an attachment but not it's archive.{@link ListAttachmentArchive#clone()} calls {@link * XWikiAttachment#clone()} and if the attachment is associated with an archive it call clone the archive. This * function prevents an infinite loop. * * @param original an attachment to clone. * @return a clone of original which has no XWikiAttachmentArchive attached. */ private static XWikiAttachment cloneAttachment(final XWikiAttachment original) { final XWikiAttachmentArchive arch = original.getAttachment_archive(); try { original.setAttachment_archive(null); return (XWikiAttachment) original.clone(); } finally { original.setAttachment_archive(arch); } }
@Override public Object clone() { final ListAttachmentArchive out = new ListAttachmentArchive(cloneAttachment(this.attachment)); out.attachment.setAttachment_archive(out); for (XWikiAttachment revision : this.revisions) { final XWikiAttachment revClone = cloneAttachment(revision); revClone.setAttachment_archive(out); out.revisions.add(revClone); } return out; }
/** * Clone an attachment but not it's archive.{@link ListAttachmentArchive#clone()} calls {@link * XWikiAttachment#clone()} and if the attachment is associated with an archive it call clone the archive. This * function prevents an infinite loop. * * @param original an attachment to clone. * @return a clone of original which has no XWikiAttachmentArchive attached. */ private static XWikiAttachment cloneAttachment(final XWikiAttachment original) { final XWikiAttachmentArchive arch = original.getAttachment_archive(); try { original.setAttachment_archive(null); return (XWikiAttachment) original.clone(); } finally { original.setAttachment_archive(arch); } }
/** * {@inheritDoc} */ public XWikiAttachmentArchive loadArchive(XWikiAttachment attachment, XWikiContext context, boolean transaction) throws XWikiException { XWikiAttachmentArchive archive = attachment.getAttachment_archive(); if (!(archive instanceof VoidAttachmentArchive)) { archive = new VoidAttachmentArchive(attachment); } attachment.setAttachment_archive(archive); return archive; }
/** * Update the archive, increment the attachment version, set the date on the attachment and add the attachment to * the list. */ private void update() { final XWikiAttachment attach = this.getAttachment(); attach.incrementVersion(); attach.setDate(new Date()); // Clone the attachment but don't clone this archive. final XWikiAttachment clone = cloneAttachment(attach); clone.setAttachment_archive(this); this.revisions.add(clone); }
/** * Constructor from List. Create a new instance of ListAttachmentArchive from a list of attachments. * * @param revisions a List of XWikiAttachment revisions to put in this archive. All revisions are the same * attachment and thus must have the same ID. */ public ListAttachmentArchive(final List<XWikiAttachment> revisions) { // Empty list: if (revisions.isEmpty()) { return; } this.revisions.addAll(revisions); Collections.sort(this.revisions, XWikiAttachmentVersionComparator.INSTANCE); // Sanity check, all revisions should have the same ID. long id = revisions.get(0).getId(); final String firstAttachName = revisions.get(0).getFilename(); for (XWikiAttachment attach : revisions) { if (attach.getId() != id) { throw new IllegalArgumentException("Attachment " + attach.getFilename() + " has a " + "different ID than the first attachment ( " + firstAttachName + " ) so they cannot all be " + "revisions of the same attachment."); } attach.setAttachment_archive(this); } // Set the attachment for this archive to the latest version. this.attachment = this.revisions.get(revisions.size() - 1); }
/** * Constructor from List. Create a new instance of ListAttachmentArchive from a list of attachments. * * @param revisions a List of XWikiAttachment revisions to put in this archive. All revisions are the same * attachment and thus must have the same ID. */ public ListAttachmentArchive(final List<XWikiAttachment> revisions) { // Empty list: if (revisions.size() == 0) { return; } this.revisions.addAll(revisions); Collections.sort(this.revisions, XWikiAttachmentVersionComparitor.INSTANCE); // Sanity check, all revisions should have the same ID. long id = revisions.get(0).getId(); final String firstAttachName = revisions.get(0).getFilename(); for (XWikiAttachment attach : revisions) { if (attach.getId() != id) { throw new IllegalArgumentException("Attachment " + attach.getFilename() + " has a " + "different ID than the first attachment ( " + firstAttachName + " ) so they cannot all be " + "revisions of the same attachment."); } attach.setAttachment_archive(this); } // Set the attachment for this archive to the latest version. this.attachment = this.revisions.get(revisions.size() - 1); }
/** * Update the archive, increment the attachment version, set the date on the attachment and add the attachment to * the list. */ private void update() { final XWikiAttachment attach = this.getAttachment(); attach.incrementVersion(); attach.setDate(new Date()); // Clone the attachment but don't clone this archive. final XWikiAttachment clone = cloneAttachment(attach); clone.setAttachment_archive(this); this.revisions.add(clone); }
@Override public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context) { if (rev == null) { return null; } for (XWikiAttachment attach : this.revisions) { if (rev.equals(attach.getVersion())) { final XWikiAttachment out = cloneAttachment(attach); out.setAttachment_archive(this); // This is silly, we set the attachment document and passed value. // Keeping to maintain current behavior. out.setDoc(attachment.getDoc(), false); return out; } } return null; }
@Override public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context) { if (rev == null) { return null; } for (XWikiAttachment attach : this.revisions) { if (rev.equals(attach.getVersion())) { final XWikiAttachment out = cloneAttachment(attach); out.setAttachment_archive(this); // This is silly, we set the attachment document and passed value. // Keeping to maintain current behavior. out.setDoc(attachment.getDoc()); return out; } } return null; }
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(this.getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
attachment.setContentStore(FileSystemStoreUtils.HINT); attachment.setAttachment_archive( ((FilesystemAttachmentVersioningStore) this.attachmentVersionStore).loadArchive(attachment, provider)); attachment.setArchiveStore(FileSystemStoreUtils.HINT);
/** * @param rcsArchive the RCS archive to import. * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails */ private void fromRCS(final Archive rcsArchive) throws Exception { if (rcsArchive == null) { return; } final Node[] nodes = rcsArchive.changeLog(); for (int i = nodes.length - 1; i > -1; i--) { final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion()); final StringBuilder content = new StringBuilder(); for (int j = 0; j < lines.length; j++) { String line = lines[j].toString(); content.append(line); if (j != lines.length - 1) { content.append("\n"); } } final XWikiAttachment rev = new XWikiAttachment(); rev.fromXML(content.toString()); rev.setDoc(getAttachment().getDoc()); rev.setAttachment_archive(this); // this should not be necessary, keeping to maintain behavior. rev.setVersion(nodes[i].getVersion().toString()); revisions.add(rev); } }
attachment.setAttachment_content(new FilesystemAttachmentContent(contentFile, attachment)); attachment.setAttachment_archive( ((FilesystemAttachmentVersioningStore) this.attachmentVersionStore) .loadArchive(attachment, provider));
/** * {@inheritDoc} */ public XWikiAttachmentArchive loadArchive(final XWikiAttachment attachment, XWikiContext context, boolean bTransaction) throws XWikiException { try { final XWikiAttachmentArchive archive = new XWikiAttachmentArchive(); archive.setAttachment(attachment); executeRead(context, bTransaction, new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { try { session.load(archive, archive.getId()); } catch (ObjectNotFoundException e) { // if none found then return empty created archive } return null; } }); attachment.setAttachment_archive(archive); return archive; } catch (Exception e) { Object[] args = {attachment.getFilename(), attachment.getDoc().getFullName()}; throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_ATTACHMENT, "Exception while loading attachment archive {0} of document {1}", e, args); } }
restoredAttachmentRevision.setAttachment_archive(restoredAttachment.getAttachment_archive()); restoredAttachmentRevision.getAttachment_archive().setAttachment(restoredAttachmentRevision); restoredAttachmentRevision.setVersion(restoredAttachment.getVersion());
attachment.setAttachment_archive((XWikiAttachmentArchive) getAttachment_archive().clone()); attachment.getAttachment_archive().setAttachment(attachment);
@Override public XWikiAttachment restoreAttachment(final XWikiAttachment attachment, final XWikiContext context) throws XWikiException { XWikiAttachment result = attachment; if (result != null) { // TODO Add XWikiAttachment#clone(XWikiAttachment) // this toXML does not copy content. result.fromXML(this.attachment.toXML(context)); if (this.attachment.getAttachment_content() != null) { attachment.setAttachment_content((XWikiAttachmentContent) this.attachment.getAttachment_content() .clone()); attachment.getAttachment_content().setAttachment(attachment); } if (this.attachment.getAttachment_archive() != null) { result.setAttachment_archive((XWikiAttachmentArchive) this.attachment.getAttachment_archive().clone()); result.getAttachment_archive().setAttachment(result); } } else { result = (XWikiAttachment) this.attachment.clone(); } result.setDoc(context.getWiki().getDocument(this.attachment.getReference().getDocumentReference(), context)); return result; }