Panagiotis Triantafyllou

added campaigns availability request

...@@ -100,6 +100,17 @@ public interface ApiService { ...@@ -100,6 +100,17 @@ public interface ApiService {
100 @Header(WarpConstants.HEADER_SIGNATURE) String signature); 100 @Header(WarpConstants.HEADER_SIGNATURE) String signature);
101 101
102 @Headers("Content-Type: application/json") 102 @Headers("Content-Type: application/json")
103 + @POST("/api/mobile/v2/{appUuid}/context/")
104 + Call<ResponseBody> getCampaignsAvailability(@Path("appUuid") String appUuid,
105 + @Body RequestBody request,
106 + @Header(WarpConstants.HEADER_DATE) String timeStamp,
107 + @Header(WarpConstants.HEADER_LOYALTY_BUNDLE_ID) String bundleId,
108 + @Header(WarpConstants.HEADER_UNIQUE_DEVICE_ID) String deviceId,
109 + @Header(WarpConstants.HEADER_CHANNEL) String channel,
110 + @Header(WarpConstants.HEADER_WEB_ID) String webId,
111 + @Header(WarpConstants.HEADER_SIGNATURE) String signature);
112 +
113 + @Headers("Content-Type: application/json")
103 @POST("/oauth/{appUuid}/context") 114 @POST("/oauth/{appUuid}/context")
104 Call<ResponseBody> getCampaignsPersonalized(@Path("appUuid") String appUuid, 115 Call<ResponseBody> getCampaignsPersonalized(@Path("appUuid") String appUuid,
105 @Body RequestBody request, 116 @Body RequestBody request,
......
...@@ -1565,7 +1565,7 @@ public class WarplyManager { ...@@ -1565,7 +1565,7 @@ public class WarplyManager {
1565 request.getSignature())); 1565 request.getSignature()));
1566 } 1566 }
1567 1567
1568 - private static ArrayList<Campaign> mergeCampaignResults(ArrayList<Campaign> resultCampaigns, ArrayList<Campaign> resultPersonalizedCampaigns) { 1568 + private static ArrayList<Campaign> mergeCampaignResults(ArrayList<Campaign> resultCampaigns, ArrayList<Campaign> resultPersonalizedCampaigns, JSONObject resultCampaignsAvailability) {
1569 ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>(); 1569 ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
1570 ArrayList<Campaign> campaignLoyaltyList = new ArrayList<>(); 1570 ArrayList<Campaign> campaignLoyaltyList = new ArrayList<>();
1571 newCampaignList.clear(); 1571 newCampaignList.clear();
...@@ -1619,14 +1619,16 @@ public class WarplyManager { ...@@ -1619,14 +1619,16 @@ public class WarplyManager {
1619 1619
1620 ListenableFuture<ArrayList<Campaign>> futureCampaigns = getCampaignsRetro(service); 1620 ListenableFuture<ArrayList<Campaign>> futureCampaigns = getCampaignsRetro(service);
1621 ListenableFuture<ArrayList<Campaign>> futurePersonalizedCampaigns = getCampaignsPersonalizedRetro(service); 1621 ListenableFuture<ArrayList<Campaign>> futurePersonalizedCampaigns = getCampaignsPersonalizedRetro(service);
1622 + ListenableFuture<JSONObject> futureCampaignAvailability = getCampaignAvailability(service);
1622 1623
1623 - ListenableFuture<List<ArrayList<Campaign>>> allResultsFuture = Futures.allAsList(futureCampaigns, futurePersonalizedCampaigns); 1624 + ListenableFuture<List<Object>> allResultsFuture = Futures.allAsList(futureCampaigns, futurePersonalizedCampaigns, futureCampaignAvailability);
1624 ListenableFuture<ArrayList<Campaign>> mergedResultFuture = Futures.transformAsync( 1625 ListenableFuture<ArrayList<Campaign>> mergedResultFuture = Futures.transformAsync(
1625 allResultsFuture, 1626 allResultsFuture,
1626 results -> { 1627 results -> {
1627 - ArrayList<Campaign> resultCampaigns = results.get(0); 1628 + ArrayList<Campaign> resultCampaigns = (ArrayList<Campaign>) results.get(0);
1628 - ArrayList<Campaign> resultPersonalizedCampaigns = results.get(1); 1629 + ArrayList<Campaign> resultPersonalizedCampaigns = (ArrayList<Campaign>) results.get(1);
1629 - return executorService.submit(() -> mergeCampaignResults(resultCampaigns, resultPersonalizedCampaigns)); 1630 + JSONObject resultCampaignsAvailability = (JSONObject) results.get(2);
1631 + return executorService.submit(() -> mergeCampaignResults(resultCampaigns, resultPersonalizedCampaigns, resultCampaignsAvailability));
1630 }, 1632 },
1631 executorService 1633 executorService
1632 ); 1634 );
...@@ -1650,8 +1652,8 @@ public class WarplyManager { ...@@ -1650,8 +1652,8 @@ public class WarplyManager {
1650 // getCampaignsPersonalizedRetro(service, new CallbackReceiver<ArrayList<Campaign>>() { 1652 // getCampaignsPersonalizedRetro(service, new CallbackReceiver<ArrayList<Campaign>>() {
1651 // @Override 1653 // @Override
1652 // public void onSuccess(ArrayList<Campaign> resultPersonalized) { 1654 // public void onSuccess(ArrayList<Campaign> resultPersonalized) {
1653 -// final ExecutorService executorShops = Executors.newFixedThreadPool(1); 1655 +// final ExecutorService executorCampaigns = Executors.newFixedThreadPool(1);
1654 -// executorShops.submit(() -> { 1656 +// executorCampaigns.submit(() -> {
1655 // ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>(); 1657 // ArrayList<Campaign> newCampaignList = new ArrayList<Campaign>();
1656 // newCampaignList.clear(); 1658 // newCampaignList.clear();
1657 // newCampaignList.addAll(result); 1659 // newCampaignList.addAll(result);
...@@ -1686,7 +1688,7 @@ public class WarplyManager { ...@@ -1686,7 +1688,7 @@ public class WarplyManager {
1686 // Set<Campaign> set = new LinkedHashSet<>(campaignLoyaltyList); 1688 // Set<Campaign> set = new LinkedHashSet<>(campaignLoyaltyList);
1687 // campaignLoyaltyList.clear(); 1689 // campaignLoyaltyList.clear();
1688 // campaignLoyaltyList.addAll(set); 1690 // campaignLoyaltyList.addAll(set);
1689 -// executorShops.shutdownNow(); 1691 +// executorCampaigns.shutdownNow();
1690 // new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(campaignLoyaltyList));//resultPersonalized 1692 // new Handler(Looper.getMainLooper()).post(() -> receiver.onSuccess(campaignLoyaltyList));//resultPersonalized
1691 // }); 1693 // });
1692 // } 1694 // }
...@@ -1759,9 +1761,9 @@ public class WarplyManager { ...@@ -1759,9 +1761,9 @@ public class WarplyManager {
1759 ArrayList<NewCampaign> tempCampaigns = new ArrayList<>(); 1761 ArrayList<NewCampaign> tempCampaigns = new ArrayList<>();
1760 ArrayList<Campaign> mCampaignsList = new ArrayList<>(); 1762 ArrayList<Campaign> mCampaignsList = new ArrayList<>();
1761 1763
1762 - final ExecutorService executorShops = Executors.newFixedThreadPool(1); 1764 + final ExecutorService executorCampaigns = Executors.newFixedThreadPool(1);
1763 JSONArray finalCampaignsJBody = jCampaignsBody; 1765 JSONArray finalCampaignsJBody = jCampaignsBody;
1764 - executorShops.submit(() -> { 1766 + executorCampaigns.submit(() -> {
1765 for (int i = 0; i < finalCampaignsJBody.length(); ++i) { 1767 for (int i = 0; i < finalCampaignsJBody.length(); ++i) {
1766 tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i))); 1768 tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i)));
1767 } 1769 }
...@@ -1809,7 +1811,7 @@ public class WarplyManager { ...@@ -1809,7 +1811,7 @@ public class WarplyManager {
1809 mCampaignsList.add(camp); 1811 mCampaignsList.add(camp);
1810 } 1812 }
1811 1813
1812 - executorShops.shutdownNow(); 1814 + executorCampaigns.shutdownNow();
1813 // receiver.onSuccess(mCampaignsList); 1815 // receiver.onSuccess(mCampaignsList);
1814 future.set(mCampaignsList); 1816 future.set(mCampaignsList);
1815 }); 1817 });
...@@ -1853,6 +1855,7 @@ public class WarplyManager { ...@@ -1853,6 +1855,7 @@ public class WarplyManager {
1853 Map<String, Object> jsonParamsCampaigns = new ArrayMap<>(); 1855 Map<String, Object> jsonParamsCampaigns = new ArrayMap<>();
1854 Map<String, Object> jsonParams = new ArrayMap<>(); 1856 Map<String, Object> jsonParams = new ArrayMap<>();
1855 jsonParams.put("action", "retrieve"); 1857 jsonParams.put("action", "retrieve");
1858 + jsonParams.put("filters", new JSONObject());
1856 jsonParams.put("language", WarplyProperty.getLanguage(Warply.getWarplyContext())); 1859 jsonParams.put("language", WarplyProperty.getLanguage(Warply.getWarplyContext()));
1857 1860
1858 jsonParamsCampaigns.put("campaigns", jsonParams); 1861 jsonParamsCampaigns.put("campaigns", jsonParams);
...@@ -1897,9 +1900,9 @@ public class WarplyManager { ...@@ -1897,9 +1900,9 @@ public class WarplyManager {
1897 ArrayList<NewCampaign> tempCampaigns = new ArrayList<>(); 1900 ArrayList<NewCampaign> tempCampaigns = new ArrayList<>();
1898 ArrayList<Campaign> mCampaignsList = new ArrayList<>(); 1901 ArrayList<Campaign> mCampaignsList = new ArrayList<>();
1899 1902
1900 - final ExecutorService executorShops = Executors.newFixedThreadPool(1); 1903 + final ExecutorService executorPersonalizedCampaigns = Executors.newFixedThreadPool(1);
1901 JSONArray finalCampaignsJBody = jCampaignsBody; 1904 JSONArray finalCampaignsJBody = jCampaignsBody;
1902 - executorShops.submit(() -> { 1905 + executorPersonalizedCampaigns.submit(() -> {
1903 for (int i = 0; i < finalCampaignsJBody.length(); ++i) { 1906 for (int i = 0; i < finalCampaignsJBody.length(); ++i) {
1904 tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i))); 1907 tempCampaigns.add(new NewCampaign(finalCampaignsJBody.optJSONObject(i)));
1905 } 1908 }
...@@ -1947,7 +1950,7 @@ public class WarplyManager { ...@@ -1947,7 +1950,7 @@ public class WarplyManager {
1947 mCampaignsList.add(camp); 1950 mCampaignsList.add(camp);
1948 } 1951 }
1949 1952
1950 - executorShops.shutdownNow(); 1953 + executorPersonalizedCampaigns.shutdownNow();
1951 // receiver.onSuccess(mCampaignsList); 1954 // receiver.onSuccess(mCampaignsList);
1952 future.set(mCampaignsList); 1955 future.set(mCampaignsList);
1953 }); 1956 });
...@@ -2000,6 +2003,93 @@ public class WarplyManager { ...@@ -2000,6 +2003,93 @@ public class WarplyManager {
2000 return future; 2003 return future;
2001 } 2004 }
2002 2005
2006 + private static ListenableFuture<JSONObject> getCampaignAvailability(ApiService service) {
2007 + SettableFuture<JSONObject> future = SettableFuture.create();
2008 +
2009 + String timeStamp = DateFormat.format("yyyy-MM-dd hh:mm:ss", System.currentTimeMillis()).toString();
2010 + String apiKey = WarpUtils.getApiKey(Warply.getWarplyContext());
2011 + String webId = WarpUtils.getWebId(Warply.getWarplyContext());
2012 +
2013 + Map<String, Object> jsonParamsCampaignsAvailability = new ArrayMap<>();
2014 + Map<String, Object> jsonParams = new ArrayMap<>();
2015 + jsonParams.put("action", "availability");
2016 + Map<String, Object> jsonParamsFilters = new ArrayMap<>();
2017 + jsonParams.put("uuids", null);
2018 + jsonParams.put("availability_enabled", true);
2019 + jsonParams.put("filters", jsonParamsFilters);
2020 + jsonParams.put("language", WarplyProperty.getLanguage(Warply.getWarplyContext()));
2021 +
2022 + jsonParamsCampaignsAvailability.put("coupon", jsonParams);
2023 + RequestBody campaignsAvailabilityRequest = RequestBody.create(MediaType.get("application/json; charset=utf-8"), (new JSONObject(jsonParamsCampaignsAvailability)).toString());
2024 +
2025 + Call<ResponseBody> campaignsAvailabilityCall = service.getCampaignsAvailability(
2026 + WarplyProperty.getAppUuid(Warply.getWarplyContext()),
2027 + campaignsAvailabilityRequest,
2028 + timeStamp,
2029 + "android:" + Warply.getWarplyContext().getPackageName(),
2030 + new WarplyDeviceInfoCollector(Warply.getWarplyContext()).getUniqueDeviceId(),
2031 + "mobile",
2032 + webId,
2033 + WarpUtils.produceSignature(apiKey + timeStamp)
2034 + );
2035 +
2036 + campaignsAvailabilityCall.enqueue(new Callback<ResponseBody>() {
2037 + @Override
2038 + public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
2039 + if (response.code() == 200 && response.body() != null) {
2040 + JSONObject jobjCampaignsAvailabilityResponse = null;
2041 + try {
2042 + jobjCampaignsAvailabilityResponse = new JSONObject(response.body().string());
2043 + } catch (Exception e) {
2044 + e.printStackTrace();
2045 + }
2046 +
2047 + if (jobjCampaignsAvailabilityResponse != null && jobjCampaignsAvailabilityResponse.has("status") && jobjCampaignsAvailabilityResponse.optString("status", "2").equals("1")) {
2048 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2049 + dynatraceEvent.setEventName("custom_success_available_coupons");
2050 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2051 +
2052 + final ExecutorService executorCampaignsAvailability = Executors.newFixedThreadPool(1);
2053 + final JSONObject finalJobjCampaignsAvailabilityResponse = jobjCampaignsAvailabilityResponse;
2054 + executorCampaignsAvailability.submit(() -> {
2055 + JSONObject jCampaignsAvailabilityBody = null;
2056 + try {
2057 + jCampaignsAvailabilityBody = finalJobjCampaignsAvailabilityResponse.optJSONObject("context").optJSONObject("MAPP_COUPON").optJSONObject("availability");
2058 + } catch (Exception e) {
2059 + e.printStackTrace();
2060 + }
2061 +
2062 + if (jCampaignsAvailabilityBody != null) {
2063 + executorCampaignsAvailability.shutdownNow();
2064 + future.set(jCampaignsAvailabilityBody);
2065 + }
2066 + });
2067 + } else {
2068 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2069 + dynatraceEvent.setEventName("custom_error_available_coupons");
2070 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2071 + future.set(new JSONObject());
2072 + }
2073 + } else {
2074 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2075 + dynatraceEvent.setEventName("custom_error_available_coupons");
2076 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2077 + future.set(new JSONObject());
2078 + }
2079 + }
2080 +
2081 + @Override
2082 + public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
2083 + LoyaltySDKDynatraceEventModel dynatraceEvent = new LoyaltySDKDynatraceEventModel();
2084 + dynatraceEvent.setEventName("custom_error_available_coupons");
2085 + EventBus.getDefault().post(new WarplyEventBusManager(dynatraceEvent));
2086 + future.set(new JSONObject());
2087 + }
2088 + });
2089 +
2090 + return future;
2091 + }
2092 +
2003 public static void getCampaignsPersonalized(WarplyGetCampaignsRequest request, final CallbackReceiver<ArrayList<Campaign>> receiver) { 2093 public static void getCampaignsPersonalized(WarplyGetCampaignsRequest request, final CallbackReceiver<ArrayList<Campaign>> receiver) {
2004 WarpUtils.log("************* WARPLY Get Campaigns Request ********************"); 2094 WarpUtils.log("************* WARPLY Get Campaigns Request ********************");
2005 WarpUtils.log("[WARP Trace] WARPLY Get Campaigns Request is active"); 2095 WarpUtils.log("[WARP Trace] WARPLY Get Campaigns Request is active");
......