Search in sources :

Example 71 with BaseClass

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

the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsWithoutLocaleAndRevision.

@Test
public void testImportDocumentsWithoutLocaleAndRevision() throws FilterException, XWikiException, ParseException {
    DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
    outputProperties.setVersionPreserved(true);
    outputProperties.setVerbose(false);
    importFromXML("document1-2", outputProperties);
    XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
    Assert.assertFalse(document.isNew());
    Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
    Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
    Assert.assertEquals("customclass", document.getCustomClass());
    Assert.assertEquals("title", document.getTitle());
    Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
    Assert.assertEquals("validationScript", document.getValidationScript());
    Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
    Assert.assertEquals(true, document.isHidden());
    Assert.assertEquals("content", document.getContent());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
    Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
    Assert.assertEquals(true, document.isMinorEdit());
    Assert.assertEquals("comment", document.getComment());
    Assert.assertEquals("1.1", document.getVersion());
    // Attachment
    Assert.assertEquals(1, document.getAttachmentList().size());
    XWikiAttachment attachment = document.getAttachment("attachment.txt");
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(10, attachment.getLongSize());
    Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
    Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
    Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
    Assert.assertEquals("1.1", attachment.getVersion());
    Assert.assertEquals("attachment comment", attachment.getComment());
    // XClass
    BaseClass xclass = document.getXClass();
    Assert.assertEquals("customClass", xclass.getCustomClass());
    Assert.assertEquals("customMapping", xclass.getCustomMapping());
    Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
    Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
    Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
    Assert.assertEquals("nameField", xclass.getNameField());
    Assert.assertEquals("validationScript", xclass.getValidationScript());
    Assert.assertEquals(1, xclass.getFieldList().size());
    NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
    Assert.assertEquals("prop1", numberFiled.getName());
    Assert.assertEquals(false, numberFiled.isDisabled());
    Assert.assertEquals(1, numberFiled.getNumber());
    Assert.assertEquals("long", numberFiled.getNumberType());
    Assert.assertEquals("Prop1", numberFiled.getPrettyName());
    Assert.assertEquals(30, numberFiled.getSize());
    Assert.assertEquals(false, numberFiled.isUnmodifiable());
    // Objects
    Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
    Assert.assertEquals(2, objects.size());
    // Object 1
    List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
    Assert.assertEquals(1, documentObjects.size());
    BaseObject documentObject = documentObjects.get(0);
    Assert.assertEquals(0, documentObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
    Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
    Assert.assertEquals(1, documentObject.getFieldList().size());
    Assert.assertEquals(1, documentObject.getIntValue("prop1"));
    // Object 2
    List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
    Assert.assertEquals(1, otherObjects.size());
    BaseObject otherObject = otherObjects.get(0);
    Assert.assertEquals(0, otherObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
    Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
    Assert.assertEquals(1, otherObject.getFieldList().size());
    Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) NumberClass(com.xpn.xwiki.objects.classes.NumberClass) List(java.util.List) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test) AbstractInstanceFilterStreamTest(com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)

Example 72 with BaseClass

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

the class DocumentInstanceOutputFilterStreamTest method testImportDocumentsPreserveVersion.

// Tests
@Test
public void testImportDocumentsPreserveVersion() throws FilterException, XWikiException, ParseException {
    DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
    outputProperties.setVersionPreserved(true);
    outputProperties.setVerbose(false);
    importFromXML("document1", outputProperties);
    XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"), this.oldcore.getXWikiContext());
    Assert.assertFalse(document.isNew());
    Assert.assertEquals(Locale.ENGLISH, document.getDefaultLocale());
    Assert.assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
    Assert.assertEquals("customclass", document.getCustomClass());
    Assert.assertEquals("title", document.getTitle());
    Assert.assertEquals("defaultTemplate", document.getDefaultTemplate());
    Assert.assertEquals("validationScript", document.getValidationScript());
    Assert.assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
    Assert.assertEquals(true, document.isHidden());
    Assert.assertEquals("content", document.getContent());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
    Assert.assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
    Assert.assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
    Assert.assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
    Assert.assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
    Assert.assertEquals(true, document.isMinorEdit());
    Assert.assertEquals("comment", document.getComment());
    Assert.assertEquals("1.42", document.getVersion());
    // Attachment
    Assert.assertEquals(1, document.getAttachmentList().size());
    XWikiAttachment attachment = document.getAttachment("attachment.txt");
    Assert.assertEquals("attachment.txt", attachment.getFilename());
    Assert.assertEquals(10, attachment.getLongSize());
    Assert.assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, attachment.getContent(this.oldcore.getXWikiContext())));
    Assert.assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
    Assert.assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
    Assert.assertEquals("15.1", attachment.getVersion());
    Assert.assertEquals("attachment comment", attachment.getComment());
    // XClass
    BaseClass xclass = document.getXClass();
    Assert.assertEquals(1, xclass.getFieldList().size());
    Assert.assertEquals("customClass", xclass.getCustomClass());
    Assert.assertEquals("customMapping", xclass.getCustomMapping());
    Assert.assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
    Assert.assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
    Assert.assertEquals("defaultWeb", xclass.getDefaultWeb());
    Assert.assertEquals("nameField", xclass.getNameField());
    Assert.assertEquals("validationScript", xclass.getValidationScript());
    NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
    Assert.assertEquals("prop1", numberFiled.getName());
    Assert.assertEquals(false, numberFiled.isDisabled());
    Assert.assertEquals(1, numberFiled.getNumber());
    Assert.assertEquals("long", numberFiled.getNumberType());
    Assert.assertEquals("Prop1", numberFiled.getPrettyName());
    Assert.assertEquals(30, numberFiled.getSize());
    Assert.assertEquals(false, numberFiled.isUnmodifiable());
    // Objects
    Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
    Assert.assertEquals(2, objects.size());
    // Object 1
    List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
    Assert.assertEquals(1, documentObjects.size());
    BaseObject documentObject = documentObjects.get(0);
    Assert.assertEquals(0, documentObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
    Assert.assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
    Assert.assertEquals(1, documentObject.getFieldList().size());
    Assert.assertEquals(1, documentObject.getIntValue("prop1"));
    // Object 2
    List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
    Assert.assertEquals(1, otherObjects.size());
    BaseObject otherObject = otherObjects.get(0);
    Assert.assertEquals(0, otherObject.getNumber());
    Assert.assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
    Assert.assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
    Assert.assertEquals(1, otherObject.getFieldList().size());
    Assert.assertEquals(2, otherObject.getIntValue("prop2"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentInstanceOutputProperties(org.xwiki.filter.instance.output.DocumentInstanceOutputProperties) SyntaxType(org.xwiki.rendering.syntax.SyntaxType) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) NumberClass(com.xpn.xwiki.objects.classes.NumberClass) List(java.util.List) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test) AbstractInstanceFilterStreamTest(com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest)

Example 73 with BaseClass

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

the class UsedValuesListQueryBuilderTest method configure.

@Before
public void configure() throws Exception {
    this.queryManager = this.mocker.getInstance(QueryManager.class);
    this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
    this.documentReferenceResolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
    DocumentReference oneReference = new DocumentReference("wiki", "Page", "one");
    when(this.documentReferenceResolver.resolve("alice")).thenReturn(oneReference);
    when(this.authorization.hasAccess(Right.VIEW, oneReference)).thenReturn(false);
    DocumentReference twoReference = new DocumentReference("wiki", "Page", "two");
    when(this.documentReferenceResolver.resolve("alice")).thenReturn(twoReference);
    when(this.authorization.hasAccess(Right.VIEW, twoReference)).thenReturn(true);
    BaseClass xclass = new BaseClass();
    xclass.setDocumentReference(new DocumentReference("apps", "Blog", "BlogPostClass"));
    PropertyMetaClass metaClass = mock(PropertyMetaClass.class);
    when(metaClass.getName()).thenReturn("Blog.BlogPostClass");
    this.listClass = new DBListClass();
    this.listClass.setName("category");
    this.listClass.setObject(xclass);
    this.listClass.setxWikiClass(metaClass);
}
Also used : ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) DBListClass(com.xpn.xwiki.objects.classes.DBListClass) QueryManager(org.xwiki.query.QueryManager) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference) PropertyMetaClass(com.xpn.xwiki.objects.meta.PropertyMetaClass) Before(org.junit.Before)

