@Override public boolean storeEntity(DeletedAttachment entity) { if (entity == null) { return true; } try { XWikiAttachment attachment = entity.restoreAttachment(null, this.context.get()); this.store.saveToRecycleBin(attachment, entity.getDeleter(), entity.getDate(), this.context.get(), false); this.logger.debug("Imported deleted attachment [{}@{}#{}] into the filesystem trash store", entity.getDocName(), entity.getFilename(), entity.getId()); return true; } catch (XWikiException ex) { this.logger.error("Failed to store deleted attachment into the filesystem store: {}", ex.getMessage(), ex); return false; } } }
@Override public DeletedAttachment next() { Long item = this.data.next(); try { DeletedAttachment result = HibernateDeletedAttachmentsReader.this.store.getDeletedAttachment( item, HibernateDeletedAttachmentsReader.this.context.get(), true); HibernateDeletedAttachmentsReader.this.logger.debug("Loaded [{}@{}#{}] from the database trash", result.getDocName(), result.getFilename(), result.getId()); return result; } catch (Exception ex) { HibernateDeletedAttachmentsReader.this.logger.error( "Failed to read deleted attachment from the database store: {}", ex.getMessage(), ex); } return null; }
if (databaseId != null) { dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null, databaseId); session.update(dbAttachment); } else { dbAttachment = new DeletedAttachment(docId, this.serializer.serialize(documentReference), filename, FileSystemStoreUtils.HINT, deleter, deleteDate, null); databaseId = (Long) session.save(dbAttachment); new File(directory.getParentFile(), encode(dbAttachment.getFilename() + "-id" + databaseId)); FileUtils.moveDirectory(directory, newDirectory);
/** * Retrieve the original name of this attachment. * * @return the original filename, for example {@code MyPhoto.png} */ public String getFilename() { return this.deletedAttachment.getFilename(); }
public long findDeletedAttachmentForDocRevision(XWikiDocument doc, String docRevision, String filename, XWikiContext context) throws XWikiException { XWikiAttachment attachment = null; XWikiDocument rdoc = context.getWiki().getDocument(doc, docRevision, context); if (context.getWiki().hasAttachmentRecycleBin(context) && filename != null) { attachment = rdoc.getAttachment(filename); if (attachment != null) { List<DeletedAttachment> deleted = context.getWiki().getAttachmentRecycleBinStore() .getAllDeletedAttachments(attachment, context, true); Collections.reverse(deleted); for (DeletedAttachment entry : deleted) { if (entry.getDate().after(rdoc.getDate())) { return entry.getId(); } } } } return -1; } }
public XWikiAttachment doInHibernate(Session session) throws HibernateException, XWikiException { try { DeletedAttachment trashAttachment = (DeletedAttachment) session.load(DeletedAttachment.class, Long.valueOf(index)); return trashAttachment.restoreAttachment(attachment, context); } catch (Exception ex) { // Invalid recycle entry. return null; } } });
DeletedAttachment correctVariant = null; for (DeletedAttachment variant : deletedVariants) { // Reverse chronological order if (variant.getDate().before(rolledbackDoc.getDate())) { break; XWikiAttachment restoredAttachment = correctVariant.restoreAttachment(null, context); XWikiAttachment restoredAttachmentRevision = restoredAttachment.getAttachmentRevision(attachmentToRestore.getVersion(), context);
/** * Restore a {@link XWikiAttachment} from a {@link DeletedAttachment}. Note that this method does not actually * restore the attachment to its owner document, it simply recomposes an {@link XWikiAttachment} object from the * saved data. * * @return restored attachment * @param attachment optional object where to put the attachment data, if not <code>null</code> * @param context the current {@link XWikiContext context} * @throws XWikiException If an exception occurs while the Attachment is restored from the XML. See * {@link XWikiAttachment#fromXML(String)}. */ public XWikiAttachment restoreAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException { XWikiAttachment result = attachment; if (result == null) { result = new XWikiAttachment(); } result.fromXML(getXml()); if (result.getDoc() == null || !(this.getDocName().equals(result.getDoc().getFullName()))) { result.setDoc(context.getWiki().getDocument(this.getDocName(), context)); } return result; } }
/** * Retrieve the name of the document this attachment belonged to. * * @return the name of the owner document, in the {@code Space.Document} format */ public String getDocName() { return this.deletedAttachment.getDocName(); }
/** * Retrieve the internal entry index, used to uniquely identify this entity in the trash. This is needed because a * file can be attached and deleted multiple times, so the document name and filename are not enough to uniquely * identify a deleted attachment. * * @return internal identifier of the corresponding trash entry */ public long getId() { return this.deletedAttachment.getId(); }
/** * Retrieve the name of the user who deleted this attachment. * * @return the user who deleted the attachment, as its document name (e.g. {@code XWiki.Admin}) */ public String getDeleter() { return this.deletedAttachment.getDeleter(); }
/** * Retrieve the date and time this attachment has been deleted. * * @return the date of the deletion */ public Date getDate() { return this.deletedAttachment.getDate(); }
/** * {@inheritDoc} */ public void saveToRecycleBin(XWikiAttachment attachment, String deleter, Date date, XWikiContext context, boolean bTransaction) throws XWikiException { final DeletedAttachment trashAtachment = new DeletedAttachment(attachment, deleter, date, context); executeWrite(context, bTransaction, new HibernateCallback<Object>() { public Object doInHibernate(Session session) throws HibernateException { session.save(trashAtachment); return null; } }); }
@Override public DeletedAttachment next() { Long item = this.data.next(); try { DeletedAttachment result = HibernateDeletedAttachmentsReader.this.store.getDeletedAttachment( item, HibernateDeletedAttachmentsReader.this.context.get(), true); HibernateDeletedAttachmentsReader.this.logger.debug("Loaded [{}@{}#{}] from the database trash", result.getDocName(), result.getFilename(), result.getId()); return result; } catch (Exception ex) { HibernateDeletedAttachmentsReader.this.logger.error( "Failed to read deleted attachment from the database store: {}", ex.getMessage(), ex); } return null; }
/** * {@inheritDoc} * <p> * bTransaction is ignored by this implementation. * </p> * * @see AttachmentRecycleBinStore#restoreFromRecycleBin(XWikiAttachment, long, XWikiContext, boolean) */ @Override public XWikiAttachment restoreFromRecycleBin(final XWikiAttachment attachment, final long index, final XWikiContext context, boolean bTransaction) throws XWikiException { final DeletedAttachment delAttach = getDeletedAttachment(index, context, false); return delAttach != null ? delAttach.restoreAttachment(attachment, context) : null; }
+ "immediately after it has been deleted from the wiki"); if (!da.getDocName().equals(doc.getFullName())) { throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION,
@Override public boolean storeEntity(DeletedAttachment entity) { if (entity == null) { return true; } try { XWikiAttachment attachment = entity.restoreAttachment(null, this.context.get()); this.store.saveToRecycleBin(attachment, entity.getDeleter(), entity.getDate(), this.context.get(), false); this.logger.debug("Imported deleted attachment [{}@{}#{}] into the filesystem trash store", entity.getDocName(), entity.getFilename(), entity.getId()); return true; } catch (XWikiException ex) { this.logger.error("Failed to store deleted attachment into the filesystem store: {}", ex.getMessage(), ex); return false; } } }
@Override public boolean discardEntity(DeletedAttachment entity) { boolean transaction = false; try { transaction = ((XWikiHibernateBaseStore) this.store).beginTransaction(this.context.get()); Session session = ((XWikiHibernateBaseStore) this.store).getSession(this.context.get()); session.delete(entity); this.logger.debug("Deleted deleted attachment [{}@{}#{}] from the database trash", entity.getDocName(), entity.getFilename(), entity.getId()); } catch (XWikiException ex) { this.logger.warn("Failed to cleanup attachment from the database trash: {}", ex.getMessage()); return false; } finally { if (transaction) { ((XWikiHibernateBaseStore) this.store).endTransaction(this.context.get(), transaction); } } return true; }
/** * Access to the real attachment object. * * @return the attachment as it was before being deleted, and as it currently is in the recycle bin */ public Attachment getAttachment() { try { Document doc = this.context.getWiki().getDocument(getDocName(), this.context).newDocument(this.context); return new Attachment(doc, this.deletedAttachment.restoreAttachment(null, this.context), this.context); } catch (XWikiException ex) { LOG.warn("Failed to parse deleted attachment: " + ex.getMessage(), ex); return null; } }
@Override public boolean discardEntity(DeletedAttachment entity) { boolean transaction = false; try { transaction = ((XWikiHibernateBaseStore) this.store).beginTransaction(this.context.get()); Session session = ((XWikiHibernateBaseStore) this.store).getSession(this.context.get()); session.delete(entity); this.logger.debug("Deleted deleted attachment [{}@{}#{}] from the database trash", entity.getDocName(), entity.getFilename(), entity.getId()); } catch (XWikiException ex) { this.logger.warn("Failed to cleanup attachment from the database trash: {}", ex.getMessage()); return false; } finally { if (transaction) { ((XWikiHibernateBaseStore) this.store).endTransaction(this.context.get(), transaction); } } return true; }