Question

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

Was it helpful?

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 :)

OTHER TIPS

Q1 No, it does not appear to be setting a class member variable, but rather a variable local to the execute() method. As soon as the method returns, those local vars are cleaned up by the GC. Well, not really, but they are now subject to GC, but that's getting really technical.

Q2 Yes, you are creating a HashMap and putting it's reference into a Map. Map is an interface, and it's good practice in Java to reference thing like this. This way you are not tying your code to a specific implementation. I believe in Objective-C they are know as Prototypes???

Q3 I am not sure why they are doing this. I assume somewhere in the code the feedback Map is used, and that value is plucked back out. Think of Maps as an NSDictionary. It looks like "year" is a String, so they use Long.parseLong() to convert it. Not sure what SMInt is...from the name it looks like a custom class that represents a "small int"???

Q4 I don't know what DataService is, but I have to guess its some sort of service the reads/write data??? From the method, I am guessing its calling the service to update the values you just changed.

Q5 Again, feedback is a Map...it's putting result in the "updated object" key of that map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top