Search in sources :

Example 26 with BaseClass

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

the class XWikiDocument method getXClass.

/**
 * @since 2.2M1
 */
public BaseClass getXClass() {
    if (this.xClass == null) {
        BaseClass emptyClass = new BaseClass();
        // Make sure not to cause any false document versions if this document is saved.
        emptyClass.setDirty(false);
        this.setXClass(emptyClass);
    }
    return this.xClass;
}
Also used : BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 27 with BaseClass

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

the class XWikiDocument method readObjectsFromForm.

public void readObjectsFromForm(EditForm eform, XWikiContext context) throws XWikiException {
    for (DocumentReference reference : getXObjects().keySet()) {
        List<BaseObject> oldObjects = getXObjects(reference);
        List<BaseObject> newObjects = new ArrayList<BaseObject>();
        while (newObjects.size() < oldObjects.size()) {
            newObjects.add(null);
        }
        for (int i = 0; i < oldObjects.size(); i++) {
            BaseObject oldobject = oldObjects.get(i);
            if (oldobject != null) {
                BaseClass baseclass = oldobject.getXClass(context);
                BaseObject newobject = (BaseObject) baseclass.fromMap(eform.getObject(LOCAL_REFERENCE_SERIALIZER.serialize(baseclass.getDocumentReference()) + "_" + i), oldobject);
                newobject.setNumber(oldobject.getNumber());
                newobject.setGuid(oldobject.getGuid());
                newobject.setOwnerDocument(this);
                newObjects.set(newobject.getNumber(), newobject);
            }
        }
        getXObjects().put(reference, newObjects);
    }
}
Also used : ArrayList(java.util.ArrayList) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 28 with BaseClass

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

the class XWikiDocument method getClassDiff.

public List<List<ObjectDiff>> getClassDiff(XWikiDocument fromDoc, XWikiDocument toDoc, XWikiContext context) {
    List<List<ObjectDiff>> difflist = new ArrayList<List<ObjectDiff>>();
    BaseClass oldClass = fromDoc.getXClass();
    BaseClass newClass = toDoc.getXClass();
    if ((newClass == null) && (oldClass == null)) {
        return difflist;
    }
    List<ObjectDiff> dlist = newClass.getDiff(oldClass, context);
    if (!dlist.isEmpty()) {
        difflist.add(dlist);
    }
    return difflist;
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) ArrayList(java.util.ArrayList) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachmentList(com.xpn.xwiki.internal.doc.XWikiAttachmentList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 29 with BaseClass

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

the class XWikiDocument method readObjectsFromFormUpdateOrCreate.

/**
 * Create and/or update objects in a document given a list of HTTP parameters of the form {@code
 * <spacename>.<classname>_<number>_<propertyname>}. If the object already exists, the field is replace by the given
 * value. If the object doesn't exist in the document, it is created then the property {@code <propertyname>} is
 * initialized with the given value. An object is only created if the given {@code <number>} is 'one-more' than the
 * existing number of objects. For example, if the document already has 2 objects of type {@code Space.Class}, then
 * it will create a new object only with {@code Space.Class_2_prop=something}. Every other parameter like {@code
 * Space.Class_42_prop=foobar} for example, will be ignore.
 *
 * @param eform is form information that contains all the query parameters
 * @param context
 * @throws XWikiException
 * @since 7.1M1
 */
public void readObjectsFromFormUpdateOrCreate(EditForm eform, XWikiContext context) throws XWikiException {
    Map<DocumentReference, SortedMap<Integer, Map<String, String[]>>> fromRequest = parseRequestUpdateOrCreate(eform.getRequest(), context);
    for (Entry<DocumentReference, SortedMap<Integer, Map<String, String[]>>> requestClassEntries : fromRequest.entrySet()) {
        DocumentReference requestClassReference = requestClassEntries.getKey();
        SortedMap<Integer, Map<String, String[]>> requestObjectMap = requestClassEntries.getValue();
        for (Entry<Integer, Map<String, String[]>> requestObjectEntry : requestObjectMap.entrySet()) {
            Integer requestObjectNumber = requestObjectEntry.getKey();
            Map<String, String[]> requestObjectPropertyMap = requestObjectEntry.getValue();
            BaseObject oldObject = getXObject(requestClassReference, requestObjectNumber);
            if (oldObject == null) {
                // Create the object only if it has been numbered one more than the number of existing objects
                if (requestObjectPropertyMap != null) {
                    oldObject = newXObject(requestClassReference, context);
                } else {
                    break;
                }
            }
            BaseClass baseClass = oldObject.getXClass(context);
            BaseObject newObject = (BaseObject) baseClass.fromMap(requestObjectPropertyMap, oldObject);
            newObject.setNumber(oldObject.getNumber());
            newObject.setGuid(oldObject.getGuid());
            newObject.setOwnerDocument(this);
            setXObject(requestObjectNumber, newObject);
        }
    }
}
Also used : SortedMap(java.util.SortedMap) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ToString(org.suigeneris.jrcs.util.ToString) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 30 with BaseClass

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

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