Example 74 with BaseClass

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

the class R40001XWIKI7540DataMigration method checkAnnotationsAndComments.

/**
 * Check if the migration can be executed by verifying if the current annotation class is the default one and that
 * the comments class does not have any custom mappings set up.
 * <p>
 * Note: We can not do this in {@link #shouldExecute(XWikiDBVersion)} because we need to read the database and we
 * can not do that until the previous migrations are executed.
 *
 * @return true if the migration can be executed, false otherwise.
 * @throws DataMigrationException if the annotation or comments class can not be properly retrieved
 */
protected boolean checkAnnotationsAndComments() throws DataMigrationException {
    XWikiContext context = getXWikiContext();
    String resultOfSkippingDatabase = "Comments and anotations will remain separated";
    try {
        EntityReference currentAnnotationClassReference = configuration.getAnnotationClassReference();
        currentAnnotationClassReference = currentAnnotationClassReference.removeParent(new WikiReference(context.getWikiId()));
        if (!XWIKI_ANNOTATION_CLASS_REFERENCE.equals(currentAnnotationClassReference)) {
            logger.warn("Skipping database [{}] because it uses a custom annotation class. " + resultOfSkippingDatabase, context.getWikiId());
            return false;
        }
        BaseClass commentsClass = context.getWiki().getCommentsClass(context);
        if (commentsClass.hasCustomMapping()) {
            logger.warn("Skipping database [{}] because it uses a custom mapping for comments. " + resultOfSkippingDatabase, context.getWikiId());
            return false;
        }
    } catch (Exception e) {
        // Should not happen
        String message = "Failed to check the current annotation and comments classes for customizations. " + "Migration will not execute";
        logger.error(message, e);
        throw new DataMigrationException(message, e);
    }
    return true;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) WikiReference(org.xwiki.model.reference.WikiReference) XWikiException(com.xpn.xwiki.XWikiException) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) HibernateException(org.hibernate.HibernateException)

