Example usage for com.liferay.portal.kernel.json JSONArray length

List of usage examples for com.liferay.portal.kernel.json JSONArray length

Introduction

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

Prototype

public int length();

Source Link

Usage

From source file:at.graz.meduni.liferay.portlet.bibbox.service.service.impl.LogapiServiceImpl.java

License:Open Source License

/**
 * //from  www.  j  a v  a 2 s  .co  m
 * @return
 */
@AccessControlled(guestAccessEnabled = true)
@JSONWebService(value = "regbbs", method = "POST")
public JSONArray regbbs(String data) {
    JSONArray jsonarray = JSONFactoryUtil.createJSONArray();
    try {
        JSONArray array = JSONFactoryUtil.createJSONArray(data);
        for (int arrayindex = 0; arrayindex < array.length(); arrayindex++) {
            jsonarray.put(regbb(array.getLong(arrayindex)));
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jsonarray;
}

From source file:br.com.petrobras.ddm.type.region.internal.RegionDDMFormFieldValueRenderer.java

License:Open Source License

@Override
public String render(DDMFormFieldValue ddmFormFieldValue, Locale locale) {
    System.out.println(this);
    JSONArray optionsValuesJSONArray = regionDDMFormFieldValueAccessor.getValue(ddmFormFieldValue, locale);

    DDMFormFieldOptions ddmFormFieldOptions = getDDMFormFieldOptions(ddmFormFieldValue);

    if (optionsValuesJSONArray.length() == 0) {
        return StringPool.BLANK;
    }//  ww  w  . j ava 2s . c om

    StringBundler sb = new StringBundler(optionsValuesJSONArray.length() * 2 - 1);

    for (int i = 0; i < optionsValuesJSONArray.length(); i++) {
        String optionValue = optionsValuesJSONArray.getString(i);

        if (isManualDataSourceType(ddmFormFieldValue.getDDMFormField())) {
            LocalizedValue optionLabel = ddmFormFieldOptions.getOptionLabels(optionValue);

            sb.append(optionLabel.getString(locale));
        } else {
            sb.append(optionValue);
        }

        sb.append(StringPool.COMMA_AND_SPACE);
    }

    sb.setIndex(sb.index() - 1);

    return sb.toString();
}

From source file:com.beorn.paymentapi.model.ApiPaymentMethod.java

License:Open Source License

public static List<ApiPaymentMethod> fromJSON(JSONArray jsonArray) {
    List<ApiPaymentMethod> results = new ArrayList<ApiPaymentMethod>(jsonArray.length());

    for (int i = 0; i < jsonArray.length(); ++i) {
        results.add(fromJSON(jsonArray.getJSONObject(i)));
    }//  w ww.j a va 2  s.  co m
    return results;
}

From source file:com.beorn.paymentapi.model.ApiTransaction.java

License:Open Source License

public static List<ApiTransaction> fromJSON(JSONArray jsonArray) {
    List<ApiTransaction> results = new ArrayList<ApiTransaction>(jsonArray.length());

    for (int i = 0; i < jsonArray.length(); ++i) {
        results.add(fromJSON(jsonArray.getJSONObject(i)));
    }/*from w  w w . j av  a 2  s  . c o m*/
    return results;
}

From source file:com.evozon.evoportal.my_account.wrapper.UserExpandoWrapper.java

private JSONObject getCurrentStatus() {
    JSONObject currentStatus = null;/*  w  w w  .  j a v a 2s . co m*/
    try {
        JSONArray statusArray = getStatusArrayLog();
        if (statusArray != null) {

            int statusCount = statusArray.length();
            if (statusCount > 0) {
                currentStatus = statusArray.getJSONObject(statusCount - 1);
            }
        }
    } catch (JSONException e) {
        log.error(e);
    }

    return currentStatus;
}

From source file:com.evozon.evoportal.my_account.wrapper.UserExpandoWrapper.java

public void removeCurrentStatusLog() {
    try {/*from w ww.  jav a2  s  .  co m*/
        JSONArray statusArray = getStatusArrayLog();
        JSONArray newStatusArray = JSONFactoryUtil.createJSONArray();

        int statusCount = statusArray.length();
        if (statusCount > 1) {
            for (int i = 0; i < statusCount - 1; i++) {
                JSONObject oldStatus = statusArray.getJSONObject(i);
                newStatusArray.put(oldStatus);
            }
        }
        user.getExpandoBridge().setAttribute(MyAccountConstants.EVOZON_USER_STATUS, newStatusArray.toString());
    } catch (Exception e) {
        log.error("Error getting current status for user: " + user.getFullName() + " in removeCurrentStatus()",
                e);
    }
}

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

License:Open Source License

public JSONArray getCollateralBreakdown(String portfolioIds) {

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    Connection conn = null;//from   w  w  w  . j  a va 2s .  c  o m
    try {
        conn = DataAccess.getConnection();

        String[] tokens = { "[$PORTFOLIO_IDS$]", "[$FING_BOND_COLUMNS$]", "[$FING_BOND_TABLE$]",
                "[$FING_BOND_WHERE_CLAUSE$]" };
        String[] replacements = { portfolioIds, ",f.*, DATEDIFF(f.maturity_dt,now()) AS maturing_after",
                ",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()) {
            String collatTyp = rs.getString("collat_typ");
            double currentMarketValue = rs.getDouble("currentMarketValue");
            totalValueOfBonds += currentMarketValue;

            JSONObject jsonObj = null;
            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject _jsonObj = jsonArray.getJSONObject(i);
                    if (_jsonObj.getString("bucket").equalsIgnoreCase(collatTyp)) {
                        jsonObj = _jsonObj;
                        break;
                    }
                }
            }

            if (Validator.isNull(jsonObj)) {
                jsonObj = JSONFactoryUtil.createJSONObject();
                jsonObj.put("bucket", collatTyp);
                jsonObj.put("market_value", 0.0);
                jsonObj.put("bond_holdings_percent", 0.0);
                jsonObj.put("total_holdings_percent", 0.0);
                jsonArray.put(jsonObj);
            }

            jsonObj.put("market_value", jsonObj.getDouble("market_value") + currentMarketValue);
            jsonObj.put("total_holdings_percent",
                    jsonObj.getDouble("total_holdings_percent") + currentMarketValue * 100 / totalMarketValue);
        }

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

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            jsonObj.put("bond_holdings_percent", jsonObj.getDouble("market_value") * 100 / totalValueOfBonds);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DataAccess.cleanUp(conn);
    }

    return jsonArray;
}

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

