Search in sources :

Example 16 with BaseClass

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

the class XWikiStats method fromXML.

/**
 * Initialize statistics object from XML schema.
 *
 * @param oel the XML root node containing statistics datas.
 * @throws XWikiException error when parsing XML schema.
 */
public void fromXML(Element oel) throws XWikiException {
    Element cel = oel.element("class");
    BaseClass bclass = new BaseClass();
    if (cel != null) {
        bclass.fromXML(cel);
        setClassName(bclass.getName());
    }
    setName(oel.element(XMLNODE_NAME).getText());
    List<?> list = oel.elements(XMLNODE_PROPERTY);
    for (int i = 0; i < list.size(); i++) {
        Element pcel = (Element) ((Element) list.get(i)).elements().get(0);
        String name = pcel.getName();
        PropertyClass pclass = (PropertyClass) bclass.get(name);
        if (pclass != null) {
            BaseProperty property = pclass.newPropertyfromXML(pcel);
            property.setName(name);
            property.setObject(this);
            safeput(name, property);
        }
    }
}
Also used : Element(org.dom4j.Element) DOMElement(org.dom4j.dom.DOMElement) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 17 with BaseClass

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

the class RightsManager method removeUserOrGroupFromAllRights.

/**
 * Remove all references to provided user or group from all rights documents.
 *
 * @param userOrGroupWiki the name of the wiki of the use or group.
 * @param userOrGroupSpace the name of the space of the use or group.
 * @param userOrGroupName the name of the use or group.
 * @param user indicate if it is a user or a group.
 * @param context the XWiki context.
 * @throws XWikiException error when browsing rights.
 */
