Search in sources :

Example 1 with BaseCollection

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

the class PropertyMetaClass method newObject.

/**
 * {@inheritDoc}
 * <p>
 * This method is deprecated. Use directly the {@link PropertyClassProvider} if you need a new XClass property
 * instance.
 * </p>
 */
@Override
public BaseCollection newObject(XWikiContext context) throws XWikiException {
    PropertyClassInterface instance = null;
    try {
        // Try to use the corresponding XClass property provider to create the new property instance.
        PropertyClassProvider provider = Utils.getComponent(PropertyClassProvider.class, getName());
        instance = provider.getInstance();
    } catch (Exception e) {
    // Fail silently.
    }
    return instance != null && instance instanceof BaseCollection ? (BaseCollection) instance : super.newObject(context);
}
Also used : PropertyClassInterface(com.xpn.xwiki.objects.classes.PropertyClassInterface) PropertyClassProvider(com.xpn.xwiki.internal.objects.classes.PropertyClassProvider) BaseCollection(com.xpn.xwiki.objects.BaseCollection) XWikiException(com.xpn.xwiki.XWikiException)

Example 2 with BaseCollection

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

the class BaseClass method fromValueMap.

public BaseCollection fromValueMap(Map<String, ?> map, BaseCollection object) {
    for (PropertyClass property : (Collection<PropertyClass>) getFieldList()) {
        String name = property.getName();
        Object formvalue = map.get(name);
        if (formvalue != null) {
            BaseProperty objprop;
            objprop = property.fromValue(formvalue);
            if (objprop != null) {
                objprop.setObject(object);
                object.safeput(name, objprop);
            }
        }
    }
    return object;
}
Also used : BaseCollection(com.xpn.xwiki.objects.BaseCollection) Collection(java.util.Collection) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 3 with BaseCollection

use of com.xpn.xwiki.objects.BaseCollection 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;
}
Also used : BaseCollection(com.xpn.xwiki.objects.BaseCollection) Collection(java.util.Collection) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 4 with BaseCollection

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

the class XWikiHibernateStore method deleteXWikiCollection.

/**
 * @deprecated This is internal to XWikiHibernateStore and may be removed in the future.
 */
