Search in sources :

Example 41 with BaseProperty

use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.

the class DocumentSolrMetadataExtractorTest method getDocumentWithObjects.

@Test
public void getDocumentWithObjects() throws Exception {
    // 
    // Mock
    // 
    BaseObject comment = mock(BaseObject.class);
    List<BaseProperty<EntityReference>> commentFields = new ArrayList<BaseProperty<EntityReference>>();
    String commentContent = "This is a comment";
    BaseProperty<EntityReference> contentField = mock(BaseProperty.class);
    when(contentField.getName()).thenReturn("comment");
    when(contentField.getValue()).thenReturn(commentContent);
    when(contentField.getObject()).thenReturn(comment);
    commentFields.add(contentField);
    String commentSummary = "summary";
    BaseProperty<EntityReference> summaryField = mock(BaseProperty.class);
    when(summaryField.getName()).thenReturn("summary");
    when(summaryField.getValue()).thenReturn(commentSummary);
    when(summaryField.getObject()).thenReturn(comment);
    commentFields.add(summaryField);
    String commentAuthor = "wiki:space.commentAuthor";
    BaseProperty<EntityReference> authorField = mock(BaseProperty.class);
    when(authorField.getName()).thenReturn("author");
    when(authorField.getValue()).thenReturn(commentAuthor);
    when(authorField.getObject()).thenReturn(comment);
    commentFields.add(authorField);
    Date commentDate = new Date();
    BaseProperty<EntityReference> dateField = mock(BaseProperty.class);
    when(dateField.getName()).thenReturn("date");
    when(dateField.getValue()).thenReturn(commentDate);
    when(dateField.getObject()).thenReturn(comment);
    commentFields.add(dateField);
    // Adding a fake password field to the comments class just to test the branch in the code.
    String commentPassword = "password";
    BaseProperty<EntityReference> passwordField = mock(BaseProperty.class);
    when(passwordField.getName()).thenReturn("password");
    when(passwordField.getValue()).thenReturn(commentPassword);
    commentFields.add(passwordField);
    List<String> commentList = Arrays.asList("a", "list");
    BaseProperty<EntityReference> listField = mock(BaseProperty.class);
    when(listField.getName()).thenReturn("list");
    when(listField.getValue()).thenReturn(commentList);
    when(listField.getObject()).thenReturn(comment);
    commentFields.add(listField);
    Long commentLikes = 13L;
    BaseProperty<EntityReference> numberField = mock(BaseProperty.class);
    when(numberField.getName()).thenReturn("likes");
    when(numberField.getValue()).thenReturn(commentLikes);
    when(numberField.getObject()).thenReturn(comment);
    commentFields.add(numberField);
    BaseProperty<EntityReference> booleanField = mock(BaseProperty.class);
    when(booleanField.getName()).thenReturn("enabled");
    when(booleanField.getValue()).thenReturn(1);
    when(booleanField.getObject()).thenReturn(comment);
    commentFields.add(booleanField);
    DocumentReference commentsClassReference = new DocumentReference("wiki", "space", "commentsClass");
    when(this.document.getXObjects()).thenReturn(Collections.singletonMap(commentsClassReference, Arrays.asList(comment)));
    EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localEntityReferenceSerializer.serialize(commentsClassReference)).thenReturn("space.commentsClass");
    BaseClass xclass = mock(BaseClass.class);
    when(comment.getXClass(this.xcontext)).thenReturn(xclass);
    when(comment.getFieldList()).thenReturn(commentFields);
    when(comment.getRelativeXClassReference()).thenReturn(commentsClassReference.removeParent(commentsClassReference.getWikiReference()));
    StringClass stringClass = mock(StringClass.class);
    when(stringClass.getClassType()).thenReturn("String");
    when(xclass.get("comment")).thenReturn(mock(TextAreaClass.class));
    when(xclass.get("summary")).thenReturn(stringClass);
    when(xclass.get("password")).thenReturn(mock(PasswordClass.class));
    when(xclass.get("enabled")).thenReturn(mock(BooleanClass.class));
    // 
    // Call
    // 
    SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.frenchDocumentReference);
    // 
    // Assert and verify
    // 
    assertEquals(Arrays.asList("space.commentsClass"), solrDocument.getFieldValues(FieldUtils.CLASS));
    // A TextArea property must be indexed as a localized text.
    assertSame(commentContent, solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.comment", Locale.FRENCH)));
    assertNull(solrDocument.getFieldValue("property.space.commentsClass.comment_string"));
    // A String property must be indexed both as localized text and as raw (unanalyzed) text.
    assertSame(commentSummary, solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.summary", Locale.FRENCH)));
    assertSame(commentSummary, solrDocument.getFieldValue("property.space.commentsClass.summary_string"));
    assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_string"));
    assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_date"));
    assertEquals(commentList, solrDocument.getFieldValues("property.space.commentsClass.list_string"));
    assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_long"));
    assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_boolean"));
    // Make sure the password is not indexed (neither as a string nor as a localized text).
    assertNull(solrDocument.getFieldValue("property.space.commentsClass.password_string"));
    assertNull(solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.password", Locale.FRENCH)));
    // Check the sort fields.
    assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_sortString"));
    assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_sortDate"));
    // The last value is used for sorting because we cannot sort on fields with multiple values.
    assertEquals("list", solrDocument.getFieldValue("property.space.commentsClass.list_sortString"));
    assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_sortLong"));
    assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_sortBoolean"));
    // Localized texts are sorted as strings if they are not too large.
    assertSame(commentContent, solrDocument.getFieldValue("property.space.commentsClass.comment_sortString"));
    Collection<Object> objectProperties = solrDocument.getFieldValues(FieldUtils.getFieldName("object.space.commentsClass", Locale.FRENCH));
    MatcherAssert.assertThat(objectProperties, Matchers.<Object>containsInAnyOrder(commentContent, commentSummary, commentAuthor, commentDate, commentList.get(0), commentList.get(1), commentLikes, true));
    assertEquals(8, objectProperties.size());
    objectProperties = solrDocument.getFieldValues(FieldUtils.getFieldName(FieldUtils.OBJECT_CONTENT, Locale.FRENCH));
    MatcherAssert.assertThat(objectProperties, Matchers.<Object>containsInAnyOrder("comment : " + commentContent, "summary : " + commentSummary, "author : " + commentAuthor, "date : " + commentDate, "list : " + commentList.get(0), "list : " + commentList.get(1), "likes : " + commentLikes, "enabled : true"));
    assertEquals(8, objectProperties.size());
}
Also used : PasswordClass(com.xpn.xwiki.objects.classes.PasswordClass) BooleanClass(com.xpn.xwiki.objects.classes.BooleanClass) ArrayList(java.util.ArrayList) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject) StringClass(com.xpn.xwiki.objects.classes.StringClass) SolrInputDocument(org.apache.solr.common.SolrInputDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 42 with BaseProperty

use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.

the class ObjectidDataSourceFactory method create.

@Override
public DataSource create(Map params, XWikiContext context) throws DataSourceException {
    int objectid;
    try {
        String id = (String) params.get("id");
        if (id != null) {
            objectid = Integer.parseInt(id);
        } else {
            throw new DataSourceException("source=type:objectid implies the presence of an id argument");
        }
    } catch (NumberFormatException e) {
        throw new DataSourceException(e);
    }
    BaseObject xobj;
    try {
        List list = context.getWiki().getStore().search("from " + BaseObject.class.getName() + " as obj where obj.id='" + objectid + "'", 0, 0, context);
        if (list.size() == 0) {
            throw new DataSourceException("Object ID not found");
        }
        xobj = (BaseObject) list.get(0);
        List propertyList = context.getWiki().getStore().search("from " + BaseProperty.class.getName() + " as p where p.id.id='" + objectid + "'", 0, 0, context);
        Iterator it = propertyList.iterator();
        while (it.hasNext()) {
            BaseProperty prop = (BaseProperty) it.next();
            xobj.addField(prop.getName(), prop);
        }
    } catch (XWikiException e) {
        throw new DataSourceException(e);
    }
    String xclass = xobj.getClassName();
    if (!xclass.startsWith("XWiki.")) {
        throw new DataSourceException("XWiki prefix missing in object class name " + xclass);
    }
    String className = DataSource.class.getPackage().getName() + "." + xclass.substring("XWiki.".length());
    try {
        Class class_ = Class.forName(className);
        Constructor ctor = class_.getConstructor(new Class[] { BaseObject.class, XWikiContext.class });
        return (DataSource) ctor.newInstance(new Object[] { xobj, context });
    } catch (InvocationTargetException e) {
        throw new DataSourceException(e.getTargetException());
    } catch (Exception e) {
        throw new DataSourceException(e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InvocationTargetException(java.lang.reflect.InvocationTargetException) XWikiException(com.xpn.xwiki.XWikiException) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BaseObject(com.xpn.xwiki.objects.BaseObject) DataSourceException(com.xpn.xwiki.plugin.charts.exceptions.DataSourceException) Iterator(java.util.Iterator) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException)

Example 43 with BaseProperty

use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.

the class PropertyChangedRule method hasEqualProperty.

public boolean hasEqualProperty(XWikiDocument newdoc, XWikiDocument olddoc, String className, String propertyName) {
    Vector vobj1 = (newdoc == null) ? null : newdoc.getObjects(className);
    Vector vobj2 = (olddoc == null) ? null : olddoc.getObjects(className);
    if ((vobj1 == null) && (vobj2 == null)) {
        return true;
    }
    if ((vobj1 == null) || (vobj2 == null)) {
        return false;
    }
    if (vobj1.size() != vobj2.size()) {
        return false;
    }
    for (int i = 0; i < vobj1.size(); i++) {
        if (!((vobj1.get(i) == null) && (vobj2.get(i) == null))) {
            if (vobj1.get(i) == null) {
                if (((BaseObject) vobj2.get(i)).safeget(propertyName) == null) {
                    return true;
                } else {
                    return false;
                }
            }
            if (vobj2.get(i) == null) {
                if (((BaseObject) vobj1.get(i)).safeget(propertyName) == null) {
                    return true;
                } else {
                    return false;
                }
            }
            BaseProperty prop1 = (BaseProperty) ((BaseObject) vobj1.get(i)).safeget(propertyName);
            BaseProperty prop2 = (BaseProperty) ((BaseObject) vobj2.get(i)).safeget(propertyName);
            if ((prop1 == null) && (prop2 == null)) {
                return true;
            }
            if ((prop1 == null) || (prop2 == null)) {
                return false;
            }
            if (!prop1.equals(prop2)) {
                // Found different property on an object, we can stop and return false
                return false;
            }
        }
    }
    return true;
}
Also used : BaseProperty(com.xpn.xwiki.objects.BaseProperty) Vector(java.util.Vector)

Example 44 with BaseProperty

use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.

the class XClassMigratorListener method migrate.

private void migrate(PropertyClass newPropertyClass, String documentName, XWikiContext xcontext) throws XWikiException {
    BaseProperty newProperty = newPropertyClass.newProperty();
    ClassPropertyReference propertyReference = newPropertyClass.getReference();
    EntityReference classReference = propertyReference.extractReference(EntityType.DOCUMENT);
    XWikiDocument document = xcontext.getWiki().getDocument(this.resolver.resolve(documentName, classReference), xcontext);
    boolean modified = false;
    for (BaseObject xobject : document.getXObjects(classReference)) {
        BaseProperty property = (BaseProperty) xobject.getField(propertyReference.getName());
        // If the existing field is of different kind than what is produced by the new class property
        if (property != null && property.getClass() != newProperty.getClass()) {
            BaseProperty<?> convertedProperty = this.propertyConverter.convertProperty(property, newPropertyClass);
            // Set new field
            if (convertedProperty != null) {
                // Mark old field for removal, only if the conversion was successful, to avoid losing data.
                xobject.removeField(propertyReference.getName());
                // Don't set the new property if it's null (it means the property is not set).
                xobject.safeput(propertyReference.getName(), convertedProperty);
                modified = true;
            }
        }
    }
    // If anything changed save the document
    if (modified) {
        xcontext.getWiki().saveDocument(document, "Migrated property [" + propertyReference.getName() + "] from class [" + this.localSerializer.serialize(classReference) + "]", xcontext);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseProperty(com.xpn.xwiki.objects.BaseProperty) ClassPropertyReference(org.xwiki.model.reference.ClassPropertyReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 45 with BaseProperty

use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.

the class XWikiTest method testValidationKeyStorage.

/**
 * Check that the user validation feature works when the validation key is stored both as plain text and as a hashed
 * field.
 *
 * @throws Exception when any exception occurs inside XWiki
 */
public void testValidationKeyStorage() throws Exception {
    XWikiContext context = getContext();
    context.setLanguage("en");
    // Prepare the request
    Mock request = mock(XWikiRequest.class);
    request.stubs().method("getParameter").with(eq("xwikiname")).will(returnValue("TestUser"));
    request.stubs().method("getParameter").with(eq("validkey")).will(returnValue("plaintextkey"));
    context.setRequest((XWikiRequest) request.proxy());
    // Prepare the user profile
    XWikiDocument testUser = new XWikiDocument(new DocumentReference("Wiki", "XWiki", "TestUser"));
    BaseObject userObject = (BaseObject) this.xwiki.getUserClass(context).newObject(context);
    testUser.addObject("XWiki.XWikiUsers", userObject);
    this.xwiki.saveDocument(testUser, context);
    // Check with a correct plaintext key
    BaseProperty validationKey = new StringProperty();
    validationKey.setValue("plaintextkey");
    userObject.safeput("validkey", validationKey);
    assertEquals(0, this.xwiki.validateUser(false, getContext()));
    // Check with an incorrect plaintext key
    validationKey.setValue("wrong key");
    assertEquals(-1, this.xwiki.validateUser(false, getContext()));
    // Check with a correct hashed key
    validationKey = ((PropertyClass) this.xwiki.getUserClass(context).get("validkey")).fromString("plaintextkey");
    assertTrue(validationKey.getValue().toString().startsWith("hash:"));
    userObject.safeput("validkey", validationKey);
    assertEquals(0, this.xwiki.validateUser(false, getContext()));
    // Check with an incorrect hashed key
    validationKey = ((PropertyClass) this.xwiki.getUserClass(context).get("validkey")).fromString("wrong key");
    assertTrue(validationKey.getValue().toString().startsWith("hash:"));
    userObject.safeput("validkey", validationKey);
    assertEquals(-1, this.xwiki.validateUser(false, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) StringProperty(com.xpn.xwiki.objects.StringProperty) BaseProperty(com.xpn.xwiki.objects.BaseProperty) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

BaseProperty (com.xpn.xwiki.objects.BaseProperty)87 BaseObject (com.xpn.xwiki.objects.BaseObject)26 ArrayList (java.util.ArrayList)16 XWikiException (com.xpn.xwiki.XWikiException)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 XMLAttributeValueFilter (com.xpn.xwiki.internal.xml.XMLAttributeValueFilter)12 org.apache.ecs.xhtml.input (org.apache.ecs.xhtml.input)11 XWikiContext (com.xpn.xwiki.XWikiContext)10 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 StringProperty (com.xpn.xwiki.objects.StringProperty)8 ListProperty (com.xpn.xwiki.objects.ListProperty)7 XWiki (com.xpn.xwiki.XWiki)5 BaseCollection (com.xpn.xwiki.objects.BaseCollection)5 LargeStringProperty (com.xpn.xwiki.objects.LargeStringProperty)5 List (java.util.List)5 Test (org.junit.Test)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)5 DBStringListProperty (com.xpn.xwiki.objects.DBStringListProperty)4 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)4