use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.
the class BaseClass method fromMap.
public BaseCollection fromMap(Map<String, ?> map, BaseCollection object) {
for (PropertyClass property : (Collection<PropertyClass>) getFieldList()) {
String name = property.getName();
Object formvalues = map.get(name);
if (formvalues != null) {
BaseProperty objprop;
if (formvalues instanceof String[]) {
objprop = property.fromStringArray(((String[]) formvalues));
} else if (formvalues instanceof String) {
objprop = property.fromString(formvalues.toString());
} else {
objprop = property.fromValue(formvalues);
}
if (objprop != null) {
objprop.setObject(object);
object.safeput(name, objprop);
}
}
}
return object;
}
use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.
the class BooleanClass method newProperty.
@Override
public BaseProperty newProperty() {
BaseProperty property = new IntegerProperty();
property.setName(getName());
return property;
}
use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.
the class BooleanClass method fromString.
@Override
public BaseProperty fromString(String value) {
BaseProperty property = newProperty();
Number nvalue = null;
if (StringUtils.isNotEmpty(value)) {
if (StringUtils.isNumeric(value)) {
nvalue = Integer.valueOf(value);
} else if (TRUE_PATTERN.matcher(value).matches()) {
nvalue = 1;
} else if (FALSE_PATTERN.matcher(value).matches()) {
nvalue = 0;
}
}
// We don't return null if there's no value, since the BooleanProperty really works that way
property.setValue(nvalue);
return property;
}
use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.
the class XWikiHibernateStoreTest method saveObjectWithPropertyTypeChange.
/**
* Save an object that has a property whose type has changed.
*
* @see "XWIKI-9716: Error while migrating SearchSuggestConfig page from 4.1.4 to 5.2.1 with DW"
*/
@Test
public void saveObjectWithPropertyTypeChange() throws Exception {
// The class must be local.
DocumentReference classReference = new DocumentReference("myWiki", "mySpace", "myClass");
when(xcontext.getWikiId()).thenReturn(classReference.getWikiReference().getName());
BaseObject object = mock(BaseObject.class);
when(object.getXClassReference()).thenReturn(classReference);
// Query to check if the object exists already (save versus update).
when(xcontext.get("hibsession")).thenReturn(session);
when(session.createQuery("select obj.id from BaseObject as obj where obj.id = :id")).thenReturn(mock(Query.class));
// Save each object property.
String propertyName = "query";
long propertyId = 1234567890L;
when(object.getPropertyList()).thenReturn(Collections.singleton(propertyName));
// The property name must match the key in the property list.
BaseProperty property = mock(BaseProperty.class);
when(object.getField(propertyName)).thenReturn(property);
when(property.getId()).thenReturn(propertyId);
when(property.getName()).thenReturn(propertyName);
when(property.getClassType()).thenReturn(LargeStringProperty.class.getName());
Query oldClassTypeQuery = mock(Query.class);
when(session.createQuery("select prop.classType from BaseProperty as prop " + "where prop.id.id = :id and prop.id.name= :name")).thenReturn(oldClassTypeQuery);
// The old value has a different type (String -> TextArea).
when(oldClassTypeQuery.uniqueResult()).thenReturn(StringProperty.class.getName());
// The old property must be loaded from the corresponding table.
Query oldPropertyQuery = mock(Query.class);
when(session.createQuery("select prop from " + StringProperty.class.getName() + " as prop where prop.id.id = :id and prop.id.name= :name")).thenReturn(oldPropertyQuery);
BaseProperty oldProperty = mock(BaseProperty.class);
when(oldPropertyQuery.uniqueResult()).thenReturn(oldProperty);
store.saveXWikiCollection(object, xcontext, false);
verify(oldClassTypeQuery).setLong("id", propertyId);
verify(oldClassTypeQuery).setString("name", propertyName);
verify(oldPropertyQuery).setLong("id", propertyId);
verify(oldPropertyQuery).setString("name", propertyName);
// Delete the old property value and then save the new one.
verify(session).delete(oldProperty);
verify(session).save(property);
}
use of com.xpn.xwiki.objects.BaseProperty in project xwiki-platform by xwiki.
the class XWikiHibernateStore method saveXWikiCollection.
/**
* @deprecated This is internal to XWikiHibernateStore and may be removed in the future.
*/
@Deprecated
public void saveXWikiCollection(BaseCollection object, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
try {
if (object == null) {
return;
}
// We need a slightly different behavior here
boolean stats = (object instanceof XWikiStats);
if (!stats) {
checkObjectClassIsLocal(object, context);
}
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(context);
}
Session session = getSession(context);
// Verify if the property already exists
Query query;
if (stats) {
query = session.createQuery("select obj.id from " + object.getClass().getName() + " as obj where obj.id = :id");
} else {
query = session.createQuery("select obj.id from BaseObject as obj where obj.id = :id");
}
query.setLong("id", object.getId());
if (query.uniqueResult() == null) {
if (stats) {
session.save(object);
} else {
session.save("com.xpn.xwiki.objects.BaseObject", object);
}
} else {
if (stats) {
session.update(object);
} else {
session.update("com.xpn.xwiki.objects.BaseObject", object);
}
}
/*
* if (stats) session.saveOrUpdate(object); else
* session.saveOrUpdate((String)"com.xpn.xwiki.objects.BaseObject", (Object)object);
*/
BaseClass bclass = object.getXClass(context);
List<String> handledProps = new ArrayList<>();
if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
// save object using the custom mapping
Map<String, Object> objmap = object.getCustomMappingMap();
handledProps = bclass.getCustomMappingPropertyList(context);
Session dynamicSession = session.getSession(EntityMode.MAP);
query = session.createQuery("select obj.id from " + bclass.getName() + " as obj where obj.id = :id");
query.setLong("id", object.getId());
if (query.uniqueResult() == null) {
dynamicSession.save(bclass.getName(), objmap);
} else {
dynamicSession.update(bclass.getName(), objmap);
}
// dynamicSession.saveOrUpdate((String) bclass.getName(), objmap);
}
if (object.getXClassReference() != null) {
// Remove all existing properties
if (object.getFieldsToRemove().size() > 0) {
for (int i = 0; i < object.getFieldsToRemove().size(); i++) {
BaseProperty prop = (BaseProperty) object.getFieldsToRemove().get(i);
if (!handledProps.contains(prop.getName())) {
session.delete(prop);
}
}
object.setFieldsToRemove(new ArrayList<BaseProperty>());
}
Iterator<String> it = object.getPropertyList().iterator();
while (it.hasNext()) {
String key = it.next();
BaseProperty prop = (BaseProperty) object.getField(key);
if (!prop.getName().equals(key)) {
Object[] args = { key, object.getName() };
throw new XWikiException(XWikiException.MODULE_XWIKI_CLASSES, XWikiException.ERROR_XWIKI_CLASSES_FIELD_INVALID, "Field {0} in object {1} has an invalid name", null, args);
}
String pname = prop.getName();
if (pname != null && !pname.trim().equals("") && !handledProps.contains(pname)) {
saveXWikiPropertyInternal(prop, context, false);
}
}
}
if (bTransaction) {
endTransaction(context, true);
}
} catch (XWikiException xe) {
throw xe;
} catch (Exception e) {
Object[] args = { object.getName() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_OBJECT, "Exception while saving object {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, true);
}
} catch (Exception e) {
}
restoreExecutionXContext();
}
}
Aggregations