Search in sources :

Example 56 with BaseClass

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

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

the class AbstractDocumentMojo method loadFromXML.

/**
 * Loads a XWikiDocument from a XML file
 *
 * @param file the xml file to load
 * @return the XWiki document loaded from XML
 * @throws MojoExecutionException
 */
protected XWikiDocument loadFromXML(File file) throws MojoExecutionException {
    XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "WebHome"));
    FileInputStream fis;
    try {
        // Load the document as a XWikiDocument from XML
        fis = new FileInputStream(file);
        doc.fromXML(fis);
        // get XML tree
        FileReader fr = new FileReader(file);
        SAXReader reader = new SAXReader();
        Document domdoc;
        domdoc = reader.read(fr);
        Element root = domdoc.getRootElement();
        // Lookup all class nodes, and add them to our xwiki context as BaseClass definitions
        List<Node> classNodes = root.selectNodes("//xwikidoc/object/class");
        for (Iterator<Node> it = classNodes.iterator(); it.hasNext(); ) {
            BaseClass bClass = new BaseClass();
            bClass.fromXML((Element) it.next());
            context.addBaseClass(bClass);
        }
        fis.close();
        return doc;
    } catch (Exception e) {
        throw new MojoExecutionException("Error loading XWikiDocument [" + file + "]", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) Document(org.dom4j.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) FileInputStream(java.io.FileInputStream) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) FileReader(java.io.FileReader) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 58 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project celements-blog by celements.

the class AddBlogDateValidationMigrator method migrate.

@Override
public void migrate(SubSystemHibernateMigrationManager manager, XWikiContext context) throws XWikiException {
    XWikiDocument doc = context.getWiki().getDocument(((BlogClasses) blogClasses).getArticleClassRef(context.getDatabase()), context);
    BaseClass bClass = doc.getXClass();
    DateClass publishDateElement = (DateClass) bClass.get("publishdate");
    DateClass archiveDateElement = (DateClass) bClass.get("archivedate");
    publishDateElement.setValidationRegExp("/^((0[1-9]|[12][0-9]|3[01])\\.(0[1-9]|1[012])\\.([0-9]{4}) " + "([01][0-9]|2[0-4])(\\:[0-5][0-9]))$/");
    publishDateElement.setValidationMessage("cel_blog_validation_publishdate");
    publishDateElement.setDateFormat("dd.MM.yyyy HH:mm");
    archiveDateElement.setValidationRegExp("/(^$)|^((0[1-9]|[12][0-9]|3[01])\\.(0[1-9]|1[012])\\.([0-9]{4}) " + "([01][0-9]|2[0-4])(\\:[0-5][0-9]))$/");
    archiveDateElement.setValidationMessage("cel_blog_validation_archivedate");
    archiveDateElement.setDateFormat("dd.MM.yyyy HH:mm");
    context.getWiki().saveDocument(doc, context);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DateClass(com.xpn.xwiki.objects.classes.DateClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 59 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project celements-blog by celements.

the class BlogClasses method getBlogArticleSubscriptionClass.

BaseClass getBlogArticleSubscriptionClass() throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = getContext().getWiki();
    boolean needsUpdate = false;
    DocumentReference getBlogArticleSubscriptionClassRef = getBlogArticleSubscriptionClassRef(getContext().getDatabase());
    try {
        doc = xwiki.getDocument(getBlogArticleSubscriptionClassRef, getContext());
    } catch (Exception e) {
        doc = new XWikiDocument(getBlogArticleSubscriptionClassRef);
        needsUpdate = true;
    }
    BaseClass bclass = doc.getXClass();
    bclass.setXClassReference(getBlogArticleSubscriptionClassRef);
    needsUpdate |= bclass.addTextField(PROPERTY_ARTICLE_SUBSCRIPTION_SUBSCRIBER, PROPERTY_ARTICLE_SUBSCRIPTION_SUBSCRIBER, 30);
    needsUpdate |= bclass.addBooleanField(PROPERTY_ARTICLE_SUBSCRIPTION_DO_SUBSCRIBE, PROPERTY_ARTICLE_SUBSCRIPTION_DO_SUBSCRIBE, "yesno");
    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 60 with BaseClass

use of com.xpn.xwiki.objects.classes.BaseClass in project celements-blog by celements.

the class BlogClasses method getNewsletterConfigClass.

BaseClass getNewsletterConfigClass() throws XWikiException {
    XWikiDocument doc;
    XWiki xwiki = getContext().getWiki();
    boolean needsUpdate = false;
    DocumentReference newsletterConfigClassRef = getNewsletterConfigClassRef(getContext().getDatabase());
    try {
        doc = xwiki.getDocument(newsletterConfigClassRef, getContext());
    } catch (Exception e) {
        doc = new XWikiDocument(newsletterConfigClassRef);
        needsUpdate = true;
    }
    BaseClass bclass = doc.getXClass();
    bclass.setXClassReference(newsletterConfigClassRef);
    needsUpdate |= bclass.addNumberField("times_sent", "times_sent", 30, "integer");
    needsUpdate |= bclass.addDateField("last_sent_date", "last_sent_date", null, 0);
    needsUpdate |= bclass.addTextField("last_sender", "last_sender", 30);
    needsUpdate |= bclass.addNumberField("last_sent_recipients", "last_sent_recipients", 30, "integer");
    needsUpdate |= bclass.addTextField("from_address", "from_address", 30);
    needsUpdate |= bclass.addTextField("reply_to_address", "reply_to_address", 30);
    needsUpdate |= bclass.addTextField("subject", "subject", 30);
    setContentAndSaveClassDocument(doc, needsUpdate);
    return bclass;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

BaseClass (com.xpn.xwiki.objects.classes.BaseClass)100 DocumentReference (org.xwiki.model.reference.DocumentReference)42 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)41 BaseObject (com.xpn.xwiki.objects.BaseObject)40 XWikiException (com.xpn.xwiki.XWikiException)26 XWikiContext (com.xpn.xwiki.XWikiContext)24 ArrayList (java.util.ArrayList)18 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)16 XWiki (com.xpn.xwiki.XWiki)15 Test (org.junit.Test)15 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 EntityReference (org.xwiki.model.reference.EntityReference)10 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)9 BaseProperty (com.xpn.xwiki.objects.BaseProperty)9 List (java.util.List)9 ToString (org.suigeneris.jrcs.util.ToString)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Before (org.junit.Before)6 TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)5 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)5