Example usage for org.apache.wicket.validation ValidationError getKeys

List of usage examples for org.apache.wicket.validation ValidationError getKeys

Introduction

In this page you can find the example usage for org.apache.wicket.validation ValidationError getKeys.

Prototype

public List<String> getKeys() 

Source Link

Document

Gets error keys

Usage

From source file:org.geogig.geoserver.web.repository.RepositoryEditPanel.java

License:Open Source License

public RepositoryEditPanel(final String wicketId, IModel<RepositoryInfo> model, final boolean isNew) {
    super(wicketId, model);

    config = new GeoGigRepositoryInfoFormComponent("repositoryConfig", model, isNew);
    config.setVisible(true);//from www.j  a  v  a 2  s  .  c o m
    add(config);

    add(new IValidator<RepositoryInfo>() {

        private static final long serialVersionUID = 224160688160723504L;

        @Override
        public void validate(IValidatable<RepositoryInfo> validatable) {
            if (isNew) {
                config.processInput();
            }
            ValidationError error = new ValidationError();
            RepositoryInfo repo = validatable.getValue();
            final URI location = repo.getLocation();
            final RepositoryResolver resolver = RepositoryResolver.lookup(location);
            final String scheme = location.getScheme();
            if (isNew && repoNameExists(repo.getRepoName())) {
                error.addKey("errRepositoryNameExists");
            } else if ("file".equals(scheme)) {
                File repoDir = new File(location);
                final File parent = repoDir.getParentFile();
                if (!parent.exists() || !parent.isDirectory()) {
                    error.addKey("errParentDoesntExist");
                }
                if (!parent.canWrite()) {
                    error.addKey("errParentReadOnly");
                }
                if (isNew) {
                    if (repoDir.exists()) {
                        error.addKey("errDirectoryExists");
                    }
                } else if (!isGeogigDirectory(repoDir)) {
                    error.addKey("notAGeogigRepository");
                }
            } else if ("postgresql".equals(scheme)) {
                try {
                    if (isNew) {
                        if (resolver.repoExists(location)) {
                            error.addKey("errRepositoryExists");
                        }
                    } else {
                        // try to connect
                        Repository repository = null;
                        try {
                            repository = resolver.open(location);
                        } finally {
                            if (repository != null) {
                                repository.close();
                            }
                        }

                    }
                } catch (Exception ex) {
                    // likely failed to connect
                    LOGGER.error("Failed to connect to PostgreSQL database", ex);
                    error.addKey("errCannotConnectToDatabase");
                    // find root cause
                    error.setVariable("message", PostgresConnectionErrorHandler.getMessage(ex));
                }
            }
            if (!error.getKeys().isEmpty()) {
                validatable.error(error);
            }
        }
    });
}

From source file:org.geogig.geoserver.web.repository.RepositoryImportPanel.java

License:Open Source License

public RepositoryImportPanel(String id, IModel<RepositoryInfo> model) {
    super(id, model);

    setOutputMarkupId(true);/*from   www.  ja v  a  2  s .  c  om*/

    // build the backing form model
    ImportRepositoryFormModel formModel = new ImportRepositoryFormModel(model);
    // add the dropdown to switch between configurations
    dropdownPanel = new DropDownChoiceParamPanel("configChoicePanel", new DropDownModel(model),
            new ResourceModel("RepositoryImportPanel.repositoryType", "Repository Type"),
            DropDownModel.CONFIG_LIST, true);
    final DropDownChoice<Serializable> dropDownChoice = dropdownPanel.getFormComponent();
    dropDownChoice.add(new AjaxFormComponentUpdatingBehavior("change") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            final String value = dropDownChoice.getModelObject().toString();
            repoDirectoryComponent.setVisible(DropDownModel.DIRECTORY_CONFIG.equals(value));
            pgPanel.setVisible(DropDownModel.PG_CONFIG.equals(value));
            repoNamePanel.setVisible(DropDownModel.PG_CONFIG.equals(value));
            target.add(settingsPanel);
        }
    });
    add(dropdownPanel);

    settingsPanel = new WebMarkupContainer("settingsContainer");
    settingsPanel.setOutputMarkupId(true);
    add(settingsPanel);

    repoNamePanel = new TextParamPanel("repositoryNamePanel", new PropertyModel(formModel, "repoName"),
            new ResourceModel("RepositoryImportPanel.repositoryName", "Repository Name"), true);
    repoNamePanel.setOutputMarkupId(true);
    repoNamePanel.getFormComponent().setOutputMarkupId(true);
    repoNamePanel.setVisible(DropDownModel.PG_CONFIG.equals(dropDownChoice.getModelObject().toString()));
    settingsPanel.add(repoNamePanel);

    pgPanel = new PostgresConfigFormPanel("pgPanel", new PropertyModel<>(formModel, "pgBean"));
    pgPanel.setVisible(DropDownModel.PG_CONFIG.equals(dropDownChoice.getModelObject().toString()));
    settingsPanel.add(pgPanel);

    repoDirectoryComponent = new RepoDirectoryComponent("repoDirectoryPanel", formModel);
    repoDirectoryComponent.setOutputMarkupId(true);
    repoDirectoryComponent
            .setVisible(DropDownModel.DIRECTORY_CONFIG.equals(dropDownChoice.getModelObject().toString()));
    settingsPanel.add(repoDirectoryComponent);

    add(new IValidator<RepositoryInfo>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void validate(IValidatable<RepositoryInfo> validatable) {
            ValidationError error = new ValidationError();
            RepositoryInfo repo = validatable.getValue();
            final URI location = repo.getLocation();
            // look for already configured repos
            List<RepositoryInfo> existingRepos = RepositoryManager.get().getAll();
            for (RepositoryInfo configuredRepo : existingRepos) {
                if (configuredRepo.getLocation().equals(location)) {
                    error.addKey("errRepositoryAlreadyConfigured");
                    // quit looking
                    break;
                }
            }
            if (error.getKeys().isEmpty()) {
                final RepositoryResolver resolver = RepositoryResolver.lookup(location);
                final String scheme = location.getScheme();
                if ("file".equals(scheme)) {
                    File repoDir = new File(location);
                    if (!repoDir.exists() || !repoDir.isDirectory()) {
                        error.addKey("errRepositoryDirectoryDoesntExist");
                    }
                    if (!isGeogigDirectory(repoDir)) {
                        error.addKey("notAGeogigRepository");
                    }
                } else if ("postgresql".equals(scheme)) {
                    try {
                        if (!resolver.repoExists(location)) {
                            error.addKey("errRepositoryDoesntExist");
                        }
                    } catch (Exception ex) {
                        // likely failed to connect
                        LOGGER.error("Failed to connect to PostgreSQL database", ex);
                        error.addKey("errCannotConnectToDatabase");
                        // find root cause
                        error.setVariable("message", PostgresConnectionErrorHandler.getMessage(ex));
                    }
                }
            }
            if (!error.getKeys().isEmpty()) {
                validatable.error(error);
            }
        }
    });
}