Example usage for org.json JSONObject accumulate

List of usage examples for org.json JSONObject accumulate

Introduction

In this page you can find the example usage for org.json JSONObject accumulate.

Prototype

public JSONObject accumulate(String key, Object value) throws JSONException 

Source Link

Document

Accumulate values under a key.

Usage

From source file:com.openerp.addons.note.AddFollowerFragment.java

public Boolean getPartnersFromServer() {

    boolean loaded = true;
    if (!flag) {//w ww  .jav a  2 s .c  om
        res_partners = new Res_PartnerDBHelper(scope.context());
        oe = res_partners.getOEInstance();
        try {
            ArrayList<Fields> cols = res_partners.getServerColumns();
            JSONObject fields = new JSONObject();
            for (Fields field : cols) {
                fields.accumulate("fields", field.getName());
            }
            JSONObject domain = new JSONObject();
            JSONArray ids = JSONDataHelper.intArrayToJSONArray(oe.getAllIds(res_partners));

            domain.accumulate("domain", new JSONArray("[[\"id\", \"not in\", " + ids.toString() + "]]"));

            JSONObject result = oe.search_read("res.partner", fields, domain, 0, 0, null, null);

            for (int i = 0; i < result.getInt("length"); i++) {
                JSONObject row = result.getJSONArray("records").getJSONObject(i);
                HashMap<String, Object> rowHash = new HashMap<String, Object>();
                @SuppressWarnings("unchecked")
                Iterator<String> keys = row.keys();

                while (keys.hasNext()) {
                    String key = keys.next();
                    rowHash.put(key, row.get(key));
                }
                final OEListViewRows listRow = new OEListViewRows(row.getInt("id"), rowHash);

                scope.context().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listRows.add(listRow);
                        listAdapters.refresh(listRows);
                    }
                });
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        loaded = false;
    }
    return loaded;
}

From source file:org.jcommon.com.util.jmx.RmiAdptor.java

public RmiAdptor(String name, String addr, String user, String CREDENTIALS, int port) {
    JSONObject json = new JSONObject();
    try {/*w  w  w .j a v a2s.  c om*/
        json.accumulate("user", user);
        json.accumulate("name", name);
        json.accumulate("CREDENTIALS", CREDENTIALS);
        json.accumulate("addr", addr);
        json.accumulate("port", port);
        init(json.toString());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        logger.error("", e);
    }
}

From source file:com.openerp.addons.note.NoteDBHelper.java

public boolean isPadExist() {

    boolean state = false;
    OEHelper oe = getOEInstance();//from  w  ww  . ja  v  a  2 s  . com
    JSONObject domain = new JSONObject();

    try {
        domain.accumulate("domain", new JSONArray("[[\"name\",\"ilike\",\"note_pad\"]]"));
        JSONObject fields = new JSONObject();
        fields.accumulate("fields", "state");
        JSONObject result = oe.search_read("ir.module.module", fields, domain, 0, 0, null, null);
        JSONArray ids = result.getJSONArray("records");
        if (ids.getJSONObject(0).getString("state").equalsIgnoreCase("installed")) {
            state = true;
        }
    } catch (Exception e) {
    }
    return state;
}

From source file:census.couchdroid.CouchDocument.java

/**
 * Add a view to this document.  If a view function already exists with the given viewName
 * it is overwritten.//from w ww  .j a v a 2s. c om
 * <p>
 * This isn't persisted until the document is saved.
 * 
 * @param designDoc document name
 * @param viewName
 * @param function
 * @return
* @throws JSONException 
 */
public CouchView addView(String designDoc, String viewName, String function) throws JSONException {
    object.put("_id", "_design/" + designDoc); //Not sure if _id or id should be used
    object.put("language", "javascript"); //FIXME specify language

    JSONObject funcs = new JSONObject();
    // System.err.println("JSON String: " + JSONUtils.stringSerializedFunction(function));
    // funcs.put("map", JSONUtils.stringSerializedFunction(function));
    funcs.accumulate("map", "\"" + function + "\"");

    System.err.println("FUNCS: " + funcs.toString());

    JSONObject viewMap = new JSONObject();
    viewMap.put(viewName, funcs);

    object.put("views", viewMap);

    return new CouchView(this, viewName, function);

}

From source file:org.sufficientlysecure.privacybox.VaultProvider.java

