Search in sources :

Example 11 with TypeToken

use of com.google.gson.reflect.TypeToken in project pratilipi by Pratilipi.

the class BatchProcessDataUtil method _execStateValidateNotificationCount.

private static void _execStateValidateNotificationCount(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    String createdBy = "BATCH_PROCESS::" + batchProcess.getId();
    Map<Long, Long> userIdNotifIdMap = processDoc.getData(BatchProcessState.VALIDATE_NOTIFICATION_COUNT.getInputName(), new TypeToken<Map<Long, Long>>() {
    }.getType());
    int notifCount = dataAccessor.getNotificationCout(createdBy);
    if (userIdNotifIdMap.size() == notifCount)
        batchProcess.setStateCompleted(BatchProcessState.VALIDATE_NOTIFICATION_COUNT);
    else
        logger.log(Level.INFO, "Validation failed !");
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) DataAccessor(com.pratilipi.data.DataAccessor) TypeToken(com.google.gson.reflect.TypeToken) DocAccessor(com.pratilipi.data.DocAccessor)

Example 12 with TypeToken

use of com.google.gson.reflect.TypeToken in project pratilipi by Pratilipi.

the class BatchProcessDataUtil method _execStateCreateNotificationsForUserIds.

private static void _execStateCreateNotificationsForUserIds(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    Set<Long> userIdSet = processDoc.getData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getInputName(), new TypeToken<Set<Long>>() {
    }.getType());
    Map<Long, Long> userIdNotifIdMap = processDoc.getData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), new TypeToken<Map<Long, Long>>() {
    }.getType());
    JsonObject execDoc = new Gson().fromJson(batchProcess.getExecDoc(), JsonElement.class).getAsJsonObject();
    NotificationType type = NotificationType.valueOf(execDoc.get("type").getAsString());
    String sourceId = execDoc.get("sourceId").getAsString();
    String createdBy = "BATCH_PROCESS::" + batchProcess.getId();
    if (userIdNotifIdMap == null) {
        // First attempt on this state
        userIdNotifIdMap = new HashMap<>();
    } else {
        for (Entry<Long, Long> entry : userIdNotifIdMap.entrySet()) {
            if (entry.getValue() != 0L) {
                userIdSet.remove(entry.getKey());
                continue;
            }
            Notification notification = dataAccessor.getNotification(entry.getKey(), type, sourceId, createdBy);
            if (notification != null) {
                entry.setValue(notification.getId());
                userIdSet.remove(entry.getKey());
            }
        }
    }
    while (!userIdSet.isEmpty()) {
        List<Long> userIdList = new ArrayList<>(100);
        for (Long userId : userIdSet) {
            // Can't put null (instead of 0) here because Gson ignores keys with null values
            userIdNotifIdMap.put(userId, 0L);
            userIdList.add(userId);
            if (// Limiting to 100 users per run
            userIdList.size() == 100)
                break;
        }
        userIdSet.removeAll(userIdList);
        processDoc.setData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), userIdNotifIdMap);
        // Saving Doc
        docAccessor.save(batchProcess.getId(), processDoc);
        List<Notification> notifList = new ArrayList<>(userIdList.size());
        for (Long userId : userIdList) notifList.add(dataAccessor.newNotification(userId, type, sourceId, createdBy));
        notifList = dataAccessor.createOrUpdateNotificationList(notifList);
        for (Notification notif : notifList) userIdNotifIdMap.put(notif.getUserId(), notif.getId());
    }
    processDoc.setData(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS.getOutputName(), userIdNotifIdMap);
    // Saving Doc
    docAccessor.save(batchProcess.getId(), processDoc);
    batchProcess.setStateCompleted(BatchProcessState.CREATE_NOTIFICATIONS_FOR_USER_IDS);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Notification(com.pratilipi.data.type.Notification) BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) TypeToken(com.google.gson.reflect.TypeToken) JsonElement(com.google.gson.JsonElement) NotificationType(com.pratilipi.common.type.NotificationType)

Example 13 with TypeToken

use of com.google.gson.reflect.TypeToken in project ETSMobile-Android2 by ApplETS.

the class ETSGcmListenerService method sendNotification.

/**
     * Create and show a simple notification containing the received GCM message.
     *
     * @param data GCM message received.
     */
