Search in sources :

Example 1 with StringClass

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

the class PropertyMetaClass method addValidationMetaProperties.

/**
 * Adds the meta properties used for validation the XClass property value.
 */
private void addValidationMetaProperties() {
    StringClass validationRegExpClass = new StringClass(this);
    validationRegExpClass.setName("validationRegExp");
    validationRegExpClass.setPrettyName("Validation Regular Expression");
    validationRegExpClass.setSize(40);
    safeput(validationRegExpClass.getName(), validationRegExpClass);
    StringClass validationMessageClass = new StringClass(this);
    validationMessageClass.setName("validationMessage");
    validationMessageClass.setPrettyName("Validation Message");
    validationMessageClass.setSize(80);
    safeput(validationMessageClass.getName(), validationMessageClass);
}
Also used : StringClass(com.xpn.xwiki.objects.classes.StringClass)

Example 2 with StringClass

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

the class ListMetaClass method addValueSeparatorMetaProperties.

/**
 * Adds the meta properties that control how list values are separated in edit and view mode.
 */
private void addValueSeparatorMetaProperties() {
    StringClass separatorsClass = new StringClass(this);
    separatorsClass.setName("separators");
    separatorsClass.setPrettyName("Multiselect separators (for editing)");
    separatorsClass.setSize(5);
    safeput(separatorsClass.getName(), separatorsClass);
    StringClass separatorClass = new StringClass(this);
    separatorClass.setName("separator");
    separatorClass.setPrettyName("Join separator (for display)");
    separatorClass.setSize(5);
    safeput(separatorClass.getName(), separatorClass);
}
Also used : StringClass(com.xpn.xwiki.objects.classes.StringClass)

Example 3 with StringClass

use of com.xpn.xwiki.objects.classes.StringClass 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 4 with StringClass

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

the class PropertyConverterTest method longToString.

@Test
public void longToString() throws Exception {
    LongProperty longProperty = new LongProperty();
    longProperty.setValue(Long.MAX_VALUE);
    StringClass stringClass = mock(StringClass.class);
    when(stringClass.newProperty()).thenReturn(new StringProperty());
    StringProperty stringProperty = new StringProperty();
    when(stringClass.fromString(longProperty.toText())).thenReturn(stringProperty);
    assertEquals(stringProperty, this.mocker.getComponentUnderTest().convertProperty(longProperty, stringClass));
}
Also used : StringClass(com.xpn.xwiki.objects.classes.StringClass) LongProperty(com.xpn.xwiki.objects.LongProperty) StringProperty(com.xpn.xwiki.objects.StringProperty) Test(org.junit.Test)

Example 5 with StringClass

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

the class XWikiHibernateStore method loadXWikiCollectionInternal.

private void loadXWikiCollectionInternal(BaseCollection object1, XWikiDocument doc, XWikiContext inputxcontext, boolean bTransaction, boolean alreadyLoaded) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    BaseCollection object = object1;
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(false, context);
        }
        Session session = getSession(context);
        if (!alreadyLoaded) {
            try {
                session.load(object, object1.getId());
            } catch (ObjectNotFoundException e) {
                // There is no object data saved
                object = null;
                return;
            }
        }
        DocumentReference classReference = object.getXClassReference();
        // If the class reference is null in the loaded object then skip loading properties
        if (classReference != null) {
            BaseClass bclass = null;
            if (!classReference.equals(object.getDocumentReference())) {
                // Let's check if the class has a custom mapping
                bclass = object.getXClass(context);
            } else {
                // we will go in an endless loop
                if (doc != null) {
                    bclass = doc.getXClass();
                }
            }
            List<String> handledProps = new ArrayList<String>();
            try {
                if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
                    Session dynamicSession = session.getSession(EntityMode.MAP);
                    Object map = dynamicSession.load(bclass.getName(), object.getId());
                    // Let's make sure to look for null fields in the dynamic mapping
                    bclass.fromValueMap((Map) map, object);
                    handledProps = bclass.getCustomMappingPropertyList(context);
                    for (String prop : handledProps) {
                        if (((Map) map).get(prop) == null) {
                            handledProps.remove(prop);
                        }
                    }
                }
            } catch (Exception e) {
            }
            // Load strings, integers, dates all at once
            Query query = session.createQuery("select prop.name, prop.classType from BaseProperty as prop where prop.id.id = :id");
            query.setLong("id", object.getId());
            for (Object[] result : (List<Object[]>) query.list()) {
                String name = (String) result[0];
                // custom mapping
                if (handledProps.contains(name)) {
                    continue;
                }
                String classType = (String) result[1];
                BaseProperty property = null;
                try {
                    property = (BaseProperty) Class.forName(classType).newInstance();
                    property.setObject(object);
                    property.setName(name);
                    loadXWikiProperty(property, context, false);
                } catch (Exception e) {
                    // WORKAROUND IN CASE OF MIXMATCH BETWEEN STRING AND LARGESTRING
                    try {
                        if (property instanceof StringProperty) {
                            LargeStringProperty property2 = new LargeStringProperty();
                            property2.setObject(object);
                            property2.setName(name);
                            loadXWikiProperty(property2, context, false);
                            property.setValue(property2.getValue());
                            if (bclass != null) {
                                if (bclass.get(name) instanceof TextAreaClass) {
                                    property = property2;
                                }
                            }
                        } else if (property instanceof LargeStringProperty) {
                            StringProperty property2 = new StringProperty();
                            property2.setObject(object);
                            property2.setName(name);
                            loadXWikiProperty(property2, context, false);
                            property.setValue(property2.getValue());
                            if (bclass != null) {
                                if (bclass.get(name) instanceof StringClass) {
                                    property = property2;
                                }
                            }
                        } else {
                            throw e;
                        }
                    } catch (Throwable e2) {
                        Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + ""), name };
                        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}', number '{2}' and property '{3}'", e, args);
                    }
                }
                object.addField(name, property);
            }
        }
        if (bTransaction) {
            endTransaction(context, false, false);
        }
    } catch (Exception e) {
        Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + "") };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}' and number '{2}'", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) StringProperty(com.xpn.xwiki.objects.StringProperty) BaseStringProperty(com.xpn.xwiki.objects.BaseStringProperty) BaseCollection(com.xpn.xwiki.objects.BaseCollection) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) StringClass(com.xpn.xwiki.objects.classes.StringClass) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) List(java.util.List) ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Aggregations

StringClass (com.xpn.xwiki.objects.classes.StringClass)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)3 BaseObject (com.xpn.xwiki.objects.BaseObject)2 BaseProperty (com.xpn.xwiki.objects.BaseProperty)2 StringProperty (com.xpn.xwiki.objects.StringProperty)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 BaseCollection (com.xpn.xwiki.objects.BaseCollection)1 BaseStringProperty (com.xpn.xwiki.objects.BaseStringProperty)1 LargeStringProperty (com.xpn.xwiki.objects.LargeStringProperty)1 LongProperty (com.xpn.xwiki.objects.LongProperty)1 BooleanClass (com.xpn.xwiki.objects.classes.BooleanClass)1 PasswordClass (com.xpn.xwiki.objects.classes.PasswordClass)1 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1