Example usage for org.apache.commons.collections15 Closure Closure

List of usage examples for org.apache.commons.collections15 Closure Closure

Introduction

In this page you can find the example usage for org.apache.commons.collections15 Closure Closure.

Prototype

Closure

Source Link

Usage

From source file:com.diversityarrays.kdxplore.trials.TrialExplorerPanel.java

/**
 * Called because the user changed the client's URL by logging in to a
 * different database.//  w w  w .j  av  a 2 s  .com
 * 
 * @param why
 * @param client
 * @param onLoadComplete
 */
private void loadDatabaseDataFromKddart(final String prefix, UrlChangeConfirmation confirmation,
        final DALClient client, final Closure<DALClient> onLoadComplete) {
    String why = prefix + confirmation.reason;

    String dbfilename = KdxploreDatabase.LOCAL_DATABASE_URL;
    switch (confirmation) {
    case NEW_DATABASE:
        dbfilename = KdxploreDatabase.LOCAL_DATABASE_URL;
        break;
    case CHANGE_APPROVED:
    case NO_CHANGE_SAME_URL:
        dbfilename = DatabaseDataUtils.normaliseUrlForFilename(client.getBaseUrl());
        break;
    case CHANGE_DENIED:
        throw new IllegalStateException("loadDatabaseDataFromKddart with " + confirmation);
    }

    final File dbdataDirectory = new File(userDataFolder, dbfilename);

    Closure<Throwable> onFailure = new Closure<Throwable>() {
        @Override
        public void execute(Throwable t) {
            messagePrinter.println("Problem loading database from: " + dbdataDirectory.getPath());
            messagePrinter.println(t.getMessage());

            MsgBox.error(TrialExplorerPanel.this, t, why);
            onLoadComplete.execute(null);
        }
    };

    Closure<DALClient> onSuccess = new Closure<DALClient>() {
        @Override
        public void execute(DALClient dalClient) {
            onLoadComplete.execute(dalClient);
        }
    };

    offlineData.loadDatabaseDataFromKddart("Loading Reference Data from KDDart", driverType, dbdataDirectory,
            client, messageLogger, backgroundRunner, onFailure, onSuccess);
}

From source file:com.diversityarrays.kdxplore.trials.TrialExplorerPanel.java

private void collectTrialInfoAfterClientCheck(final String doWhat, Trial trial,
        final Closure<TrialLoadResult> onFinish) {
    DALClient client = null;//from w w w  .  j  a  v a  2s .com
    clientUrlChanger.setIgnoreClientUrlChanged(true);

    client = clientProvider.getDALClient(doWhat);
    if (client == null) {
        clientUrlChanger.setIgnoreClientUrlChanged(false);
        return; // user cancelled
    }

    final UrlChangeConfirmation confirm = confirmChangedClientIsOk(client, doWhat, WILL_CANCEL_PENDING);

    switch (confirm) {
    case CHANGE_DENIED:
        // User said "no"
        clientUrlChanger.setIgnoreClientUrlChanged(false);
        return;

    case CHANGE_APPROVED:
    case NEW_DATABASE:
        // Database changed - so we can't collect the TrialInfo because
        // the trial is from a different database !
        Closure<DALClient> onLoadComplete = new Closure<DALClient>() {
            @Override
            public void execute(DALClient dalClient) {
                clientUrlChanger.setIgnoreClientUrlChanged(false);
                clientUrlChanger.clientUrlChanged();

                // Note: use of WILL_CANCEL_PENDING parameter above
                // has already informed use of the Trial load cancel.
                // MsgBox.info(
                // TrialExplorerPanel.this,
                // "Database was changed so Trial Information retrieval was
                // cancelled",
                // REFRESH_TRIAL_INFO);
            }
        };

        loadDatabaseDataFromKddart(doWhat, confirm, client, onLoadComplete);
        break;

    case NO_CHANGE_SAME_URL:
        // Database not changed.
        clientUrlChanger.setIgnoreClientUrlChanged(false);
        try {
            collectTrialInfo(client, trial, onFinish);
        } catch (KdxploreConfigException | IOException e) {
            MsgBox.error(TrialExplorerPanel.this, e, REFRESH_TRIAL_INFO);
        }
        break;

    default:
        throw new RuntimeException("Unhandled value " + confirm);
    }
}

From source file:com.diversityarrays.kdxplore.trials.TrialExplorerPanel.java

