use of com.xpn.xwiki.objects.classes.StringClass in project xwiki-platform by xwiki.
the class PropertyMetaClass method addValidationMetaProperties.
/**
* Adds the meta properties used for validation the XClass property value.
*/
private void addValidationMetaProperties() {
StringClass validationRegExpClass = new StringClass(this);
validationRegExpClass.setName("validationRegExp");
validationRegExpClass.setPrettyName("Validation Regular Expression");
validationRegExpClass.setSize(40);
safeput(validationRegExpClass.getName(), validationRegExpClass);
StringClass validationMessageClass = new StringClass(this);
validationMessageClass.setName("validationMessage");
validationMessageClass.setPrettyName("Validation Message");
validationMessageClass.setSize(80);
safeput(validationMessageClass.getName(), validationMessageClass);
}
use of com.xpn.xwiki.objects.classes.StringClass in project xwiki-platform by xwiki.
the class ListMetaClass method addValueSeparatorMetaProperties.
/**
* Adds the meta properties that control how list values are separated in edit and view mode.
*/
private void addValueSeparatorMetaProperties() {
StringClass separatorsClass = new StringClass(this);
separatorsClass.setName("separators");
separatorsClass.setPrettyName("Multiselect separators (for editing)");
separatorsClass.setSize(5);
safeput(separatorsClass.getName(), separatorsClass);
StringClass separatorClass = new StringClass(this);
separatorClass.setName("separator");
separatorClass.setPrettyName("Join separator (for display)");
separatorClass.setSize(5);
safeput(separatorClass.getName(), separatorClass);
}
use of com.xpn.xwiki.objects.classes.StringClass in project xwiki-platform by xwiki.
the class DocumentSolrMetadataExtractorTest method getDocumentWithObjects.
@Test
public void getDocumentWithObjects() throws Exception {
//
// Mock
//
BaseObject comment = mock(BaseObject.class);
List<BaseProperty<EntityReference>> commentFields = new ArrayList<BaseProperty<EntityReference>>();
String commentContent = "This is a comment";
BaseProperty<EntityReference> contentField = mock(BaseProperty.class);
when(contentField.getName()).thenReturn("comment");
when(contentField.getValue()).thenReturn(commentContent);
when(contentField.getObject()).thenReturn(comment);
commentFields.add(contentField);
String commentSummary = "summary";
BaseProperty<EntityReference> summaryField = mock(BaseProperty.class);
when(summaryField.getName()).thenReturn("summary");
when(summaryField.getValue()).thenReturn(commentSummary);
when(summaryField.getObject()).thenReturn(comment);
commentFields.add(summaryField);
String commentAuthor = "wiki:space.commentAuthor";
BaseProperty<EntityReference> authorField = mock(BaseProperty.class);
when(authorField.getName()).thenReturn("author");
when(authorField.getValue()).thenReturn(commentAuthor);
when(authorField.getObject()).thenReturn(comment);
commentFields.add(authorField);
Date commentDate = new Date();
BaseProperty<EntityReference> dateField = mock(BaseProperty.class);
when(dateField.getName()).thenReturn("date");
when(dateField.getValue()).thenReturn(commentDate);
when(dateField.getObject()).thenReturn(comment);
commentFields.add(dateField);
// Adding a fake password field to the comments class just to test the branch in the code.
String commentPassword = "password";
BaseProperty<EntityReference> passwordField = mock(BaseProperty.class);
when(passwordField.getName()).thenReturn("password");
when(passwordField.getValue()).thenReturn(commentPassword);
commentFields.add(passwordField);
List<String> commentList = Arrays.asList("a", "list");
BaseProperty<EntityReference> listField = mock(BaseProperty.class);
when(listField.getName()).thenReturn("list");
when(listField.getValue()).thenReturn(commentList);
when(listField.getObject()).thenReturn(comment);
commentFields.add(listField);
Long commentLikes = 13L;
BaseProperty<EntityReference> numberField = mock(BaseProperty.class);
when(numberField.getName()).thenReturn("likes");
when(numberField.getValue()).thenReturn(commentLikes);
when(numberField.getObject()).thenReturn(comment);
commentFields.add(numberField);
BaseProperty<EntityReference> booleanField = mock(BaseProperty.class);
when(booleanField.getName()).thenReturn("enabled");
when(booleanField.getValue()).thenReturn(1);
when(booleanField.getObject()).thenReturn(comment);
commentFields.add(booleanField);
DocumentReference commentsClassReference = new DocumentReference("wiki", "space", "commentsClass");
when(this.document.getXObjects()).thenReturn(Collections.singletonMap(commentsClassReference, Arrays.asList(comment)));
EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
when(localEntityReferenceSerializer.serialize(commentsClassReference)).thenReturn("space.commentsClass");
BaseClass xclass = mock(BaseClass.class);
when(comment.getXClass(this.xcontext)).thenReturn(xclass);
when(comment.getFieldList()).thenReturn(commentFields);
when(comment.getRelativeXClassReference()).thenReturn(commentsClassReference.removeParent(commentsClassReference.getWikiReference()));
StringClass stringClass = mock(StringClass.class);
when(stringClass.getClassType()).thenReturn("String");
when(xclass.get("comment")).thenReturn(mock(TextAreaClass.class));
when(xclass.get("summary")).thenReturn(stringClass);
when(xclass.get("password")).thenReturn(mock(PasswordClass.class));
when(xclass.get("enabled")).thenReturn(mock(BooleanClass.class));
//
// Call
//
SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.frenchDocumentReference);
//
// Assert and verify
//
assertEquals(Arrays.asList("space.commentsClass"), solrDocument.getFieldValues(FieldUtils.CLASS));
// A TextArea property must be indexed as a localized text.
assertSame(commentContent, solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.comment", Locale.FRENCH)));
assertNull(solrDocument.getFieldValue("property.space.commentsClass.comment_string"));
// A String property must be indexed both as localized text and as raw (unanalyzed) text.
assertSame(commentSummary, solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.summary", Locale.FRENCH)));
assertSame(commentSummary, solrDocument.getFieldValue("property.space.commentsClass.summary_string"));
assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_string"));
assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_date"));
assertEquals(commentList, solrDocument.getFieldValues("property.space.commentsClass.list_string"));
assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_long"));
assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_boolean"));
// Make sure the password is not indexed (neither as a string nor as a localized text).
assertNull(solrDocument.getFieldValue("property.space.commentsClass.password_string"));
assertNull(solrDocument.getFieldValue(FieldUtils.getFieldName("property.space.commentsClass.password", Locale.FRENCH)));
// Check the sort fields.
assertSame(commentAuthor, solrDocument.getFieldValue("property.space.commentsClass.author_sortString"));
assertSame(commentDate, solrDocument.getFieldValue("property.space.commentsClass.date_sortDate"));
// The last value is used for sorting because we cannot sort on fields with multiple values.
assertEquals("list", solrDocument.getFieldValue("property.space.commentsClass.list_sortString"));
assertSame(commentLikes, solrDocument.getFieldValue("property.space.commentsClass.likes_sortLong"));
assertTrue((Boolean) solrDocument.getFieldValue("property.space.commentsClass.enabled_sortBoolean"));
// Localized texts are sorted as strings if they are not too large.
assertSame(commentContent, solrDocument.getFieldValue("property.space.commentsClass.comment_sortString"));
Collection<Object> objectProperties = solrDocument.getFieldValues(FieldUtils.getFieldName("object.space.commentsClass", Locale.FRENCH));
MatcherAssert.assertThat(objectProperties, Matchers.<Object>containsInAnyOrder(commentContent, commentSummary, commentAuthor, commentDate, commentList.get(0), commentList.get(1), commentLikes, true));
assertEquals(8, objectProperties.size());
objectProperties = solrDocument.getFieldValues(FieldUtils.getFieldName(FieldUtils.OBJECT_CONTENT, Locale.FRENCH));
MatcherAssert.assertThat(objectProperties, Matchers.<Object>containsInAnyOrder("comment : " + commentContent, "summary : " + commentSummary, "author : " + commentAuthor, "date : " + commentDate, "list : " + commentList.get(0), "list : " + commentList.get(1), "likes : " + commentLikes, "enabled : true"));
assertEquals(8, objectProperties.size());
}
use of com.xpn.xwiki.objects.classes.StringClass in project xwiki-platform by xwiki.
the class PropertyConverterTest method longToString.
@Test
public void longToString() throws Exception {
LongProperty longProperty = new LongProperty();
longProperty.setValue(Long.MAX_VALUE);
StringClass stringClass = mock(StringClass.class);
when(stringClass.newProperty()).thenReturn(new StringProperty());
StringProperty stringProperty = new StringProperty();
when(stringClass.fromString(longProperty.toText())).thenReturn(stringProperty);
assertEquals(stringProperty, this.mocker.getComponentUnderTest().convertProperty(longProperty, stringClass));
}
use of com.xpn.xwiki.objects.classes.StringClass in project xwiki-platform by xwiki.
the class XWikiHibernateStore method loadXWikiCollectionInternal.
private void loadXWikiCollectionInternal(BaseCollection object1, XWikiDocument doc, XWikiContext inputxcontext, boolean bTransaction, boolean alreadyLoaded) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
BaseCollection object = object1;
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(false, context);
}
Session session = getSession(context);
if (!alreadyLoaded) {
try {
session.load(object, object1.getId());
} catch (ObjectNotFoundException e) {
// There is no object data saved
object = null;
return;
}
}
DocumentReference classReference = object.getXClassReference();
// If the class reference is null in the loaded object then skip loading properties
if (classReference != null) {
BaseClass bclass = null;
if (!classReference.equals(object.getDocumentReference())) {
// Let's check if the class has a custom mapping
bclass = object.getXClass(context);
} else {
// we will go in an endless loop
if (doc != null) {
bclass = doc.getXClass();
}
}
List<String> handledProps = new ArrayList<String>();
try {
if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
Session dynamicSession = session.getSession(EntityMode.MAP);
Object map = dynamicSession.load(bclass.getName(), object.getId());
// Let's make sure to look for null fields in the dynamic mapping
bclass.fromValueMap((Map) map, object);
handledProps = bclass.getCustomMappingPropertyList(context);
for (String prop : handledProps) {
if (((Map) map).get(prop) == null) {
handledProps.remove(prop);
}
}
}
} catch (Exception e) {
}
// Load strings, integers, dates all at once
Query query = session.createQuery("select prop.name, prop.classType from BaseProperty as prop where prop.id.id = :id");
query.setLong("id", object.getId());
for (Object[] result : (List<Object[]>) query.list()) {
String name = (String) result[0];
// custom mapping
if (handledProps.contains(name)) {
continue;
}
String classType = (String) result[1];
BaseProperty property = null;
try {
property = (BaseProperty) Class.forName(classType).newInstance();
property.setObject(object);
property.setName(name);
loadXWikiProperty(property, context, false);
} catch (Exception e) {
// WORKAROUND IN CASE OF MIXMATCH BETWEEN STRING AND LARGESTRING
try {
if (property instanceof StringProperty) {
LargeStringProperty property2 = new LargeStringProperty();
property2.setObject(object);
property2.setName(name);
loadXWikiProperty(property2, context, false);
property.setValue(property2.getValue());
if (bclass != null) {
if (bclass.get(name) instanceof TextAreaClass) {
property = property2;
}
}
} else if (property instanceof LargeStringProperty) {
StringProperty property2 = new StringProperty();
property2.setObject(object);
property2.setName(name);
loadXWikiProperty(property2, context, false);
property.setValue(property2.getValue());
if (bclass != null) {
if (bclass.get(name) instanceof StringClass) {
property = property2;
}
}
} else {
throw e;
}
} catch (Throwable e2) {
Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + ""), name };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}', number '{2}' and property '{3}'", e, args);
}
}
object.addField(name, property);
}
}
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + "") };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}' and number '{2}'", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
}
}
Aggregations