Package org.apache.ojb.broker.metadata

Examples of org.apache.ojb.broker.metadata.FieldDescriptor


        {
            try
            {
                int jdbcType = m_jdbcTypes[i];
                String attr = q.getAttributes()[i];
                FieldDescriptor fld = (FieldDescriptor) q.getAttributeFieldDescriptors().get(attr);
                Object val =JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
               
                if (fld != null && fld.getFieldConversion() != null)
                {
                    val = fld.getFieldConversion().sqlToJava(val);
                }
                result[i] = val;
            }
            catch (SQLException e)
            {
View Full Code Here


            String className = (String) source.get(CLASS_NAME_STR);
            ClassDescriptor cld = broker.getDescriptorRepository().getDescriptorFor(className);
            Object target = ClassHelper.buildNewObjectInstance(cld);
            // perform main object values
            FieldDescriptor[] flds = cld.getFieldDescriptor(true);
            FieldDescriptor fld;
            int length = flds.length;
            for(int i = 0; i < length; i++)
            {
                fld = flds[i];
                // read the field value
                Object value = source.get(fld.getPersistentField().getName());
                // copy the field value
                if(value != null) value = fld.getJdbcType().getFieldType().copy(value);
                // now make a field-conversion to java-type, because we only
                // the sql type of the field
                value = fld.getFieldConversion().sqlToJava(value);
                // set the copied field value in new object
                fld.getPersistentField().set(target, value);
            }
            return target;
        }
View Full Code Here

            ClassDescriptor cld = broker.getClassDescriptor(obj.getClass());
            // we store field values by name in a Map
            HashMap target = oldObject != null ? (HashMap) oldObject : new HashMap();
            // perform main object values
            FieldDescriptor[] flds = cld.getFieldDescriptor(true);
            FieldDescriptor fld;
            int length = flds.length;
            for(int i = 0; i < length; i++)
            {
                fld = flds[i];
                // get the value
                Object value = fld.getPersistentField().get(obj);
                // convert value to a supported sql type
                value = fld.getFieldConversion().javaToSql(value);
                // copy the sql type
                value = fld.getJdbcType().getFieldType().copy(value);
                target.put(fld.getPersistentField().getName(), value);
            }
            target.put(CLASS_NAME_STR, obj.getClass().getName());
            return target;
        }