private void collectTrialInfo(final DALClient client, final Trial trial,
        final Closure<TrialLoadResult> onFinish) throws KdxploreConfigException, IOException {

    Closure<Throwable> errorConsumer = new Closure<Throwable>() {
        @Override/*from   ww  w. j  a va  2 s. co m*/
        public void execute(Throwable cause) {

            String errmsg = cause.getMessage();

            Object optionMessage;

            if (cause instanceof DalException) {
                Either<String, DalException> check = TrialLoadResult
                        .checkPermissionDenied((DalException) cause);
                if (check.isRight()) {
                    DalException dalException = check.right();
                    if (dalException instanceof DalResponseHttpException) {
                        DalResponseHttpException he = (DalResponseHttpException) dalException;

                        for (DalHeader h : he.responseInfo.headers) {
                            if ("content-type".equalsIgnoreCase(h.getName())) {
                                String contentType = h.getValue();
                                if (contentType.startsWith("text/html")) {
                                    String html = he.responseInfo.serverResponse;
                                    if (html != null && !html.isEmpty()) {
                                        String plainText = net.pearcan.util.Util.htmlToPlainText(html);
                                        if (plainText != null) {
                                            errmsg = plainText;
                                        }

                                        if (!html.startsWith("<HTML>") && !html.startsWith("<html>")) {
                                            html = "<HTML>" + html;
                                        }
                                        optionMessage = new JLabel(html);
                                    }
                                }
                                break;
                            }
                        }
                    }
                } else {
                    optionMessage = check.left();
                }
            } else {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                cause.printStackTrace(pw);
                pw.close();
                optionMessage = sw.toString();
            }

            trialOverviewPanel.setPayloadErrorMessage(trial, errmsg);
            messagePrinter.println(errmsg);

            MsgBox.error(TrialExplorerPanel.this, cause,
                    "Unable to get data for '" + trial.getTrialName() + "'");
        }
    };

    boolean wantSpecimens = getSpecimensRequired();

    Date today = new Date();
    offlineData.collectTrialInfo(client, backgroundRunner, dartSchemaHelper, wantSpecimens, errorConsumer,
            messageLogger, trial, today, onFinish);
}

From source file:org.drugis.addis.gui.WelcomeDialog.java

private void initComps() {

    final ButtonGroup examples = new ButtonGroup();
    examples.add(new JRadioButton(Main.Examples.DEPRESSION.name, true));
    examples.add(new JRadioButton(Main.Examples.HYPERTENSION.name));

    final AbstractAction exampleAction = new AbstractAction() {
        public void actionPerformed(ActionEvent arg0) {
            d_main.loadExampleDomain(Main.Examples.findFileName(getSelection(examples).getText()));
            closeWelcome();/*from   ww  w  .ja  va  2  s. c  om*/
        }
    };

    final AbstractAction loadAction = new AbstractAction() {
        public void actionPerformed(ActionEvent arg0) {
            if (d_main.fileLoadActions() == JFileChooser.APPROVE_OPTION) {
                closeWelcome();
            }
        }
    };

    final AbstractAction newAction = new AbstractAction() {
        public void actionPerformed(ActionEvent arg0) {
            d_main.newFileActions();
            closeWelcome();
        }
    };

    FormLayout layout = new FormLayout("left:pref, " + SPACING + "px, left:pref",
            "p, 3dlu, p, " + SPACING + "px, p, " + SPACING + "px, p, 3dlu, p");
    PanelBuilder builder = new PanelBuilder(layout);
    final CellConstraints cc = new CellConstraints();

    builder.add(createImageLabel(FileNames.IMAGE_HEADER), cc.xyw(1, 1, 3));
    builder.add(createButton("Load example", FileNames.ICON_TIP, exampleAction), cc.xy(1, 3));

    final PanelBuilder radios = new PanelBuilder(new FormLayout("p, fill:pref:grow, right:pref", "p, 3dlu, p"));

    final ArrayList<AbstractButton> buttons = Collections.list(examples.getElements());
    forAllDo(buttons, new Closure<AbstractButton>() {
        public void execute(final AbstractButton exampleOption) {
            int row = buttons.indexOf(exampleOption) == 0 ? 1 : buttons.indexOf(exampleOption) + 2;
            exampleOption.setOpaque(false);
            radios.add(exampleOption, cc.xy(1, row));
            radios.add(createHelpButton(exampleOption), cc.xy(3, row));
        }

        private JButton createHelpButton(final AbstractButton exampleOption) {
            JButton help = GUIFactory.createIconButton(org.drugis.mtc.gui.FileNames.ICON_ABOUT,
                    "Information about this example");
            removeBackground(help);

            help.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Examples example = Examples.findByName(exampleOption.getText());
                    String helpText = s_help.getHelpText(example.name().toLowerCase());
                    showExampleInfo(helpText);
                }
            });
            return help;
        }
    });

    JPanel radiosPanel = radios.getPanel();
    setBorder(radiosPanel);
    builder.add(radiosPanel, cc.xy(3, 3));

    builder.add(createButton("Open file", FileNames.ICON_OPENFILE, loadAction), cc.xy(1, 5));
    JTextPane load = createLabel("Load an existing ADDIS data file stored on your computer.");
    builder.add(load, cc.xy(3, 5));

    builder.add(createButton("New dataset", FileNames.ICON_FILE_NEW, newAction), cc.xy(1, 7));
    builder.add(createLabel("Start with an empty file to build up your own data and analyses."), cc.xy(3, 7));

    builder.add(createImageLabel(FileNames.IMAGE_FOOTER), cc.xyw(1, 9, 3));

    setContentPane(builder.getPanel());
}

