Search in sources :

Example 1 with IndexedObject

use of org.eclipse.persistence.internal.helper.IndexedObject in project eclipselink by eclipse-ee4j.

the class OrderedListContainerPolicy method addAll.

protected boolean addAll(List elements, Object container, AbstractSession session, List<AbstractRecord> dbRows, ReadQuery query, CacheKey parentCacheKey) {
    int size = dbRows.size();
    if (this.elementDescriptor != null && this.elementDescriptor.getObjectBuilder().hasWrapperPolicy()) {
        ObjectBuilder objectBuilder = this.elementDescriptor.getObjectBuilder();
        List wrappedElements = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            wrappedElements.add(objectBuilder.wrapObject(elements.get(i), session));
        }
        elements = wrappedElements;
    }
    ConversionManager conversionManager = session.getDatasourcePlatform().getConversionManager();
    // populate container with a dummy so the container.set(i, obj) could be used later.
    for (int i = 0; i < size; i++) {
        ((List) container).add(NOT_SET);
    }
    // insert the elements into container
    boolean failed = false;
    for (int i = 0; i < size; i++) {
        AbstractRecord row = dbRows.get(i);
        Object orderValue = row.get(this.listOrderField);
        // order value is null
        if (orderValue == null) {
            failed = true;
            break;
        }
        int intOrderValue = conversionManager.convertObject(orderValue, Integer.class);
        try {
            // one or more elements have the same order value
            if (NOT_SET != ((List) container).set(intOrderValue, elements.get(i))) {
                failed = true;
                break;
            }
        } catch (IndexOutOfBoundsException indexException) {
            // order value negative or greater/equal to size
            failed = true;
            break;
        }
    }
    if (failed) {
        ((List) container).clear();
        // extract order list - it will be set into exception or used by order correction.
        List<Integer> orderList = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            AbstractRecord row = dbRows.get(i);
            Object orderValue = row.get(this.listOrderField);
            if (orderValue == null) {
                orderList.add(null);
            } else {
                orderList.add(conversionManager.convertObject(orderValue, Integer.class));
            }
        }
        if (this.orderCorrectionType == OrderCorrectionType.READ || this.orderCorrectionType == OrderCorrectionType.READ_WRITE) {
            // pair each element with its order index
            List<IndexedObject> indexedElements = new ArrayList(size);
            for (int i = 0; i < size; i++) {
                indexedElements.add(new IndexedObject(orderList.get(i), elements.get(i)));
            }
            // put elements in order and add to container
            ((List) container).addAll(correctOrderList(indexedElements));
            if (this.orderCorrectionType == OrderCorrectionType.READ_WRITE) {
                // mark IndirectList to have broken order
                ((IndirectList) container).setIsListOrderBrokenInDb(true);
            }
        } else {
            // this.orderCorrectionType == OrderCorrectionType.EXCEPTION
            throw QueryException.listOrderFieldWrongValue(query, this.listOrderField, orderList);
        }
    }
    return size > 0;
}
Also used : ArrayList(java.util.ArrayList) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) IndirectList(org.eclipse.persistence.indirection.IndirectList) IndexedObject(org.eclipse.persistence.internal.helper.IndexedObject) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) ArrayList(java.util.ArrayList) List(java.util.List) IndirectList(org.eclipse.persistence.indirection.IndirectList) IndexedObject(org.eclipse.persistence.internal.helper.IndexedObject) OrderedChangeObject(org.eclipse.persistence.internal.sessions.OrderedChangeObject)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 IndirectList (org.eclipse.persistence.indirection.IndirectList)1 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)1 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)1 IndexedObject (org.eclipse.persistence.internal.helper.IndexedObject)1 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)1 OrderedChangeObject (org.eclipse.persistence.internal.sessions.OrderedChangeObject)1