public void removeUserOrGroupFromAllRights(String userOrGroupWiki, String userOrGroupSpace, String userOrGroupName, boolean user, XWikiContext context) throws XWikiException {
    List<String> parameterValues = new ArrayList<String>();
    String fieldName;
    if (user) {
        fieldName = RIGHTSFIELD_USERS;
    } else {
        fieldName = RIGHTSFIELD_GROUPS;
    }
    BaseClass rightClass = context.getWiki().getRightsClass(context);
    BaseClass globalRightClass = context.getWiki().getGlobalRightsClass(context);
    String fieldTypeName = ((PropertyClass) rightClass.get(fieldName)).newProperty().getClass().getSimpleName();
    StringBuilder where = new StringBuilder(", BaseObject as obj" + ", " + fieldTypeName + " as prop where doc.fullName=obj.name" + " and (obj.className=? or obj.className=?)");
    parameterValues.add(rightClass.getName());
    parameterValues.add(globalRightClass.getName());
    where.append(" and obj.id=prop.id.id");
    where.append(" and prop.name=?");
    parameterValues.add(fieldName);
    where.append(" and prop.value like ?");
    if (context.getWikiId() == null || context.getWikiId().equalsIgnoreCase(userOrGroupWiki)) {
        if (userOrGroupSpace == null || userOrGroupSpace.equals(DEFAULT_USERORGROUP_SPACE)) {
            parameterValues.add(HQLLIKE_ALL_SYMBOL + userOrGroupName + HQLLIKE_ALL_SYMBOL);
        } else {
            parameterValues.add(HQLLIKE_ALL_SYMBOL + userOrGroupSpace + SPACEPAGENAME_SEP + userOrGroupName + HQLLIKE_ALL_SYMBOL);
        }
    } else {
        parameterValues.add(HQLLIKE_ALL_SYMBOL + userOrGroupWiki + WIKIFULLNAME_SEP + userOrGroupName + HQLLIKE_ALL_SYMBOL);
    }
    List<XWikiDocument> documentList = context.getWiki().getStore().searchDocuments(where.toString(), parameterValues, context);
    for (XWikiDocument groupDocument : documentList) {
        if (removeUserOrGroupFromAllRights(groupDocument, userOrGroupWiki, userOrGroupSpace, userOrGroupName, user, context)) {
            context.getWiki().saveDocument(groupDocument, context);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 18 with BaseClass

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

the class ObjectsResourceImpl method addObject.

@Override
public Response addObject(String wikiName, String spaceName, String pageName, Boolean minorRevision, Object object) throws XWikiRestException {
    if (object.getClassName() == null) {
        throw new WebApplicationException(Status.BAD_REQUEST);
    }
    try {
        List<String> spaces = parseSpaceSegments(spaceName);
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.newObject(object.getClassName(), Utils.getXWikiContext(componentManager));
        if (xwikiObject == null) {
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        // We must initialize all the fields to an empty value in order to correctly create the object
        BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
        for (java.lang.Object propertyNameObject : xwikiClass.getPropertyNames()) {
            String propertyName = (String) propertyNameObject;
            xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
        }
        for (Property property : object.getProperties()) {
            xwikiObject.set(property.getName(), property.getValue(), Utils.getXWikiContext(componentManager));
        }
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.created(Utils.createURI(uriInfo.getBaseUri(), ObjectResource.class, wikiName, spaces, pageName, object.getClassName(), xwikiObject.getNumber())).entity(DomainObjectFactory.createObject(objectFactory, uriInfo.getBaseUri(), Utils.getXWikiContext(componentManager), doc, xwikiObject, false, Utils.getXWikiApi(componentManager), false)).build();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ObjectResource(org.xwiki.rest.resources.objects.ObjectResource) Property(org.xwiki.rest.model.jaxb.Property) XWikiException(com.xpn.xwiki.XWikiException)

Example 19 with BaseClass

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

the class PageTagsResourceImpl method setTags.

@Override
public Response setTags(String wikiName, String spaceName, String pageName, Boolean minorRevision, Tags tags) throws XWikiRestException {
    try {
        DocumentInfo documentInfo = getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
        Document doc = documentInfo.getDocument();
        if (!doc.hasAccessLevel("edit", Utils.getXWikiUser(componentManager))) {
            throw new WebApplicationException(Status.UNAUTHORIZED);
        }
        List<String> tagNames = new ArrayList<String>();
        for (Tag tag : tags.getTags()) {
            tagNames.add(tag.getName());
        }
        XWikiDocument xwikiDocument = Utils.getXWiki(componentManager).getDocument(doc.getDocumentReference(), Utils.getXWikiContext(componentManager));
        BaseObject xwikiObject = xwikiDocument.getObject("XWiki.TagClass", 0);
        if (xwikiObject == null) {
            int objectNumber = xwikiDocument.createNewObject("XWiki.TagClass", Utils.getXWikiContext(componentManager));
            xwikiObject = xwikiDocument.getObject("XWiki.TagClass", objectNumber);
            if (xwikiObject == null) {
                throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
            }
            // We must initialize all the fields to an empty value in order to correctly create the object
            BaseClass xwikiClass = Utils.getXWiki(componentManager).getClass(xwikiObject.getClassName(), Utils.getXWikiContext(componentManager));
            for (Object propertyNameObject : xwikiClass.getPropertyNames()) {
                String propertyName = (String) propertyNameObject;
                xwikiObject.set(propertyName, "", Utils.getXWikiContext(componentManager));
            }
        }
        xwikiObject.set("tags", tagNames, Utils.getXWikiContext(componentManager));
        doc.save("", Boolean.TRUE.equals(minorRevision));
        return Response.status(Status.ACCEPTED).entity(tags).build();
    } catch (XWikiException e) {
        throw new XWikiRestException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) XWikiRestException(org.xwiki.rest.XWikiRestException) ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) Tag(org.xwiki.rest.model.jaxb.Tag) XWikiException(com.xpn.xwiki.XWikiException)

Example 20 with BaseClass

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

the class DefaultClassPropertyValuesProvider method getPropertyType.

private String getPropertyType(ClassPropertyReference propertyReference) throws XWikiRestException {
    try {
        XWikiContext xcontext = this.xcontextProvider.get();
        XWikiDocument document = xcontext.getWiki().getDocument(propertyReference.getParent(), xcontext);
        BaseClass xclass = document.getXClass();
        PropertyInterface xproperty = xclass.get(propertyReference.getName());
        if (xproperty instanceof PropertyClass) {
            return ((PropertyClass) xproperty).getClassType();
        } else {
            throw new XWikiRestException(String.format("No such property [%s].", this.entityReferenceSerializer.serialize(propertyReference)));
        }
    } catch (XWikiException e) {
        throw new XWikiRestException(String.format("Failed to determine the property type for [{}].", this.entityReferenceSerializer.serialize(propertyReference)));
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) XWikiRestException(org.xwiki.rest.XWikiRestException) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) 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