I have two class definitions in my Spring MVC web application named Class and Object respectively:
public Class {
//instance variables
int classId;
int className;
}
public Object {
//instance variables
int objectId;
int objectName;
}
I also have a service that that returns a list of Class and Object defined as follows.
package com.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import com.domain.Class;
import com.domain.Object;
import com.service.SearchManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.io.Serializable;
public class SempediaSearchManager implements com.service.SearchManager {
private SessionFactory sessionFactory;
private List<Class> classes;
private List<Object> objects;
public List<Class> getClassSeedSearch(String classSeed) {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
try {
Query query = session.createQuery("from Class c where lower(c.className) like lower('"
+ classSeed + "%')");
return query.list();
} finally {
session.close();
}
}
public List<Object> getObjectSeedSearch(String objectSeed) {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
try {
Query query = session.createQuery("from Object o where lower(o.objectName) like lower('"
+ objectSeed + "%')");
return query.list();
} finally {
session.close();
}
}
?????
skeleton method
public List<ClassesAndObjects> getObjectorClassSeedSearch(String objectOrClassSeed) {
?????
}
The view I present has a search box that allow a user to search for either classes or objects based on the repsective list returned by the methods getObjectSeedSearch, and getClassSeedSearch – List, List respectively
What I’d like to allow the search text box to simply search for both classes and objects, returning perhaps a merged list – List – if you would that has both classes and Objects by leveraging a method getObjectorClassSeedSearch
What would be the best way forward in implementing this.
I vaguely know I’d want to create a wrapper class that would take both Class and Object and perhaps polymorphically at run time determine what instance and item is when being returned of propped from the list. I guessing I would have a corresponding bean for this in my application. perhaps an exercise with generics.
What would be an efficient way to proceed?
Thanks
Have a common interface/abstract class for both of them:
You can return one List<Name> as needed that way.
Good names matter. Overloading “Class” and “Object”, when they’re already part of the java.lang package, will only cause you and future maintainers of your code grief.
These might be examples, but I see no reason for either of these classes. There’s no abstraction or rich behavior. I’d say this is a design worth rethinking, based on what I see here.