Package org.apache.cayenne.validation

Examples of org.apache.cayenne.validation.ValidationResult


    boolean validateAndCheckNoop() {
        if (getChangesByObjectId().isEmpty()) {
            return true;
        }

        ValidationResult result = new ValidationResult();
        boolean noop = true;

        Iterator it = getChangesByObjectId().entrySet().iterator();
        while (it.hasNext()) {

            Map.Entry entry = (Map.Entry) it.next();

            if (!((ObjectDiff) entry.getValue()).isNoop()) {

                noop = false;

                // accessing objectMap directly to avoid unneeded synchronization.
                DataObject object = (DataObject) objectStore.getNodeNoSync(entry.getKey());
                switch (object.getPersistenceState()) {
                    case PersistenceState.NEW:
                        object.validateForInsert(result);
                        break;
                    case PersistenceState.MODIFIED:
                        object.validateForUpdate(result);
                        break;
                    case PersistenceState.DELETED:
                        object.validateForDelete(result);
                        break;
                }
            }
        }

        if (result.hasFailures()) {
            throw new ValidationException(result);
        }

        return noop;
    }
View Full Code Here


        synchronized (graphManager) {

            if (graphManager.hasChanges()) {

                ValidationResult result = new ValidationResult();
                Iterator<?> it = graphManager.dirtyNodes().iterator();
                while (it.hasNext()) {
                    Persistent p = (Persistent) it.next();
                    if (p instanceof Validating) {
                        switch (p.getPersistenceState()) {
                            case PersistenceState.NEW:
                                ((Validating) p).validateForInsert(result);
                                break;
                            case PersistenceState.MODIFIED:
                                ((Validating) p).validateForUpdate(result);
                                break;
                            case PersistenceState.DELETED:
                                ((Validating) p).validateForDelete(result);
                                break;
                        }
                    }
                }

                if (result.hasFailures()) {
                    throw new ValidationException(result);
                }

                graphManager.graphCommitStarted();
View Full Code Here

            }
        }

        if (objectsToValidate != null) {
            ValidationResult result = new ValidationResult();

            Iterator validationIt = objectsToValidate.iterator();
            while (validationIt.hasNext()) {
                Validating object = (Validating) validationIt.next();
                switch (((Persistent) object).getPersistenceState()) {
                    case PersistenceState.NEW:
                        object.validateForInsert(result);
                        break;
                    case PersistenceState.MODIFIED:
                        object.validateForUpdate(result);
                        break;
                    case PersistenceState.DELETED:
                        object.validateForDelete(result);
                        break;
                }
            }

            if (result.hasFailures()) {
                throw new ValidationException(result);
            }
        }

        return noop;
