Search in sources :

Example 1 with TextAreaClass

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

the class XWikiDocument method convertSyntax.

/**
 * Convert the current document content from its current syntax to the new syntax passed as parameter.
 *
 * @param targetSyntax the syntax to convert to (e.g. {@code xwiki/2.0}, {@code xhtml/1.0}, etc)
 * @throws XWikiException if an exception occurred during the conversion process
 */
public void convertSyntax(Syntax targetSyntax, XWikiContext context) throws XWikiException {
    // convert content
    setContent(performSyntaxConversion(getContent(), getDocumentReference(), getSyntax(), targetSyntax));
    // convert objects
    Map<DocumentReference, List<BaseObject>> objectsByClass = getXObjects();
    for (List<BaseObject> objects : objectsByClass.values()) {
        for (BaseObject bobject : objects) {
            if (bobject != null) {
                BaseClass bclass = bobject.getXClass(context);
                for (Object fieldClass : bclass.getProperties()) {
                    if (fieldClass instanceof TextAreaClass && ((TextAreaClass) fieldClass).isWikiContent()) {
                        TextAreaClass textAreaClass = (TextAreaClass) fieldClass;
                        LargeStringProperty field = (LargeStringProperty) bobject.getField(textAreaClass.getName());
                        if (field != null) {
                            field.setValue(performSyntaxConversion(field.getValue(), getDocumentReference(), getSyntax(), targetSyntax));
                        }
                    }
                }
            }
        }
    }
    // change syntax
    setSyntax(targetSyntax);
}
Also used : LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachmentList(com.xpn.xwiki.internal.doc.XWikiAttachmentList) ArrayList(java.util.ArrayList) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with TextAreaClass

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

the class XWikiDocumentMockitoTest method generateFakeClass.

/**
 * Generate the fake class that is used for the test of {@link #readObjectsFromForm()} and
 * {@link #readObjectsFromFormUpdateOrCreate()}.
 *
 * @return The fake BaseClass
 */
private BaseClass generateFakeClass() {
    BaseClass baseClass = this.document.getXClass();
    baseClass.addTextField("string", "String", 30);
    baseClass.addTextAreaField("area", "Area", 10, 10);
    baseClass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
    // set the text areas an non interpreted content
    ((TextAreaClass) baseClass.getField("puretextarea")).setContentType("puretext");
    baseClass.addPasswordField("passwd", "Password", 30);
    baseClass.addBooleanField("boolean", "Boolean", "yesno");
    baseClass.addNumberField("int", "Int", 10, "integer");
    baseClass.addStaticListField("stringlist", "StringList", "value1, value2");
    return baseClass;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass)

Example 3 with TextAreaClass

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