@Override
public String createDocument(String parentDocumentId, String mimeType, String displayName)
        throws FileNotFoundException {
    final long parentDocId = Long.parseLong(parentDocumentId);

    // Allocate the next available ID
    final long childDocId;
    synchronized (mIdLock) {
        final SharedPreferences prefs = getContext().getSharedPreferences(PREF_NEXT_ID, Context.MODE_PRIVATE);
        childDocId = prefs.getLong(PREF_NEXT_ID, 1);
        if (!prefs.edit().putLong(PREF_NEXT_ID, childDocId + 1).commit()) {
            throw new IllegalStateException("Failed to allocate document ID");
        }/*  w  w  w  .  j  a  v  a2s .co  m*/
    }

    try {
        initDocument(childDocId, mimeType, displayName);

        // Update parent to reference new child
        final EncryptedDocument parentDoc = getDocument(parentDocId);
        final JSONObject parentMeta = parentDoc.readMetadata();
        parentMeta.accumulate(KEY_CHILDREN, childDocId);
        parentDoc.writeMetadataAndContent(parentMeta, null);

        return String.valueOf(childDocId);

    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (GeneralSecurityException e) {
        throw new IllegalStateException(e);
    } catch (JSONException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.androidsoft.games.memory.kids.model.Tile.java

/**
 * Build a JSONObject representing the tile
 * @return a JSONObject//  w ww  .  ja va  2s . c  o  m
 */
JSONObject json() {
    JSONObject object = new JSONObject();
    try {
        object.accumulate(ATTR_FOUND, mFound);
        object.accumulate(ATTR_SELECTED, mSelected);
        object.accumulate(ATTR_RESID, mResId);
    } catch (JSONException ex) {
        Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex);
    }
    return object;

}

From source file:com.cartlc.tracker.server.DCPing.java

public void sendRegistration() {
    Timber.i("sendRegistration()");
    try {/*from ww  w .  ja v a2 s.co  m*/
        String deviceId = ServerHelper.getInstance().getDeviceId();
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("first_name", PrefHelper.getInstance().getFirstName());
        jsonObject.accumulate("last_name", PrefHelper.getInstance().getLastName());
        jsonObject.accumulate("device_id", deviceId);
        String result = post(REGISTER, jsonObject);
        if (TextUtils.isDigitsOnly(result)) {
            int tech_id = Integer.parseInt(result);
            PrefHelper.getInstance().setTechID(tech_id);
            PrefHelper.getInstance().setRegistrationChanged(false);
            Timber.i("TECH ID=" + tech_id);
        } else {
            Timber.i("sendRegistration() failed");
        }
    } catch (Exception ex) {
        Timber.e(ex);
    }
}

From source file:com.cartlc.tracker.server.DCPing.java

public void ping() {
    Timber.i("ping()");
    try {/*from w w  w. ja v  a2 s . c om*/
        if (PrefHelper.getInstance().getTechID() == 0) {
            return;
        }
        String deviceId = ServerHelper.getInstance().getDeviceId();
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("device_id", deviceId);
        jsonObject.accumulate("tech_id", PrefHelper.getInstance().getTechID());
        String response = post(PING, jsonObject);
        if (response == null) {
            Timber.e("ping(): Unexpected NULL response from server");
            return;
        }
        JSONObject object = parseResult(response);
        if (object.has(UPLOAD_RESET_TRIGGER)) {
            if (object.getBoolean(UPLOAD_RESET_TRIGGER)) {
                Timber.i("UPLOAD RESET!");
                DatabaseManager.getInstance().clearUploaded();
            }
        }
        if (object.has(RE_REGISTER_TRIGGER)) {
            if (object.getBoolean(RE_REGISTER_TRIGGER)) {
                Timber.i("RE-REGISTER DETECTED!");
                sendRegistration();
            }
        }
        int version_project = object.getInt(PrefHelper.VERSION_PROJECT);
        int version_equipment = object.getInt(PrefHelper.VERSION_EQUIPMENT);
        int version_note = object.getInt(PrefHelper.VERSION_NOTE);
        int version_company = object.getInt(PrefHelper.VERSION_COMPANY);
        int version_truck = object.getInt(PrefHelper.VERSION_TRUCK);
        if (PrefHelper.getInstance().getVersionProject() != version_project) {
            Timber.i("New project version " + version_project);
            queryProjects();
            PrefHelper.getInstance().setVersionProject(version_project);
        }
        if (PrefHelper.getInstance().getVersionCompany() != version_company) {
            queryCompanies();
            Timber.i("New company version " + version_company);
            PrefHelper.getInstance().setVersionCompany(version_company);
        }
        if (PrefHelper.getInstance().getVersionEquipment() != version_equipment) {
            queryEquipments();
            Timber.i("New equipment version " + version_equipment);
            PrefHelper.getInstance().setVersionEquipment(version_equipment);
        }
        if (PrefHelper.getInstance().getVersionNote() != version_note) {
            queryNotes();
            Timber.i("New note version " + version_note);
            PrefHelper.getInstance().setVersionNote(version_note);
        }
        if (PrefHelper.getInstance().getVersionTruck() != version_truck) {
            queryTrucks();
            Timber.i("New truck version " + version_truck);
            PrefHelper.getInstance().setVersionTruck(version_truck);
        }
        List<DataEntry> entries = TableEntry.getInstance().queryPendingDataToUploadToMaster();
        int count = 0;
        if (entries.size() > 0) {
            count = sendEntries(entries);
        }
        if (count > 0) {
            EventBus.getDefault().post(new EventPingDone());
        }
        entries = TableEntry.getInstance().queryPendingPicturesToUpload();
        if (entries.size() > 0) {
            AmazonHelper.getInstance().sendPictures(entries);
        }
        TablePictureCollection.getInstance().clearUploadedUnscaledPhotos();
        List<TableCrash.CrashLine> lines = TableCrash.getInstance().queryNeedsUploading();
        sendCrashLines(lines);
        // If any entries do not yet have server-id's, try to get them.
        entries = TableEntry.getInstance().queryServerIds();
        if (entries.size() > 0) {
            Timber.i("FOUND " + entries.size() + " entries without server IDS");
            sendEntries(entries);
        } else {
            Timber.i("All entries have server ids");
        }
    } catch (Exception ex) {
        Timber.e(ex);
    }
}

From source file:com.cartlc.tracker.server.DCPing.java

boolean sendEntry(DataEntry entry) {
    boolean success = false;
    Timber.i("sendEntry(" + entry.id + ")");
    try {//from   www  .  jav  a  2s . c om
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("tech_id", PrefHelper.getInstance().getTechID());
        jsonObject.accumulate("date", entry.date);
        jsonObject.accumulate("server_id", entry.serverId);
        DataTruck truck = entry.getTruck();
        if (truck.truckNumber > 0) {
            jsonObject.accumulate("truck_number", truck.truckNumber);
        }
        if (truck.licensePlateNumber != null) {
            jsonObject.accumulate("license_plate", truck.licensePlateNumber);
        }
        DataProject project = entry.getProject();
        if (project == null) {
            Timber.e("No project name for entry -- abort");
            return false;
        }
        if (project.serverId > 0) {
            jsonObject.accumulate("project_id", project.serverId);
        } else {
            jsonObject.accumulate("project_name", project.name);
        }
        DataAddress address = entry.getAddress();
        if (address == null) {
            Timber.e("No address for entry -- abort");
            return false;
        }
        if (address.serverId > 0) {
            jsonObject.accumulate("address_id", address.serverId);
        } else {
            jsonObject.accumulate("address", address.getLine());
        }
        if (entry.status != null && entry.status != TruckStatus.UNKNOWN) {
            jsonObject.accumulate("status", entry.status.toString());
        }
        List<DataEquipment> equipments = entry.getEquipment();
        if (equipments.size() > 0) {
            JSONArray jarray = new JSONArray();
            for (DataEquipment equipment : equipments) {
                JSONObject jobj = new JSONObject();
                if (equipment.serverId > 0) {
                    jobj.accumulate("equipment_id", equipment.serverId);
                } else {
                    jobj.accumulate("equipment_name", equipment.name);
                }
                jarray.put(jobj);
            }
            jsonObject.put("equipment", jarray);
        }
        List<DataPicture> pictures = entry.getPictures();
        if (pictures.size() > 0) {
            JSONArray jarray = new JSONArray();
            for (DataPicture picture : pictures) {
                JSONObject jobj = new JSONObject();
                jobj.put("filename", picture.getTailname());
                if (!TextUtils.isEmpty(picture.note)) {
                    jobj.put("note", picture.note);
                }
                jarray.put(jobj);
            }
            jsonObject.put("picture", jarray);
        }
        List<DataNote> notes = entry.getNotesWithValuesOnly();
        if (notes.size() > 0) {
            JSONArray jarray = new JSONArray();
            for (DataNote note : notes) {
                JSONObject jobj = new JSONObject();
                if (note.serverId > 0) {
                    jobj.put("id", note.serverId);
                } else {
                    jobj.put("name", note.name);
                }
                jobj.put("value", note.value);
                jarray.put(jobj);
            }
            jsonObject.put("notes", jarray);
        }
        Timber.i("SENDING " + jsonObject.toString());
        String result = post(ENTER, jsonObject);
        if (TextUtils.isDigitsOnly(result)) {
            entry.uploadedMaster = true;
            entry.serverId = Integer.parseInt(result);
            TableEntry.getInstance().save(entry);
            success = true;
            Timber.i("SUCCESS, ENTRY SERVER ID is " + entry.serverId);
        } else {
            Timber.e("While trying to send entry " + entry.id + ": " + result);
        }
    } catch (Exception ex) {
        Timber.e(ex);
        return false;
    }
    return success;
}

From source file:com.cartlc.tracker.server.DCPing.java

String post(String target) {
    try {// w w  w. j a va 2s  .c  om
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("device_id", ServerHelper.getInstance().getDeviceId());
        jsonObject.accumulate("tech_id", PrefHelper.getInstance().getTechID());
        return post(target, jsonObject);
    } catch (Exception ex) {
        Timber.e(ex);
    }
    return null;
}