* @param <PK>
* Primary key class
*/
-public abstract class AbstractGenericDAOImpl<T, PK extends Serializable> extends
- HibernateDaoSupport implements GenericDAO<T, PK> {
+public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
+ extends HibernateDaoSupport implements GenericDAO<T, PK> {
/**
* Unchecked warning specification.
*/
private static final String UNCHECKED = "unchecked";
+
/**
* Persist the newInstance object into database.
*
*/
public T findByCriteria(final Properties andParams) {
Criterion aCondition = null;
- for (String aName: andParams.stringPropertyNames()) {
- aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
+ for (String aName : andParams.stringPropertyNames()) {
+ aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
+ andParams.get(aName)));
}
return findByCriteria(aCondition);
}
* @return an ordered list of all objects of the considered type T
*/
@SuppressWarnings(UNCHECKED)
- public List<T> getAll(final Order ... anOrder) {
+ public List<T> getAll(final Order... anOrder) {
Criteria aCriteria = getSession().createCriteria(getType());
for (Order order : anOrder) {
- if (anOrder != null) {
+ if (order != null) {
aCriteria.addOrder(order);
}
}
* @return a list of objects filtered according to the given criteria
*/
@SuppressWarnings(UNCHECKED)
- public List<T> getFilteredList(final Criterion aCondition, final Order ... anOrder) {
+ public List<T> getFilteredList(final Criterion aCondition,
+ final Order... anOrder) {
Criteria aCriteria = getSession().createCriteria(getType()).add(
aCondition);
for (Order order : anOrder) {
- if (anOrder != null) {
+ if (order != null) {
aCriteria.addOrder(order);
}
}
return aCriteria.list();
}
+ /**
+ * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+ *
+ * @param joinField
+ * a field containing object to apply the condition
+ *
+ * @param aCondition
+ * a search condition
+ * @return a list of objects filtered according to the given criteria
+ */
+ public List<T> getFilteredList(final String joinField,
+ final Criterion aCondition) {
+ return getFilteredList(joinField, aCondition, (Order) null);
+ }
+
+ /**
+ * Retrieve a list of objects which were previously persisted to the database using the given criteria.
+ *
+ * @param joinField
+ * a field containing object to apply the condition
+ *
+ * @param aCondition
+ * a search condition
+ * @param anOrder
+ * a result list order. Null is ignored and in such case the result list is unordered.
+ * @return a list of objects filtered according to the given criteria
+ */
+ @SuppressWarnings(UNCHECKED)
+ public List<T> getFilteredList(final String joinField,
+ final Criterion aCondition, final Order... anOrder) {
+ Criteria aCriteria = getSession().createCriteria(getType());
+ for (Order order : anOrder) {
+ if (order != null) {
+ aCriteria.addOrder(order);
+ }
+ }
+ return aCriteria.createCriteria(joinField).add(aCondition).list();
+ }
+
/**
* Retrieve a list of objects which were previously persisted to the database using the given criteria.
*
* @return a list of objects filtered according to the given criteria
*/
public List<T> getFilteredList(final Properties andParams) {
- return getFilteredList(andParams);
+ return getFilteredList(andParams, (Order) null);
}
/**
* a result list order. Null is ignored and in such case the result list is unordered.
* @return a list of objects filtered according to the given criteria
*/
- public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
+ public List<T> getFilteredList(final Properties andParams,
+ final Order... anOrder) {
Criterion aCondition = null;
- for (String aName: andParams.stringPropertyNames()) {
- aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
+ for (String aName : andParams.stringPropertyNames()) {
+ aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
+ andParams.get(aName)));
}
return getFilteredList(aCondition, anOrder);
}
package test.splat.dao;
import java.util.Date;
+import java.util.List;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Restrictions;
import org.splat.dal.bo.kernel.User;
import org.splat.dal.bo.som.KnowledgeElement;
import org.splat.dal.bo.som.KnowledgeElementType;
* <ul>
* <li>Object is created in the database successfully<BR>
* </li>
- * <li>Exception is thrown<BR>
+ * <li>Another new object is created with new generated id<BR>
* </li>
* </ul>
* </i>
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testCreate()");
startNestedTransaction();
-
+
KnowledgeElement aKelm = getKnowledgeElement();
// Call DAO's create method for a good transient knowledge element.
- Long id = _knowledgeElementDAO.create(aKelm);
+ long id = _knowledgeElementDAO.create(aKelm);
Assert.assertNotNull(id,
"Create method returns null instead of a new id.");
Assert.assertTrue(id > 0, "The new id is not a positive number.");
+
+ LOG.debug("Created id: " + id);
+
KnowledgeElement aKelmFound = getHibernateTemplate().get(
KnowledgeElement.class, id);
compareObjects(aKelmFound, aKelm);
+ _knowledgeElementDAO.flush();
// Call DAO's create method for a knowledge element with non-zero id.
KnowledgeElement aBadKelm = new KnowledgeElement(
(new KnowledgeElement.Properties())
- .setTitle("Test knowledge element")
+ .setTitle("2.Bad test knowledge element")
.setAuthor(aKelm.getAuthor())
.setOwnerScenario(aKelm.getOwnerScenario())
.setType(aKelm.getType())
.setValue(
"This is another test knowledge element.\nIt is created by the unit test."));
aBadKelm.setIndex(aKelmFound.getIndex());
- try {
- _knowledgeElementDAO.create(aBadKelm);
- getHibernateTemplate().flush();
- Assert.fail("Creation with existing id must be failed.");
- } catch (Exception e) {
- LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
- }
-
+
+ _knowledgeElementDAO.flush();
+ List<KnowledgeElement> res = _knowledgeElementDAO.getAll(Order
+ .asc("rid"));
+
+ Assert.assertNotNull(res,
+ "Method getFilteredList must not return null.");
+ Assert.assertEquals(res.size(), 1, "Number of found objects is wrong.");
+ Assert.assertEquals(aBadKelm.getIndex(), id, "Id must be equal.");
+
+ Assert.assertNotNull(aBadKelm.getIndex(),
+ "Create method returns null instead of a new id.");
+ Assert.assertTrue(aBadKelm.getIndex() > 0,
+ "The new id is not a positive number.");
+
+ long id2 = _knowledgeElementDAO.create(aBadKelm);
+
+ _knowledgeElementDAO.flush();
+
+ LOG.debug("Created second id: " + id2);
+
+ res = _knowledgeElementDAO.getAll();
+ _knowledgeElementDAO.flush();
+
+ Assert.assertNotNull(res,
+ "Method getFilteredList must not return null.");
+ Assert.assertNotNull(res.get(0),
+ "Method getFilteredList must not return null.");
+ Assert.assertNotNull(res.get(1),
+ "Method getFilteredList must not return null.");
+ Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
+ Assert.assertEquals(res.get(0).getIndex(), id,
+ "Sorting is incorrect or id of the first element is wrong.");
+ Assert.assertEquals(res.get(1).getIndex(), id2,
+ "Sorting is incorrect or id of the second element is wrong.");
+ id = res.get(0).getIndex();
+ id2 = res.get(1).getIndex();
+ Assert.assertFalse(id == id2, "Ids of objects must not be duplicated.");
+
rollbackNestedTransaction();
LOG.debug(">>>>> END testCreate()");
}
* <ul>
* <li>Object is found in the database successfully<BR>
* </li>
- * <li>Exception is thrown<BR>
+ * <li>Result of search is null<BR>
* </li>
* </ul>
* </i>
MissedPropertyException, MultiplyDefinedException {
LOG.debug(">>>>> BEGIN testGet()");
startNestedTransaction();
-
+
KnowledgeElement aKelm = getKnowledgeElement();
// Call DAO's create method for a good transient knowledge element.
Long id = _knowledgeElementDAO.create(aKelm);
compareObjects(aKelmFound, aKelm);
// Call DAO's get method for a not existing id.
- try {
- aKelmFound = _knowledgeElementDAO.get(-1L);
- getHibernateTemplate().flush();
- Assert
- .fail("Getting an object with not existing id must be failed.");
- } catch (Exception e) {
- LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
- }
- try {
- aKelmFound = _knowledgeElementDAO.get(0L);
- getHibernateTemplate().flush();
- Assert
- .fail("Getting an object with not existing id must be failed.");
- } catch (Exception e) {
- LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
- }
- try {
- aKelmFound = _knowledgeElementDAO.get(id + 1);
- getHibernateTemplate().flush();
- Assert
- .fail("Getting an object with not existing id must be failed.");
- } catch (Exception e) {
- LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
- }
-
+ aKelmFound = _knowledgeElementDAO.get(-1L);
+ getHibernateTemplate().flush();
+ Assert.assertNull(aKelmFound,
+ "A found object with not existing id must be null.");
+
+ aKelmFound = _knowledgeElementDAO.get(0L);
+ getHibernateTemplate().flush();
+ Assert.assertNull(aKelmFound,
+ "A found object with not existing id must be null.");
+
+ aKelmFound = _knowledgeElementDAO.get(id + 1);
+ getHibernateTemplate().flush();
+ Assert.assertNull(aKelmFound,
+ "A found object with not existing id must be null.");
+
rollbackNestedTransaction();
LOG.debug(">>>>> END testGet()");
}
LOG.debug("Expected exception is thrown: "
+ e.getClass().getSimpleName() + ": " + e.getMessage());
}
-
+
rollbackNestedTransaction();
LOG.debug(">>>>> END testUpdate()");
}
Assert.assertNull(aKelmFound, "Deleted object must not be found.");
// Call DAO's delete method for a not existing id.
- try {
- _knowledgeElementDAO.delete(aKelm);
- getHibernateTemplate().flush();
- Assert.fail("Delete with with not existing id must be failed.");
- } catch (Exception e) {
- LOG.debug("Expected exception is thrown: "
- + e.getClass().getSimpleName() + ": " + e.getMessage());
- }
-
+ getHibernateTemplate().flush();
+
rollbackNestedTransaction();
LOG.debug(">>>>> END testDelete()");
}
"noreply@salome-platform.org");
uprop.disableCheck();
User anAuthor = new User(uprop);
- ht.update(anAuthor);
+ ht.saveOrUpdate(anAuthor);
// Create a test study
Study.Properties stprops = new Study.Properties().setReference(
"TST_SID_01").setTitle("TST_Study").setManager(anAuthor);
// Prepare a knowledge element transient object
kprops
- .setTitle("Test knowledge element")
+ .setTitle("1. Test knowledge element")
.setAuthor(anAuthor)
.setOwnerScenario(aScenario)
.setType(aKType)
Assert.assertEquals(anActual.getIndex(), anExpected.getIndex(),
"Knowledge Element index is not saved.");
}
+
+ /**
+ * Test of getting a filtered list of knowledge elements.<BR>
+ * <B>Description :</B> <BR>
+ * <i>Create two knowledge elements and try to get them from the database.</i><BR>
+ * <B>Action : </B><BR>
+ * <i>1. Find objects with the given value of the property of a child object.</i><BR>
+ * <i>2. Get ordered list of objects with the given value of the property of a child object.</i><BR>
+ * <B>Test data : </B><BR>
+ * <i>no input parameters</i><BR>
+ * <i>no input parameters</i><BR>
+ *
+ * <B>Outcome results:</B><BR>
+ * <i>
+ * <ul>
+ * <li>Objects are found in the database successfully<BR>
+ * </li>
+ * <li>Objects are found in the database and sorted in the right order<BR>
+ * </li>
+ * </ul>
+ * </i>
+ *
+ * @throws InvalidPropertyException
+ * if an invalid property is used when creating objects
+ * @throws MultiplyDefinedException
+ * when trying to create an object with already existing id
+ * @throws MissedPropertyException
+ * if a mandatory property is not defined for an object to be created
+ *
+ */
+ @Test
+ public void testGetFilteredList() throws InvalidPropertyException,
+ MissedPropertyException, MultiplyDefinedException {
+ LOG.debug(">>>>> BEGIN testGetFilteredList()");
+ startNestedTransaction();
+
+ // Create several objects in the database
+ KnowledgeElement aKelm = getKnowledgeElement();
+ long id = _knowledgeElementDAO.create(aKelm);
+ KnowledgeElement aNewKelm = new KnowledgeElement(
+ (new KnowledgeElement.Properties())
+ .setTitle("Test knowledge element N2")
+ .setAuthor(aKelm.getAuthor())
+ .setOwnerScenario(aKelm.getOwnerScenario())
+ .setType(aKelm.getType())
+ .setDate(aKelm.getDate())
+ .setValue(
+ "This is another test knowledge element.\nIt is created by the unit test."));
+ long id2 = _knowledgeElementDAO.create(aNewKelm);
+
+ _knowledgeElementDAO.flush();
+
+ // ////////////////////////////////////////////////////
+ // Call DAO's getFilteredList method without order parameter.
+ List<KnowledgeElement> res = _knowledgeElementDAO.getFilteredList(
+ "type", Restrictions.eq("name", "TST_kelmtype"));
+
+ Assert.assertNotNull(res,
+ "Method getFilteredList must not return null.");
+ Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
+
+ // ////////////////////////////////////////////////////
+ // Call DAO's getFilteredList method with defined order parameter.
+ res = _knowledgeElementDAO.getFilteredList("type", Restrictions.eq(
+ "name", "TST_kelmtype"), Order.asc("title"));
+
+ Assert.assertNotNull(res,
+ "Method getFilteredList must not return null.");
+ Assert.assertEquals(res.size(), 2, "Number of found objects is wrong.");
+ Assert.assertEquals(res.get(0).getIndex(), id,
+ "Sorting of results is not correct.");
+ Assert.assertEquals(res.get(1).getIndex(), id2,
+ "Sorting of results is not correct.");
+
+ rollbackNestedTransaction();
+ LOG.debug(">>>>> END testGetFilteredList()");
+ }
}