use of com.xpn.xwiki.objects.classes.ListItem in project xwiki-platform by xwiki.
the class DocumentSolrMetadataExtractorTest method setStaticListPropertyValue.
/**
* @see "XWIKI-9417: Search does not return any results for Static List values"
*/
@Test
public void setStaticListPropertyValue() throws Exception {
BaseObject xobject = mock(BaseObject.class);
@SuppressWarnings("unchecked") BaseProperty<EntityReference> listProperty = mock(BaseProperty.class);
when(listProperty.getName()).thenReturn("color");
when(listProperty.getValue()).thenReturn(Arrays.asList("red", "green"));
when(listProperty.getObject()).thenReturn(xobject);
DocumentReference classReference = new DocumentReference("wiki", "Space", "MyClass");
when(this.document.getXObjects()).thenReturn(Collections.singletonMap(classReference, Arrays.asList(xobject)));
BaseClass xclass = mock(BaseClass.class);
when(xobject.getXClass(this.xcontext)).thenReturn(xclass);
when(xobject.getFieldList()).thenReturn(Arrays.<Object>asList(listProperty));
when(xobject.getRelativeXClassReference()).thenReturn(classReference.removeParent(classReference.getWikiReference()));
StaticListClass staticListClass = mock(StaticListClass.class);
when(xclass.get("color")).thenReturn(staticListClass);
when(staticListClass.getMap(xcontext)).thenReturn(Collections.singletonMap("red", new ListItem("red", "Dark Red")));
SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
// Make sure both the raw value (which is saved in the database) and the display value (specified in the XClass)
// are indexed. The raw values are indexed as strings in order to be able to perform exact matches.
assertEquals(Arrays.asList("red", "green"), solrDocument.getFieldValues("property.Space.MyClass.color_string"));
assertEquals(Collections.singletonList("Dark Red"), solrDocument.getFieldValues(FieldUtils.getFieldName("property.Space.MyClass.color", Locale.US)));
// Check the sort field. Only the last value we set is used for sorting because we cannot sort on fields that
// have multiple values.
assertEquals(Collections.singletonList("green"), solrDocument.getFieldValues("property.Space.MyClass.color_sortString"));
}
use of com.xpn.xwiki.objects.classes.ListItem in project xwiki-platform by xwiki.
the class AbstractSolrMetadataExtractor method setStaticListPropertyValue.
/**
* Add the values of a static list property to a Solr document. We add both the raw value (what is saved in the
* database) and the display value (the label seen by the user, which is specified in the XClass).
*
* @param solrDocument the document to add the property value to
* @param property the static list property whose value to add
* @param propertyClass the static list class that should be used to get the list of known values
* @param locale the locale of the indexed document
* @see "XWIKI-9417: Search does not return any results for Static List values"
*/
private void setStaticListPropertyValue(SolrInputDocument solrDocument, BaseProperty<EntityReference> property, StaticListClass propertyClass, Locale locale) {
// The list of known values specified in the XClass.
Map<String, ListItem> knownValues = propertyClass.getMap(this.xcontextProvider.get());
Object propertyValue = property.getValue();
// When multiple selection is on the value is a list. Otherwise, for single selection, the value is a string.
List<?> rawValues = propertyValue instanceof List ? (List<?>) propertyValue : Arrays.asList(propertyValue);
for (Object rawValue : rawValues) {
// Avoid indexing null values.
if (rawValue != null) {
// Index the raw value that is saved in the database. This is most probably a string so we'll be able to
// perform exact matches on this value.
setPropertyValue(solrDocument, property, new TypedValue(rawValue), locale);
ListItem valueInfo = knownValues.get(rawValue);
if (valueInfo != null && valueInfo.getValue() != null && !valueInfo.getValue().equals(rawValue)) {
// Index the display value as text (based on the given locale). This is the text seen by the user
// when he edits the static list property. This text is specified on the XClass (but can be
// overwritten by translations!).
setPropertyValue(solrDocument, property, new TypedValue(valueInfo.getValue(), TypedValue.TEXT), locale);
}
}
}
}
use of com.xpn.xwiki.objects.classes.ListItem in project xwiki-platform by xwiki.
the class ZipExplorerPlugin method getFileTreeList.
/**
* Finds the ZIP attachment with passed name from the passed document matching and parse the ZIP to generate a list
* of {@link com.xpn.xwiki.objects.classes.ListItem} elements representing a tree view of all directories and files
* in the ZIP. For example the following zip:
* <pre><code>
* zipfile.zip:
* Directory/File.txt
* File2.txt
* </code></pre>
* generates the following ListItem list:
* <pre><code>
* { id = "Directory/", value = "Directory", parent = ""}
* { id = "Directory/File.txt", value = "File.txt", parent = "Directory/"}
* { id = "File2.txt", value = "File2.txt", parent = ""}
* </code></pre>
*
* @param document the document containing the ZIP file as an attachment
* @param attachmentName the name under which the ZIP file is attached in the document
* @param context not used
* @return a tree view list of {@link com.xpn.xwiki.objects.classes.ListItem} elements representing the content of
* the ZIP file
* @see com.xpn.xwiki.plugin.zipexplorer.ZipExplorerPluginAPI#getFileTreeList
*/
public List<ListItem> getFileTreeList(Document document, String attachmentName, XWikiContext context) {
List<String> flatList = getFileList(document, attachmentName, context);
Map<String, ListItem> fileTree = new HashMap<String, ListItem>();
List<ListItem> res = new ArrayList<ListItem>();
for (String url : flatList) {
StringBuilder buf = new StringBuilder(url.length());
String parentBuf = "";
String[] aUrl = url.split(URL_SEPARATOR);
for (int i = 0; i < aUrl.length; i++) {
if (i == aUrl.length - 1 && !url.endsWith(URL_SEPARATOR)) {
buf.append(aUrl[i]);
} else {
buf.append(aUrl[i] + URL_SEPARATOR);
}
ListItem item = new ListItem(buf.toString(), aUrl[i], parentBuf);
if (!fileTree.containsKey(buf.toString())) {
res.add(item);
}
fileTree.put(buf.toString(), item);
parentBuf = buf.toString();
}
}
return res;
}
use of com.xpn.xwiki.objects.classes.ListItem in project xwiki-platform by xwiki.
the class ZipExplorerTest method testGetFileTreeList.
public void testGetFileTreeList() throws Exception {
XWikiDocument document = createXWikiDocumentWithZipFileAttachment();
List<ListItem> entries = this.plugin.getFileTreeList(new Document(document, null), "zipfile.zip", null);
Assert.assertEquals(3, entries.size());
Assert.assertEquals("Directory/", entries.get(0).getId());
Assert.assertEquals("Directory", entries.get(0).getValue());
Assert.assertEquals("", entries.get(0).getParent());
Assert.assertEquals("Directory/File.txt", entries.get(1).getId());
Assert.assertEquals("File.txt", entries.get(1).getValue());
Assert.assertEquals("Directory/", entries.get(1).getParent());
Assert.assertEquals("File2.txt", entries.get(2).getId());
Assert.assertEquals("File2.txt", entries.get(2).getValue());
Assert.assertEquals("", entries.get(2).getParent());
}
Aggregations