1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.script.parentchild

File ParentChildScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

6
22
4
1
134
74
8
0.36
5.5
4
2

Classes

Class Line # Actions
ParentChildScriptService 54 22 0% 8 14
0.562556.2%
 

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 com.xpn.xwiki.script.parentchild;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
24    import java.util.List;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.EntityReference;
36    import org.xwiki.model.reference.SpaceReference;
37    import org.xwiki.script.service.ScriptService;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.doc.XWikiDocument;
42    import com.xpn.xwiki.internal.parentchild.ParentChildConfiguration;
43   
44    /**
45    * This script service give an access to the configuration of the parent/child mechanism, which has been deprecated
46    * but can still be enabled for retro-compatibility issues.
47    *
48    * @version $Id: 0f62ada1ee18061b22a8ba8ce23a851e391f8edb $
49    * @since 7.2M2
50    */
51    @Component
52    @Named("parentchild")
53    @Singleton
 
54    public class ParentChildScriptService implements ScriptService
55    {
56    @Inject
57    private ParentChildConfiguration configuration;
58   
59    @Inject
60    private DocumentReferenceResolver<EntityReference> documentReferenceResolver;
61   
62    @Inject
63    private Provider<XWikiContext> contextProvider;
64   
65    @Inject
66    private Logger logger;
67   
68    /**
69    * @return whether or not legacy parent/child mechanism is enabled for the hierarchy handling
70    */
 
71  2621 toggle public boolean isParentChildMechanismEnabled()
72    {
73  2619 return configuration.isParentChildMechanismEnabled();
74    }
75   
76    /**
77    * @param docRef a reference of the document
78    * @return the list of parents of the document
79    */
 
80  0 toggle public List<DocumentReference> getParents(DocumentReference docRef)
81    {
82  0 if (isParentChildMechanismEnabled()) {
83  0 return getParentsBasedOnParentChildRelationship(docRef);
84    } else {
85  0 return getParentsBasedOnReference(docRef);
86    }
87    }
88   
89    /**
90    * Returns the parents of a document, based on the nested document paradigm (described in the document reference).
91    * @param docRef a reference of the document
92    * @return the list of parents of the document
93    */
 
94  0 toggle public List<DocumentReference> getParentsBasedOnReference(DocumentReference docRef)
95    {
96  0 List<DocumentReference> parents = new ArrayList<>();
97   
98  0 for (SpaceReference spaceReference : docRef.getSpaceReferences()) {
99  0 parents.add(documentReferenceResolver.resolve(spaceReference));
100    }
101   
102  0 return parents;
103    }
104   
105    /**
106    * Returns the parents of a document, based on the parent/child relationship.
107    * @param docRef a reference of the document
108    * @return the list of parents of the document
109    */
 
110  29 toggle public List<DocumentReference> getParentsBasedOnParentChildRelationship(DocumentReference docRef)
111    {
112  29 XWikiContext context = contextProvider.get();
113  29 XWiki xwiki = context.getWiki();
114   
115  29 List<DocumentReference> parents = new ArrayList<>();
116  29 try {
117  29 XWikiDocument document = xwiki.getDocument(docRef, context);
118  79 while (document.getParentReference() != null) {
119  50 DocumentReference parentReference = document.getParentReference();
120  50 if (parents.contains(parentReference)) {
121  0 throw new Exception("Cyclic references of parent documents");
122    }
123  50 parents.add(document.getParentReference());
124  50 document = xwiki.getDocument(document.getParentReference(), context);
125    }
126    } catch (Exception e) {
127  0 logger.error("Failed to get the parents of [{}].", docRef, e);
128    }
129   
130  29 Collections.reverse(parents);
131   
132  29 return parents;
133    }
134    }