Search in sources :

Example 1 with AbstractSerializableNestedObject

use of com.emc.storageos.db.client.model.AbstractSerializableNestedObject in project coprhd-controller by CoprHD.

the class ColumnField method serialize.

/**
 * Serializes object field into database updates
 *
 * @param obj data object to serialize
 * @param mutator row mutator to hold insertion queries
 * @return boolean
 * @throws DatabaseException
 */
public boolean serialize(DataObject obj, RowMutator mutator) {
    try {
        String id = obj.getId().toString();
        if (isLazyLoaded() || _property.getReadMethod() == null) {
            return false;
        }
        Object val = _property.getReadMethod().invoke(obj);
        if (val == null) {
            return false;
        }
        boolean changed = false;
        switch(_colType) {
            case NamedURI:
            case Primitive:
                {
                    if (!obj.isChanged(_name)) {
                        return false;
                    }
                    changed = addColumn(id, getColumnName(null, mutator), val, mutator, obj);
                    break;
                }
            case TrackingSet:
                {
                    AbstractChangeTrackingSet valueSet = (AbstractChangeTrackingSet) val;
                    Set<?> addedSet = valueSet.getAddedSet();
                    if (addedSet != null) {
                        Iterator<?> it = valueSet.getAddedSet().iterator();
                        while (it.hasNext()) {
                            Object itVal = it.next();
                            String targetVal = valueSet.valToString(itVal);
                            changed |= addColumn(id, getColumnName(targetVal, mutator), itVal, mutator);
                        }
                    }
                    Set<?> removedVal = valueSet.getRemovedSet();
                    if (removedVal != null) {
                        Iterator<?> removedIt = removedVal.iterator();
                        while (removedIt.hasNext()) {
                            String targetVal = valueSet.valToString(removedIt.next());
                            if (_index == null) {
                                changed |= removeColumn(id, new ColumnWrapper(getColumnName(targetVal, mutator), targetVal), mutator);
                            } else {
                                addDeletionMark(id, getColumnName(targetVal, mutator), mutator);
                                changed = true;
                            }
                        }
                    }
                    break;
                }
            case TrackingMap:
                {
                    AbstractChangeTrackingMap valueMap = (AbstractChangeTrackingMap) val;
                    Set<String> changedSet = valueMap.getChangedKeySet();
                    if (changedSet != null) {
                        Iterator<String> it = valueMap.getChangedKeySet().iterator();
                        while (it.hasNext()) {
                            String key = it.next();
                            Object entryVal = valueMap.get(key);
                            CompositeColumnName colName = getColumnName(key, mutator);
                            if (clockIndValue != null) {
                                int ordinal = ((ClockIndependentValue) entryVal).ordinal();
                                colName = getColumnName(key, String.format("%08d", ordinal), mutator);
                            }
                            changed |= addColumn(id, colName, valueMap.valToByte(entryVal), mutator);
                        }
                    }
                    Set<String> removedKey = valueMap.getRemovedKeySet();
                    if (removedKey != null) {
                        Iterator<String> removedIt = removedKey.iterator();
                        while (removedIt.hasNext()) {
                            String key = removedIt.next();
                            CompositeColumnName colName = getColumnName(key, mutator);
                            if (clockIndValue != null) {
                                Object removedVal = valueMap.getRemovedValue(key);
                                if (removedVal != null) {
                                    colName = getColumnName(key, String.format("%08d", ((ClockIndependentValue) removedVal).ordinal()), mutator);
                                }
                            }
                            if (_index == null) {
                                changed |= removeColumn(id, new ColumnWrapper(colName, null), mutator);
                            } else {
                                addDeletionMark(id, colName, mutator);
                                changed = true;
                            }
                        }
                    }
                    break;
                }
            case TrackingSetMap:
                {
                    AbstractChangeTrackingSetMap valueMap = (AbstractChangeTrackingSetMap) val;
                    Set<String> keys = valueMap.keySet();
                    if (keys != null) {
                        Iterator<String> it = keys.iterator();
                        while (it.hasNext()) {
                            String key = it.next();
                            AbstractChangeTrackingSet valueSet = valueMap.get(key);
                            Set<?> addedSet = valueSet.getAddedSet();
                            if (addedSet != null) {
                                Iterator<?> itSet = valueSet.getAddedSet().iterator();
                                while (itSet.hasNext()) {
                                    String value = valueSet.valToString(itSet.next());
                                    changed |= addColumn(id, getColumnName(key, value, mutator), value, mutator);
                                }
                            }
                            Set<?> removedVal = valueSet.getRemovedSet();
                            if (removedVal != null) {
                                Iterator<?> removedIt = removedVal.iterator();
                                while (removedIt.hasNext()) {
                                    String targetVal = valueSet.valToString(removedIt.next());
                                    if (_index == null) {
                                        changed |= removeColumn(id, new ColumnWrapper(getColumnName(key, targetVal, mutator), targetVal), mutator);
                                    } else {
                                        addDeletionMark(id, getColumnName(key, targetVal, mutator), mutator);
                                        changed = true;
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
            case NestedObject:
                {
                    if (!obj.isChanged(_name)) {
                        break;
                    }
                    AbstractSerializableNestedObject nestedObject = (AbstractSerializableNestedObject) val;
                    changed |= addColumn(id, getColumnName(null, mutator), nestedObject.toBytes(), mutator);
                }
        }
        return changed;
    } catch (final InvocationTargetException e) {
        throw DatabaseException.fatals.serializationFailedId(obj.getId(), e);
    } catch (final IllegalAccessException e) {
        throw DatabaseException.fatals.serializationFailedId(obj.getId(), e);
    }
}
Also used : Set(java.util.Set) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet) StringSet(com.emc.storageos.db.client.model.StringSet) AbstractChangeTrackingSetMap(com.emc.storageos.db.client.model.AbstractChangeTrackingSetMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClockIndependentValue(com.emc.storageos.db.client.model.ClockIndependentValue) AbstractChangeTrackingMap(com.emc.storageos.db.client.model.AbstractChangeTrackingMap) AbstractSerializableNestedObject(com.emc.storageos.db.client.model.AbstractSerializableNestedObject) Iterator(java.util.Iterator) AbstractSerializableNestedObject(com.emc.storageos.db.client.model.AbstractSerializableNestedObject) DataObject(com.emc.storageos.db.client.model.DataObject) AbstractChangeTrackingSet(com.emc.storageos.db.client.model.AbstractChangeTrackingSet)

Example 2 with AbstractSerializableNestedObject

use of com.emc.storageos.db.client.model.AbstractSerializableNestedObject in project coprhd-controller by CoprHD.

the class ColumnValue method getPrimitiveColumnValue.

public static <T> Object getPrimitiveColumnValue(Column<T> column, PropertyDescriptor pd) {
    Object objValue = null;
    try {
        Class type = pd.getPropertyType();
        if (AbstractSerializableNestedObject.class.isAssignableFrom(type)) {
            objValue = type.newInstance();
            AbstractSerializableNestedObject value = (AbstractSerializableNestedObject) objValue;
            value.loadBytes(column.getByteArrayValue());
        } else if (type == String.class) {
            objValue = column.getStringValue();
        } else if (type == URI.class) {
            objValue = URI.create(column.getStringValue());
        } else if (type == Byte.class) {
            objValue = (byte) (column.getIntegerValue() & 0xff);
        } else if (type == Boolean.class) {
            objValue = column.getBooleanValue();
        } else if (type == Short.class) {
            objValue = (short) (column.getShortValue());
        } else if (type == Integer.class) {
            objValue = column.getIntegerValue();
        } else if (type == Long.class) {
            objValue = column.getLongValue();
        } else if (type == Float.class) {
            objValue = (float) column.getDoubleValue();
        } else if (type == Double.class) {
            objValue = column.getDoubleValue();
        } else if (type == Date.class) {
            objValue = column.getDateValue();
        } else if (type == NamedURI.class) {
            objValue = NamedURI.fromString(column.getStringValue());
        } else if (type == byte[].class) {
            objValue = column.getByteArrayValue();
        } else if (type == ScopedLabel.class) {
            objValue = ScopedLabel.fromString(column.getStringValue());
        } else if (type.isEnum()) {
            objValue = Enum.valueOf(type, column.getStringValue());
        } else if (type == Calendar.class) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(column.getLongValue());
            objValue = cal;
        } else {
            throw new UnsupportedOperationException();
        }
    } catch (IllegalAccessException e) {
        // should never get here
        throw DatabaseException.fatals.deserializationFailedProperty(pd.getName(), e);
    } catch (InstantiationException e) {
        throw DatabaseException.fatals.deserializationFailedProperty(pd.getName(), e);
    }
    return objValue;
}
Also used : NamedURI(com.emc.storageos.db.client.model.NamedURI) Calendar(java.util.Calendar) AbstractSerializableNestedObject(com.emc.storageos.db.client.model.AbstractSerializableNestedObject) ScopedLabel(com.emc.storageos.db.client.model.ScopedLabel) AbstractSerializableNestedObject(com.emc.storageos.db.client.model.AbstractSerializableNestedObject)

Aggregations

AbstractSerializableNestedObject (com.emc.storageos.db.client.model.AbstractSerializableNestedObject)2 AbstractChangeTrackingMap (com.emc.storageos.db.client.model.AbstractChangeTrackingMap)1 AbstractChangeTrackingSet (com.emc.storageos.db.client.model.AbstractChangeTrackingSet)1 AbstractChangeTrackingSetMap (com.emc.storageos.db.client.model.AbstractChangeTrackingSetMap)1 ClockIndependentValue (com.emc.storageos.db.client.model.ClockIndependentValue)1 DataObject (com.emc.storageos.db.client.model.DataObject)1 NamedURI (com.emc.storageos.db.client.model.NamedURI)1 ScopedLabel (com.emc.storageos.db.client.model.ScopedLabel)1 StringSet (com.emc.storageos.db.client.model.StringSet)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Calendar (java.util.Calendar)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1