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

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

Introduction

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

Prototype

public int getInt(int index);

Source Link

Usage

From source file:com.idetronic.subur.service.impl.AuthorLocalServiceImpl.java

License:Open Source License

/**
 * Update all author with up to date item count
 * @param companyId//from w  w w .  ja va  2s  . c o m
 * @param groupId
 */
public void updateAllItemCount(long companyId, long groupId) {
    List authorItemCounts = AuthorFinderUtil.getItemCountByGroupCompanyScalar(companyId, groupId);
    String serilizeString = null;
    JSONArray authorJsonArray = null;

    for (Object elemObject : authorItemCounts) {
        serilizeString = JSONFactoryUtil.serialize(elemObject);
        try {
            authorJsonArray = JSONFactoryUtil.createJSONArray(serilizeString);
            long authorId = authorJsonArray.getLong(0);
            int itemCount = authorJsonArray.getInt(1);
            Author author = authorPersistence.fetchByPrimaryKey(authorId);
            author.setItemCount(itemCount);
            authorPersistence.update(author);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:com.liferay.events.global.mobile.portlet.PollsPortlet.java

License:Open Source License

@ProcessAction(name = "generateChoices")
public void generateChoices(ActionRequest request, ActionResponse response) throws Exception {

    long questionId = ParamUtil.getLong(request, "questionId");

    EventPollQuestion q = EventPollQuestionLocalServiceUtil.getEventPollQuestion(questionId);

    if (q.getQuestionType().equals(PollQuestionType.QUESTION_TYPE_SINGLE.getTypeName())
            || q.getQuestionType().equals(PollQuestionType.QUESTION_TYPE_MULTIPLE.getTypeName())) {
        JSONArray choices = JSONFactoryUtil.createJSONArray(q.getChoices());

        // pick an answer to tend toward
        int tend = (int) Math.floor(Math.random() * (double) choices.length());

        for (int i = 0; i < ANSWER_COUNT; i++) {
            JSONArray answers = JSONFactoryUtil.createJSONArray();

            if (q.getQuestionType().equals("multiple")) {
                int count = (int) (Math.random() * (double) choices.length());
                for (int j = 0; j < count; j++) {
                    String ans = choices.getString((int) (Math.random() * (double) choices.length()));
                    if (Math.random() > .7) {
                        ans = choices.getString(tend);
                    }//from  w  w w .  j a va 2 s  .  co m
                    answers.put(ans);
                }
            } else if (q.getQuestionType().equals("single")) {
                String ans = choices.getString((int) (Math.random() * (double) choices.length()));
                if (Math.random() > .7) {
                    ans = choices.getString(tend);
                }
                answers.put(ans);
            } else {
                throw new Exception("Invalid question type: must be single or multiple");
            }

            EventPollAnswerLocalServiceUtil.addAnswer(questionId, "FAKE_GEN", "{}", answers.toString(),
                    new ServiceContext());

            Thread.sleep(500 + (long) (1000.0 * Math.random()));
        }
    } else if (q.getQuestionType().equals(PollQuestionType.QUESTION_TYPE_RATING.getTypeName())) {
        JSONArray choices = JSONFactoryUtil.createJSONArray(q.getChoices());

        int min = choices.getInt(0);
        int max = choices.getInt(1);

        for (int i = 0; i < ANSWER_COUNT; i++) {
            int inc = (int) Math.floor(Math.random() * (double) ((max - min) + 1));
            if (Math.random() > .5) {
                inc = 4;
            }
            int ansNum = min + inc;
            EventPollAnswerLocalServiceUtil.addAnswer(questionId, "FAKE_GEN", "{}", String.valueOf(ansNum),
                    new ServiceContext());
            Thread.sleep(500 + (long) (1000.0 * Math.random()));

        }
    } else if (q.getQuestionType().equals(PollQuestionType.QUESTION_TYPE_RANKING.getTypeName())) {
        JSONArray choices = JSONFactoryUtil.createJSONArray(q.getChoices());
        List<String> ans = new ArrayList<String>();
        List<String> unshuffled = new ArrayList<String>();
        for (int i = 0; i < choices.length(); i++) {
            ans.add(choices.getString(i));
        }
        unshuffled.addAll(ans);

        for (int i = 0; i < ANSWER_COUNT; i++) {
            Collections.shuffle(ans);

            JSONArray res = JSONFactoryUtil.createJSONArray();
            for (String s : (Math.random() > .2) ? ans : unshuffled) {
                res.put(s);
            }
            EventPollAnswerLocalServiceUtil.addAnswer(questionId, "FAKE_GEN", "{}", res.toString(),
                    new ServiceContext());
            Thread.sleep(500 + (long) (1000.0 * Math.random()));

        }

    } else if (q.getQuestionType().equals(PollQuestionType.QUESTION_TYPE_TEXT.getTypeName())) {

        for (int i = 0; i < ANSWER_COUNT; i++) {

            int cnt = (int) (Math.floor((double) 8 * Math.random()));

            List<String> ansStr = new ArrayList<String>();

            for (int j = 0; j < cnt; j++) {
                ansStr.add(DEMO_WORDS[(int) (Math.floor((double) DEMO_WORDS.length * Math.random()))]);
            }

            if (Math.random() > .5) {
                ansStr.add("digital");
            }
            EventPollAnswerLocalServiceUtil.addAnswer(questionId, "FAKE_GEN", "{}",
                    StringUtil.merge(ansStr, " "), new ServiceContext());
            Thread.sleep(500 + (long) (1000.0 * Math.random()));
        }

    }
}