Search in sources :

Example 1 with BaseProperty

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

the class SendMailConfigClassDocumentConfigurationSourceTest method getPropertyWhenSendMailConfigClassXObjectExists.

@Test
public void getPropertyWhenSendMailConfigClassXObjectExists() throws Exception {
    ConverterManager converterManager = this.mocker.getInstance(ConverterManager.class);
    when(converterManager.convert(String.class, "value")).thenReturn("value");
    Cache<Object> cache = mock(Cache.class);
    CacheManager cacheManager = this.mocker.getInstance(CacheManager.class);
    when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(cache);
    WikiDescriptorManager wikiDescriptorManager = this.mocker.getInstance(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wiki");
    LocalDocumentReference classReference = new LocalDocumentReference("Mail", "SendMailConfigClass");
    BaseProperty property = mock(BaseProperty.class);
    when(property.toText()).thenReturn("value");
    BaseObject object = mock(BaseObject.class);
    when(object.getField("key")).thenReturn(property);
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getXObject(classReference)).thenReturn(object);
    DocumentReference documentReference = new DocumentReference("wiki", "Mail", "MailConfig");
    XWiki xwiki = mock(XWiki.class);
    when(xwiki.getDocument(eq(documentReference), any(XWikiContext.class))).thenReturn(document);
    XWikiContext xcontext = mock(XWikiContext.class);
    when(xcontext.getWiki()).thenReturn(xwiki);
    Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(xcontext);
    assertEquals("value", this.mocker.getComponentUnderTest().getProperty("key", "defaultValue"));
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ConverterManager(org.xwiki.properties.ConverterManager) CacheManager(org.xwiki.cache.CacheManager) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Test(org.junit.Test)

Example 2 with BaseProperty

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

Example 3 with BaseProperty

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

the class XClassMigratorListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XClassPropertyUpdatedEvent propertyEvent = (XClassPropertyUpdatedEvent) event;
    XWikiDocument newDocument = (XWikiDocument) source;
    XWikiDocument previousDocument = newDocument.getOriginalDocument();
    PropertyClass newPropertyClass = (PropertyClass) newDocument.getXClass().getField(propertyEvent.getReference().getName());
    PropertyClass previousPropertyClass = (PropertyClass) previousDocument.getXClass().getField(propertyEvent.getReference().getName());
    if (newPropertyClass != null && previousPropertyClass != null) {
        BaseProperty newProperty = newPropertyClass.newProperty();
        BaseProperty previousProperty = previousPropertyClass.newProperty();
        // New and previous class property generate different kind of properties
        if (newProperty.getClass() != previousProperty.getClass()) {
            try {
                migrate(newPropertyClass);
            } catch (QueryException e) {
                this.logger.error("Failed to migrate XClass property [{}]", newPropertyClass.getReference(), e);
            }
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) XClassPropertyUpdatedEvent(com.xpn.xwiki.internal.event.XClassPropertyUpdatedEvent) BaseProperty(com.xpn.xwiki.objects.BaseProperty) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass)

Example 4 with BaseProperty

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

the class DefaultRatingsConfigurationTest method getConfigurationParameter.

@Test
public void getConfigurationParameter() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Space1", "Space2"), "Page");
    XWikiDocument spaceConfigurationDocument = mock(XWikiDocument.class, "Space Configuration Document");
    DocumentReference configurationDocumentReference = new DocumentReference("wiki", "Space1", "WebPreferences");
    when(xcontext.getWiki().getDocument((EntityReference) configurationDocumentReference, xcontext)).thenReturn(spaceConfigurationDocument);
    BaseObject configurationObject = mock(BaseObject.class);
    when(spaceConfigurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE)).thenReturn(configurationObject);
    BaseProperty displayRatings = mock(BaseProperty.class);
    when(configurationObject.get("displayRatings")).thenReturn(displayRatings);
    when(displayRatings.getValue()).thenReturn("1");
    assertEquals("1", mocker.getComponentUnderTest().getConfigurationParameter(documentReference, "displayRatings", "1"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 5 with BaseProperty

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

the class ConfiguredReputationAlgorithmProvider method get.

/**
 * Retrieve an instance of the desired ReputationAlorithm (default/simple/custom).
 *
 * @param documentRef documentRef the document to which the ratings are associated to
 * @return the reputation algorithm selected by looking at the current configuration settings
 */
@Override
public ReputationAlgorithm get(DocumentReference documentRef) {
    String defaultAlgorithmHint = "default";
    String reputationAlgorithmHint = getXWiki().Param(RatingsManager.RATINGS_CONFIG_PARAM_PREFIX + RatingsManager.RATINGS_CONFIG_FIELDNAME_REPUTATIONALGORITHM_HINT, defaultAlgorithmHint);
    try {
        XWikiDocument configurationDocument = ratingsConfiguration.getConfigurationDocument(documentRef);
        if (configurationDocument != null && !configurationDocument.isNew() && configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE) != null) {
            BaseProperty prop = (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_ALGORITHM_HINT);
            String hint = (prop == null) ? null : (String) prop.getValue();
            if (hint == "custom") {
                prop = (BaseProperty) configurationDocument.getXObject(RatingsManager.RATINGS_CONFIG_CLASSREFERENCE).get(RatingsManager.RATINGS_CONFIG_CLASS_FIELDNAME_REPUTATION_CUSTOM_ALGORITHM);
                hint = (prop == null) ? null : (String) prop.getValue();
            }
            reputationAlgorithmHint = (hint == null) ? reputationAlgorithmHint : hint;
        }
    } catch (Exception e) {
        logger.error("Cannot read reputation algorithm config", e);
    }
    // if the reputation algorithm hint is a page let's try to get the instance from groovy
    if (reputationAlgorithmHint.contains(".")) {
        try {
            ReputationAlgorithmGroovy reputationInstance = (ReputationAlgorithmGroovy) getXWiki().parseGroovyFromPage(reputationAlgorithmHint, getXWikiContext());
            if (reputationInstance != null) {
                reputationInstance.setComponentManager(componentManager);
                reputationInstance.setExecution(execution);
                reputationInstance.setXWikiContext(getXWikiContext());
                reputationInstance.setRatingsManager(ratingsManagerProvider.get(documentRef));
                return reputationInstance;
            }
        } catch (Throwable e) {
            logger.error("Cannot instanciate Reputation algorithm from page " + reputationAlgorithmHint, e);
        }
    }
    try {
        return componentManager.getInstance(ReputationAlgorithm.class, reputationAlgorithmHint);
    } catch (ComponentLookupException e) {
        // TODO Auto-generated catch block
        logger.error("Error loading ratings manager component for hint " + reputationAlgorithmHint, e);
        try {
            return componentManager.getInstance(ReputationAlgorithm.class, defaultAlgorithmHint);
        } catch (ComponentLookupException e1) {
            return null;
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) BaseProperty(com.xpn.xwiki.objects.BaseProperty) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

BaseProperty (com.xpn.xwiki.objects.BaseProperty)87 BaseObject (com.xpn.xwiki.objects.BaseObject)26 ArrayList (java.util.ArrayList)16 XWikiException (com.xpn.xwiki.XWikiException)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)12 XMLAttributeValueFilter (com.xpn.xwiki.internal.xml.XMLAttributeValueFilter)12 org.apache.ecs.xhtml.input (org.apache.ecs.xhtml.input)11 XWikiContext (com.xpn.xwiki.XWikiContext)10 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 StringProperty (com.xpn.xwiki.objects.StringProperty)8 ListProperty (com.xpn.xwiki.objects.ListProperty)7 XWiki (com.xpn.xwiki.XWiki)5 BaseCollection (com.xpn.xwiki.objects.BaseCollection)5 LargeStringProperty (com.xpn.xwiki.objects.LargeStringProperty)5 List (java.util.List)5 Test (org.junit.Test)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)5 DBStringListProperty (com.xpn.xwiki.objects.DBStringListProperty)4 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)4