From source file:org.springframework.jdbc.repo.impl.RawPropertiesIdentifiedEntityRepoImpl.java

@Override
public String createEntity(E entity) {
    Assert.notNull(entity, "No entity instance");

    final String prevId = entity.getId();
    final String assignedId = idGenerator.build();
    if (!StringUtils.isEmpty(prevId)) {
        logger.warn("createEntity(" + prevId + ") overwrite entity ID: " + assignedId);
    }//from  w ww.  ja  va2 s .c  o m
    entity.setId(assignedId);

    Map<String, ?> props = repo.setProperties(entity, entity2propsTransfomer);
    logger.info("createEntity(" + getEntityClass() + ")[" + prevId + "] created");
    if (logger.isTraceEnabled()) {
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace("createEntity(" + assignedId + ") " + e.getKey() + ": " + e.getValue());
            }
        });
    }

    return assignedId;
}

From source file:org.springframework.jdbc.repo.impl.RawPropertiesIdentifiedEntityRepoImpl.java

@Override
public void updateEntity(final E entity) {
    Assert.notNull(entity, "No entity provided");
    if (!entityExists(entity)) {
        throw new UnsupportedOperationException("updateEntity(" + entity + ") entity not persisted");
    }/*from   www.  ja v a2s .co m*/

    if (logger.isTraceEnabled()) {
        Map<String, ?> props = repo.getProperties(entity);
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace(
                        "updateEntity(" + entity.getId() + ")[" + e.getKey() + "]-BEFORE: " + e.getValue());
            }
        });
    }

    Map<String, ?> props = repo.setProperties(entity, entity2propsTransfomer);
    if (logger.isTraceEnabled()) {
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace("updateEntity(" + entity.getId() + ")[" + e.getKey() + "]-AFTER: " + e.getValue());
            }
        });
    }
}

From source file:org.springframework.jdbc.repo.impl.RawPropertiesPolymorphicIdentifiedEntityRepoImpl.java

@Override
public String createEntity(E entity) {
    Assert.notNull(entity, "No entity");

    final IdentifiedEntityIdGenerator<?> entityIdGenerator = getEntityIdGenerator(entity);
    if (entityIdGenerator == null) {
        throw new IllegalStateException(
                "createEntity(" + entity.getClass().getSimpleName() + ") no identity generator");
    }//from w ww . j  av a2s . c  o m

    final String prevId = entity.getId();
    final String assignedId = entityIdGenerator.build();
    if (!StringUtils.isEmpty(prevId)) {
        logger.warn("createEntity(" + prevId + ") overwrite entity ID: " + assignedId);
    }
    entity.setId(assignedId);

    Transformer<E, ? extends Map<String, ?>> transformer = getEntityTransformer(entity);
    if (transformer == null) {
        throw new IllegalStateException("createEntity(" + entity + ") no properties transformer");
    }

    Map<String, ?> props = repo.setProperties(entity, transformer);
    if (logger.isTraceEnabled()) {
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace("createEntity(" + assignedId + ") " + e.getKey() + ": " + e.getValue());
            }
        });
    }

    return assignedId;
}

From source file:org.springframework.jdbc.repo.impl.RawPropertiesPolymorphicIdentifiedEntityRepoImpl.java

@Override
public void updateEntity(final E entity) {
    Assert.notNull(entity, "No entity");
    if (!entityExists(entity)) {
        throw new UnsupportedOperationException("updateEntity(" + entity + ") entity not persisted");
    }/* w  w  w.  ja v  a 2s.  c om*/

    if (logger.isTraceEnabled()) {
        Map<String, ?> props = repo.getProperties(entity);
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace(
                        "updateEntity(" + entity.getId() + ")[" + e.getKey() + "]-BEFORE: " + e.getValue());
            }
        });
    }

    Transformer<E, ? extends Map<String, ?>> transformer = getEntityTransformer(entity);
    if (transformer == null) {
        throw new IllegalStateException("updateEntity(" + entity + ") no properties transformer");
    }

    Map<String, ?> props = repo.setProperties(entity, transformer);
    if (logger.isTraceEnabled()) {
        ExtendedMapUtils.forAllEntriesDo(props, new Closure<Map.Entry<String, ?>>() {
            @Override
            @SuppressWarnings("synthetic-access")
            public void execute(Entry<String, ?> e) {
                logger.trace("updateEntity(" + entity.getId() + ")[" + e.getKey() + "]-AFTER: " + e.getValue());
            }
        });
    }
}