use of com.xpn.xwiki.stats.impl.XWikiStats 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