Example 75 with BaseClass

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

the class XWikiHibernateStore method deleteXWikiCollection.

/**
 * @deprecated This is internal to XWikiHibernateStore and may be removed in the future.
 */
@Deprecated
public void deleteXWikiCollection(BaseCollection object, XWikiContext inputxcontext, boolean bTransaction, boolean evict) throws XWikiException {
    if (object == null) {
        return;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // Let's check if the class has a custom mapping
        BaseClass bclass = object.getXClass(context);
        List<String> handledProps = new ArrayList<String>();
        if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
            handledProps = bclass.getCustomMappingPropertyList(context);
            Session dynamicSession = session.getSession(EntityMode.MAP);
            Object map = dynamicSession.get(bclass.getName(), object.getId());
            if (map != null) {
                if (evict) {
                    dynamicSession.evict(map);
                }
                dynamicSession.delete(map);
            }
        }
        if (object.getXClassReference() != null) {
            for (BaseElement property : (Collection<BaseElement>) object.getFieldList()) {
                if (!handledProps.contains(property.getName())) {
                    if (evict) {
                        session.evict(property);
                    }
                    if (session.get(property.getClass(), property) != null) {
                        session.delete(property);
                    }
                }
            }
        }
        // In case of custom class we need to force it as BaseObject to delete the xwikiobject row
        if (!"".equals(bclass.getCustomClass())) {
            BaseObject cobject = new BaseObject();
            cobject.setDocumentReference(object.getDocumentReference());
            cobject.setClassName(object.getClassName());
            cobject.setNumber(object.getNumber());
            if (object instanceof BaseObject) {
                cobject.setGuid(((BaseObject) object).getGuid());
            }
            cobject.setId(object.getId());
            if (evict) {
                session.evict(cobject);
            }
            session.delete(cobject);
        } else {
            if (evict) {
                session.evict(object);
            }
            session.delete(object);
        }
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (Exception e) {
        Object[] args = { object.getName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_OBJECT, "Exception while deleting object {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : BaseElement(com.xpn.xwiki.objects.BaseElement) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseCollection(com.xpn.xwiki.objects.BaseCollection) Collection(java.util.Collection) BaseObject(com.xpn.xwiki.objects.BaseObject) 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) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session) BaseObject(com.xpn.xwiki.objects.BaseObject)

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