private void sendNotification(Bundle data) {
    SecurePreferences securePreferences = new SecurePreferences(this);
    Gson gson = new Gson();
    String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");
    ArrayList<MonETSNotification> receivedNotif = gson.fromJson(receivedNotifString, new TypeToken<ArrayList<MonETSNotification>>() {
    }.getType());
    if (receivedNotif == null) {
        receivedNotif = new ArrayList<>();
    }
    MonETSNotification nouvelleNotification = getMonETSNotificationFromBundle(data);
    receivedNotif.add(nouvelleNotification);
    int numberOfNotifications = receivedNotif.size();
    Intent intent = new Intent(this, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
    NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
    // Sets a title for the Inbox in expanded layout
    String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
    inBoxStyle.setBigContentTitle(bigContentTitle);
    String username = ApplicationManager.userCredentials.getUsername();
    Spannable sb = new SpannableString(username);
    sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    inBoxStyle.setSummaryText(sb);
    securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
    int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
    minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
    for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
        inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
    }
    if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
        int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
        String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
        Spannable others = new SpannableString(plusOthersString);
        others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        inBoxStyle.addLine(others);
    }
    mBuilder.setStyle(inBoxStyle);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}
Also used : MonETSNotification(ca.etsmtl.applets.etsmobile.model.MonETSNotification) NotificationManager(android.app.NotificationManager) Gson(com.google.gson.Gson) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) SecurePreferences(ca.etsmtl.applets.etsmobile.util.SecurePreferences) SpannableString(android.text.SpannableString) SpannableString(android.text.SpannableString) Bitmap(android.graphics.Bitmap) TypeToken(com.google.gson.reflect.TypeToken) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Spannable(android.text.Spannable)

Example 14 with TypeToken

use of com.google.gson.reflect.TypeToken in project UltimateAndroid by cymcsg.

the class JsonUtil method getObjectListFromJson.

public static Object getObjectListFromJson(String jsonString) {
    Gson gson = new Gson();
    Object object = gson.fromJson(jsonString, new TypeToken<List<Object>>() {
    }.getType());
    return object;
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson)

Example 15 with TypeToken

use of com.google.gson.reflect.TypeToken in project gerrit by GerritCodeReview.

the class ExternalIdIT method getExternalIdsOfOtherUserWithAccessDatabase.

@Test
public void getExternalIdsOfOtherUserWithAccessDatabase() throws Exception {
    allowGlobalCapabilities(REGISTERED_USERS, GlobalCapability.ACCESS_DATABASE);
    Collection<ExternalId> expectedIds = accountCache.get(admin.getId()).getExternalIds();
    List<AccountExternalIdInfo> expectedIdInfos = toExternalIdInfos(expectedIds);
    RestResponse response = userRestSession.get("/accounts/" + admin.id + "/external.ids");
    response.assertOK();
    List<AccountExternalIdInfo> results = newGson().fromJson(response.getReader(), new TypeToken<List<AccountExternalIdInfo>>() {
    }.getType());
    Collections.sort(expectedIdInfos);
    Collections.sort(results);
    assertThat(results).containsExactlyElementsIn(expectedIdInfos);
}
Also used : RestResponse(com.google.gerrit.acceptance.RestResponse) TypeToken(com.google.gson.reflect.TypeToken) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountExternalIdInfo(com.google.gerrit.extensions.common.AccountExternalIdInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)419 Gson (com.google.gson.Gson)178 Test (org.junit.Test)100 IOException (java.io.IOException)83 Map (java.util.Map)71 List (java.util.List)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 ArrayList (java.util.ArrayList)53 HashMap (java.util.HashMap)53 GsonBuilder (com.google.gson.GsonBuilder)45 File (java.io.File)34 Notebook (org.apache.zeppelin.notebook.Notebook)32 Type (java.lang.reflect.Type)31 FileNotFoundException (java.io.FileNotFoundException)29 Paragraph (org.apache.zeppelin.notebook.Paragraph)27 RestResponse (com.google.gerrit.acceptance.RestResponse)24 JsonElement (com.google.gson.JsonElement)24 JsonObject (com.google.gson.JsonObject)24 OutputStreamWriter (java.io.OutputStreamWriter)22 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21