/*
* Copyright (c) 2014 Jacob D. Parr
*
* This software may not be used without permission.
*/
package com.cosmicpush.app.resources.api;
import com.cosmicpush.app.domain.accounts.Account;
import com.cosmicpush.app.domain.clients.ApiClient;
import com.cosmicpush.app.domain.clients.config.GoogleTalkConfig;
import com.cosmicpush.app.domain.requests.*;
import com.cosmicpush.app.resources.api.jabber.JabberFactory;
import com.cosmicpush.app.system.CpServerObjectMapper;
import com.cosmicpush.pub.common.RequestStatus;
import com.cosmicpush.pub.push.NotificationPush;
import org.crazyyak.dev.common.StringUtils;
import org.crazyyak.dev.common.exceptions.ExceptionUtils;
public class NotificationDelegate extends AbstractDelegate {
private final Account account;
private final ApiClient apiClient;
private final NotificationPush push;
public NotificationDelegate(CpServerObjectMapper objectMapper, ApiRequestStore apiRequestStore, Account account, ApiClient apiClient, ApiRequest apiRequest, NotificationPush push) {
super(objectMapper, apiRequest, apiRequestStore);
this.push = ExceptionUtils.assertNotNull(push, "push");
this.account = ExceptionUtils.assertNotNull(account, "account");
this.apiClient = ExceptionUtils.assertNotNull(apiClient, "apiClient");
}
@Override
public synchronized RequestStatus processRequest() throws Exception {
String reasonNotPermitted = account.getReasonNotPermitted(push);
if (StringUtils.isNotBlank(reasonNotPermitted)) {
return apiRequest.denyRequest(reasonNotPermitted);
}
GoogleTalkConfig gtConfig = apiClient.getGoogleTalkConfig();
if (gtConfig == null) {
return apiRequest.failed("Google Talk configuration was not specified.");
} else {
JabberFactory factory = new JabberFactory(gtConfig.getUserName(), gtConfig.getPassword());
String id = apiRequest.getApiRequestId();
id = id.substring(id.indexOf(":")+1);
String message = push.getMessage() + " >> " + "https://www.cosmicpush.com/q/" + id;
factory.sendTo("jacob.parr@gmail.com", message);
return apiRequest.processed();
}
}
}