protected boolean checkDataObjectValidation(DataObject object) { ValidationResult vr = new ValidationResult(); switch (object.getPersistenceState()) { case PersistenceState.NEW: ((CayenneDataObject) object).validateForInsert(vr); break; case PersistenceState.MODIFIED: ((CayenneDataObject) object).validateForUpdate(vr); break; case PersistenceState.DELETED: ((CayenneDataObject) object).validateForDelete(vr); break; } for (Object o : vr.getFailures()) { ValidationFailure vf = (ValidationFailure) o; String message = vf.getDescription(); if (vf instanceof BeanValidationFailure) { BeanValidationFailure bvf = (BeanValidationFailure) vf; if (bvf.getProperty().equals("modification")) continue; // Défini automatiquement // Champ requis non rempli for (String tail : Arrays.asList(" is required.", " is a required field.", " can not be empty.")) { if (message.endsWith(tail)) { message = propertyMissing(propertyName(object, bvf.getProperty())); break; } } } error(message); } return vr.getFailures().isEmpty(); }
/** Tests how parameter substitution algorithm works on an expression with no parameters. */ @Test public void testInParameter() throws Exception { Expression inExp = ExpressionFactory.exp("k1 in $test"); Expression e1 = ExpressionFactory.exp("k1 in ('a', 'b')"); Expression transformed = inExp.params(Collections.singletonMap("test", new Object[] {"a", "b"})); TstTraversalHandler.compareExps(e1, transformed); // just in case manually check params DataObject o1 = new CayenneDataObject(); o1.writePropertyDirectly("k1", "a"); assertTrue(transformed.match(o1)); DataObject o2 = new CayenneDataObject(); o2.writePropertyDirectly("k1", "x"); assertFalse(transformed.match(o2)); }
@Override protected Object buildValue(ConstRef ref) { Class<?> clazz = ref.clazz; String nom = ref.nom; DataContext dc = DataContext.createDataContext(); String constEntity = "Const" + clazz.getSimpleName(); if (dc.getEntityResolver().getObjEntity(constEntity) == null) { throw new RuntimeException("Pas de constantes de type " + clazz.getName()); } DataObject result = CommonQueries.unique(dc, constEntity, "nom", nom); if (result == null) { result = dc.createAndRegisterNewObject(constEntity); result.writeProperty("nom", nom); result.writeProperty("libelle", nom); dc.commitChanges(); // throw new RuntimeException("Pas de constante nommée " + nom // + " (type:" + clazz.getName() + ")"); } return result.readProperty("valeur"); }
/** * Comme <code>require</code>, mais valide en profondeur les <code>DataObject</code>s. * * @param error true si l'absence est une erreur, false si c'est juste un warning. * @param source L'objet source. * @param property Le nom de la propriété à vérifier. * @return true ssi la propriété est définie et n'a pas d'erreurs. */ protected boolean requireProperty(boolean error, DataObject source, String property) { Object value = source.readProperty(property); String propertyName = propertyName(source, property); if (require(error, value, propertyName)) { if (value instanceof DataObject) { return subValidate(propertyName, (DataObject) value); } else { return true; } } else { return false; } }
@Test public void testInParameter_AsValues() throws Exception { Expression inExp = ExpressionFactory.exp("k1 in ($ap, $bp)"); String e1String = "k1 in (\"a\", \"b\")"; Expression e1 = ExpressionFactory.exp(e1String); Map<String, Object> params = new HashMap<String, Object>(); params.put("ap", "a"); params.put("bp", "b"); Expression transformed = inExp.params(params); TstTraversalHandler.compareExps(e1, transformed); assertEquals(e1String, transformed.toString()); // just in case manually check params DataObject o1 = new CayenneDataObject(); o1.writePropertyDirectly("k1", "a"); assertTrue(transformed.match(o1)); DataObject o2 = new CayenneDataObject(); o2.writePropertyDirectly("k1", "x"); assertFalse(transformed.match(o2)); }
protected boolean isInDb(DataObject object) { return !object.getObjectId().isTemporary(); }
protected ObjEntity entityFor(DataObject object) { return object.getObjectContext().getEntityResolver().lookupObjEntity(object); }
protected boolean checkPeriode(DataObject object, String debut, String fin) { return checkPeriode( object, // (Date) object.readProperty(debut), // (Date) object.readProperty(fin)); }