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

File DefaultSheetManager.java

 

Coverage histogram

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

Code metrics

12
33
4
1
198
100
12
0.36
8.25
4
3

Classes

Class Line # Actions
DefaultSheetManager 50 33 0% 12 3
0.9387755493.9%
 

Contributing tests

This file is covered by 6 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.sheet.internal;
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.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.bridge.DocumentModelBridge;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.context.Execution;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.DocumentReferenceResolver;
38    import org.xwiki.model.reference.EntityReferenceSerializer;
39    import org.xwiki.sheet.SheetBinder;
40    import org.xwiki.sheet.SheetManager;
41   
42    /**
43    * Default {@link SheetManager} implementation.
44    *
45    * @version $Id: 6f518a5e30255c92e564372ddb209e8fb2f0b352 $
46    * @since 3.2M3
47    */
48    @Component
49    @Singleton
 
50    public class DefaultSheetManager implements SheetManager
51    {
52    /**
53    * The name of the class that describes a sheet.
54    */
55    private static final String SHEET_CLASS = "XWiki.SheetDescriptorClass";
56   
57    /**
58    * The object used to log message.
59    */
60    @Inject
61    private Logger logger;
62   
63    /**
64    * Execution context handler.
65    */
66    @Inject
67    private Execution execution;
68   
69    /**
70    * The component used to resolve a string document reference.
71    */
72    @Inject
73    private DocumentReferenceResolver<String> documentReferenceResolver;
74   
75    /**
76    * The component used to serialize entity references.
77    */
78    @Inject
79    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
80   
81    /**
82    * The bridge to the old XWiki core API.
83    */
84    @Inject
85    private DocumentAccessBridge documentAccessBridge;
86   
87    /**
88    * The component used to access the XWiki model.
89    */
90    @Inject
91    private ModelBridge modelBridge;
92   
93    /**
94    * The component used to retrieve the list of sheets bound to a XWiki document.
95    */
96    @Inject
97    @Named("document")
98    private SheetBinder documentSheetBinder;
99   
100    /**
101    * The component used to retrieve the list of sheets bound to a XWiki class.
102    */
103    @Inject
104    @Named("class")
105    private SheetBinder classSheetBinder;
106   
 
107  25986 toggle @Override
108    public List<DocumentReference> getSheets(DocumentModelBridge document, String action)
109    {
110  25983 DocumentReference documentReference = document.getDocumentReference();
111   
112    // (1) Check if there is a sheet specified for the current execution context. Apply it only if the given
113    // document is the current document on the execution context.
114  25980 String sheetStringRef = (String) execution.getContext().getProperty("sheet");
115  25982 if (sheetStringRef != null && documentReference.equals(documentAccessBridge.getCurrentDocumentReference())) {
116  287 DocumentReference sheetReference = documentReferenceResolver.resolve(sheetStringRef, documentReference);
117  287 if (matchSheet(sheetReference, action)) {
118  142 return Collections.singletonList(sheetReference);
119    }
120    }
121   
122    // (2) Look for document sheets.
123  25842 List<DocumentReference> sheets = getDocumentSheets(document, action);
124  25837 if (sheets.isEmpty()) {
125   
126    // (3) Look for class sheets.
127  25613 sheets = new ArrayList<DocumentReference>();
128  25611 for (DocumentReference classReference : modelBridge.getXObjectClassReferences(document)) {
129  6002 sheets.addAll(getClassSheets(classReference, action));
130    }
131    }
132   
133  25840 return sheets;
134    }
135   
136    /**
137    * @param classReference a reference to a XWiki class
138    * @param action the action for which to retrieve the class sheets
139    * @return the list of sheets bound to the specified class and matching the specified action; these are sheets
140    * designed to be displayed when the specified action is performed on a document holding an object of the
141    * specified class
142    */
 
143  6001 toggle private List<DocumentReference> getClassSheets(DocumentReference classReference, String action)
144    {
145  6001 DocumentModelBridge classDocument;
146  6001 try {
147  6002 classDocument = documentAccessBridge.getDocument(classReference);
148    } catch (Exception e) {
149  0 String classStringReference = defaultEntityReferenceSerializer.serialize(classReference);
150  0 logger.warn("Failed to get class sheets for [{}]. Reason: [{}]", classStringReference, e.getMessage());
151  0 return Collections.emptyList();
152    }
153  6004 List<DocumentReference> sheetReferences = new ArrayList<DocumentReference>();
154  6004 for (DocumentReference sheetReference : classSheetBinder.getSheets(classDocument)) {
155  1765 if (matchSheet(sheetReference, action)) {
156  1662 sheetReferences.add(sheetReference);
157    }
158    }
159  6004 return sheetReferences;
160    }
161   
162    /**
163    * @param document the document where to look for sheet references
164    * @param action the action for which to retrieve the sheets
165    * @return the list of sheets that are referenced by the given document and which are associated with the specified
166    * action
167    */
 
168  25842 toggle private List<DocumentReference> getDocumentSheets(DocumentModelBridge document, String action)
169    {
170  25840 List<DocumentReference> sheets = new ArrayList<DocumentReference>();
171  25836 for (DocumentReference sheetReference : documentSheetBinder.getSheets(document)) {
172  525 if (matchSheet(sheetReference, action)) {
173  223 sheets.add(sheetReference);
174    }
175    }
176  25837 return sheets;
177    }
178   
179    /**
180    * @param sheetReference specifies the sheet that is matched
181    * @param expectedAction the expected value of the action sheet property
182    * @return {@code true} if the given document reference points to a sheet that matches the action and display
183    * constraints
184    */
 
185  2575 toggle private boolean matchSheet(DocumentReference sheetReference, String expectedAction)
186    {
187  2577 if (!documentAccessBridge.exists(sheetReference)) {
188  510 return false;
189    }
190   
191  2067 DocumentReference sheetClassReference = documentReferenceResolver.resolve(SHEET_CLASS, sheetReference);
192  2067 String actualAction = (String) documentAccessBridge.getProperty(sheetReference, sheetClassReference, "action");
193    // We assume the sheet matches all actions if it doesn't specify an action (is empty).
194    // We assume all actions are matched if the expected value is not specified (is empty).
195  2067 return StringUtils.isEmpty(expectedAction) || StringUtils.isEmpty(actualAction)
196    || actualAction.equals(expectedAction);
197    }
198    }