Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion xwiki-platform-core/xwiki-platform-oldcore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,6 @@
**/objects/classes/ListItem.java,
**/objects/classes/NumberClass.java,
**/objects/classes/PasswordClass.java,
**/objects/classes/PropertyClassInterface.java,
**/objects/classes/PropertyClass.java,
**/objects/classes/StaticListClass.java,
**/objects/classes/StringClass.java,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void set(String fieldname, java.lang.Object value, XWikiContext context)
PropertyClass pclass = (PropertyClass) bclass.get(fieldname);
BaseProperty prop = (BaseProperty) safeget(fieldname);
if ((value instanceof String) && (pclass != null)) {
prop = pclass.fromString((String) value);
prop = pclass.fromString((String) value, prop);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's really the cleanest code in general, it's good to reuse the property, but it's a bit strange to re-put it in the object.

} else {
if ((prop == null) && (pclass != null)) {
prop = pclass.newProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,23 @@ public BaseCollection fromMap(Map<String, ?> map, BaseCollection object)
for (PropertyClass property : (Collection<PropertyClass>) getFieldList()) {
String name = property.getName();
Object formvalues = map.get(name);
BaseProperty baseProperty = null;
try {
PropertyInterface propertyInterface = object.get(name);
if (propertyInterface instanceof BaseProperty obtainedProperty) {
baseProperty = obtainedProperty;
}
} catch (XWikiException e) {
LOGGER.error("Error while loading property [{}] from object [{}]", name, object, e);
}
if (formvalues != null) {
BaseProperty objprop;
if (formvalues instanceof String[]) {
objprop = property.fromStringArray(((String[]) formvalues));
objprop = property.fromStringArray(((String[]) formvalues), baseProperty);
} else if (formvalues instanceof String) {
objprop = property.fromString(formvalues.toString());
objprop = property.fromString(formvalues.toString(), baseProperty);
} else {
objprop = property.fromValue(formvalues);
objprop = property.fromValue(formvalues, baseProperty);
}

if (objprop != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import com.xpn.xwiki.objects.IntegerProperty;
import com.xpn.xwiki.objects.meta.PropertyMetaClass;

/**
* Representation of a boolean property.
*
* @version $Id$
*/
public class BooleanClass extends PropertyClass
{
private static final long serialVersionUID = 1L;
Expand All @@ -47,12 +52,19 @@ public class BooleanClass extends PropertyClass

/** Other string values that might be used to represent "false" values. */
private static final Pattern FALSE_PATTERN = Pattern.compile("no|false", Pattern.CASE_INSENSITIVE);


/**
* Constructor taking the given metaclass.
* @param wclass the metaclass to use.
*/
public BooleanClass(PropertyMetaClass wclass)
{
super(XCLASSNAME, "Boolean", wclass);
}

/**
* Default constructor.
*/
public BooleanClass()
{
this(null);
Expand Down Expand Up @@ -98,9 +110,9 @@ public int getDefaultValue()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty property = newProperty();
BaseProperty property = getCurrentOrNewProperty(baseProperty);
Number nvalue = null;
if (StringUtils.isNotEmpty(value)) {
if (StringUtils.isNumeric(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ public void setDateFormat(String format)
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty property = newProperty();
BaseProperty property = getCurrentOrNewProperty(baseProperty);

if (StringUtils.isEmpty(value)) {
if (getEmptyIsToday() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ public BaseProperty newProperty()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
prop.setValue(value);
return prop;
}

@Override
public BaseProperty fromStringArray(String[] strings)
public BaseProperty fromStringArray(String[] strings, BaseProperty baseProperty)
{
List<String> list;
if ((strings.length == 1) && (getDisplayType().equals(DISPLAYTYPE_INPUT) || isMultiSelect())) {
Expand All @@ -156,7 +156,7 @@ public BaseProperty fromStringArray(String[] strings)
list = Arrays.asList(strings);
}

BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
fromList(prop, list);
return prop;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public BaseProperty newProperty()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
prop.setValue(value);
return prop;
}

@Override
public BaseProperty fromStringArray(String[] strings)
public BaseProperty fromStringArray(String[] strings, BaseProperty baseProperty)
{
List<String> list;
if ((strings.length == 1) && (getDisplayType().equals(DISPLAYTYPE_INPUT) || isMultiSelect())) {
Expand All @@ -123,7 +123,7 @@ public BaseProperty fromStringArray(String[] strings)
list = Arrays.asList(strings);
}

BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
fromList(prop, list, true);
return prop;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ public BaseProperty newProperty()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
if (isMultiSelect()) {
((ListProperty) prop).setList(getListFromString(value, getSeparators(), false));
} else {
Expand All @@ -601,12 +601,12 @@ public BaseProperty fromString(String value)
}

@Override
public BaseProperty fromStringArray(String[] strings)
public BaseProperty fromStringArray(String[] strings, BaseProperty baseProperty)
{
if (!isMultiSelect()) {
return fromString(strings[0]);
}
BaseProperty prop = newProperty();
BaseProperty prop = getCurrentOrNewProperty(baseProperty);
// FIXME: this should be probably removed since we can never reach it.
if (prop instanceof StringProperty) {
return fromString(strings[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public BaseProperty newProperty()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
BaseProperty property = newProperty();
BaseProperty property = getCurrentOrNewProperty(baseProperty);
String ntype = getNumberType();
Number nvalue = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public PasswordClass()
}

@Override
public BaseProperty fromString(String value)
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
if (value.equals(FORM_PASSWORD_PLACEHODLER)) {
return null;
}
BaseProperty property = newProperty();
BaseProperty property = getCurrentOrNewProperty(baseProperty);
if (value.isEmpty() || value.startsWith(HASH_IDENTIFIER + SEPARATOR)
|| value.startsWith(CRYPT_IDENTIFIER + SEPARATOR)) {
property.setValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,28 @@ public String toString(BaseProperty property)

@Override
public BaseProperty fromString(String value)
{
return fromString(value, null);
}

@Override
public BaseProperty fromString(String value, BaseProperty baseProperty)
{
return null;
}

/**
* Retrieve the already existing base property based on current name, or create a new one.
*
* @return the existing {@link BaseProperty} or a new one.
* @since 17.3.0RC1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be updated.

*/
@Unstable
protected BaseProperty getCurrentOrNewProperty(BaseProperty property)
{
return (property != null) ? property : newProperty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be outside the scope of this issue, but I'm wondering if the xclass should "auto-repair" the property if it does not have the right type. Right now, the following code might fail if getCurrentOrNewProperty gives a property of the wrong type (compared to the previous situation where that code was expecting to always get a new property).

}

public BaseProperty newPropertyfromXML(Element ppcel)
{
String value = ppcel.getText();
Expand Down Expand Up @@ -696,7 +714,12 @@ public void setDisabled(boolean disabled)

public BaseProperty fromStringArray(String[] strings)
{
return fromString(strings[0]);
return fromStringArray(strings, null);
}

public BaseProperty fromStringArray(String[] strings, BaseProperty baseProperty)
{
return fromString(strings[0], baseProperty);
}

public boolean isValidColumnTypes(Property hibprop)
Expand All @@ -707,7 +730,13 @@ public boolean isValidColumnTypes(Property hibprop)
@Override
public BaseProperty fromValue(Object value)
{
BaseProperty property = newProperty();
return fromValue(value, null);
}

@Override
public BaseProperty fromValue(Object value, BaseProperty baseProperty)
{
BaseProperty property = getCurrentOrNewProperty(baseProperty);
if (property != null) {
property.setValue(value);
return property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package com.xpn.xwiki.objects.classes;

import org.xwiki.stability.Unstable;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.objects.BaseCollection;
import com.xpn.xwiki.objects.BaseProperty;
Expand All @@ -36,17 +38,85 @@
*/
public interface PropertyClassInterface extends ObjectInterface, PropertyInterface
{
/**
* Serialize the given property as a string.
* @param property the property to serialize.
* @return the serialized property.
*/
String toString(BaseProperty property);

/**
* Creates a new property and set its value based on the given string.
* @param value the value to set in the new property.
* @return the created property
*/
BaseProperty fromString(String value);

/**
* Sets the value of the given property from the given string parameter. Note that if the given property is
* {@code null} then the method should create a new property and set its value.
* @param value the value to set in the new property.
* @param baseProperty the property instance to set the value for, or {@code null} to create a new property.
* @return the modified property
* @since 17.4.0RC1
*/
@Unstable
default BaseProperty fromString(String value, BaseProperty baseProperty)
{
return fromString(value);
}

/**
* Creates a new property and set its value based on the given parameter.
* @param value the value to set in the new property.
* @return the created property
*/
BaseProperty fromValue(Object value);

/**
* Sets the value of the given property from the given parameter. Note that if the given property is {@code null}
* then the method should create a new property and set its value.
*
* @param value the value to set in the new property.
* @param baseProperty the property instance to set the value for, or {@code null} to create a new property.
* @return the modified property
* @since 17.4.0RC1
*/
@Unstable
default BaseProperty fromValue(Object value, BaseProperty baseProperty)
{
return fromValue(value);
}

/**
* Output in the given buffer, hidden form elements for the property identified by the name in the given object.
* @param buffer the buffer where to output the result
* @param name the name of the property for which to output a form
* @param prefix the prefix to use for the form id and name elements
* @param object the object where to find the property
* @param context the context to use
*/
void displayHidden(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context);

/**
* Output in the given buffer, a visual representation for the property identified by the name in the given object.
* @param buffer the buffer where to output the result
* @param name the name of the property for which to output a view of the property
* @param prefix the prefix to use for the form id and name elements
* @param object the object where to find the property
* @param context the context to use
*/
void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context);

/**
* Output in the given buffer, a visual representation for the property identified by the name in the given object.
* @param buffer the buffer where to output the result
* @param name the name of the property for which to output a view of the property
* @param prefix the prefix to use for the form id and name elements
* @param object the object where to find the property
* @param isolated {@code true} if the representation should be computed outside of the context of the current
* document reference.
* @param context the context to use
* @since 13.0
*/
default void displayView(StringBuffer buffer, String name, String prefix, BaseCollection object, boolean isolated,
Expand All @@ -55,9 +125,25 @@ default void displayView(StringBuffer buffer, String name, String prefix, BaseCo
displayView(buffer, name, prefix, object, context);
}

/**
* Output in the given buffer, a representation allowing to edit the property identified by the name in the given
* object.
* @param buffer the buffer where to output the result
* @param name the name of the property for which to output a view of the property
* @param prefix the prefix to use for the form id and name elements
* @param object the object where to find the property
* document reference.
* @param context the context to use
*/
void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object, XWikiContext context);

/**
* @return a new property corresponding to the current class.
*/
BaseProperty newProperty();

/**
* Invalidate all caches used in the implementations (e.g. the cache of displayers)
*/
void flushCache();
}
Loading