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

File AttachmentArchiveSaveRunnable.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

8
16
2
1
122
63
8
0.5
8
2
4

Classes

Class Line # Actions
AttachmentArchiveSaveRunnable 49 16 0% 8 5
0.807692380.8%
 

Contributing tests

This file is covered by 3 tests. .

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.store.internal;
21   
22    import java.io.File;
23    import java.util.ArrayList;
24    import java.util.List;
25   
26    import com.xpn.xwiki.doc.XWikiAttachment;
27    import com.xpn.xwiki.doc.XWikiAttachmentArchive;
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.store.VoidAttachmentVersioningStore;
31   
32    import org.suigeneris.jrcs.rcs.Version;
33    import org.xwiki.store.FileSaveTransactionRunnable;
34    import org.xwiki.store.StartableTransactionRunnable;
35    import org.xwiki.store.StreamProvider;
36    import org.xwiki.store.filesystem.internal.AttachmentFileProvider;
37    import org.xwiki.store.filesystem.internal.FilesystemStoreTools;
38    import org.xwiki.store.serialization.SerializationStreamProvider;
39    import org.xwiki.store.serialization.Serializer;
40   
41    /**
42    * A TransactionRunnable for saving attachment archives.
43    * It uses a chain of FileSaveTransactionRunnable so the attachment will either be saved or fail
44    * safely, it should not hang in a halfway state.
45    *
46    * @version $Id: db93ccd14eadf1b977d59506c452633f5fb6b2d0 $
47    * @since 3.0M2
48    */
 
49    public class AttachmentArchiveSaveRunnable extends StartableTransactionRunnable
50    {
51    /**
52    * The Constructor.
53    *
54    * @param archive the attachment archive to save.
55    * @param fileTools a set of tools for getting the file corrisponding to each version of the
56    * attachment content and the file for the meta data, as well as temporary
57    * and backup files corrisponding to each. Also for getting locks.
58    * @param provider the means to get the files to store each version of the attachment.
59    * @param serializer an attachment list metadata serializer for serializing the metadata of each
60    * version of the attachment.
61    * @param context the XWikiContext used to get the revisions of the attachment.
62    * @throws XWikiException if it is unable to get a revision of an attachment using archive.getRevision()
63    */
 
64  3 toggle public AttachmentArchiveSaveRunnable(final XWikiAttachmentArchive archive,
65    final FilesystemStoreTools fileTools,
66    final AttachmentFileProvider provider,
67    final Serializer<List<XWikiAttachment>,
68    List<XWikiAttachment>> serializer,
69    final XWikiContext context)
70    throws XWikiException
71    {
72  3 if (archive instanceof VoidAttachmentVersioningStore.VoidAttachmentArchive) {
73  0 return;
74    }
75  3 if (archive.getVersions().length == 0 && archive.getAttachment() != null) {
76  0 archive.updateArchive(context);
77    }
78   
79  3 final Version[] versions = archive.getVersions();
80  3 final List<XWikiAttachment> attachmentVersions = new ArrayList<XWikiAttachment>(versions.length);
81   
82    // Add the content files which need updating and add the attachments to the list.
83  12 for (int i = 0; i < versions.length; i++) {
84  9 final String versionName = versions[i].toString();
85  9 final XWikiAttachment attachVer =
86    archive.getRevision(archive.getAttachment(), versionName, context);
87  9 attachmentVersions.add(attachVer);
88   
89    // If the content is not dirty and the file was already saved then we will not update.
90  9 if (attachVer.isContentDirty()
91    || !provider.getAttachmentVersionContentFile(versionName).exists())
92    {
93  9 final StreamProvider contentProvider =
94    new AttachmentContentStreamProvider(attachVer, context);
95  9 addSaver(contentProvider, fileTools, provider.getAttachmentVersionContentFile(versionName));
96    }
97    }
98   
99    // Then do the metadata.
100  3 final StreamProvider metaProvider =
101    new SerializationStreamProvider<List<XWikiAttachment>>(serializer, attachmentVersions);
102  3 addSaver(metaProvider, fileTools, provider.getAttachmentVersioningMetaFile());
103    }
104   
105    /**
106    * Save some content safely in this runnable.
107    *
108    * @param provider the means to get the content to save.
109    * @param fileTools the means to get the backup file, temporary file, and lock.
110    * @param saveHere the location to save the data.
111    */
 
112  12 toggle private void addSaver(final StreamProvider provider,
113    final FilesystemStoreTools fileTools,
114    final File saveHere)
115    {
116  12 new FileSaveTransactionRunnable(saveHere,
117    fileTools.getTempFile(saveHere),
118    fileTools.getBackupFile(saveHere),
119    fileTools.getLockForFile(saveHere),
120    provider).runIn(this);
121    }
122    }