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"));
}
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;
}
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);
}
}
}
}
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"));
}
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;
}
}
}
Aggregations