View Full Code Here

     * @param obj
     * @param oldLockingValues
     */
    private void setLockingValues(ClassDescriptor cld, Object obj, ValueContainer[] oldLockingValues)
    {
        FieldDescriptor fields[] = cld.getLockingFields();

        for (int i=0; i<fields.length; i++)
        {
            PersistentField field = fields[i].getPersistentField();
            Object lockVal = oldLockingValues[i].getValue();
View Full Code Here

    protected void assignAutoincrementSequences(ClassDescriptor cld, Object target) throws SequenceManagerException
    {
        // TODO: refactor auto-increment handling, auto-increment should only be supported by PK fields?
        // FieldDescriptor[] fields = cld.getPkFields();
        FieldDescriptor[] fields = cld.getFieldDescriptor(false);
        FieldDescriptor field;
        for(int i = 0; i < fields.length; i++)
        {
            field = fields[i];
            if(field.isAutoIncrement() && !field.isAccessReadOnly())
            {
                Object value = field.getPersistentField().get(target);
                if(broker.serviceBrokerHelper().representsNull(field, value))
                {
                    Object id = broker.serviceSequenceManager().getUniqueValue(field);
                    field.getPersistentField().set(target, id);
                }
            }
        }
    }
View Full Code Here

     * @param referencedObject The referenced object or <i>null</i>
     */
    private void setFKField(Object targetObject, ClassDescriptor cld, ObjectReferenceDescriptor rds, Object referencedObject)
    {
        ValueContainer[] refPkValues;
        FieldDescriptor fld;
        FieldDescriptor[] objFkFields = rds.getForeignKeyFieldDescriptors(cld);
        if (objFkFields == null)
        {
            throw new PersistenceBrokerException("No foreign key fields defined for class '"+cld.getClassNameOfObject()+"'");
        }
        if(referencedObject == null)
        {
            refPkValues = null;
        }
        else
        {
            Class refClass = proxyFactory.getRealClass(referencedObject);
            ClassDescriptor refCld = getClassDescriptor(refClass);
            refPkValues = brokerHelper.getKeyValues(refCld, referencedObject, false);
        }
        for (int i = 0; i < objFkFields.length; i++)
        {
            fld = objFkFields[i];
            /*
            arminw:
            we set the FK value when the extracted PK fields from the referenced object are not null at all
            or if null, the FK field was not a PK field of target object too.
            Should be ok, because the values of the extracted PK field values should never be null and never
            change, so it doesn't matter if the target field is a PK too.
            */
            if(refPkValues != null || !fld.isPrimaryKey())
            {
                fld.getPersistentField().set(targetObject, refPkValues != null ? refPkValues[i].getValue(): null);
            }
        }
    }
View Full Code Here

        // read in fresh copy from the db, but do not cache it
        Object freshInstance = getPlainDBObject(cld, oid);

        // update all primitive typed attributes
        FieldDescriptor[] fields = cld.getFieldDescriptions();
        FieldDescriptor fmd;
        PersistentField fld;
        for (int i = 0; i < fields.length; i++)
        {
            fmd = fields[i];
            fld = fmd.getPersistentField();
            fld.set(cachedInstance, fld.get(freshInstance));
        }
    }
View Full Code Here

     * @throws PersistenceBrokerException if there ewas an error creating the new object
     */
    protected Object buildOrRefreshObject(Map row, ClassDescriptor targetClassDescriptor, Object targetObject)
    {
        Object result = targetObject;
        FieldDescriptor fmd;
        FieldDescriptor[] fields = targetClassDescriptor.getFieldDescriptor(true);

        if(targetObject == null)
        {
            // 1. create new object instance if needed
            result = ClassHelper.buildNewObjectInstance(targetClassDescriptor);
        }

        // 2. fill all scalar attributes of the new object
        for (int i = 0; i < fields.length; i++)
        {
            fmd = fields[i];
            fmd.getPersistentField().set(result, row.get(fmd.getColumnName()));
        }

        if(targetObject == null)
        {
            // 3. for new build objects, invoke the initialization method for the class if one is provided
View Full Code Here

    protected void readValuesFrom(ResultSetAndStatement rs_stmt, Map row, FieldDescriptor[] fields)
    {
        int size = fields.length;
        Object val;
        FieldDescriptor fld = null;
        try
        {
            for (int j = 0; j < size; j++)
            {
                fld = fields[j];
                if(!row.containsKey(fld.getColumnName()))
                {
                    int idx = rs_stmt.m_sql.getColumnIndex(fld);
                    val = fld.getJdbcType().getObjectFromColumn(rs_stmt.m_rs, null, fld.getColumnName(), idx);
                    row.put(fld.getColumnName(), fld.getFieldConversion().sqlToJava(val));
                }
            }
        }
        catch (SQLException t)
        {
            throw new PersistenceBrokerException("Error reading class '"
                    + (fld != null ? fld.getClassDescriptor().getClassNameOfObject() : m_cld.getClassNameOfObject())
                    + "' from result set, current read field was '"
                    + (fld != null ? fld.getPersistentField().getName() + "'" : null), t);
        }
    }
View Full Code Here

        }
    }

    protected String extractOjbConcreteClass(ClassDescriptor cld, ResultSet rs, Map row)
    {
        FieldDescriptor fld = m_cld.getOjbConcreteClassField();
        if (fld == null)
        {
            return null;
        }
        try
        {
            Object tmp = fld.getJdbcType().getObjectFromColumn(rs, fld.getColumnName());
            // allow field-conversion for discriminator column too
            String result = (String) fld.getFieldConversion().sqlToJava(tmp);
            result = result != null ? result.trim() : null;
            if (result == null || result.length() == 0)
            {
                throw new PersistenceBrokerException(
                        "ojbConcreteClass field for class " + cld.getClassNameOfObject()
                        + " returned null or 0-length string");
            }
            else
            {
                /*
                arminw: Make sure that we don't read discriminator field twice from the ResultSet.
                */
                row.put(fld.getColumnName(), result);
                return result;
            }
        }
        catch(SQLException e)
        {
            throw new PersistenceBrokerException("Unexpected error while try to read 'ojbConcretClass'" +
                    " field from result set using column name " + fld.getColumnName() + " main class" +
                    " was " + m_cld.getClassNameOfObject(), e);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.metadata.FieldDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.