public void update(String objectId, JSONObject data) throws ParseException { final String endPoint = retrieveEndpoint(); final ParsePutCommand command = new ParsePutCommand(endPoint + "/" + objectId); command.setData(data); final ParseResponse response = command.perform(); if (response.isFailed()) { LOGGER.debug("Request failed."); throw response.getException(); } }
protected void delete(boolean useMasterKey) throws ParseException { if (objectId == null) return; ParseCommand command = new ParseDeleteCommand(getEndPoint(), getObjectId(), useMasterKey); ParseResponse response = command.perform(); if (response.isFailed()) { throw response.getException(); } this.updatedAt = null; this.createdAt = null; this.objectId = null; this.isDirty = false; this.operations.clear(); this.dirtyKeys.clear(); }
private void save(boolean useMasterKey) throws ParseException { if (!isDirty) return; ParseCommand command; if (objectId == null) { command = new ParsePostCommand(getEndPoint(), useMasterKey); } else { command = new ParsePutCommand(getEndPoint(), getObjectId(), useMasterKey); } command.setData(getParseData()); ParseResponse response = command.perform(); if (!response.isFailed()) { JSONObject jsonResponse = response.getJsonObject(); if (jsonResponse == null) { LOGGER.error("Empty response"); throw response.getException(); } try { if (getObjectId() == null) { setObjectId(jsonResponse.getString(ParseConstants.FIELD_OBJECT_ID)); String createdAt = jsonResponse.getString(ParseConstants.FIELD_CREATED_AT); setCreatedAt(Parse.parseDate(createdAt)); setUpdatedAt(Parse.parseDate(createdAt)); } else { String updatedAt = jsonResponse.getString(ParseConstants.FIELD_UPDATED_AT); setUpdatedAt(Parse.parseDate(updatedAt)); } this.isDirty = false; this.operations.clear(); this.dirtyKeys.clear(); } catch (JSONException e) { LOGGER.error("Request failed."); throw new ParseException( ParseException.INVALID_JSON, "Although Parse reports object successfully saved, the response was invalid.", e); } } else { LOGGER.error("Request failed."); throw response.getException(); } }
private <T extends ParseObject> T fetchIfNeeded(boolean useMasterKey) throws ParseException { ParseGetCommand command = new ParseGetCommand(getEndPoint(), getObjectId(), useMasterKey); // JSONObject query = new JSONObject(); // query.put("objectId", getObjectId()); // command.setData(query); ParseResponse response = command.perform(); if (!response.isFailed()) { JSONObject jsonResponse = response.getJsonObject(); if (jsonResponse == null) { throw response.getException(); } T obj = parseData(jsonResponse); return obj; } else { throw response.getException(); } }
public int count(JSONObject query) throws ParseException { final String endPoint = retrieveEndpoint(); ParseGetCommand command = new ParseGetCommand(endPoint); query.put("count", 1); query.put("limit", 0); query.remove("className"); command.setData(query); ParseResponse response = command.perform(); if (!response.isFailed()) { if (response.getJsonObject() == null) { LOGGER.debug("Empty response."); throw response.getException(); } try { JSONObject json = response.getJsonObject(); int count = json.getInt("count"); return count; } catch (JSONException e) { LOGGER.error( "Although Parse reports object successfully saved, the response was invalid.", e); throw new ParseException( ParseException.INVALID_JSON, "Although Parse reports object successfully saved, the response was invalid.", e); } } else { LOGGER.debug("Request failed."); throw response.getException(); } }
@SuppressWarnings("unchecked") public List<T> find(JSONObject query) throws ParseException { final String endPoint = retrieveEndpoint(); ParseGetCommand command = new ParseGetCommand(endPoint); query.remove("className"); command.setData(query); ParseResponse response = command.perform(); List<T> results = null; if (!response.isFailed()) { if (response.getJsonObject() == null) { LOGGER.debug("Empty response."); throw response.getException(); } try { JSONObject json = response.getJsonObject(); JSONArray objs = json.getJSONArray("results"); if (objs.length() == 0) { return null; } if (trace) { strTrace = json.getString("trace"); if (LOGGER.isDebugEnabled()) { LOGGER.debug(strTrace); } } results = new ArrayList<T>(); for (int i = 0; i < objs.length(); i++) { results.add(createPojo((JSONObject) objs.get(i))); } return results; } catch (JSONException e) { LOGGER.error( "Although Parse reports object successfully saved, the response was invalid.", e); throw new ParseException( ParseException.INVALID_JSON, "Although Parse reports object successfully saved, the response was invalid.", e); } catch (InstantiationException e) { LOGGER.error("Error while instantiating class. Did you register your subclass?", e); throw new ParseException( ParseException.INVALID_JSON, "Although Parse reports object successfully saved, the response was invalid.", e); } catch (IllegalAccessException e) { LOGGER.error("Error while instantiating class. Did you register your subclass?", e); throw new ParseException( ParseException.INVALID_JSON, "Although Parse reports object successfully saved, the response was invalid.", e); } } else { LOGGER.debug("Request failed."); throw response.getException(); } }
/** * Creates and saves new instance from JSON object. * * @param json object data in JSON format. * @return created object with objectId. * @throws ParseException */ public T createObject(JSONObject json) throws ParseException { final String endPoint = retrieveEndpoint(); final ParsePostCommand command = new ParsePostCommand(endPoint); command.setData(json); final ParseResponse response = command.perform(); if (!response.isFailed()) { final JSONObject resp = response.getJsonObject(); json.put("objectId", resp.get("objectId")); json.put("createdAt", resp.get("createdAt")); try { return createPojo(json); } catch (InstantiationException e) { throw new ParseException(e); } catch (IllegalAccessException e) { throw new ParseException(e); } } else { LOGGER.debug("Request failed."); throw response.getException(); } }