Search in sources :

Example 11 with BaseObject

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

the class DefaultMailTemplateManager method getMailObjectsCount.

private int getMailObjectsCount(DocumentReference templateReference, DocumentReference mailClassReference) throws MessagingException {
    XWikiContext context = this.xwikiContextProvider.get();
    int objectsCount;
    try {
        List<BaseObject> objects = context.getWiki().getDocument(templateReference, context).getXObjects(mailClassReference);
        if (objects != null) {
            objectsCount = objects.size();
        } else {
            objectsCount = 0;
        }
    } catch (XWikiException e) {
        throw new MessagingException(String.format("Failed to find number of [%s] objects in Document [%s]", mailClassReference, templateReference), e);
    }
    return objectsCount;
}
Also used : MessagingException(javax.mail.MessagingException) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 12 with BaseObject

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

the class DefaultModelBridge method setStartDateForUser.

@Override
public void setStartDateForUser(DocumentReference userReference, Date startDate) throws NotificationException {
    try {
        XWikiContext context = contextProvider.get();
        XWiki xwiki = context.getWiki();
        XWikiDocument document = xwiki.getDocument(userReference, context);
        List<BaseObject> objects = document.getXObjects(NOTIFICATION_PREFERENCE_CLASS);
        if (objects != null) {
            for (BaseObject object : objects) {
                if (object != null) {
                    object.setDateValue(START_DATE_FIELD, startDate);
                }
            }
        }
        // Make this change a minor edit so it's not displayed, by default, in notifications
        xwiki.saveDocument(document, NOTIFICATION_START_DATE_UPDATE_COMMENT, true, context);
    } catch (Exception e) {
        throw new NotificationException(String.format(SET_USER_START_DATE_ERROR_MESSAGE, userReference), e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 13 with BaseObject

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

the class DefaultModelBridge method getNotificationPreferences.

private List<NotificationPreference> getNotificationPreferences(DocumentReference document, String providerHint) throws NotificationException {
    XWikiContext context = contextProvider.get();
    XWiki xwiki = context.getWiki();
    final DocumentReference notificationPreferencesClass = NOTIFICATION_PREFERENCE_CLASS.setWikiReference(document.getWikiReference());
    List<NotificationPreference> preferences = new ArrayList<>();
    try {
        XWikiDocument doc = xwiki.getDocument(document, context);
        List<BaseObject> preferencesObj = doc.getXObjects(notificationPreferencesClass);
        if (preferencesObj != null) {
            for (BaseObject obj : preferencesObj) {
                if (obj != null) {
                    String objFormat = obj.getStringValue(FORMAT_FIELD);
                    Date objStartDate = obj.getDateValue(START_DATE_FIELD);
                    Map<NotificationPreferenceProperty, Object> properties = extractNotificationPreferenceProperties(obj);
                    notificationPreferenceBuilder.prepare();
                    notificationPreferenceBuilder.setProperties(properties);
                    notificationPreferenceBuilder.setStartDate((objStartDate != null) ? objStartDate : doc.getCreationDate());
                    notificationPreferenceBuilder.setFormat(StringUtils.isNotBlank(objFormat) ? NotificationFormat.valueOf(objFormat.toUpperCase()) : NotificationFormat.ALERT);
                    notificationPreferenceBuilder.setTarget(document);
                    notificationPreferenceBuilder.setProviderHint(providerHint);
                    notificationPreferenceBuilder.setEnabled(obj.getIntValue(NOTIFICATION_ENABLED_FIELD, 0) != 0);
                    notificationPreferenceBuilder.setCategory(NotificationPreferenceCategory.DEFAULT);
                    preferences.add(notificationPreferenceBuilder.build());
                }
            }
        }
    } catch (Exception e) {
        throw new NotificationException(String.format("Failed to get the notification preferences from the document [%s].", document), e);
    }
    return preferences;
}
Also used : ArrayList(java.util.ArrayList) NotificationException(org.xwiki.notifications.NotificationException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) NotificationPreferenceProperty(org.xwiki.notifications.preferences.NotificationPreferenceProperty) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) NotificationException(org.xwiki.notifications.NotificationException) BaseObject(com.xpn.xwiki.objects.BaseObject) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 14 with BaseObject

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

the class WikiSkinUtils method getSkinResourceFromDocumentSkin.

private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin) {
    if (skinDocument != null) {
        // Try to find a XWikiSkinFileOverrideClass object
        BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
        if (obj != null) {
            ObjectPropertyReference reference = new ObjectPropertyReference(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, obj.getReference());
            return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT));
        }
        // Try parsing the object property
        BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
        if (property != null) {
            ObjectPropertyReference reference = property.getReference();
            return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue());
        }
        // Try parsing a document attachment
        // Convert "/" into "." in order to support wiki skin attachments to override some resources located in
        // subdirectories.
        String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
        XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
        if (attachment != null) {
            AttachmentReference reference = attachment.getReference();
            return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), this.xcontextProvider);
        }
    }
    return null;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 15 with BaseObject

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

the class WikiSkinUtils method getSkinResourceProperty.

private BaseProperty<ObjectPropertyReference> getSkinResourceProperty(String resource, XWikiDocument skinDocument) {
    // Try parsing the object property
    BaseObject skinObject = skinDocument.getXObject(SKINCLASS_REFERENCE);
    if (skinObject != null) {
        BaseProperty<ObjectPropertyReference> resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(resource);
        // If not found try by replacing '/' with '.'
        if (resourceProperty == null) {
            String escapedTemplateName = StringUtils.replaceChars(resource, '/', '.');
            resourceProperty = (BaseProperty<ObjectPropertyReference>) skinObject.safeget(escapedTemplateName);
        }
        if (resourceProperty != null) {
            Object value = resourceProperty.getValue();
            if (value instanceof String && StringUtils.isNotEmpty((String) value)) {
                return resourceProperty;
            }
        }
    }
    return null;
}
Also used : ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) 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