You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by Ricardo Tercero Lozano <rt...@isoco.com> on 2003/07/10 20:16:17 UTC

Simplifying the RowReader implementation for extents

Some time ago I need to extent classes based on a
boolean database field not named 'ojbConcreteClass'.
It was a problem because selectClassDescriptor method
showed in documentation was not the last implementation, 
so I had to deal with OJB internal classes (not a
problem really but uncomfortable).

I found a better aproach based on little modifications
to RowReaderDefaultImpl.java file. The idea is to put
out of this class the things useful to do a no default
extent: the field name and the choose of concrete class
name.

It can be easily done by just renaming RowReaderDefaultImpl
to (for example) AbstractRowReaderDefaultImpl with this
changes:

...
  /**
   * Check if there is an attribute which tells us which concrete class is
   * to be instantiated.
   */
  protected ClassDescriptor selectClassDescriptor(Map row) throws
      PersistenceBrokerException {

    // check if there is an attribute which tells us which concrete class
    // is to be instantiated
    
    FieldDescriptor concreteClassFD = getClassDescriptor().
        getFieldDescriptorByName(getFieldCriteriaName());
    if (concreteClassFD == null) {
      return getClassDescriptor();
    } else {
      try {
        /* Modified */
        String concreteClass = getConcreteClass(row.get(concreteClassFD.
                                         getColumnName()));
        /* End Modifications */
        if (concreteClass == null || concreteClass.trim().length() == 0) {
          throw new PersistenceBrokerException(
              /* Modified */
              getFieldCriteriaName().toUpperCase() +
              " field returned null or 0-length string");
              /* End Modifications */
        } else {
          concreteClass = concreteClass.trim();
        }
        ClassDescriptor result = getClassDescriptor().getRepository().
            getDescriptorFor(concreteClass);
        if (result == null) {
          result = getClassDescriptor();
        }
        return result;
      } catch (PBFactoryException e) {
        throw new PersistenceBrokerException(e);
      }
    }
  }

  /* to be filled in concrete scenario */

  /**
   * Gets the criteria column name on which is based the selection of the
class
   * to be instantiated
   * @return the criteria column name
   */
  protected abstract String getFieldCriteriaName();

  /**
   * Gets a string representation of the full qualified name of the class to
   * be instantiated based on criteria param
   * @param criteria object on which is based the choose of the class to be
returned
   * @return a full qualified name of a class
   */
  protected abstract String getConcreteClass(Object criteria);
...

Then create a RowReaderDefaultImpl extending the Abstact one
whith this code:

public class RowReaderDefaultImpl extends AbstractRowReaderDefaultImpl {

  public RowReaderDefaultImpl(ClassDescriptor cld) {
    super(cld);
  }

  protected String getFieldCriteriaName() {
    return ClassDescriptor.OJB_CONCRETE_CLASS;
  }

  protected String getConcreteClass(Object criteria) {
	return (String)criteria;
  }

}


This approach simplifies the task to do my own extent
by just filling that two abstract methods and haven't 
to deal with OJB internal classes.

Saludos,


--
------------------------------------------------------
Ricardo Tercero Lozano
iSOCO - Intelligent Software for the Networked Economy
e-mail: rtercero@isoco.com
Web: http://www.isoco.com
Francisca Delgado 11, 2nd floor
28108 Alcobendas
Madrid (Spain)
#T: +34 91 334 9750
    +34 91 334 9797 (Switchboard)
#F: +34 91 334 9799


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org