1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.wiki.creationjob.internal

File WikiCreationJob.java

 

Coverage histogram

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

Code metrics

0
12
3
1
111
61
4
0.33
4
3
1.33

Classes

Class Line # Actions
WikiCreationJob 52 12 0% 4 0
1.0100%
 

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.platform.wiki.creationjob.internal;
21   
22    import java.util.Collections;
23    import java.util.Comparator;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29   
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.annotation.InstantiationStrategy;
32    import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.job.AbstractJob;
35    import org.xwiki.job.DefaultJobStatus;
36    import org.xwiki.model.reference.DocumentReferenceResolver;
37    import org.xwiki.platform.wiki.creationjob.WikiCreationException;
38    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
39    import org.xwiki.platform.wiki.creationjob.WikiCreationStep;
40   
41    import com.xpn.xwiki.XWikiContext;
42   
43    /**
44    * Job that create a wiki and execute the WikiCreationSteps.
45    *
46    * @version $Id: 3e52fe2da92d1cfef3816d3b064a49b929a4ce40 $
47    * @since 7.0M2
48    */
49    @Component
50    @InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
51    @Named(WikiCreationJob.JOB_TYPE)
 
52    public class WikiCreationJob extends AbstractJob<WikiCreationRequest, DefaultJobStatus<WikiCreationRequest>>
53    {
54    /**
55    * The prefix put behind all job ids.
56    */
57    public static final String JOB_ID_PREFIX = "wikicreation";
58   
59    /**
60    * The job type.
61    */
62    public static final String JOB_TYPE = "wikicreationjob";
63   
64    @Inject
65    private Provider<XWikiContext> xcontextProvider;
66   
67    @Inject
68    private DocumentReferenceResolver<String> defaultDocumentReferenceResolver;
69   
 
70  5 toggle @Override
71    protected void runInternal() throws Exception
72    {
73  5 try {
74    // Consider that the owner id is a serialized user reference and put it in the context as the current user
75    // so that all steps are executed under that user.
76  5 this.xcontextProvider.get().setUserReference(
77    this.defaultDocumentReferenceResolver.resolve(getRequest().getOwnerId()));
78   
79  5 List<WikiCreationStep> wikiCreationStepList = componentManager.getInstanceList(WikiCreationStep.class);
80    // Some extra steps needs to be executed AFTER some others, so we have introduce a getOrder() method in the
81    // interface. We use this method to sort the list of extra steps by this order.
82  5 Collections.sort(wikiCreationStepList, new Comparator<WikiCreationStep>()
83    {
 
84  20 toggle @Override
85    public int compare(WikiCreationStep o1, WikiCreationStep o2)
86    {
87  20 return o1.getOrder() - o2.getOrder();
88    }
89    });
90    // Now we can execute these extra steps
91  5 this.progressManager.pushLevelProgress(wikiCreationStepList.size(), this);
92   
93  5 for (WikiCreationStep step : wikiCreationStepList) {
94  16 this.progressManager.startStep(this);
95   
96  16 step.execute(request);
97    }
98   
99  4 this.progressManager.popLevelProgress(this);
100    } catch (WikiCreationException | ComponentLookupException e) {
101  1 throw new WikiCreationException(
102    String.format("Failed to execute creation steps on the wiki [%s].", request.getWikiId()), e);
103    }
104    }
105   
 
106  26 toggle @Override
107    public String getType()
108    {
109  26 return JOB_TYPE;
110    }
111    }