@Deprecated
public void deleteXWikiCollection(BaseCollection object, XWikiContext inputxcontext, boolean bTransaction, boolean evict) throws XWikiException {
    if (object == null) {
        return;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // Let's check if the class has a custom mapping
        BaseClass bclass = object.getXClass(context);
        List<String> handledProps = new ArrayList<String>();
        if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
            handledProps = bclass.getCustomMappingPropertyList(context);
            Session dynamicSession = session.getSession(EntityMode.MAP);
            Object map = dynamicSession.get(bclass.getName(), object.getId());
            if (map != null) {
                if (evict) {
                    dynamicSession.evict(map);
                }
                dynamicSession.delete(map);
            }
        }
        if (object.getXClassReference() != null) {
            for (BaseElement property : (Collection<BaseElement>) object.getFieldList()) {
                if (!handledProps.contains(property.getName())) {
                    if (evict) {
                        session.evict(property);
                    }
                    if (session.get(property.getClass(), property) != null) {
                        session.delete(property);
                    }
                }
            }
        }
        // In case of custom class we need to force it as BaseObject to delete the xwikiobject row
        if (!"".equals(bclass.getCustomClass())) {
            BaseObject cobject = new BaseObject();
            cobject.setDocumentReference(object.getDocumentReference());
            cobject.setClassName(object.getClassName());
            cobject.setNumber(object.getNumber());
            if (object instanceof BaseObject) {
                cobject.setGuid(((BaseObject) object).getGuid());
            }
            cobject.setId(object.getId());
            if (evict) {
                session.evict(cobject);
            }
            session.delete(cobject);
        } else {
            if (evict) {
                session.evict(object);
            }
            session.delete(object);
        }
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (Exception e) {
        Object[] args = { object.getName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_OBJECT, "Exception while deleting object {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : BaseElement(com.xpn.xwiki.objects.BaseElement) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseCollection(com.xpn.xwiki.objects.BaseCollection) Collection(java.util.Collection) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with BaseCollection

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

the class XWikiHibernateStore method loadXWikiCollectionInternal.

private void loadXWikiCollectionInternal(BaseCollection object1, XWikiDocument doc, XWikiContext inputxcontext, boolean bTransaction, boolean alreadyLoaded) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    BaseCollection object = object1;
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(false, context);
        }
        Session session = getSession(context);
        if (!alreadyLoaded) {
            try {
                session.load(object, object1.getId());
            } catch (ObjectNotFoundException e) {
                // There is no object data saved
                object = null;
                return;
            }
        }
        DocumentReference classReference = object.getXClassReference();
        // If the class reference is null in the loaded object then skip loading properties
        if (classReference != null) {
            BaseClass bclass = null;
            if (!classReference.equals(object.getDocumentReference())) {
                // Let's check if the class has a custom mapping
                bclass = object.getXClass(context);
            } else {
                // we will go in an endless loop
                if (doc != null) {
                    bclass = doc.getXClass();
                }
            }
            List<String> handledProps = new ArrayList<String>();
            try {
                if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
                    Session dynamicSession = session.getSession(EntityMode.MAP);
                    Object map = dynamicSession.load(bclass.getName(), object.getId());
                    // Let's make sure to look for null fields in the dynamic mapping
                    bclass.fromValueMap((Map) map, object);
                    handledProps = bclass.getCustomMappingPropertyList(context);
                    for (String prop : handledProps) {
                        if (((Map) map).get(prop) == null) {
                            handledProps.remove(prop);
                        }
                    }
                }
            } catch (Exception e) {
            }
            // Load strings, integers, dates all at once
            Query query = session.createQuery("select prop.name, prop.classType from BaseProperty as prop where prop.id.id = :id");
            query.setLong("id", object.getId());
            for (Object[] result : (List<Object[]>) query.list()) {
                String name = (String) result[0];
                // custom mapping
                if (handledProps.contains(name)) {
                    continue;
                }
                String classType = (String) result[1];
                BaseProperty property = null;
                try {
                    property = (BaseProperty) Class.forName(classType).newInstance();
                    property.setObject(object);
                    property.setName(name);
                    loadXWikiProperty(property, context, false);
                } catch (Exception e) {
                    // WORKAROUND IN CASE OF MIXMATCH BETWEEN STRING AND LARGESTRING
                    try {
                        if (property instanceof StringProperty) {
                            LargeStringProperty property2 = new LargeStringProperty();
                            property2.setObject(object);
                            property2.setName(name);
                            loadXWikiProperty(property2, context, false);
                            property.setValue(property2.getValue());
                            if (bclass != null) {
                                if (bclass.get(name) instanceof TextAreaClass) {
                                    property = property2;
                                }
                            }
                        } else if (property instanceof LargeStringProperty) {
                            StringProperty property2 = new StringProperty();
                            property2.setObject(object);
                            property2.setName(name);
                            loadXWikiProperty(property2, context, false);
                            property.setValue(property2.getValue());
                            if (bclass != null) {
                                if (bclass.get(name) instanceof StringClass) {
                                    property = property2;
                                }
                            }
                        } else {
                            throw e;
                        }
                    } catch (Throwable e2) {
                        Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + ""), name };
                        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}', number '{2}' and property '{3}'", e, args);
                    }
                }
                object.addField(name, property);
            }
        }
        if (bTransaction) {
            endTransaction(context, false, false);
        }
    } catch (Exception e) {
        Object[] args = { object.getName(), object.getClass(), Integer.valueOf(object.getNumber() + "") };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading object '{0}' of class '{1}' and number '{2}'", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) StringProperty(com.xpn.xwiki.objects.StringProperty) BaseStringProperty(com.xpn.xwiki.objects.BaseStringProperty) BaseCollection(com.xpn.xwiki.objects.BaseCollection) TextAreaClass(com.xpn.xwiki.objects.classes.TextAreaClass) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) StringClass(com.xpn.xwiki.objects.classes.StringClass) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) List(java.util.List) ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Aggregations

BaseCollection (com.xpn.xwiki.objects.BaseCollection)7 XWikiException (com.xpn.xwiki.XWikiException)5 BaseProperty (com.xpn.xwiki.objects.BaseProperty)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)4 SQLException (java.sql.SQLException)4 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)4 Session (org.hibernate.Session)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 InitializationException (org.xwiki.component.phase.InitializationException)4 QueryException (org.xwiki.query.QueryException)4 UnexpectedException (org.xwiki.store.UnexpectedException)4 Collection (java.util.Collection)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 BaseStringProperty (com.xpn.xwiki.objects.BaseStringProperty)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 ArrayList (java.util.ArrayList)2 Query (org.hibernate.Query)2 PropertyClassProvider (com.xpn.xwiki.internal.objects.classes.PropertyClassProvider)1 BaseElement (com.xpn.xwiki.objects.BaseElement)1