Search in sources :

Example 51 with BaseProperty

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

the class DateClass method fromString.

@Override
public BaseProperty fromString(String value) {
    BaseProperty property = newProperty();
    if (StringUtils.isEmpty(value)) {
        if (getEmptyIsToday() == 1) {
            property.setValue(new Date());
        }
        return property;
    }
    try {
        SimpleDateFormat sdf = new SimpleDateFormat(getDateFormat(), getCurrentLocale());
        property.setValue(sdf.parse(value));
        return property;
    } catch (ParseException e) {
        return null;
    }
}
Also used : ParseException(java.text.ParseException) BaseProperty(com.xpn.xwiki.objects.BaseProperty) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 52 with BaseProperty

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

the class DateClass method newPropertyfromXML.

/**
 * {@inheritDoc}
 * <p>
 * We have to overwrite this method because the value of a date property is not serialized using the date format
 * specified in the XClass nor the time stamp but a custom hard-coded date format.. Changing this now will break
 * existing XARs..
 * </p>
 */
@Override
public BaseProperty newPropertyfromXML(Element element) {
    String value = element.getText();
    BaseProperty property = newProperty();
    if (StringUtils.isEmpty(value)) {
        return property;
    }
    // FIXME: The value of a date property should be serialized using the date timestamp or the date format
    // specified in the XClass the date property belongs to.
    SimpleDateFormat sdf = DateXarObjectPropertySerializer.DEFAULT_FORMAT;
    try {
        property.setValue(sdf.parse(value));
    } catch (ParseException e) {
        // I suppose this is a date format used a long time ago. DateProperty is using the above date format now.
        SimpleDateFormat sdfOld = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
        LOGGER.warn("Failed to parse date [{}] using format [{}]. Trying again with format [{}].", value, sdf.toString(), sdfOld.toString());
        try {
            property.setValue(sdfOld.parse(value));
        } catch (ParseException exception) {
            LOGGER.warn("Failed to parse date [{}] using format [{}]. Defaulting to the current date.", value, sdfOld.toString());
            property.setValue(new Date());
        }
    }
    return property;
}
Also used : ParseException(java.text.ParseException) BaseProperty(com.xpn.xwiki.objects.BaseProperty) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 53 with BaseProperty

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

the class GroupsClass method fromString.

@Override
public BaseProperty fromString(String value) {
    BaseProperty prop = newProperty();
    prop.setValue(value);
    return prop;
}
Also used : BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 54 with BaseProperty

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

the class GroupsClass method fromStringArray.

@Override
public BaseProperty fromStringArray(String[] strings) {
    List<String> list = Arrays.asList(strings);
    BaseProperty prop = newProperty();
    fromList(prop, list);
    return prop;
}
Also used : BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 55 with BaseProperty

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

the class LevelsClass method fromStringArray.

@Override
public BaseProperty fromStringArray(String[] strings) {
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < strings.length; i++) {
        if (!strings[i].trim().equals("")) {
            list.add(strings[i]);
        }
    }
    BaseProperty prop = newProperty();
    fromList(prop, list);
    return prop;
}
Also used : ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty)

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