Author: tchemit Date: 2013-07-12 09:05:37 +0200 (Fri, 12 Jul 2013) New Revision: 2753 Url: http://nuiton.org/projects/topia/repository/revisions/2753 Log: fixes #2751: Improve TopiaDAO create methods Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2013-07-11 16:43:58 UTC (rev 2752) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2013-07-12 07:05:37 UTC (rev 2753) @@ -315,12 +315,14 @@ @Override public E create(E entity) throws TopiaException { - // TODO EC-20100322 this code may be merged with other create() methods try { // first set topiaId + if (entity.getTopiaId() == null) { - String topiaId = getContext().getTopiaIdFactory().newTopiaId(entityClass, entity); - entity.setTopiaId(topiaId); + // only set id if not already on + String topiaId = getContext().getTopiaIdFactory().newTopiaId(entityClass, entity); + entity.setTopiaId(topiaId); + } if (entity instanceof TopiaEntityContextable) { TopiaEntityContextable contextable = (TopiaEntityContextable)entity; @@ -329,7 +331,7 @@ // save entity getSession().save(entity); - getContext().getFiresSupport().warnOnUpdateEntity(entity); + getContext().getFiresSupport().warnOnCreateEntity(entity); return entity; } catch (HibernateException eee) { throw new TopiaException(eee); @@ -338,24 +340,23 @@ @Override public E create(Object... properties) throws TopiaException { + + int propertiesLength = properties.length; + Preconditions.checkArgument(propertiesLength % 2 == 0, + "Wrong number of argument " + + propertiesLength + + ", you must have even number."); + Map<String, Object> map = new HashMap<String, Object>(); - Object propertyName = null; - Object value; - try { - for (int i = 0; i < properties.length; ) { - propertyName = properties[i++]; - value = properties[i++]; - map.put((String) propertyName, value); - } - } catch (ArrayIndexOutOfBoundsException eee) { - throw new IllegalArgumentException("Wrong number of argument " - + properties.length - + ", you must have even number. Last property name read: " - + propertyName); - } catch (ClassCastException eee) { - throw new IllegalArgumentException( - "Wrong argument type, wait property name as String and " + - "have " + propertyName.getClass().getName()); + for (int i = 0; i < propertiesLength; ) { + Object propertyName = properties[i++]; + Object value = properties[i++]; + Preconditions.checkArgument( + propertyName instanceof String, + "Argument at position [" + (i - 1) + "] " + + "shoud be a property name (says a String) but was " + + propertyName); + map.put((String) propertyName, value); } E result = create(map); @@ -389,22 +390,8 @@ "Can't put properties on new Object", eee); } - if (result.getTopiaId() == null) { - // tchemit 2013/07/11 : avant ce code était avant le block précédent, donc si on avait un topiaId on n'en regénérait pas un - // tchemit 2013/07/11 : donc je remet le même comportement qu'avant - String newTopiaId = getContext().getTopiaIdFactory().newTopiaId(entityClass, result); - result.setTopiaId(newTopiaId); - } - if (result instanceof TopiaEntityContextable) { - TopiaEntityContextable contextable = (TopiaEntityContextable)result; - contextable.setTopiaContext(getContext()); - } + create(result); - // on fait un save maintenant, car puisqu'on a creer l'entity au - // travers du DAO, on s'attend a l'avoir a disposition tout de - // suite pour les requetes sans avoir a faire un update dessus - getSession().save(result); - getContext().getFiresSupport().warnOnCreateEntity(result); return result; } Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2013-07-11 16:43:58 UTC (rev 2752) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/NaturalIdTest.java 2013-07-12 07:05:37 UTC (rev 2753) @@ -92,9 +92,14 @@ // Note : this is possible to create an empty entity if the type // is primitive like 'int' which have a default value of '0' - } catch (PropertyValueException eee) { - Assert.assertEquals("naturalIdNotNull", eee.getPropertyName()); + } catch (TopiaException eee) { + Assert.assertNotNull(eee.getCause()); + Assert.assertTrue(eee.getCause() instanceof PropertyValueException); + Assert.assertEquals("naturalIdNotNull", ((PropertyValueException)eee.getCause()).getPropertyName()); } +// catch (PropertyValueException eee) { +// Assert.assertEquals("naturalIdNotNull", eee.getPropertyName()); +// } } @Test
participants (1)
-
tchemit@users.nuiton.org