Search in sources :

Example 41 with BaseObject

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

the class SxDocumentSource method getContent.

@Override
public String getContent() {
    StringBuilder resultBuilder = new StringBuilder();
    List<BaseObject> objects = this.document.getObjects(this.extension.getClassName());
    if (objects != null) {
        for (BaseObject sxObj : objects) {
            if (sxObj == null) {
                continue;
            }
            String sxContent = sxObj.getLargeStringValue(CONTENT_PROPERTY_NAME);
            int parse = sxObj.getIntValue(PARSE_CONTENT_PROPERTY_NAME);
            if ("LESS".equals(sxObj.getStringValue(CONTENT_TYPE_PROPERTY_NAME))) {
                LESSCompiler lessCompiler = Utils.getComponent(LESSCompiler.class);
                LESSResourceReferenceFactory lessResourceReferenceFactory = Utils.getComponent(LESSResourceReferenceFactory.class);
                ObjectPropertyReference objectPropertyReference = new ObjectPropertyReference(CONTENT_PROPERTY_NAME, sxObj.getReference());
                LESSResourceReference lessResourceReference = lessResourceReferenceFactory.createReferenceForXObjectProperty(objectPropertyReference);
                try {
                    sxContent = lessCompiler.compile(lessResourceReference, true, (parse == 1), false);
                } catch (LESSCompilerException e) {
                    // Set the error message in a CSS comment to help the developer understand why its SSX is not
                    // working (it will work only if the CSS minifier is not used).
                    sxContent = String.format("/* LESS errors while parsing skin extension [%s]. */\n/* %s */", sxObj.getStringValue(NAME_PROPERTY_NAME), ExceptionUtils.getRootCauseMessage(e));
                }
            } else if (parse == 1) {
                try {
                    StringWriter writer = new StringWriter();
                    VelocityManager velocityManager = Utils.getComponent(VelocityManager.class);
                    VelocityContext vcontext = velocityManager.getVelocityContext();
                    velocityManager.getVelocityEngine().evaluate(vcontext, writer, this.document.getPrefixedFullName(), sxContent);
                    sxContent = writer.toString();
                } catch (XWikiVelocityException ex) {
                    LOGGER.warn("Velocity errors while parsing skin extension [{}] with content [{}]: ", this.document.getPrefixedFullName(), sxContent, ExceptionUtils.getRootCauseMessage(ex));
                }
            }
            // Also add a newline, in case the different object contents don't end with a blank
            // line, and could cause syntax errors when concatenated.
            resultBuilder.append(sxContent + "\n");
        }
    }
    return resultBuilder.toString();
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) VelocityContext(org.apache.velocity.VelocityContext) LESSCompiler(org.xwiki.lesscss.compiler.LESSCompiler) LESSCompilerException(org.xwiki.lesscss.compiler.LESSCompilerException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiVelocityException(org.xwiki.velocity.XWikiVelocityException) LESSResourceReferenceFactory(org.xwiki.lesscss.resources.LESSResourceReferenceFactory) StringWriter(java.io.StringWriter) VelocityManager(org.xwiki.velocity.VelocityManager) LESSResourceReference(org.xwiki.lesscss.resources.LESSResourceReference)

Example 42 with BaseObject

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

the class DefaultWikiUserManager method removeMembers.

@Override
public void removeMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // Get the member objects
    List<BaseObject> objects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (objects != null) {
        // Get the member objects to remove
        List<BaseObject> objectsToRemove = new ArrayList<>();
        for (String userId : userIds) {
            for (BaseObject object : objects) {
                if (object == null) {
                    continue;
                }
                String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
                if (userId.equals(member)) {
                    objectsToRemove.add(object);
                }
            }
        }
        // Remove them
        for (BaseObject object : objectsToRemove) {
            groupDoc.removeXObject(object);
        }
        // Save the document
        saveGroupDocument(groupDoc, "Remove some users from the group.");
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 43 with BaseObject

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

the class DefaultWikiUserManager method addMembers.

@Override
public void addMembers(Collection<String> userIds, String wikiId) throws WikiUserManagerException {
    Collection<String> members = getMembers(wikiId);
    // Get the group document
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    // If the group does not contain any user yet, add an empty member (cf: XWIKI-6275).
    List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (memberObjects == null || memberObjects.isEmpty()) {
        addMemberObject(groupDoc, "");
    }
    // Add members
    for (String userId : userIds) {
        if (!members.contains(userId)) {
            // Add a member object
            addMemberObject(groupDoc, userId);
        }
    }
    // Save the document
    saveGroupDocument(groupDoc, "Add members to the group.");
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 44 with BaseObject

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

the class DefaultWikiUserManager method addMemberObject.

private void addMemberObject(XWikiDocument groupDoc, String userId) throws WikiUserManagerException {
    try {
        XWikiContext xcontext = xcontextProvider.get();
        int objectNumber = groupDoc.createXObject(GROUPCLASS_REFERENCE, xcontext);
        BaseObject object = groupDoc.getXObject(GROUPCLASS_REFERENCE, objectNumber);
        object.set(GROUP_CLASS_MEMBER_FIELD, userId, xcontext);
    } catch (XWikiException e) {
        throw new WikiUserManagerException("Fail to add a member to the group", e);
    }
}
Also used : WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 45 with BaseObject

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

the class DefaultWikiUserManager method getMembers.

@Override
public Collection<String> getMembers(String wikiId) throws WikiUserManagerException {
    List<String> members = new ArrayList<>();
    try {
        // Get the descriptor
        WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
        // Add the wiki owner
        members.add(descriptor.getOwnerId());
    } catch (WikiManagerException e) {
        throw new WikiUserManagerException(String.format("Failed to get the descriptor for [%s]", wikiId), e);
    }
    // Get the other members from the wiki AllGroup
    XWikiDocument groupDoc = getMembersGroupDocument(wikiId);
    List<BaseObject> memberObjects = groupDoc.getXObjects(GROUPCLASS_REFERENCE);
    if (memberObjects != null) {
        for (BaseObject object : memberObjects) {
            if (object == null) {
                continue;
            }
            String member = object.getStringValue(GROUP_CLASS_MEMBER_FIELD);
            if (!member.isEmpty() && !members.contains(member)) {
                members.add(member);
            }
        }
    }
    return members;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiUserManagerException(org.xwiki.wiki.user.WikiUserManagerException) ArrayList(java.util.ArrayList) WikiDescriptor(org.xwiki.wiki.descriptor.WikiDescriptor) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16