1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store.legacy.doc.internal

File DeletedFilesystemAttachment.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

12
24
8
1
153
74
15
0.62
3
8
1.88

Classes

Class Line # Actions
DeletedFilesystemAttachment 38 24 0% 15 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.store.legacy.doc.internal;
21   
22    import java.util.Date;
23   
24    import com.xpn.xwiki.XWikiContext;
25    import com.xpn.xwiki.XWikiException;
26    import com.xpn.xwiki.doc.DeletedAttachment;
27    import com.xpn.xwiki.doc.XWikiAttachment;
28    import com.xpn.xwiki.doc.XWikiAttachmentArchive;
29    import com.xpn.xwiki.doc.XWikiAttachmentContent;
30   
31    /**
32    * Filesystem based Archive of deleted attachment, stored in
33    * {@link org.xwiki.store.legacy.store.internal.FilesystemAttachmentRecycleBinStore}.
34    *
35    * @version $Id: dbbf88c29fb83d734dc5024a40964ada826d75d9 $
36    * @since 3.0M3
37    */
 
38    public class DeletedFilesystemAttachment extends DeletedAttachment
39    {
40    /**
41    * The attachment which was deleted.
42    */
43    private XWikiAttachment attachment;
44   
45    /**
46    * Protected Constructor. Used by MutableDeletedFilesystemAttachment.
47    */
 
48  0 toggle protected DeletedFilesystemAttachment()
49    {
50    }
51   
52    /**
53    * A constructor with all the information about the deleted attachment.
54    *
55    * @param attachment Deleted attachment.
56    * @param deleter User which deleted the attachment.
57    * @param deleteDate Date of delete action.
58    * @throws XWikiException is never thrown, we just have to declare it in order to call the super constructor
59    */
 
60  0 toggle public DeletedFilesystemAttachment(final XWikiAttachment attachment, final String deleter, final Date deleteDate)
61    throws XWikiException
62    {
63  0 super(attachment, deleter, deleteDate, null);
64    }
65   
 
66  0 toggle @Override
67    public long getDocId()
68    {
69    // TODO deprecate me.
70  0 if (this.attachment != null && this.attachment.getDoc() != null) {
71  0 return this.attachment.getDocId();
72    }
73  0 return 0;
74    }
75   
76    /**
77    * {@inheritDoc}
78    * <p>
79    * The XWiki context is unused and may safely be null.
80    * </p>
81    *
82    * @see com.xpn.xwiki.doc.DeletedAttachment#setAttachment(XWikiAttachment, XWikiContext)
83    */
 
84  0 toggle @Override
85    protected void setAttachment(final XWikiAttachment attachment, final XWikiContext context)
86    {
87  0 this.attachment = (XWikiAttachment) attachment.clone();
88  0 if (this.getDate() != null) {
89  0 this.setId(generateId(this.attachment, this.getDate()));
90    }
91    }
92   
 
93  0 toggle @Override
94    public void setDate(Date date)
95    {
96  0 super.setDate(date);
97  0 if (this.getAttachment() != null) {
98  0 this.setId(generateId(this.getAttachment(), date));
99    }
100    }
101   
102    /**
103    * Get the attachment. This does not clone the attachment. To get a clone, use {@link
104    * #restoreAttachment(XWikiAttachment, XWikiContext)}
105    *
106    * @return the attachment which was deleted.
107    */
 
108  0 toggle public XWikiAttachment getAttachment()
109    {
110  0 return this.attachment;
111    }
112   
 
113  0 toggle @Override
114    public XWikiAttachment restoreAttachment(final XWikiAttachment attachment, final XWikiContext context)
115    throws XWikiException
116    {
117  0 XWikiAttachment result = attachment;
118  0 if (result != null) {
119    // TODO Add XWikiAttachment#clone(XWikiAttachment)
120    // this toXML does not copy content.
121  0 result.fromXML(this.attachment.toXML(context));
122  0 if (this.attachment.getAttachment_content() != null) {
123  0 attachment.setAttachment_content((XWikiAttachmentContent) this.attachment.getAttachment_content()
124    .clone());
125  0 attachment.getAttachment_content().setAttachment(attachment);
126    }
127  0 if (this.attachment.getAttachment_archive() != null) {
128  0 result.setAttachment_archive((XWikiAttachmentArchive) this.attachment.getAttachment_archive().clone());
129  0 result.getAttachment_archive().setAttachment(result);
130    }
131    } else {
132  0 result = (XWikiAttachment) this.attachment.clone();
133    }
134   
135  0 result.setDoc(context.getWiki().getDocument(this.attachment.getReference().getDocumentReference(), context));
136  0 return result;
137    }
138   
139    /**
140    * Generate an ID which will be as collision resistant as possible. Because {@link
141    * com.xpn.xwiki.doc.XWikiAttachment#getId()} returns an int cast to a long, this ID is guaranteed to be unique
142    * unless the same attachment is deleted twice in the same second or again in a second which will come around in
143    * another 136 years.
144    *
145    * @param attachment the attachment to get an ID number for.
146    * @param deleteDate the Date the attachment was deleted.
147    * @return an ID number for this deleted attachment.
148    */
 
149  0 toggle private static long generateId(final XWikiAttachment attachment, final Date deleteDate)
150    {
151  0 return (attachment.getId() << 32) ^ ((deleteDate.getTime() / 1000) & 0x00000000FFFFFFFFL);
152    }
153    }