Search in sources :

Example 71 with BaseProperty

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;
}
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 72 with BaseProperty

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;
}
Also used : IntegerProperty(com.xpn.xwiki.objects.IntegerProperty) BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 73 with BaseProperty

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;
}
Also used : BaseProperty(com.xpn.xwiki.objects.BaseProperty)

Example 74 with BaseProperty

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);
}
Also used : LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) StringProperty(com.xpn.xwiki.objects.StringProperty) LargeStringProperty(com.xpn.xwiki.objects.LargeStringProperty) BaseProperty(com.xpn.xwiki.objects.BaseProperty) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 75 with BaseProperty

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();
    }
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) 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) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiStats(com.xpn.xwiki.stats.impl.XWikiStats) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

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