the class XWikiDocumentTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.defaultEntityReferenceSerializer = getComponentManager().getInstance(EntityReferenceSerializer.TYPE_STRING);
    this.document = new XWikiDocument(new DocumentReference(DOCWIKI, DOCSPACE, DOCNAME));
    this.document.setSyntax(Syntax.XWIKI_2_0);
    this.document.setLanguage("en");
    this.document.setDefaultLanguage("en");
    this.document.setNew(false);
    this.translatedDocument = new XWikiDocument();
    this.translatedDocument.setSyntax(Syntax.XWIKI_2_0);
    this.translatedDocument.setLanguage("fr");
    this.translatedDocument.setNew(false);
    getContext().put("isInRenderingEngine", true);
    this.mockXWiki = mock(XWiki.class);
    this.mockXWikiVersioningStore = mock(XWikiVersioningStoreInterface.class);
    this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
    this.mockXWikiStoreInterface = mock(XWikiStoreInterface.class);
    this.document.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
    this.mockXWikiMessageTool = mock(XWikiMessageTool.class, new Class[] { ResourceBundle.class, XWikiContext.class }, new Object[] { null, getContext() });
    this.mockXWikiMessageTool.stubs().method("get").will(returnValue("message"));
    this.mockXWikiRightService = mock(XWikiRightService.class);
    this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
    this.mockXWiki.stubs().method("getVersioningStore").will(returnValue(this.mockXWikiVersioningStore.proxy()));
    this.mockXWiki.stubs().method("getStore").will(returnValue(this.mockXWikiStoreInterface.proxy()));
    this.mockXWiki.stubs().method("getDocument").will(returnValue(this.document));
    this.mockXWiki.stubs().method("getLanguagePreference").will(returnValue("en"));
    this.mockXWiki.stubs().method("getSectionEditingDepth").will(returnValue(2L));
    this.mockXWiki.stubs().method("getRightService").will(returnValue(this.mockXWikiRightService.proxy()));
    getContext().setWiki((XWiki) this.mockXWiki.proxy());
    getContext().put("msg", this.mockXWikiMessageTool.proxy());
    this.baseClass = this.document.getxWikiClass();
    this.baseClass.addTextField("string", "String", 30);
    this.baseClass.addTextAreaField("area", "Area", 10, 10);
    this.baseClass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
    // set the text areas an non interpreted content
    ((TextAreaClass) this.baseClass.getField("puretextarea")).setContentType("puretext");
    this.baseClass.addPasswordField("passwd", "Password", 30);
    this.baseClass.addBooleanField("boolean", "Boolean", "yesno");
    this.baseClass.addNumberField("int", "Int", 10, "integer");
    this.baseClass.addStaticListField("stringlist", "StringList", "value1, value2");
    this.mockXWiki.stubs().method("getClass").will(returnValue(this.baseClass));
    this.mockXWiki.stubs().method("getXClass").will(returnValue(this.baseClass));
    this.baseObject = this.document.newObject(CLASSNAME, getContext());
    this.baseObject.setStringValue("string", "string");
    this.baseObject.setLargeStringValue("area", "area");
    this.baseObject.setStringValue("passwd", "passwd");
    this.baseObject.setIntValue("boolean", 1);
    this.baseObject.setIntValue("int", 42);
    this.baseObject.setStringListValue("stringlist", Arrays.asList("VALUE1", "VALUE2"));
    this.mockXWikiStoreInterface.stubs().method("search").will(returnValue(new ArrayList<XWikiDocument>()));
    // Set the default link label generator format to %np for some tests below.
    // We need to do this since we don't depend on xwiki-platform-rendering-configuration-default (which contains
    // an overridden RenderingConfiguration impl that sets the format to %np by default).
    DefaultRenderingConfiguration renderingConfiguration = getComponentManager().getInstance(RenderingConfiguration.class);
    renderingConfiguration.setLinkLabelFormat("%np");
}
Also used : DefaultRenderingConfiguration(org.xwiki.rendering.internal.configuration.DefaultRenderingConfiguration) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiVersioningStoreInterface(com.xpn.xwiki.store.XWikiVersioningStoreInterface) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ResourceBundle(java.util.ResourceBundle) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiMessageTool(com.xpn.xwiki.web.XWikiMessageTool) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 4 with TextAreaClass

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

the class XWikiDocumentMergeTest method before.

@Before
public void before() throws Exception {
    this.oldcore.registerMockEnvironment();
    this.currentDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
    this.previousDocument = this.currentDocument.clone();
    this.nextDocument = this.currentDocument.clone();
    this.xclass = new BaseClass();
    this.xclass.setDocumentReference(new DocumentReference("wiki", "classspace", "class"));
    this.xclass.addTextField("string", "String", 30);
    this.xclass.addTextAreaField("area", "Area", 10, 10);
    this.xclass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
    // set the text areas an non interpreted content
    ((TextAreaClass) this.xclass.getField("puretextarea")).setContentType("puretext");
    this.xclass.addPasswordField("passwd", "Password", 30);
    this.xclass.addBooleanField("boolean", "Boolean", "yesno");
    this.xclass.addNumberField("int", "Int", 10, "integer");
    this.xclass.addStaticListField("stringlist", "StringList", "value1, value2");
    this.xobject = new BaseObject();
    this.xobject.setXClassReference(this.xclass.getDocumentReference());
    this.xobject.setStringValue("string", "string");
    this.xobject.setLargeStringValue("area", "area");
    this.xobject.setStringValue("passwd", "passwd");
    this.xobject.setIntValue("boolean", 1);
    this.xobject.setIntValue("int", 42);
    this.xobject.setStringListValue("stringlist", Arrays.asList("VALUE1", "VALUE2"));
    this.configuration = new MergeConfiguration();
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass) MergeConfiguration(com.xpn.xwiki.doc.merge.MergeConfiguration) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Before(org.junit.Before)

Example 5 with TextAreaClass

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

TextAreaClass (com.xpn.xwiki.objects.classes.TextAreaClass)6 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ArrayList (java.util.ArrayList)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 LargeStringProperty (com.xpn.xwiki.objects.LargeStringProperty)2 StringClass (com.xpn.xwiki.objects.classes.StringClass)2 List (java.util.List)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiException (com.xpn.xwiki.XWikiException)1 MergeConfiguration (com.xpn.xwiki.doc.merge.MergeConfiguration)1 XWikiAttachmentList (com.xpn.xwiki.internal.doc.XWikiAttachmentList)1 BaseCollection (com.xpn.xwiki.objects.BaseCollection)1 BaseProperty (com.xpn.xwiki.objects.BaseProperty)1 BaseStringProperty (com.xpn.xwiki.objects.BaseStringProperty)1 StringProperty (com.xpn.xwiki.objects.StringProperty)1 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiVersioningStoreInterface (com.xpn.xwiki.store.XWikiVersioningStoreInterface)1