Example usage for org.json.simple JSONArray iterator

List of usage examples for org.json.simple JSONArray iterator

Introduction

In this page you can find the example usage for org.json.simple JSONArray iterator.

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:spnodechecker.client.SPAddressHolder.java

public void loadAddress() {
    try {/*from w w w .  jav a  2s.co  m*/
        FileReader reader = new FileReader(filePath);
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

        JSONArray targets = (JSONArray) jsonObject.get("targets");
        if (targets != null) {
            Iterator i = targets.iterator();
            while (i.hasNext()) {
                JSONObject target = (JSONObject) i.next();
                SPAddress address = new SPAddress((String) target.get("address"), (long) target.get("port"));
                addresses.add(address);
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(SPNodeChecker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ParseException ex) {
        Logger.getLogger(SPNodeChecker.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:utils.JsonDatabase.java

/**
 * Parses the received json in order to output a list of vm configurations
 * @param json A json representing vm configurations with several parameters
 * @return The different vm configurations available
 *///ww  w . j av a2  s . co m
private String[] digestAlternatives(JSONArray json) {
    String[] returnObject = new String[json.size()];
    Iterator<?> it = json.iterator();
    JSONObject object;
    String name, type;
    int i = 0;

    while (it.hasNext()) {
        object = (JSONObject) it.next();
        name = ((JSONObject) object.get("provider")).get("name").toString();
        type = object.get("type").toString();
        returnObject[i] = name + "-" + type;
        ++i;
    }

    return returnObject;
}

From source file:Workers.Worker.java

@Override
public void loadParamsFromJson(JSONObject obj) {
    JSONArray jsonContracts = (JSONArray) obj.get("contractsTable");
    for (Iterator<JSONObject> it = jsonContracts.iterator(); it.hasNext();) {
        JSONObject c = it.next();/*  ww w  .j a  v  a 2  s.c o  m*/
        AbstractContract contract;
        long id = Long.parseLong(c.get("id").toString());
        if (id == 0) {
            contract = new BudgetContract(c);
            this.contractsTable.put(Long.toString(contract.getContractUniqueId()), contract);
        } else {
            contract = new FinanceContract(c);
            this.contractsTable.put(Long.toString(contract.getContractUniqueId()), contract);
        }

    }

    this.currentWorkerId = Long.parseLong((String) obj.get("currentWorkerId"));
    ;
    this.middleName = (String) obj.get("middleName");
    this.surName = (String) obj.get("surName");
    this.name = (String) obj.get("name");
}

From source file:woto.business.PaymentService.java

public JSONArray deletePayment(JSONObject methodParam) throws ClassNotFoundException {
    JSONArray joArray = new JSONArray();
    DBConnection db = new DBConnection(true);
    try {/*from  w w  w .  j  av a 2s  .  com*/

        int pay_id = Integer.parseInt("" + methodParam.get("pay_id"));

        //Get Payment detail for pay id to be deleted
        ParameterList paramList = new ParameterList();
        paramList.add("pay_id", "" + pay_id);
        JSONObject delPayment = (JSONObject) db
                .Execute("payment.logic", "GetPaymentById", paramList.toJSONArray()).iterator().next();
        int sch_id = Integer.parseInt("" + delPayment.get("sch_id"));
        int s_id = Integer.parseInt("" + delPayment.get("s_id"));

        paramList = new ParameterList();
        paramList.add("s_id", "" + s_id);
        paramList.add("sch_id", "" + sch_id);
        JSONArray paymentsFromSchId1 = db.Execute("payment.logic", "GetPaymentAfterSchedule",
                paramList.toJSONArray());

        //Delete the  payment
        paramList = new ParameterList();
        paramList.add("pay_id", "" + pay_id);
        db.Execute("payment.logic", "deletePayment", paramList.toJSONArray());

        //Get the  payment to be reshuffed. Reschuffle all schedule from deleted scheddule
        paramList = new ParameterList();
        paramList.add("s_id", "" + s_id);
        paramList.add("sch_id", "" + sch_id);
        JSONArray paymentsFromSchId = db.Execute("payment.logic", "GetPaymentAfterSchedule",
                paramList.toJSONArray());
        if (paymentsFromSchId.size() > 0) {

            //Delete all schedule from deleted pay id schedule
            db.Execute("payment.logic", "deletePaymentFromSchId", paramList.toJSONArray());

            paramList = new ParameterList();
            paramList.add("s_id", "" + s_id);
            paramList.add("sch_id", "" + sch_id);
            JSONArray paymentsFromSchId5 = db.Execute("payment.logic", "GetPaymentAfterSchedule",
                    paramList.toJSONArray());

            //REpay the amount without the deleted payid
            Iterator<JSONObject> iterator = paymentsFromSchId.iterator();
            while (iterator.hasNext()) {
                JSONObject paymentFromSchId = (JSONObject) iterator.next();

                Float f = Float.parseFloat("" + paymentFromSchId.get("amount"));
                int ramount = f.intValue();

                JSONObject payment = new JSONObject();
                payment.put("mem_id", paymentFromSchId.get("mem_id"));
                payment.put("s_id", paymentFromSchId.get("s_id"));
                payment.put("pay_date", paymentFromSchId.get("pay_date"));
                payment.put("amount", "" + ramount);
                payment.put("collector_id", paymentFromSchId.get("collector_id"));
                payment.put("created_by", paymentFromSchId.get("created_by"));
                joArray.addAll(payAmountAtEnd(db, payment));
            }
        }

        db.commit();
    } catch (Exception e) {
        db.rollback();
        throw e;
    }

    return joArray;
}

From source file:woto.business.PaymentService.java

public JSONArray payAmount(JSONObject methodParam) throws ClassNotFoundException {

    JSONArray joArray = new JSONArray();

    DBConnection db = new DBConnection(true);
    try {//from   w  ww. j a v a2s.  c  om

        int mem_id = Integer.parseInt("" + methodParam.get("mem_id"));
        int s_id = Integer.parseInt("" + methodParam.get("s_id"));
        long amount = Long.parseLong("" + methodParam.get("amount"));
        String pay_date = (String) methodParam.get("pay_date");
        int collector_id = Integer.parseInt("" + methodParam.get("collector_id"));
        int created_by = Integer.parseInt("" + getUserId());

        //Insert a new Bill for student    
        ParameterList paramList = new ParameterList();
        paramList.add("mem_id", "" + mem_id);
        paramList.add("s_id", "" + s_id);
        paramList.add("pay_date", pay_date);
        JSONArray paymentsFromDate = db.Execute("payment.logic", "getMemberPaymentFromDate",
                paramList.toJSONArray());
        if (paymentsFromDate.size() == 0) {
            joArray.addAll(payAmountAtEnd(db, methodParam));
        } else {

            //Get the list of amount pay after the min sche"select     gm_id,    substring(cast(t_date as char), 1, 10) p_date,    sum(amount) amount,    user_id from ( (select "+gmId+" gm_id,'"+ChittyUtil.ToDBDate(payDate)+"' t_date,"+amount+" amount,"+userId+" user_id,-999 trans_id) union (select gm_id,t_date,amount,user_id,p.trans_id from     payment p  inner join chitty.transaction  t ON t.trans_id = p.trans_id  where     gm_id = "+gmId+" ) ) dt group by gm_id , substring(cast(t_date as char), 1, 10) , user_id order by t_date "
            db.Execute("payment.logic", "deletePaymentFromDate", paramList.toJSONArray());

            joArray.addAll(payAmountAtEnd(db, methodParam));
            //Pay the amount payed till now.
            Iterator<JSONObject> iterator = paymentsFromDate.iterator();
            while (iterator.hasNext()) {
                JSONObject paymentFromDate = (JSONObject) iterator.next();

                Float f = Float.parseFloat("" + paymentFromDate.get("amount"));
                int ramount = f.intValue();

                JSONObject payment = new JSONObject();
                payment.put("mem_id", paymentFromDate.get("mem_id"));
                payment.put("s_id", paymentFromDate.get("s_id"));
                payment.put("pay_date", paymentFromDate.get("pay_date"));
                payment.put("amount", "" + ramount);
                payment.put("collector_id", paymentFromDate.get("collector_id"));
                payment.put("created_by", paymentFromDate.get("created_by"));
                joArray.addAll(payAmountAtEnd(db, payment));
            }

        }

        db.commit();
    } catch (Exception e) {
        db.rollback();
        throw e;
    }

    return joArray;
}

From source file:woto.business.PaymentService.java

public JSONArray payAmountAtEnd(DBConnection db, JSONObject methodParam) throws ClassNotFoundException {

    JSONArray joArray = new JSONArray();

    // DBConnection db = new DBConnection(true);

    int mem_id = Integer.parseInt("" + methodParam.get("mem_id"));
    int s_id = Integer.parseInt("" + methodParam.get("s_id"));
    long amount = Long.parseLong("" + methodParam.get("amount"));
    String pay_date = (String) methodParam.get("pay_date");
    int collector_id = Integer.parseInt("" + methodParam.get("collector_id"));
    int created_by = Integer.parseInt("" + getUserId());

    //Insert a new Bill for student    
    ParameterList paramList = new ParameterList();
    paramList.add("mem_id", "" + mem_id);
    paramList.add("s_id", "" + s_id);
    JSONArray freeSchedules = db.Execute("payment.logic", "getFreeSchedule", paramList.toJSONArray());

    if (freeSchedules.size() == 0) {
        throw new BusinessException("Payment Failed. Reason: All payment already completed.");
    } else {/*w w w.  j  a v a 2 s.c  o m*/
        Iterator<JSONObject> iterator = freeSchedules.iterator();
        while (iterator.hasNext()) {
            //If all amount is paid    
            if (amount <= 0) {
                break;
            }
            //Read the free sch_id and balance that is not yet paid
            JSONObject freeSchedule = (JSONObject) iterator.next();
            int sch_id = Integer.parseInt("" + freeSchedule.get("sch_id"));
            Float f = Float.parseFloat("" + freeSchedule.get("balance"));
            int balance = f.intValue();

            //4 50
            //5 100

            long payamount = 0;
            if (balance >= amount) {
                payamount = amount;
            } else {
                payamount = balance;
            }
            amount = amount - payamount;

            paramList = new ParameterList();
            paramList.add("s_id", "" + s_id);
            paramList.add("pay_date", pay_date, "Date");
            paramList.add("amount", "" + payamount);
            paramList.add("sch_id", "" + sch_id);
            paramList.add("collector_id", "" + collector_id);
            paramList.add("created_by", "" + created_by);
            JSONArray payments = db.Execute("payment.logic", "insertPayment", paramList.toJSONArray());
            int pay_id = Integer.parseInt((String) ((JSONObject) payments.iterator().next()).get("key_value"));

            JSONObject payment = new JSONObject();
            payment.put("pay_id", pay_id);

            payment.put("sch_id", "" + sch_id);
            payment.put("pay_date", pay_date);
            payment.put("amount", "" + payamount);
            payment.put("collector_id", "" + collector_id);
            payment.put("created_by", "" + created_by);
            joArray.add(payment);
        }

    }
    //  db.commit();

    return joArray;
}