-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusertoken.cls
70 lines (62 loc) · 2.01 KB
/
usertoken.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class usertoken {
public static UserTokens__c u;
public static String name;
public static Boolean isAuthorized;
public static String token;
public static String code;
public static String nonce;
public static String token_type;
public static String expires_in;
public static String refresh_token;
public static String service;
public static String MYOB_File_GUID;
// Class initialisation block (executed on first class variable reference)
static {
// If there is an existing UserToken custom object then populate static variables from it
try {
u = [SELECT name,isAuthorized__c, token__c, nonce__c, refresh_token__c, Service_Name__c, MYOB_File_GUID__c FROM UserTokens__c WHERE name = :myob.service];
name = u.name;
isAuthorized = u.isAuthorized__c;
token = u.token__c;
nonce = u.nonce__c;
refresh_token = u.refresh_token__c;
service = u.Service_Name__c;
MYOB_File_GUID = u.MYOB_File_GUID__c;
}
catch (QueryException e) {
// Create new place marker usertoken object
name = myob.service;
service = myob.service;
isAuthorized = false;
nonce = '';
u = new UserTokens__c(Name=myob.service,Service_Name__c = myob.service,isAuthorized__c = false,nonce__c = '');
}
}
public static void write() {
u.token__c = token;
u.isAuthorized__c = isAuthorized;
u.refresh_token__c = refresh_token;
u.MYOB_File_GUID__c = MYOB_File_GUID;
u.token_type__c = token_type;
u.expires_in__c = expires_in;
u.nonce__c = nonce;
// service = u.Service_Name__c;
try {
upsert u Service_Name__c;
}
catch (CalloutException e) {
system.debug('Not possible to write usertoken cache to storage at this time');
}
}
@future
public static void deferredWrite() {
u.token__c = token;
u.isAuthorized__c = isAuthorized;
u.refresh_token__c = refresh_token;
u.MYOB_File_GUID__c = MYOB_File_GUID;
u.token_type__c = token_type;
u.expires_in__c = expires_in;
u.nonce__c = nonce;
upsert u Service_Name__c;
}
}