View Full Code Here

            statement.execute(sql);
            return true;
        }
        catch (SQLException ex) {
            if (this.failures == null) {
                this.failures = new ValidationResult();
            }

            failures.addFailure(new SimpleValidationFailure(sql, ex.getMessage()));
            QueryLogger.logQueryError(ex);
            return false;
View Full Code Here

        conn.close();
        conn = null;
      }

      // Bring the schema up to date by merging the differences
      ValidationResult vr = null;
      if(!schemaValid) {
        // Invalid schema
        Out.error(SchemaValidator.class, "The database requires rebuilding");

        // Generate schema from the mapping file
        DbGenerator generator = new DbGenerator(adapter, dataMap);
        generator.setShouldCreateFKConstraints(adapter.supportsFkConstraints());
        generator.setShouldCreatePKSupport(!adapter.supportsGeneratedKeys());
        generator.setShouldCreateTables(true);
        generator.setShouldDropPKSupport(true);
        generator.setShouldDropTables(true);
        generator.runGenerator(dataSource);

        vr = generator.getFailures();

        Out.info(SchemaValidator.class, "Database rebuild complete");
      } else {
        // Valid schema; check if merge is needed
        Out.debug(SchemaValidator.class, "The database schema is valid; checking if upgrade is needed");

        MergerContext mc = new ExecutingMergerContext(dataMap, dataNode);
        List<MergerToken> allMergeTokens = new DbMerger().createMergeTokens(dataNode, dataMap);

        List<MergerToken> nonAutoMergeTokens = new ArrayList<MergerToken>();
        for(MergerToken mt : allMergeTokens) {
          if(mt instanceof DropTableToDb) {
            // Why would you want to drop AUTO_PK_SUPPORT?
            DropTableToDb drop = (DropTableToDb)mt;
            if(drop.getEntity().getName().equals("AUTO_PK_SUPPORT"))
              continue;
          }

          if((mt instanceof AddRelationshipToDb)
          || (mt instanceof CreateTableToDb)
          || (mt instanceof AddColumnToDb)
          || (mt instanceof SetAllowNullToDb)) {
            // These non-destructive types are allowed to merge in automatically
            Out.info(SchemaValidator.class, mt.toString());
            mt.execute(mc);
          } else {
            // All other types require user approval
            nonAutoMergeTokens.add(mt);
          }
        }

        if(nonAutoMergeTokens.size() == 0) {
          Out.debug(SchemaValidator.class, "No upgrade necessary");
          checkRunWizard(schemaValid);
          return true;
        }

        try {
          String msg = "BNU-Bot must perform the following action(s) to upgrade your database:\n";
          for(MergerToken mt : nonAutoMergeTokens)
            msg += mt.toString() + "\n";
          msg += "\n" +
            "It is strongly recommended to backup your database before continuing!\n" +
            "Okay to continue?";
          if(JOptionPane.showConfirmDialog(
              null,
              msg,
              "Database schema update required",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.QUESTION_MESSAGE)
            != JOptionPane.YES_OPTION) {
            throw new RuntimeException("User rejected schema update");
          }
        } catch(HeadlessException e) {
          // GUI is probably broken
          throw new Exception("A schema merge is required, but was unable to display " +
              "a window to query user if schema upgrade is acceptable.");
        }

        for(MergerToken mt : nonAutoMergeTokens) {
          Out.info(SchemaValidator.class, mt.toString());
          mt.execute(mc);
        }

        vr = mc.getValidationResult();
      }

      if(vr != null) {
        for(ValidationFailure vf : vr.getFailures()) {
          // Don't bother the user with these
          if(vf.getSource().toString().startsWith("DROP TABLE "))
            continue;
          Out.error(SchemaValidator.class,
              vf.getDescription().toString() + "\n" +
View Full Code Here

            statement.execute(sql);
            return true;
        }
        catch (SQLException ex) {
            if (this.failures == null) {
                this.failures = new ValidationResult();
            }

            failures.addFailure(new SimpleValidationFailure(sql, ex.getMessage()));
            QueryLogger.logQueryError(ex);
            return false;
View Full Code Here

            return true;

        } catch (ValidationException e) {
            getDataContext().rollbackChanges();

            ValidationResult validation = e.getValidationResult();

            setError(validation != null
                     ? validation.toString()
                     : "Unknown validation error on save.");
            return false;
        }
    }
View Full Code Here

    public void testValidateForSave1() throws Exception {
        MeaningfulFK testObject = createDataContext().newObject(
                MeaningfulFK.class);

        ValidationResult validation = new ValidationResult();
        testObject.validateForSave(validation);
        assertTrue(
                "Must fail validation due to missing required relationship",
                validation.hasFailures());
        assertEquals(
                "Must fail validation due to missing required relationship",
                1,
                validation.getFailures().size());
    }
View Full Code Here

        RelationshipHelper related = testObject
                .getObjectContext()
                .newObject(RelationshipHelper.class);
        testObject.setToRelationshipHelper(related);

        ValidationResult validation = new ValidationResult();
        testObject.validateForSave(validation);
        assertFalse(validation.hasFailures());
    }
View Full Code Here

        DataContext context = createDataContext();
        Exhibit exhibit = context.newObject(Exhibit.class);
        exhibit.setOpeningDate(new Date());
        exhibit.setClosingDate(new Date());

        ValidationResult result = new ValidationResult();
        exhibit.validateForSave(result);

        assertTrue("Validation of 'toGallery' should've failed.", result.hasFailures());
        assertTrue(result.hasFailures(exhibit));

        List failures = result.getFailures();
        assertEquals(1, failures.size());

        BeanValidationFailure failure = (BeanValidationFailure) failures.get(0);
        assertEquals(Exhibit.TO_GALLERY_PROPERTY, failure.getProperty());

        // fix the problem and see if it goes away
        Gallery gallery = context.newObject(Gallery.class);
        exhibit.setToGallery(gallery);
        result = new ValidationResult();
        exhibit.validateForSave(result);
        assertFalse("No failures expected: " + result, result.hasFailures());
    }
View Full Code Here

TOP

Related Classes of org.apache.cayenne.validation.ValidationResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.