1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.refactoring.internal.job

File AbstractDeletedDocumentsJob.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
33
6
1
157
90
11
0.33
5.5
6
1.83

Classes

Class Line # Actions
AbstractDeletedDocumentsJob 48 33 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 11 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.refactoring.internal.job;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.xwiki.job.AbstractJob;
29    import org.xwiki.job.AbstractJobStatus;
30    import org.xwiki.job.GroupedJob;
31    import org.xwiki.job.JobGroupPath;
32    import org.xwiki.job.Request;
33    import org.xwiki.job.api.AbstractCheckRightsRequest;
34    import org.xwiki.model.ModelContext;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.model.reference.WikiReference;
37    import org.xwiki.refactoring.internal.ModelBridge;
38    import org.xwiki.refactoring.job.AbstractDeletedDocumentsRequest;
39    import org.xwiki.refactoring.job.RefactoringJobs;
40   
41    /**
42    * Define an abstract job that handles deleted documents.
43    * Typically this class has been created to abstract mechanism of {@link RestoreJob} and {@link PermanentlyDeleteJob}.
44    *
45    * @version $Id: 3fd729d38361c759ebf6b89ed43d9ffa2333d321 $
46    * @since 10.10RC1
47    */
 
48    public abstract class AbstractDeletedDocumentsJob extends AbstractJob<AbstractDeletedDocumentsRequest,
49    AbstractJobStatus<AbstractDeletedDocumentsRequest>> implements GroupedJob
50    {
51    static final JobGroupPath ROOT_GROUP = new JobGroupPath(RefactoringJobs.GROUP, null);
52   
53    @Inject
54    protected ModelBridge modelBridge;
55   
56    @Inject
57    protected ModelContext modelContext;
58   
59    private JobGroupPath groupPath;
60   
 
61  14 toggle @Override
62    protected AbstractJobStatus<AbstractDeletedDocumentsRequest> createNewStatus(
63    AbstractDeletedDocumentsRequest request)
64    {
65  14 AbstractJobStatus<AbstractDeletedDocumentsRequest> status = super.createNewStatus(request);
66   
67  14 status.setCancelable(true);
68   
69  14 return status;
70    }
71   
 
72  12 toggle @Override
73    protected void runInternal() throws Exception
74    {
75  12 AbstractDeletedDocumentsRequest request = getRequest();
76   
77  12 initializeContext(request);
78   
79  10 this.progressManager.pushLevelProgress(2, this);
80   
81  10 this.progressManager.startStep(this);
82   
83    // Read the request and build the final list of IDs to restore.
84  10 List<Long> idsToRestore = getIdsToHandle(request);
85   
86  10 this.progressManager.startStep(this);
87   
88    // Process each ID and try to restore it.
89  10 handleDeletedDocuments(idsToRestore, request);
90   
91  10 this.progressManager.popLevelProgress(this);
92    }
93   
94    protected abstract void handleDeletedDocuments(List<Long> idsDeletedDocuments, AbstractCheckRightsRequest request);
95   
 
96  12 toggle private void initializeContext(AbstractDeletedDocumentsRequest request) throws IllegalArgumentException
97    {
98    // Set the context user to the one that made the request.
99  12 DocumentReference userReference = request.getUserReference();
100  12 modelBridge.setContextUserReference(userReference);
101   
102    // Set the context wiki to the one specified in the request.
103    // All DeletedDocuments APIs, specifically the IDs (single document or batch), are local to the current wiki.
104  12 WikiReference wikiReference = request.getWikiReference();
105    // If the wiki is not specified, then we can not continue.
106  12 if (wikiReference == null) {
107  2 throw new IllegalArgumentException("No wiki reference was specified in the job request");
108    }
109  10 modelContext.setCurrentEntityReference(wikiReference);
110    }
111   
 
112  10 toggle private List<Long> getIdsToHandle(AbstractDeletedDocumentsRequest request)
113    {
114  10 List<Long> result = new ArrayList<>();
115   
116    // Expand the batch in individual deleted document IDs.
117  10 String batchId = request.getBatchId();
118  10 if (StringUtils.isNotBlank(batchId)) {
119  4 List<Long> batchDeletedDocumentIds = modelBridge.getDeletedDocumentIds(batchId);
120  4 result.addAll(batchDeletedDocumentIds);
121    }
122   
123    // Merge any individually specified IDs, if they are not already in the batch.
124  10 List<Long> deletedDocumentIds = request.getDeletedDocumentIds();
125  10 if (deletedDocumentIds != null) {
126  8 for (long deletedDocumentId : deletedDocumentIds) {
127  11 if (!result.contains(deletedDocumentId)) {
128  9 result.add(deletedDocumentId);
129    }
130    }
131    }
132   
133  10 return result;
134    }
135   
 
136  5 toggle @Override
137    public JobGroupPath getGroupPath()
138    {
139  5 return groupPath;
140    }
141   
 
142  14 toggle @Override
143    public void initialize(Request request)
144    {
145  14 super.initialize(request);
146   
147    // Build the job group path.
148    // Note: Because of the nature of the AbstractDeletedDocumentsJobs that works with with DeletedDocument IDs and
149    // BatchIDs (and not with EntityReferences), the only way we can try to avoid executing operation at the same
150    // time on the same reference is to use a group path at the wiki level, hoping most restore operations are fast
151    // and do not block for long.
152  14 WikiReference wikiReference = ((AbstractDeletedDocumentsRequest) request).getWikiReference();
153  14 if (wikiReference != null) {
154  12 this.groupPath = new JobGroupPath(wikiReference.getName(), ROOT_GROUP);
155    }
156    }
157    }