r2099 - trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util
Author: tchemit Date: 2010-08-14 21:56:03 +0200 (Sat, 14 Aug 2010) New Revision: 2099 Url: http://nuiton.org/repositories/revision/topia/2099 Log: transfer usefull methods from replication service (must do javadoc + i18n) Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-08-14 13:54:40 UTC (rev 2098) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2010-08-14 19:56:03 UTC (rev 2099) @@ -29,6 +29,7 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; import org.nuiton.topia.persistence.TopiaEntityEnum; @@ -55,6 +56,8 @@ import java.util.SortedMap; import java.util.TreeMap; +import static org.nuiton.i18n.I18n._; + /** * Une classe avec des méthodes utiles sur les entités. * @@ -1077,4 +1080,92 @@ } interfaces.add(klass); } + + @SuppressWarnings({"unchecked"}) + public static <E extends TopiaEntity> List<E> getEntities( + TopiaContextImplementor srcCtxt, + List<E> entityList, + boolean canBeNull) throws TopiaException { + List<E> srcList = new ArrayList<E>(entityList.size()); + for (E e : entityList) { + E e2 = (E) srcCtxt.findByTopiaId(e.getTopiaId()); + if (e2 == null && !canBeNull) { +// if (!canBeNull) { +// throw new IllegalStateException( +// "topia.replication.engine.error.entity.must.exists"); +// } + continue; + } + srcList.add(e2); + } + return srcList; + } + + public static TopiaEntity[] getEntities(TopiaContext srcCtxt, + String... entityList) + throws TopiaException { + TopiaEntity[] srcList = new TopiaEntity[entityList.length]; + int index = 0; + for (String id : entityList) { + TopiaEntity e2 = srcCtxt.findByTopiaId(id); + srcList[index++] = e2; + } + return srcList; + } + + public static List<? extends TopiaEntity> getEntitiesList( + TopiaContext srcCtxt, + String... entityList) throws TopiaException { + List<TopiaEntity> srcList = new ArrayList<TopiaEntity>(entityList.length); + for (String id : entityList) { + TopiaEntity e2 = srcCtxt.findByTopiaId(id); + srcList.add(e2); + } + return srcList; + } + + public static void checkNotNull(String methodName, + String parameterName, + Object value) { + if (value == null) { + throw new NullPointerException( + _("topia.replication.engine.error.null.param", + parameterName, methodName)); + } + } + + public static void checkParameters(Class<?>[] paramsType, + Object... params) { + checkSize(paramsType.length, params); + for (int i = 0, j = paramsType.length; i < j; i++) { + checkType(paramsType, i, params); + } + } + + public static void checkSize(int size, Object[] params) { + if (params.length != size) { + throw new IllegalArgumentException( + "l'operation requiere " + size + " parametres mais en a " + + params.length); + } + } + + public static void checkType(Class<?>[] paramsType, + int index, + Object[] params) { + Class<?> requiredType = paramsType[index]; + Object value = params[index]; + if (value == null) { + throw new IllegalArgumentException( + "le parametre de positiion" + index + " est null!"); + } + Class<?> foundType = value.getClass(); + + if (!requiredType.isAssignableFrom(foundType)) { + throw new IllegalArgumentException( + "le paremetre de position " + index + + " requiere un parametre de type " + requiredType + + " mais est de type " + foundType); + } + } }
participants (1)
-
tchemit@users.nuiton.org