License:Open Source License

public JSONArray getBondsMaturing(String portfolioIds) {

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    // initialization of JSONArray with default values
    for (int i = 0; i < bucketNames.length; i++) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
        jsonObject.put("bucket", bucketNames[i]);
        jsonObject.put("market_value", 0.0);
        jsonObject.put("bond_holdings_percent", 0.0);
        jsonObject.put("total_holdings_percent", 0.0);
        jsonArray.put(jsonObject);/*  w w  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.*, round(mty_years_tdy * 360) AS maturing_after",
                ",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()) {
            int maturingAfter = rs.getInt("maturing_after");
            double currentMarketValue = rs.getDouble("currentMarketValue");
            totalValueOfBonds += currentMarketValue;

            int index = 0;
            if (maturingAfter > 0 && maturingAfter <= 210) {
                index = 1;
            } else if (maturingAfter > 210 && maturingAfter <= 365) {
                index = 2;
            } else if (maturingAfter > 365 && maturingAfter <= 730) {
                index = 3;
            } else if (maturingAfter > 730 && maturingAfter <= 1825) {
                index = 4;
            } else if (maturingAfter > 1825 && maturingAfter <= 3650) {
                index = 5;
            } else if (maturingAfter > 3650) {
                index = 6;
            }

            JSONObject jsonObj = jsonArray.getJSONObject(index);

            jsonObj.put("market_value", jsonObj.getDouble("market_value") + currentMarketValue);
            jsonObj.put("total_holdings_percent",
                    jsonObj.getDouble("total_holdings_percent") + currentMarketValue * 100 / totalMarketValue);
        }

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

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObj = jsonArray.getJSONObject(i);
            jsonObj.put("bond_holdings_percent", jsonObj.getDouble("market_value") * 100 / totalValueOfBonds);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DataAccess.cleanUp(conn);
    }

    return jsonArray;
}

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 www  .j  av  a 2s . 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.idetronic.subur.service.impl.AuthorLocalServiceImpl.java

License:Open Source License

public long[] createAuthor(JSONArray authors, ServiceContext serviceContext) throws SystemException {
    long[] authorIds = new long[authors.length()];
    for (int i = 0; i < authors.length(); i++) {
        JSONObject jsonObj = authors.getJSONObject(i);
        String firstName = jsonObj.getString("firstName");
        String lastName = jsonObj.getString("lastName");
        Author author = newAuthor(firstName, null, lastName, null, null, null, serviceContext);

        authorIds[i] = author.getAuthorId();

    }/*from   w ww .  j a  v  a  2  s.  co m*/
    return authorIds;
}