Search in sources :

Example 61 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultRatingsConfigurationTest method getWikiConfigurationDocument.

@Test
public void getWikiConfigurationDocument() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    XWikiDocument wikiConfigurationDocument = mock(XWikiDocument.class, "Wiki Configuration Document");
    when(xcontext.getWiki().getDocument(RatingsManager.RATINGS_CONFIG_GLOBAL_REFERENCE, xcontext)).thenReturn(wikiConfigurationDocument);
    assertEquals(wikiConfigurationDocument, mocker.getComponentUnderTest().getConfigurationDocument(documentReference));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 62 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DefaultRatingsConfigurationTest method getConfigurationParameterDefaultValue.

@Test
public void getConfigurationParameterDefaultValue() 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);
    assertEquals("1", mocker.getComponentUnderTest().getConfigurationParameter(documentReference, "displayRatings", "1"));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 63 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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 64 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument 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)

Example 65 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class ExtensionUpdaterListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    XWikiDocument document = (XWikiDocument) source;
    BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
    if (extensionObject != null) {
        try {
            this.repositoryManagerProvider.get().validateExtension(document, false);
        } catch (XWikiException e) {
            this.logger.error("Failed to validate extension in document [{}]", document.getDocumentReference(), e);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23