Example usage for com.liferay.portal.kernel.util ParamUtil getShort

List of usage examples for com.liferay.portal.kernel.util ParamUtil getShort

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util ParamUtil getShort.

Prototype

public static short getShort(ServiceContext serviceContext, String param, short defaultValue) 

Source Link

Document

Returns the service context parameter value as a short.

Usage

From source file:com.commsen.liferay.portlet.customglobalmarkup.CustomGlobalMarkupConfigurationPortlet.java

License:Open Source License

@ProcessAction(name = "save")
public void saveMarkups(ActionRequest request, ActionResponse response)
        throws SystemException, PortalException {

    List<String> paramNames = Collections.list(request.getParameterNames());

    long max_markup_length = 2000;
    Map<String, String> markup_hints = ModelHintsUtil.getHints(Markup.class.getName(), "markup");
    if (markup_hints.containsKey("max-length")) {
        try {//  w w  w. ja  va2s.  co m
            max_markup_length = Long.parseLong(markup_hints.get("max-length"));
        } catch (NumberFormatException e) {
            _log.warn("Failed to parse max-length!", e);
        }
    }

    for (String key : paramNames) {
        if (key.startsWith("markup_")) {
            long id = Long.parseLong(key.substring(7));
            Markup markup = MarkupLocalServiceUtil.getMarkup(id);
            if (markup != null) {
                if (ParamUtil.getBoolean(request, "delete_" + id)) {
                    MarkupLocalServiceUtil.deleteMarkup(markup);
                } else {
                    boolean changed = false;
                    boolean error = false;
                    // process text
                    String markupText = ParamUtil.getString(request, key, "");
                    if (markupText.length() > max_markup_length) {
                        error = true;
                        SessionErrors.add(request, "custom-global-markup-error-too-long-" + id);
                    }
                    if (!markup.getMarkup().equals(markupText)) {
                        changed = true;
                        markup.setMarkup(markupText);
                    }
                    // process status
                    boolean markupStatus = ParamUtil.getBoolean(request, "active_" + id);
                    if (markup.getActive() != markupStatus) {
                        changed = true;
                        markup.setActive(markupStatus);
                    }
                    // process location
                    short location = ParamUtil.getShort(request, "location_" + id, (short) -1);
                    if (CustomGlobalMarkupLocation.isValid(location) && markup.getLocation() != location) {
                        changed = true;
                        markup.setLocation(location);
                    }

                    // finally if there are any changes update database
                    if (error) {
                        rememberMarkupInSession(request, markup);
                    } else if (changed) {
                        SessionMessages.add(request, "custom-global-markup-save-ok-" + id);
                        MarkupLocalServiceUtil.updateMarkup(markup);
                    }
                }
            } else {
                _log.warn("Markup with id " + id + " not found!");
            }
        }
    }

    redirect(request, response);

}