Example usage for com.liferay.portal.kernel.json JSONObject getDouble

List of usage examples for com.liferay.portal.kernel.json JSONObject getDouble

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.json JSONObject getDouble.

Prototype

public double getDouble(String key, double defaultValue);

Source Link

Usage

From source file:com.liferay.dynamic.data.mapping.internal.util.DDMIndexerImpl.java

License:Open Source License

@Override
public void addAttributes(Document document, DDMStructure ddmStructure, DDMFormValues ddmFormValues) {

    Set<Locale> locales = ddmFormValues.getAvailableLocales();

    Fields fields = toFields(ddmStructure, ddmFormValues);

    for (Field field : fields) {
        try {/*from  w w  w  . ja va2s .co  m*/
            String indexType = ddmStructure.getFieldProperty(field.getName(), "indexType");

            if (Validator.isNull(indexType)) {
                continue;
            }

            for (Locale locale : locales) {
                String name = encodeName(ddmStructure.getStructureId(), field.getName(), locale, indexType);

                Serializable value = field.getValue(locale);

                if (value instanceof BigDecimal) {
                    document.addNumberSortable(name, (BigDecimal) value);
                } else if (value instanceof BigDecimal[]) {
                    document.addNumberSortable(name, (BigDecimal[]) value);
                } else if (value instanceof Boolean) {
                    document.addKeywordSortable(name, (Boolean) value);
                } else if (value instanceof Boolean[]) {
                    document.addKeywordSortable(name, (Boolean[]) value);
                } else if (value instanceof Date) {
                    document.addDateSortable(name, (Date) value);
                } else if (value instanceof Date[]) {
                    document.addDateSortable(name, (Date[]) value);
                } else if (value instanceof Double) {
                    document.addNumberSortable(name, (Double) value);
                } else if (value instanceof Double[]) {
                    document.addNumberSortable(name, (Double[]) value);
                } else if (value instanceof Integer) {
                    document.addNumberSortable(name, (Integer) value);
                } else if (value instanceof Integer[]) {
                    document.addNumberSortable(name, (Integer[]) value);
                } else if (value instanceof Long) {
                    document.addNumberSortable(name, (Long) value);
                } else if (value instanceof Long[]) {
                    document.addNumberSortable(name, (Long[]) value);
                } else if (value instanceof Float) {
                    document.addNumberSortable(name, (Float) value);
                } else if (value instanceof Float[]) {
                    document.addNumberSortable(name, (Float[]) value);
                } else if (value instanceof Number[]) {
                    Number[] numbers = (Number[]) value;

                    Double[] doubles = new Double[numbers.length];

                    for (int i = 0; i < numbers.length; i++) {
                        doubles[i] = numbers[i].doubleValue();
                    }

                    document.addNumberSortable(name, doubles);
                } else if (value instanceof Object[]) {
                    String[] valuesString = ArrayUtil.toStringArray((Object[]) value);

                    if (indexType.equals("keyword")) {
                        document.addKeywordSortable(name, valuesString);
                    } else {
                        document.addTextSortable(name, valuesString);
                    }
                } else {
                    String valueString = String.valueOf(value);

                    String type = field.getType();

                    if (type.equals(DDMFormFieldType.GEOLOCATION)) {
                        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(valueString);

                        double latitude = jsonObject.getDouble("latitude", 0);
                        double longitude = jsonObject.getDouble("longitude", 0);

                        document.addGeoLocation(name.concat("_geolocation"), latitude, longitude);
                    } else if (type.equals(DDMImpl.TYPE_SELECT)) {
                        JSONArray jsonArray = JSONFactoryUtil.createJSONArray(valueString);

                        String[] stringArray = ArrayUtil.toStringArray(jsonArray);

                        document.addKeywordSortable(name, stringArray);
                    } else {
                        if (type.equals(DDMImpl.TYPE_DDM_TEXT_HTML)) {
                            valueString = HtmlUtil.extractText(valueString);
                        }

                        if (indexType.equals("keyword")) {
                            document.addKeywordSortable(name, valueString);
                        } else {
                            document.addTextSortable(name, valueString);
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e, e);
            }
        }
    }
}

From source file:com.liferay.wiki.web.internal.display.context.util.WikiSocialActivityHelper.java

License:Open Source License

public String getSocialActivityDescription(WikiPage page, SocialActivity socialActivity,
        JSONObject extraDataJSONObject, ResourceBundle resourceBundle) throws PortalException {

    double version = extraDataJSONObject.getDouble("version", 0);

    WikiPage socialActivityWikiPage = null;

    if (version == 0) {
        socialActivityWikiPage = WikiPageLocalServiceUtil.fetchPage(page.getNodeId(), page.getTitle());
    } else {//from   w w w.j  a  v  a 2s .com
        socialActivityWikiPage = WikiPageLocalServiceUtil.fetchPage(page.getNodeId(), page.getTitle(), version);
    }

    User socialActivityUser = UserLocalServiceUtil.fetchUser(socialActivity.getUserId());

    if (socialActivityUser == null) {
        socialActivityUser = UserLocalServiceUtil.getDefaultUser(socialActivity.getCompanyId());
    }

    String userName = HtmlUtil.escape(socialActivityUser.getFullName());

    if (Validator.isNull(userName)) {
        userName = "Liferay";
    }

    int type = socialActivity.getType();

    if ((type == SocialActivityConstants.TYPE_ADD_ATTACHMENT)
            || (type == SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH)
            || (type == SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH)) {

        String label = "x-added-the-attachment-x";

        if (type == SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH) {
            label = "x-removed-the-attachment-x";
        } else if (type == SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH) {

            label = "x-restored-the-attachment-x";
        }

        String title = extraDataJSONObject.getString("fileEntryTitle");

        long fileEntryId = extraDataJSONObject.getLong("fileEntryId");

        String url = getDownloadURL(fileEntryId);

        String titleLink = getLink(title, url);

        return LanguageUtil.format(resourceBundle, label, new Object[] { userName, titleLink }, false);
    } else if (type == SocialActivityConstants.TYPE_ADD_COMMENT) {
        LiferayPortletResponse liferayPortletResponse = _wikiRequestHelper.getLiferayPortletResponse();

        StringBundler sb = new StringBundler(4);

        sb.append(getPageURL(page));
        sb.append("#");
        sb.append(liferayPortletResponse.getNamespace());
        sb.append("wikiCommentsPanel");

        return LanguageUtil.format(resourceBundle, "x-added-a-comment",
                new Object[] { userName, sb.toString() }, false);
    } else if ((type == SocialActivityConstants.TYPE_MOVE_TO_TRASH)
            || (type == SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) || (type == WikiActivityKeys.ADD_PAGE)
            || (type == WikiActivityKeys.UPDATE_PAGE)) {

        String pageURL = null;

        if (version == 0) {
            pageURL = getPageURL(socialActivityWikiPage);
        } else {
            pageURL = getPageURL(socialActivityWikiPage, version);
        }

        if (type == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
            return LanguageUtil.format(resourceBundle, "activity-wiki-page-move-to-trash",
                    new Object[] { StringPool.BLANK, userName, page.getTitle() }, false);
        } else if (type == SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
            String titleLink = getLink(page.getTitle(), pageURL);

            return LanguageUtil.format(resourceBundle, "activity-wiki-page-restore-from-trash",
                    new Object[] { StringPool.BLANK, userName, titleLink }, false);
        } else if (type == WikiActivityKeys.ADD_PAGE) {
            String titleLink = getLink(page.getTitle(), pageURL.toString());

            return LanguageUtil.format(resourceBundle, "x-added-the-page-x",
                    new Object[] { userName, titleLink }, false);
        } else if (type == WikiActivityKeys.UPDATE_PAGE) {
            String title = String.valueOf(version);
            String url = pageURL;

            if ((socialActivityWikiPage != null) && socialActivityWikiPage.isMinorEdit()) {

                title += String.format(" (%s)", LanguageUtil.get(resourceBundle, "minor-edit"));
            }

            String titleURL = getLink(title, url);

            return LanguageUtil.format(resourceBundle, "x-updated-the-page-to-version-x",
                    new Object[] { userName, titleURL }, false);
        } else {
            return StringPool.BLANK;
        }
    } else {
        return StringPool.BLANK;
    }
}