use of com.pratilipi.data.DataListCursorTuple in project pratilipi by Pratilipi.
the class PersonalizedInitUtil method prepareResponse.
public static PersonalizedInitApi.Response prepareResponse(Language language, String cursor, String appVersion, String androidVersion, Integer resultCount) throws UnexpectedServerException {
logger.log(Level.INFO, "Preparing json response. ");
logger.log(Level.INFO, "Cursor : " + cursor);
logger.log(Level.INFO, "Result Count : " + resultCount);
DateTime startTime = new DateTime();
JsonArray jsonArray = new JsonArray();
List<String> widgetList = DataAccessorFactory.getDataAccessor().getPersonalizedHomeWidgetList(language);
int intCursor;
try {
intCursor = Integer.parseInt(cursor);
} catch (NumberFormatException e) {
intCursor = 0;
}
logger.log(Level.INFO, "Next Round Cursor : " + (resultCount + intCursor));
int index = 0;
for (String widget : widgetList) {
index = widgetList.indexOf(widget);
if (index < intCursor) {
// Skip if index is less than cursor
index++;
continue;
}
String[] properties = widget.split("~!", -1);
// Line doesn't contain all required parameters.
if (properties.length < 31) {
logger.log(Level.INFO, "Widget properties length : " + properties.length);
continue;
}
JsonObject widgetObj = new JsonObject();
widgetObj.addProperty(POSITION, properties[0]);
widgetObj.addProperty(TEMPLATE_TYPE, properties[2]);
if (properties[3] != null && !properties[3].isEmpty())
widgetObj.addProperty(BACKGROUND_COLOR, properties[3]);
widgetObj.addProperty(EXPERIMENT_ID, properties[4]);
// Title
if (properties[5] != null && !properties[5].isEmpty()) {
JsonObject titleObj = prepareTextElementJson(// Title Text
properties[5], // Title text size
properties[6], // Title text color
properties[7], // Title image url mostly used for AUTHOR_CONTENT widget type
properties[8]);
widgetObj.add(TITLE, titleObj);
}
// Subtitle
if (properties[9] != null && !properties[9].isEmpty()) {
JsonObject subtitleObj = prepareTextElementJson(// Subtitle Text
properties[9], // Subtitle text size
properties[10], // Subtitle text color
properties[11], // Subtitle image url
null);
widgetObj.add(SUBTITLE, subtitleObj);
}
// Click event of the widget
if (properties[12] != null && !properties[12].isEmpty()) {
widgetObj.addProperty(DESTINATION_TYPE, properties[12]);
widgetObj.addProperty(DESTINATION_PARAMS, properties[13]);
}
// Content list
if (properties[14] != null && !properties[14].isEmpty()) {
JsonArray array = new JsonArray();
if (properties[14].equals(SourceType.CURATED_CONTENT_LIST.toString())) {
array = prepareCurratedContentListJson(language, properties[15], Integer.parseInt(properties[16]));
} else if (properties[14].equals(SourceType.USER_LIST.toString())) {
// 1/9/17 fetch recommendation list.
DataListCursorTuple<AuthorData> recommendedAuthorList = AuthorDataUtil.getRecommendedAuthorList(AccessTokenFilter.getAccessToken().getUserId(), language, null, 8);
array = prepareRecommendedUserListJson(recommendedAuthorList.getDataList());
logger.log(Level.INFO, "Cursor : " + recommendedAuthorList.getCursor());
logger.log(Level.INFO, "Number of users. : " + array.size());
widgetObj.addProperty(CURSOR, recommendedAuthorList.getCursor());
}
widgetObj.add(DATA_LIST, array);
}
if (properties[17] != null && !properties[17].isEmpty()) {
widgetObj.addProperty(IMAGE_URL, properties[17]);
}
if (properties[18] != null && !properties[18].isEmpty()) {
widgetObj.addProperty(MESSAGE, properties[18]);
}
// Button one
if (properties[19] != null && !properties[19].isEmpty()) {
JsonObject button1Obj = prepareElementJson(// Button Title
properties[19], // Button text size
properties[20], // Button text color
properties[21], // Button Background color
properties[22], // Button Border color
properties[23], // Button image url
properties[24]);
widgetObj.add(POSITIVE_BUTTON, button1Obj);
}
// Button two
if (properties[25] != null && !properties[25].isEmpty()) {
JsonObject buttonObj = prepareElementJson(// Button Title
properties[25], // Button text size
properties[26], // Button text color
properties[27], // Button Background color
properties[28], // Button Border color
properties[29], // Button image url
properties[30]);
widgetObj.add(NEGATIVE_BUTTON, buttonObj);
}
jsonArray.add(widgetObj);
// Break loop when number of number of elements added to jsonArray is equal to result count
if (jsonArray.size() == resultCount)
break;
}
DateTime endTime = new DateTime();
logger.log(Level.INFO, "Time Taken (in Millis) : " + (endTime.getMillis() - startTime.getMillis()));
logger.log(Level.INFO, "Cursor : " + intCursor + jsonArray.size());
boolean hasMoreData = index != widgetList.size() - 1;
return new PersonalizedInitApi.Response(jsonArray, hasMoreData, (index + 1) + "");
}
Aggregations