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

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

Introduction

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

Prototype

public JSONArray getJSONArray(String key);

Source Link

Usage

From source file:at.graz.meduni.bibbox.liferay.portlet.model.impl.ApplicationInstanceImpl.java

License:Open Source License

public JSONArray getApplicationTags() {
    if (tags_ == null) {
        JSONObject application = getApplicationfile();
        tags_ = application.getJSONArray("tags");
    }/* w w w  .j ava2 s  .  c  om*/
    return tags_;
}

From source file:com.fingence.slayer.service.impl.MyResultServiceImpl.java

License:Open Source License

public JSONArray getBondsQuality(String portfolioIds) {

    String[] categories = { "Investment", "Non Investment", "Others" };

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    // initialization of JSONArray with default values
    for (int i = 0; i < categories.length; i++) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
        jsonObject.put("category", categories[i]);
        jsonObject.put("children", JSONFactoryUtil.createJSONArray());
        jsonArray.put(jsonObject);/*from ww w.  j  a  v a 2 s . c o m*/
    }

    Connection conn = null;
    try {
        conn = DataAccess.getConnection();

        String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]",
                "[$FING_BOND_WHERE_CLAUSE$]" };
        String[] replacements = { portfolioIds, ",f.*", ",fing_Bond f", "and a.assetId = f.assetId" };

        String sql = StringUtil.replace(CustomSQLUtil.get(QUERY), tokens, replacements);

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        double totalMarketValue = getTotalMarketValue(portfolioIds);
        double totalValueOfBonds = 0.0;

        while (rs.next()) {
            double currentMarketValue = rs.getDouble("currentMarketValue");
            totalValueOfBonds += currentMarketValue;

            String spRating = rs.getString("rtg_sp");
            String moodyRating = rs.getString("rtg_moody");

            Rating rating = null;
            try {
                rating = ratingPersistence.fetchBySP_Moody(spRating, moodyRating);
            } catch (SystemException e) {
                e.printStackTrace();
            }

            // identify the object 
            int index = 2;
            String description = "No Rating Available";
            if (Validator.isNotNull(rating)) {
                String category = rating.getCategory();
                description = rating.getDescription();

                for (int i = 0; i < categories.length; i++) {
                    if (categories[i].equalsIgnoreCase(category)) {
                        index = i;
                    }
                }
            }

            JSONArray children = jsonArray.getJSONObject(index).getJSONArray("children");

            // identify the child within the parent
            JSONObject child = null;

            if (children.length() == 0) {
                child = JSONFactoryUtil.createJSONObject();
                child.put("bucket", description);
                child.put("market_value", 0.0);
                child.put("bond_holdings_percent", 0.0);
                child.put("total_holdings_percent", 0.0);
                children.put(child);
            }

            for (int i = 0; i < children.length(); i++) {
                child = children.getJSONObject(i);
                if (child.getString("bucket").equalsIgnoreCase(description)) {
                    child.put("market_value", child.getDouble("market_value") + currentMarketValue);
                    child.put("total_holdings_percent", child.getDouble("total_holdings_percent")
                            + currentMarketValue * 100 / totalMarketValue);
                }
            }
        }

        rs.close();
        stmt.close();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject parent = jsonArray.getJSONObject(i);
            JSONArray children = parent.getJSONArray("children");

            for (int j = 0; j < children.length(); j++) {
                JSONObject child = children.getJSONObject(j);
                child.put("bond_holdings_percent", child.getDouble("market_value") * 100 / totalValueOfBonds);
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DataAccess.cleanUp(conn);
    }

    return jsonArray;
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testAddingFileEntryWithHDMediaQueries() throws Exception {
    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 100, 100);
        _addTestVariant("medium", "uuid2", 300, 300);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(3, sourcesJSONArray.length());

        _assertHDSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0", "uuid1",
                fileEntry.getTitle());/*from  www .  j  av a 2 s.c  o m*/
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());
        _assertSrcSource(sourcesJSONArray.getJSONObject(2), fileEntry.getFileEntryId(), "uuid2",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 100, 50);
        _assertAttibutes(sourcesJSONArray.getJSONObject(2), 300, 100);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testAddingFileEntryWithImageCreatesMedia() throws Exception {
    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("big", "uuid1", 400, 280);
        _addTestVariant("medium", "uuid2", 300, 200);
        _addTestVariant("extra", "uuid3", 500, 330);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(4, sourcesJSONArray.length());

        _assertSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0",
                fileEntry.getTitle());/*from   www .  ja v  a2 s  . c om*/
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid2",
                fileEntry.getTitle());
        _assertSrcSource(sourcesJSONArray.getJSONObject(2), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());
        _assertSrcSource(sourcesJSONArray.getJSONObject(3), fileEntry.getFileEntryId(), "uuid3",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 200, 50);
        _assertAttibutes(sourcesJSONArray.getJSONObject(2), 280, 200);
        _assertAttibutes(sourcesJSONArray.getJSONObject(3), 330, 280);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryAppliesWhenHeightHas1PXLessThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 99, 100);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertHDSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0", "uuid1",
                fileEntry.getTitle());// w  ww.  j  a  va  2  s .co  m
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 100, 50);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryAppliesWhenHeightHas1PXMoreThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 101, 100);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertHDSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0", "uuid1",
                fileEntry.getTitle());/*from  w  w w  .j  a va2s . c o  m*/
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 100, 50);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryAppliesWhenWidthHas1PXLessThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 100, 99);

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertHDSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0", "uuid1",
                fileEntry.getTitle());//from w w w  .j  a v  a2s.c om
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 99, 50);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryAppliesWhenWidthHas1PXMoreThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 100, 101);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertHDSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0", "uuid1",
                fileEntry.getTitle());/* w w  w .ja  va2s.c o m*/
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 101, 50);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryNotAppliesWhenHeightHas2PXLessThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 98, 200);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0",
                fileEntry.getTitle());/*from w ww.j  a v  a  2s .  com*/
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 200, 50);
    }
}

From source file:com.liferay.adaptive.media.image.item.selector.internal.resolver.test.FileEntryAdaptiveMediaImageURLItemSelectorReturnTypeResolverTest.java

License:Open Source License

@Test
public void testHDMediaQueryNotAppliesWhenHeightHas2PXMoreThanExpected() throws Exception {

    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        _addTestVariant("small", "uuid0", 50, 50);
        _addTestVariant("small.hd", "uuid1", 102, 200);

        String value = _resolver.getValue(fileEntry, null);

        JSONObject jsonObject = JSONFactoryUtil.createJSONObject(value);

        String defaultSource = jsonObject.getString("defaultSource");

        Assert.assertEquals(DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK,
                false, false), defaultSource);

        JSONArray sourcesJSONArray = jsonObject.getJSONArray("sources");

        Assert.assertEquals(2, sourcesJSONArray.length());

        _assertSrcSource(sourcesJSONArray.getJSONObject(0), fileEntry.getFileEntryId(), "uuid0",
                fileEntry.getTitle());//  w w  w .  ja  va 2 s .c o  m
        _assertSrcSource(sourcesJSONArray.getJSONObject(1), fileEntry.getFileEntryId(), "uuid1",
                fileEntry.getTitle());

        _assertAttibutes(sourcesJSONArray.getJSONObject(0), 50, 0);
        _assertAttibutes(sourcesJSONArray.getJSONObject(1), 200, 50);
    }
}