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

File CreateJob.java

 

Coverage histogram

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

Code metrics

20
47
7
1
161
106
24
0.51
6.71
7
3.43

Classes

Class Line # Actions
CreateJob 42 47 0% 24 2
0.97297397.3%
 

Contributing tests

This file is covered by 10 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 javax.inject.Named;
23   
24    import org.xwiki.component.annotation.Component;
25    import org.xwiki.model.EntityType;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.model.reference.EntityReference;
28    import org.xwiki.model.reference.SpaceReference;
29    import org.xwiki.refactoring.job.CreateRequest;
30    import org.xwiki.refactoring.job.EntityJobStatus;
31    import org.xwiki.refactoring.job.RefactoringJobs;
32    import org.xwiki.security.authorization.Right;
33   
34    /**
35    * A job that can create entities (optionally, from a template).
36    *
37    * @version $Id: 09ac2cf46beda6da980f4d9a06dcac79e1640c57 $
38    * @since 7.4M2
39    */
40    @Component
41    @Named(RefactoringJobs.CREATE)
 
42    public class CreateJob extends AbstractEntityJob<CreateRequest, EntityJobStatus<CreateRequest>>
43    {
 
44  135 toggle @Override
45    public String getType()
46    {
47  135 return RefactoringJobs.CREATE;
48    }
49   
 
50  27 toggle @Override
51    protected EntityJobStatus<CreateRequest> createNewStatus(CreateRequest request)
52    {
53  27 return new EntityJobStatus<>(request, this.observationManager, this.loggerManager, null);
54    }
55   
 
56  27 toggle @Override
57    protected void process(EntityReference entityReference)
58    {
59    // Dispatch the create operation based on the entity type.
60   
61  27 switch (entityReference.getType()) {
62  25 case DOCUMENT:
63  25 process(new DocumentReference(entityReference));
64  25 break;
65  2 case SPACE:
66  2 process(new SpaceReference(entityReference));
67  2 break;
68  0 default:
69  0 this.logger.error("Unsupported entity type [{}].", entityReference.getType());
70    }
71    }
72   
 
73  25 toggle private void process(DocumentReference documentReference)
74    {
75    // The template, if specified, must be a document.
76  25 DocumentReference templateDocumentReference = null;
77  25 if (this.request.getTemplateReference() != null
78    && this.request.getTemplateReference().extractReference(EntityType.DOCUMENT) != null) {
79  21 templateDocumentReference =
80    new DocumentReference(this.request.getTemplateReference().extractReference(EntityType.DOCUMENT));
81    }
82   
83  25 progressManager.pushLevelProgress(3, this);
84   
85  25 try {
86  25 progressManager.startStep(this, "Main document");
87   
88    // Handle the target document creation first
89  25 maybeCreate(documentReference, templateDocumentReference);
90   
91  25 progressManager.startStep(this, "Template children");
92   
93    // If both the target and template documents are non-terminal and also the operation is deep, handle
94    // possible children of the template document.
95  25 if (this.request.isDeep() && isSpaceHomeReference(documentReference) && templateDocumentReference != null
96    && isSpaceHomeReference(templateDocumentReference)) {
97  1 process(documentReference.getLastSpaceReference());
98    }
99   
100  25 progressManager.startStep(this, "Remove lock from the main document");
101   
102    // Remove any existing lock of the user that started this job on the target document.
103  25 this.modelBridge.removeLock(documentReference);
104    } finally {
105    // Done, go back to parent progress level
106  25 this.progressManager.popLevelProgress(this);
107    }
108    }
109   
 
110  3 toggle private void process(final SpaceReference newSpaceReference)
111    {
112    // The template, if specified, must be a space.
113  3 SpaceReference extractedSpaceReference = null;
114  3 if (this.request.getTemplateReference() != null
115    && this.request.getTemplateReference().extractReference(EntityType.SPACE) != null) {
116  2 extractedSpaceReference =
117    new SpaceReference(this.request.getTemplateReference().extractReference(EntityType.SPACE));
118    }
119  3 final SpaceReference templateSpaceReference = extractedSpaceReference;
120   
121  3 if (templateSpaceReference != null) {
122    // Space from template space.
123  2 visitDocuments(templateSpaceReference, new Visitor<DocumentReference>()
124    {
 
125  2 toggle @Override
126    public void visit(final DocumentReference templateDocumentReference)
127    {
128  2 DocumentReference newDocumentReference =
129    templateDocumentReference.replaceParent(templateSpaceReference, newSpaceReference);
130  2 maybeCreate(newDocumentReference, templateDocumentReference);
131    }
132    });
133    } else {
134    // Empty space (webhome document).
135  1 DocumentReference newSpaceWebHomeReference = new DocumentReference("WebHome", newSpaceReference);
136  1 maybeCreate(newSpaceWebHomeReference, null);
137    }
138    }
139   
 
140  28 toggle private void maybeCreate(DocumentReference newDocumentReference, DocumentReference templateDocumentReference)
141    {
142  28 if (request.getSkippedEntities().contains(newDocumentReference)) {
143  18 this.logger.debug("Skipping creation of document [{}], as specified in the request.", newDocumentReference);
144  10 } else if (this.modelBridge.exists(newDocumentReference)) {
145    // TODO: Ask the user if it's OK to override. For now, just log.
146  1 this.logger.warn("Skipping creation of document [{}] because it already exists.", newDocumentReference);
147  9 } else if (!hasAccess(Right.EDIT, newDocumentReference)) {
148  1 this.logger.error("You are not allowed to create the document [{}].", newDocumentReference);
149  8 } else if (templateDocumentReference == null) {
150    // If no template is specified, then we are just creating an empty document.
151  2 this.modelBridge.create(newDocumentReference);
152  6 } else if (!hasAccess(Right.VIEW, templateDocumentReference)) {
153  1 this.logger.error("You are not allowed to view the template document [{}].", templateDocumentReference);
154  5 } else if (!this.modelBridge.exists(templateDocumentReference)) {
155    // Should generally not happen, but you never know.
156  2 this.logger.error("Template document [{}] does not exist.", templateDocumentReference);
157    } else {
158  3 this.modelBridge.copy(templateDocumentReference, newDocumentReference);
159    }
160    }
161    }