shuklavatsal1992

XeroSettings

Dec 22nd, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /*
  2. * Created by Vatsal Shukla on 16-Dec-2019
  3. *
  4. * Descriptioin:-
  5. * this class is created to fetch & insert Access_Token & refresh_token & client_id & client_secrate
  6. *
  7. */
  8.  
  9. public class XeroSettings {
  10. //---- Properties---------
  11. private Xero_Setting__c settings;
  12.  
  13. public String tenantId {
  14. get {
  15. return this.tenantId;
  16. }
  17. set {
  18. System.debug('MARK:==> XeroSettings --> tenantId --'+value);
  19. this.tenantId = value;
  20. }
  21. }
  22.  
  23. public String accessToken {
  24. get {
  25. return this.accessToken;
  26. }
  27. set {
  28. System.debug('MARK:==> XeroSettings --> accessToken --'+value);
  29. this.accessToken = value;
  30. }
  31. }
  32.  
  33. public String authCode {
  34. get {
  35. return this.authCode;
  36. }
  37. set {
  38. System.debug('MARK:==> XeroSettings --> authCode --'+value);
  39. this.authCode = value;
  40. }
  41. }
  42.  
  43. public String clientId {
  44. get {
  45. return this.clientId;
  46. }
  47. set {
  48. System.debug('MARK:==> XeroSettings --> clientId --'+value);
  49. this.clientId = value;
  50. }
  51. }
  52.  
  53. public String clientSecret {
  54. get {
  55. return this.clientSecret;
  56. }
  57. set {
  58. System.debug('MARK:==> XeroSettings --> clientSecret --'+value);
  59. this.clientSecret = value;
  60. }
  61. }
  62.  
  63. public String refreshToken {
  64. get {
  65. return this.refreshToken;
  66. }
  67. set {
  68. System.debug('MARK:==> XeroSettings --> refreshToken --'+value);
  69. this.refreshToken = value;
  70. }
  71. }
  72.  
  73. public Datetime expiryDate {
  74. get {
  75. return this.expiryDate;
  76. }
  77. set {
  78. System.debug('MARK:==> XeroSettings --> expiryDate --'+value);
  79. this.expiryDate = value;
  80. }
  81. }
  82.  
  83. public Double expirySeconds {
  84. get {
  85. return this.expirySeconds;
  86. }
  87. set {
  88.  
  89. System.debug('MARK:==> XeroSettings --> expirySeconds --'+value);
  90. this.expirySeconds = value == null ? 0:value;
  91. this.calculateExpiryDate(this.expirySeconds);
  92. }
  93. }
  94.  
  95. //MARK:- Constructor
  96. public XeroSettings() {
  97. this.settings = [SELECT Id,Name,Xero_Tenant_Id__c,Access_Token__c,authCode__c,Client_Id__c,Client_secret__c,Refresh_Token__c,expired_in_seconds__c,Expiry_Date__c FROM Xero_Setting__c limit 1];
  98.  
  99. this.accessToken = this.settings.Access_Token__c;
  100. this.tenantId = this.settings.Xero_Tenant_Id__c;
  101. this.authCode = this.settings.authCode__c;
  102. this.clientId = this.settings.Client_Id__c;
  103. this.clientSecret = this.settings.Client_secret__c;
  104. this.refreshToken = this.settings.Refresh_Token__c;
  105. this.expirySeconds = this.settings.expired_in_seconds__c;
  106. this.expiryDate = this.settings.Expiry_Date__c;
  107. }
  108.  
  109. //MARK:-
  110. public void save() {
  111. this.settings.expired_in_seconds__c = this.expirySeconds;
  112. this.settings.Access_Token__c = this.accessToken;
  113. this.settings.Refresh_Token__c = this.refreshToken;
  114. this.settings.Expiry_Date__c = this.expiryDate;
  115. UPDATE this.settings;
  116. }
  117.  
  118. public void addTenant(Tenant tnnt) {
  119. this.tenantId = tnnt.tenantId;
  120. System.debug('MARK:==> XeroSettings --> addTenant() --'+this.tenantId);
  121. }
  122.  
  123. public void printSettings() {
  124. System.debug('MARK:==> XeroSettings = '+this.settings);
  125. }
  126.  
  127. public Boolean checkTokenExpiredOrNot() {
  128. DateTime dt = this.expiryDate;
  129. if (dt > System.now()) {
  130. System.debug('MARK:==> XeroSetting - checkTokenExpiredOrNot() - not expired(false)');
  131. return false;
  132. } else {
  133. System.debug('MARK:==> XeroSetting - checkTokenExpiredOrNot() - expired(true)');
  134. return true;
  135. }
  136. }
  137.  
  138. private void calculateExpiryDate(Double seconds) {
  139. if(seconds == 0) {
  140. return;
  141. }
  142. Double o = seconds;
  143. Integer expSeconds = Integer.valueOf(o);
  144. Datetime nextDatetime = System.now().addSeconds(expSeconds);
  145. this.expiryDate = nextDatetime;
  146. }
  147. }
Add Comment
Please, Sign In to add comment