use of com.xpn.xwiki.objects.BaseCollection in project xwiki-platform by xwiki.
the class XWikiHibernateStore method saveXWikiPropertyInternal.
private void saveXWikiPropertyInternal(final PropertyInterface property, final XWikiContext context, final boolean runInOwnTransaction) throws XWikiException {
// Clone runInOwnTransaction so the value passed is not altered.
boolean bTransaction = runInOwnTransaction;
try {
if (bTransaction) {
this.checkHibernate(context);
bTransaction = this.beginTransaction(context);
}
final Session session = this.getSession(context);
Query query = session.createQuery("select prop.classType from BaseProperty as prop " + "where prop.id.id = :id and prop.id.name= :name");
query.setLong("id", property.getId());
query.setString("name", property.getName());
String oldClassType = (String) query.uniqueResult();
String newClassType = ((BaseProperty) property).getClassType();
if (oldClassType == null) {
session.save(property);
} else if (oldClassType.equals(newClassType)) {
session.update(property);
} else {
// The property type has changed. We cannot simply update its value because the new value and the old
// value are stored in different tables (we're using joined-subclass to map different property types).
// We must delete the old property value before saving the new one and for this we must load the old
// property from the table that corresponds to the old property type (we cannot delete and save the new
// property or delete a clone of the new property; loading the old property from the BaseProperty table
// doesn't work either).
query = session.createQuery("select prop from " + oldClassType + " as prop where prop.id.id = :id and prop.id.name= :name");
query.setLong("id", property.getId());
query.setString("name", property.getName());
session.delete(query.uniqueResult());
session.save(property);
}
((BaseProperty) property).setValueDirty(false);
if (bTransaction) {
endTransaction(context, true);
}
} catch (Exception e) {
// Something went wrong, collect some information.
final BaseCollection obj = property.getObject();
final Object[] args = { (obj != null) ? obj.getName() : "unknown", property.getName() };
// Try to roll back the transaction if this is in it's own transaction.
try {
if (bTransaction) {
this.endTransaction(context, false);
}
} catch (Exception ee) {
// Not a lot we can do here if there was an exception committing and an exception rolling back.
}
// Throw the exception.
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while saving property {1} of object {0}", e, args);
}
}
use of com.xpn.xwiki.objects.BaseCollection in project xwiki-platform by xwiki.
the class XWikiHibernateStore method loadXWikiProperty.
private void loadXWikiProperty(PropertyInterface property, XWikiContext context, boolean bTransaction) throws XWikiException {
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(false, context);
}
Session session = getSession(context);
try {
session.load(property, (Serializable) property);
// safe to assume that a retrieved NULL value should actually be an empty string.
if (property instanceof BaseStringProperty) {
BaseStringProperty stringProperty = (BaseStringProperty) property;
if (stringProperty.getValue() == null) {
stringProperty.setValue("");
}
}
((BaseProperty) property).setValueDirty(false);
} catch (ObjectNotFoundException e) {
// Let's accept that there is no data in property tables but log it
this.logger.error("No data for property [{}] of object id [{}]", property.getName(), property.getId());
}
// Without this test ViewEditTest.testUpdateAdvanceObjectProp fails
if (property instanceof ListProperty) {
((ListProperty) property).getList();
}
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
BaseCollection obj = property.getObject();
Object[] args = { (obj != null) ? obj.getName() : "unknown", property.getName() };
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading property {1} of object {0}", e, args);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
}
}
Aggregations