Search code examples
javastackmob

Java: Code Understanding


I'm new to JAVA, but I know Objective-C. I have to write a server side Custom Code and I'm having trouble with the code below:

/**
 * This example will show a user how to write a custom code method
 * with two parameters that updates the specified object in their schema
 * when given a unique ID and a `year` field on which to update.
 */

public class UpdateObject implements CustomCodeMethod {

  @Override
  public String getMethodName() {
    return "CRUD_Update";
  }

  @Override
  public List<String> getParams() {
    return Arrays.asList("car_ID","year");
  }

  @Override
  public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider serviceProvider) {
    String carID = "";
    String year  = "";

    LoggerService logger = serviceProvider.getLoggerService(UpdateObject.class);
    logger.debug(request.getBody());
    Map<String, String> errMap = new HashMap<String, String>();

    /* The following try/catch block shows how to properly fetch parameters for PUT/POST operations
     * from the JSON request body
     */

    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(request.getBody());
      JSONObject jsonObject = (JSONObject) obj;

      // Fetch the values passed in by the user from the body of JSON
      carID = (String) jsonObject.get("car_ID");
      year = (String) jsonObject.get("year");
      //Q1:  This is assigning the values to  fields in the fetched Object?

    } catch (ParseException pe) {
      logger.error(pe.getMessage(), pe);
      return Util.badRequestResponse(errMap, pe.getMessage());
    }

    if (Util.hasNulls(year, carID)){
      return Util.badRequestResponse(errMap);
    }

    //Q2:  Is this creating a new HashMap?  If so, why is there a need?
    Map<String, SMValue> feedback = new HashMap<String, SMValue>();
    //Q3:  This is taking the key "updated year" and assigning a value (year)?  Why?
    feedback.put("updated year", new SMInt(Long.parseLong(year)));

    DataService ds = serviceProvider.getDataService();
    List<SMUpdate> update = new ArrayList<SMUpdate>();

    /* Create the changes in the form of an Update that you'd like to apply to the object
     * In this case I want to make changes to year by overriding existing values with user input
     */
    update.add(new SMSet("year", new SMInt(Long.parseLong(year))));

    SMObject result;
    try {
      // Remember that the primary key in this car schema is `car_id`

      //Q4: If the Object is updated earlier with update.add... What is the code below doing?
      result = ds.updateObject("car", new SMString(carID), update);

      //Q5: What's the need for the code below?
      feedback.put("updated object", result);

    } catch (InvalidSchemaException ise) {
      return Util.internalErrorResponse("invalid_schema", ise, errMap);  // http 500 - internal server error
    } catch (DatastoreException dse) {
      return Util.internalErrorResponse("datastore_exception", dse, errMap);  // http 500 - internal server error
    }

    return new ResponseToProcess(HttpURLConnection.HTTP_OK, feedback);
  }

}

Q1: Code below is assigning the values to fields in the fetched Object?

carID = (String) jsonObject.get("car_ID");
year = (String) jsonObject.get("year");

Q2: Is this creating a new HashMap? If so, why is there a need?

Map<String, SMValue> feedback = new HashMap<String, SMValue>();

Q3: This is taking the key "updated year" and assigning a value (year)? Why?

feedback.put("updated year", new SMInt(Long.parseLong(year)));

Q4: If the Object is updated earlier with update.add... What is the code below doing?

result = ds.updateObject("car", new SMString(carID), update);

Q5: What's the code below doing?

feedback.put("updated object", result);

Original Code

SMSet

SMInt


Solution

  • Q1: They read from the fetched JSON object and stores the values of the fields car_ID and year in two local variables with the same names.

    Q2: Yes. The feedback seems to be a map that will be sent back to the client as JSON

    Q3: It stores the value read into the local variable 'year' (as described earlier) in the newly created hashmap 'feedback'

    Q4: Not sure, I assume the ds object is some sort of database. If so it looks like it takes the updated values stored in the hashmap 'update' and pushes it to the database.

    Q5: It stores the "result" object under the key "updated object" in the feedback hashmap.

    Hope this helps :)