Example usage for org.apache.wicket.util.time Duration toString

List of usage examples for org.apache.wicket.util.time Duration toString

Introduction

In this page you can find the example usage for org.apache.wicket.util.time Duration toString.

Prototype

public String toString(final Locale locale) 

Source Link

Document

Retrieves the String representation of this Duration in days, hours, minutes, seconds or milliseconds, as appropriate.

Usage

From source file:org.hippoecm.frontend.plugins.cms.admin.plugins.ChangePasswordShortcutPlugin.java

License:Apache License

public ChangePasswordShortcutPlugin(final IPluginContext context, final IPluginConfig config) {
    super(context, config);

    notificationPeriod = (long) getPluginConfig().getDouble("passwordexpirationnotificationdays", 3)
            * ONE_DAY_MS;/*  w  ww . j a va2s . c om*/

    final UserSession session = UserSession.get();
    // password max age is defined on the /hippo:configuration/hippo:security node
    try {
        Node securityNode = session.getRootNode().getNode(SECURITY_PATH);
        if (securityNode.hasProperty(HippoNodeType.HIPPO_PASSWORDMAXAGEDAYS)) {
            passwordMaxAge = (long) (securityNode.getProperty(HippoNodeType.HIPPO_PASSWORDMAXAGEDAYS)
                    .getDouble() * ONE_DAY_MS);
        }
    } catch (RepositoryException e) {
        log.error("Failed to determine configured password maximum age", e);
    }

    username = session.getJcrSession().getUserID();
    user = new User(username);

    AjaxLink link = new AjaxLink("link") {
        @Override
        public void onClick(AjaxRequestTarget target) {
            IDialogService dialogService = getDialogService();
            if (user != null && canChangePassword()) {
                currentPassword = newPassword = checkPassword = "";
                dialogService.show(new ChangePasswordDialog());
            } else {
                dialogService.show(new CannotChangePasswordDialog());
            }
        }
    };
    add(link);

    final IModel<String> labelModel = new AbstractReadOnlyModel<String>() {
        @Override
        public String getObject() {
            if (user.isPasswordExpired()) {
                return translate("password-is-expired");
            } else if (isPasswordAboutToExpire(user)) {
                final long expirationTime = user.getPasswordExpirationTime();
                final Duration expirationDuration = Duration
                        .valueOf(expirationTime - System.currentTimeMillis());
                String expiration = expirationDuration.toString(getLocale());

                final Matcher matcher = EXPIRATION_PATTERN.matcher(expiration);
                if (matcher.matches()) {
                    final String expirationMatch = matcher.group(1);
                    expiration = expiration.replace(expirationMatch, translate(expirationMatch));
                }

                final StringResourceModel model = new StringResourceModel("password-about-to-expire",
                        ChangePasswordShortcutPlugin.this, null, null, expiration);
                return model.getObject();
            }
            return StringUtils.EMPTY;
        }
    };
    label = new Label("label", labelModel) {
        @Override
        public boolean isVisible() {
            return StringUtils.isNotEmpty(labelModel.getObject());
        }
    };
    label.setOutputMarkupId(true);
    add(label);

    final HippoIcon icon = HippoIcon.fromSprite("change-password-icon", Icon.PENCIL_SQUARE);
    link.add(icon);
}