David,

I retrieve a list of objects and sort them manually if a sort order is
requested (this could be done by default also).

I use the sorting features of the collection framework, using specialty
comparators on my business objects. A code snippet might clarify:

    public Iterator getUsers() {
        logger.setLevel(Level.DEBUG);

        PersistenceBroker broker = null;
        try {

            List users = null;
            if (mySession.getAttribute(UserSearch.SEARCH_ATTRIBUTE) !=
null && !mySession.getAttribute(UserSearch.SEARCH_ATTRIBUTE).equals(""))
{
                logger.debug("User requested a search on:" + (String)
mySession.getAttribute(UserSearch.SEARCH_ATTRIBUTE));
                users = new ArrayList(searchUsers((String)
mySession.getAttribute(UserSearch.SEARCH_ATTRIBUTE)));
            } else {
                logger.debug("no search required");
                broker =
PersistenceBrokerFactory.defaultPersistenceBroker();
                logger.debug("broker acquired");
                Query query = new QueryByCriteria(User.class, null);
                logger.debug("query created");
                users = new
ArrayList(broker.getCollectionByQuery(query));
                logger.debug("arraylist obtained");
            }
            // check if we need to sort the users
            if (mySession.getAttribute(UserSort.SORT_ATTRIBUTE) != null
&& !mySession.getAttribute(UserSort.SORT_ATTRIBUTE).equals("")) {
                logger.debug("user requested a sort");
                String sortAttribute = (String)
mySession.getAttribute(UserSort.SORT_ATTRIBUTE);

                User user = null;
                for (Iterator iterator = users.iterator();
iterator.hasNext();) {
                    logger.debug("setting sort attributes");
                    user = (User) iterator.next();
                    user.setSortAttribute(sortAttribute);
                }
                logger.debug("comparator to use: " +
user.getSortedAttribute().getComparator().getClass());
                Collections.sort(users,
user.getSortedAttribute().getComparator());
            }
            return users.iterator();

        } catch (PBFactoryException ex) {
            logger.fatal("Could not get a PersistenceBroker ", ex);
            //ex.printStackTrace();

        } catch (PersistenceBrokerException ex) {
            // if something went wrong: rollback
            logger.error("Could not get Users", ex);
            //ex.printStackTrace();

        } finally {
            // return broker to pool
            if (broker != null) {
                broker.close();
            }
        }
        // if we did not return yet, something went wrong with the
persistence broker!
        // Since this method is called from the presentation layer, we
cannot show an appropriate error!
        // so we just return an empty iterator.
        return new ArrayList().iterator();
    }



To recap, I sort using the collection framework, I search using the OJB
framework.

hope this helps.

ilya

 

>>> [EMAIL PROTECTED] 10/16/02 11:18pm >>>
I know OJB supports lists/collections in various manners.  But it is my

guess, that it is completely unordered - that is, even when the Java 
object is a List (ordered), nothing is done to ensure that the database

reflects this order, and that upon reload, the order may be different.

We are going to have several places where the list is ordered 
(arbitrarily, by the user), and we need to preserve this ordering. 
 What's the best way to do this in OJB?

David


--
To unsubscribe, e-mail:  
<mailto:[EMAIL PROTECTED]>
For additional commands, e-mail:
<mailto:[EMAIL PROTECTED]>

--
****************************************************************************
This message contains information that may be privileged or confidential and
is the property of the Cap Gemini Ernst & Young Group. It is only intended
for the person to whom it is addressed. If you are not the intended
recipient, you are not authorized to read, print, retain, copy disseminate,
distribute, or use this message or any part thereof. If you receive this
message in error, please notify the sender immediately and delete all copies
of this message.
****************************************************************************

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to