Search in sources :

Example 1 with ListItem

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"));
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) StaticListClass(com.xpn.xwiki.objects.classes.StaticListClass) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ListItem(com.xpn.xwiki.objects.classes.ListItem) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 2 with ListItem

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);
            }
        }
    }
}
Also used : BaseObject(com.xpn.xwiki.objects.BaseObject) List(java.util.List) ListItem(com.xpn.xwiki.objects.classes.ListItem)

Example 3 with ListItem

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ListItem(com.xpn.xwiki.objects.classes.ListItem)

Example 4 with ListItem

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());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ListItem(com.xpn.xwiki.objects.classes.ListItem) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument)

Aggregations

ListItem (com.xpn.xwiki.objects.classes.ListItem)4 BaseObject (com.xpn.xwiki.objects.BaseObject)2 Document (com.xpn.xwiki.api.Document)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 StaticListClass (com.xpn.xwiki.objects.classes.StaticListClass)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 Test (org.junit.Test)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1