Search in sources :

Example 11 with StringProperty

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

the class PropertyConverterTest method singleToMultipleSelectOnUnsetDBList.

@Test
public void singleToMultipleSelectOnUnsetDBList() throws Exception {
    // The Database List property that was switched from single select to multiple select.
    DBListClass dbListClass = mock(DBListClass.class);
    when(dbListClass.isMultiSelect()).thenReturn(true);
    StringProperty stringProperty = mock(StringProperty.class);
    when(stringProperty.getValue()).thenReturn(null);
    assertNull(this.mocker.getComponentUnderTest().convertProperty(stringProperty, dbListClass));
}
Also used : DBListClass(com.xpn.xwiki.objects.classes.DBListClass) StringProperty(com.xpn.xwiki.objects.StringProperty) Test(org.junit.Test)

Example 12 with StringProperty

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

the class PropertyConverterTest method longToString.

@Test
public void longToString() throws Exception {
    LongProperty longProperty = new LongProperty();
    longProperty.setValue(Long.MAX_VALUE);
    StringClass stringClass = mock(StringClass.class);
    when(stringClass.newProperty()).thenReturn(new StringProperty());
    StringProperty stringProperty = new StringProperty();
    when(stringClass.fromString(longProperty.toText())).thenReturn(stringProperty);
    assertEquals(stringProperty, this.mocker.getComponentUnderTest().convertProperty(longProperty, stringClass));
}
Also used : StringClass(com.xpn.xwiki.objects.classes.StringClass) LongProperty(com.xpn.xwiki.objects.LongProperty) StringProperty(com.xpn.xwiki.objects.StringProperty) Test(org.junit.Test)

Example 13 with StringProperty

use of com.xpn.xwiki.objects.StringProperty 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)

Example 14 with StringProperty

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

the class R73000XWIKI12277DataMigration method doWork.

private Object doWork(Session session) throws HibernateException {
    Query query = session.createQuery(createQueryString());
    List results = query.list();
    for (Object result : results) {
        Object[] resultLine = (Object[]) result;
        BaseObject object = (BaseObject) resultLine[0];
        StringProperty typeProperty = (StringProperty) resultLine[1];
        // Migrate each property.
        migrateProperty(typeProperty, object, session);
    }
    return Boolean.TRUE;
}
Also used : Query(org.hibernate.Query) StringProperty(com.xpn.xwiki.objects.StringProperty) List(java.util.List) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

StringProperty (com.xpn.xwiki.objects.StringProperty)14 BaseProperty (com.xpn.xwiki.objects.BaseProperty)7 Test (org.junit.Test)6 BaseObject (com.xpn.xwiki.objects.BaseObject)4 StringListProperty (com.xpn.xwiki.objects.StringListProperty)4 DBListClass (com.xpn.xwiki.objects.classes.DBListClass)3 DBStringListProperty (com.xpn.xwiki.objects.DBStringListProperty)2 ListProperty (com.xpn.xwiki.objects.ListProperty)2 StringClass (com.xpn.xwiki.objects.classes.StringClass)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Query (org.hibernate.Query)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseCollection (com.xpn.xwiki.objects.BaseCollection)1 BaseStringProperty (com.xpn.xwiki.objects.BaseStringProperty)1 IntegerProperty (com.xpn.xwiki.objects.IntegerProperty)1 LargeStringProperty (com.xpn.xwiki.objects.LargeStringProperty)1