public static Like createLikeRequest( String memoryId, int categoryType, Context context, String memoryType) { Like like = new Like( null, null, TJPreferences.getActiveJourneyId(context), memoryId, TJPreferences.getUserId(context), memoryType, true, null, HelpMe.getCurrentTime(), HelpMe.getCurrentTime()); like.setId(String.valueOf(LikeDataSource.createLike(like, context))); Log.d(TAG, "like created in database = " + like + memoryType); Request request = new Request( null, like.getId(), TJPreferences.getActiveJourneyId(context), Request.OPERATION_TYPE_LIKE, categoryType, Request.REQUEST_STATUS_NOT_STARTED, 0); RequestQueueDataSource.createRequest(request, context); if (HelpMe.isNetworkAvailable(context)) { Intent intent = new Intent(context, MakeServerRequestsService.class); context.startService(intent); } else { Log.d(TAG, "since no network not starting service RQ"); } return like; }
public static boolean deleteMemoryOnServer( Context context, String memoryId, String journeyId, String memType) { String url = Constants.URL_MEMORY_UPDATE + journeyId + "/memories/" + memoryId + "?api_key=" + TJPreferences.getApiKey(context); Log.d(TAG, "url is " + url + " api key " + TJPreferences.getApiKey(context)); HttpDelete deleteRequest = new HttpDelete(url); HttpResponse response; try { response = new DefaultHttpClient().execute(deleteRequest); JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity())); MemoriesDataSource.deleteMemoryWithServerId(context, memType, memoryId); Log.d(TAG, "response on deleting memory" + object); return true; } catch (Exception e) { Log.d(TAG, "error in deleting memory" + e.getMessage()); return false; } }
public static boolean likeMemoryOnServer(Context context, Like like) { String url = Constants.URL_MEMORY_UPDATE + like.getJourneyId() + "/memories/" + like.getMemoryLocalId() + "/like"; Map<String, String> params = new HashMap<>(); params.put("api_key", TJPreferences.getApiKey(context)); Log.d(TAG, "uploading like with parameters " + params); Log.d(TAG, "uploading like with url " + url); final RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture(); CustomJsonRequest jsonRequest = new CustomJsonRequest( com.android.volley.Request.Method.POST, url, params, futureRequest, futureRequest); AppController.getInstance().getRequestQueue().add(jsonRequest); try { JSONObject response = futureRequest.get(30, TimeUnit.SECONDS); Log.d(TAG, "memory liked successfully on server with response " + response); like.setIdOnServer(response.getJSONArray("likes").getJSONObject(0).getString("id")); /*LikeDataSource.updateLike(like, context);*/ return true; } catch (InterruptedException e) { Log.d(TAG, "like couldnot be uploaded InterruptedException"); e.printStackTrace(); } catch (ExecutionException e) { Log.d(TAG, "like couldnot be uploaded ExecutionException"); e.printStackTrace(); } catch (TimeoutException e) { Log.d(TAG, "like couldnot be uploaded TimeoutException"); e.printStackTrace(); } catch (JSONException e) { Log.d(TAG, "like couldnot be parsed although uploaded successfully"); e.printStackTrace(); } return false; }
public static void createUnlikeRequest(Like like, int categoryType, Context context) { Log.d(TAG, "unliking a memory with id = " + like.getId() + like.getMemType()); // LikeDataSource.deleteLike(context, like); like.setIsValid(false); LikeDataSource.updateLike(like, context); Request request = new Request( null, like.getId(), TJPreferences.getActiveJourneyId(context), Request.OPERATION_TYPE_UNLIKE, categoryType, Request.REQUEST_STATUS_NOT_STARTED, 0); RequestQueueDataSource.createRequest(request, context); if (HelpMe.isNetworkAvailable(context)) { Intent intent = new Intent(context, MakeServerRequestsService.class); context.startService(intent); } else { Log.d(TAG, "since no network not starting service RQ"); } }
public static boolean unlikeMemoryOnServer(Context context, Like like) { String url = Constants.URL_MEMORY_UPDATE + like.getJourneyId() + "/memories/" + like.getMemoryLocalId() + "/unlike"; Map<String, String> params = new HashMap<>(); params.put("api_key", TJPreferences.getApiKey(context)); Log.d(TAG, "unliking memory with parameters " + params); Log.d(TAG, "unliking memory with url " + url); final RequestFuture<JSONObject> futureRequest = RequestFuture.newFuture(); CustomJsonRequest jsonRequest = new CustomJsonRequest( com.android.volley.Request.Method.PUT, url, params, futureRequest, futureRequest); AppController.getInstance().getRequestQueue().add(jsonRequest); try { JSONObject response = futureRequest.get(30, TimeUnit.SECONDS); LikeDataSource.deleteLike(context, like.getId()); Log.d(TAG, "memory unliked successfully on server with response " + response); return true; } catch (InterruptedException e) { Log.d(TAG, "couldnot unlike InterruptedException"); e.printStackTrace(); } catch (ExecutionException e) { Log.d(TAG, "couldnot unlike ExecutionException"); e.printStackTrace(); } catch (TimeoutException e) { Log.d(TAG, "couldnot unlike TimeoutException"); e.printStackTrace(); } return false; }