Skip to content

Commit

Permalink
#318 - refactored ConfigService
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Dec 30, 2024
1 parent fcb444a commit 5d5a10d
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public interface ConfigService {
* Save Config.
*
* @param siteId Optional Grouping siteId
* @param obj {@link Map}
* @param config {@link SiteConfiguration}
* @return boolean
*/
void save(String siteId, Map<String, Object> obj);
boolean save(String siteId, SiteConfiguration config);

/**
* Increment Key for SiteId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.formkiq.aws.dynamodb.SiteIdKeyGenerator.DEFAULT_SITE_ID;
import static com.formkiq.aws.dynamodb.SiteIdKeyGenerator.isDefaultSiteId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -36,6 +37,7 @@
import com.formkiq.aws.dynamodb.DynamoDbService;
import com.formkiq.aws.dynamodb.DynamoDbServiceImpl;
import com.formkiq.aws.dynamodb.MapToAttributeValue;
import com.formkiq.aws.dynamodb.objects.Strings;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/** Implementation of the {@link ConfigService}. */
Expand Down Expand Up @@ -81,18 +83,38 @@ public Map<String, Object> get(final String siteId) {
}

@Override
public void save(final String siteId, final Map<String, Object> map) {
public boolean save(final String siteId, final SiteConfiguration config) {

Map<String, Object> map = new HashMap<>();
put(map, CHATGPT_API_KEY, config.getChatGptApiKey());
put(map, MAX_DOCUMENT_SIZE_BYTES, config.getMaxContentLengthBytes());
put(map, MAX_DOCUMENTS, config.getMaxDocuments());
put(map, MAX_WEBHOOKS, config.getMaxWebhooks());
put(map, NOTIFICATION_EMAIL, config.getNotificationEmail());
put(map, DOCUMENT_TIME_TO_LIVE, config.getDocumentTimeToLive());
put(map, WEBHOOK_TIME_TO_LIVE, config.getWebhookTimeToLive());

updateGoogle(config, map);

updateOcr(config, map);

updateDocusign(config, map);

Map<String, AttributeValue> item =
keysGeneric(null, PREFIX_CONFIG, siteId != null ? siteId : DEFAULT_SITE_ID);

Map<String, AttributeValue> values = new MapToAttributeValue().apply(map);
item.putAll(values);

if (this.db.exists(item.get(PK), item.get(SK))) {
this.db.updateValues(item.get(PK), item.get(SK), values);
} else {
this.db.putItem(item);
if (!map.isEmpty()) {
if (this.db.exists(item.get(PK), item.get(SK))) {
this.db.updateValues(item.get(PK), item.get(SK), values);
} else {
this.db.putItem(item);
}
}

return !map.isEmpty();
}

@Override
Expand All @@ -113,4 +135,65 @@ public long getIncrement(final String siteId, final String key) {
Map<String, AttributeValue> values = this.db.get(keys.get(PK), keys.get(SK));
return !values.isEmpty() ? Long.parseLong(values.get("Number").n()) : -1;
}

private void updateDocusign(final SiteConfiguration config, final Map<String, Object> map) {

SiteConfigurationDocusign docusign = config.getDocusign();
if (docusign != null) {

String docusignUserId = docusign.getUserId();
if (!Strings.isEmpty(docusignUserId)) {
map.put(KEY_DOCUSIGN_USER_ID, docusignUserId.trim());
}

String docusignIntegrationKey = docusign.getIntegrationKey();
if (!Strings.isEmpty(docusignIntegrationKey)) {
map.put(KEY_DOCUSIGN_INTEGRATION_KEY, docusignIntegrationKey.trim());
}

String docusignRsaPrivateKey = docusign.getRsaPrivateKey();
if (!Strings.isEmpty(docusignRsaPrivateKey)) {
map.put(KEY_DOCUSIGN_RSA_PRIVATE_KEY, docusignRsaPrivateKey.trim());
}

String docusignHmacSignature = docusign.getHmacSignature();
if (!Strings.isEmpty(docusignHmacSignature)) {
map.put(KEY_DOCUSIGN_HMAC_SIGNATURE, docusignHmacSignature.trim());
}
}
}

private void updateOcr(final SiteConfiguration config, final Map<String, Object> map) {
SiteConfigurationOcr ocr = config.getOcr();

if (ocr != null) {
long maxTransactions = ocr.getMaxTransactions();
long maxPagesPerTransaction = ocr.getMaxPagesPerTransaction();
map.put("maxTransactions", maxTransactions != 0 ? maxTransactions : -1);
map.put("maxPagesPerTransaction", maxPagesPerTransaction != 0 ? maxPagesPerTransaction : -1);
}
}

private void updateGoogle(final SiteConfiguration config, final Map<String, Object> map) {
SiteConfigurationGoogle google = config.getGoogle();
if (google != null) {

String workloadIdentityAudience = google.getWorkloadIdentityAudience();
String workloadIdentityServiceAccount = google.getWorkloadIdentityServiceAccount();

if (!Strings.isEmpty(workloadIdentityAudience)) {
map.put("googleWorkloadIdentityAudience", workloadIdentityAudience);
}

if (!Strings.isEmpty(workloadIdentityServiceAccount)) {
map.put("googleWorkloadIdentityServiceAccount", workloadIdentityServiceAccount);
}
}
}

private void put(final Map<String, Object> map, final String mapKey, final String value) {
if (value != null) {
map.put(mapKey, value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.handler;
package com.formkiq.stacks.dynamodb.config;

import com.formkiq.graalvm.annotations.Reflectable;

Expand All @@ -46,6 +46,10 @@ public class SiteConfiguration {
private SiteConfigurationGoogle google;
/** Docusign Configuration. */
private SiteConfigurationDocusign docusign;
/** Document Time to Live. */
private String documentTimeToLive;
/** Webhook Time to Live. */
private String webhookTimeToLive;

/**
* constructor.
Expand All @@ -54,67 +58,93 @@ public SiteConfiguration() {

}

public String getWebhookTimeToLive() {
return this.webhookTimeToLive;
}

public SiteConfiguration setWebhookTimeToLive(final String ttl) {
this.webhookTimeToLive = ttl;
return this;
}

public String getDocumentTimeToLive() {
return this.documentTimeToLive;
}

public SiteConfiguration setDocumentTimeToLive(final String ttl) {
this.documentTimeToLive = ttl;
return this;
}

public SiteConfigurationDocusign getDocusign() {
return this.docusign;
}

public void setDocusign(final SiteConfigurationDocusign docusignConfig) {
public SiteConfiguration setDocusign(final SiteConfigurationDocusign docusignConfig) {
this.docusign = docusignConfig;
return this;
}

public SiteConfigurationGoogle getGoogle() {
return this.google;
}

public void setGoogle(final SiteConfigurationGoogle googleConfig) {
public SiteConfiguration setGoogle(final SiteConfigurationGoogle googleConfig) {
this.google = googleConfig;
return this;
}

public SiteConfigurationOcr getOcr() {
return ocr;
}

public void setOcr(final SiteConfigurationOcr ocrConfig) {
public SiteConfiguration setOcr(final SiteConfigurationOcr ocrConfig) {
this.ocr = ocrConfig;
return this;
}

public String getNotificationEmail() {
return notificationEmail;
}

public void setNotificationEmail(final String email) {
public SiteConfiguration setNotificationEmail(final String email) {
this.notificationEmail = email;
return this;
}

public String getMaxWebhooks() {
return maxWebhooks;
}

public void setMaxWebhooks(final String max) {
public SiteConfiguration setMaxWebhooks(final String max) {
this.maxWebhooks = max;
return this;
}

public String getMaxDocuments() {
return maxDocuments;
}

public void setMaxDocuments(final String max) {
public SiteConfiguration setMaxDocuments(final String max) {
this.maxDocuments = max;
return this;
}

public String getMaxContentLengthBytes() {
return maxContentLengthBytes;
}

public void setMaxContentLengthBytes(final String max) {
public SiteConfiguration setMaxContentLengthBytes(final String max) {
this.maxContentLengthBytes = max;
return this;
}

public String getChatGptApiKey() {
return chatGptApiKey;
}

public void setChatGptApiKey(final String apiKey) {
public SiteConfiguration setChatGptApiKey(final String apiKey) {
this.chatGptApiKey = apiKey;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.handler;
package com.formkiq.stacks.dynamodb.config;

import com.formkiq.graalvm.annotations.Reflectable;

Expand Down Expand Up @@ -57,11 +57,13 @@ public String getUserId() {

/**
* Set User Id.
*
*
* @param user {@link String}
* @return SiteConfigurationDocusign
*/
public void setUserId(final String user) {
public SiteConfigurationDocusign setUserId(final String user) {
this.userId = user;
return this;
}

/**
Expand All @@ -75,11 +77,13 @@ public String getIntegrationKey() {

/**
* Set Integration Key.
*
*
* @param key {@link String}
* @return SiteConfigurationDocusign
*/
public void setIntegrationKey(final String key) {
public SiteConfigurationDocusign setIntegrationKey(final String key) {
this.integrationKey = key;
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.handler;
package com.formkiq.stacks.dynamodb.config;

import com.formkiq.graalvm.annotations.Reflectable;

Expand Down Expand Up @@ -51,11 +51,13 @@ public String getWorkloadIdentityAudience() {

/**
* Set WorkloadIdentityAudience.
*
*
* @param identityAudience {@link String}
* @return SiteConfigurationGoogle
*/
public void setWorkloadIdentityAudience(final String identityAudience) {
public SiteConfigurationGoogle setWorkloadIdentityAudience(final String identityAudience) {
this.workloadIdentityAudience = identityAudience;
return this;
}

/**
Expand All @@ -69,10 +71,12 @@ public String getWorkloadIdentityServiceAccount() {

/**
* Set WorkloadIdentityServiceAccount.
*
*
* @param serviceAccount {@link String}
* @return SiteConfigurationGoogle
*/
public void setWorkloadIdentityServiceAccount(final String serviceAccount) {
public SiteConfigurationGoogle setWorkloadIdentityServiceAccount(final String serviceAccount) {
this.workloadIdentityServiceAccount = serviceAccount;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.handler;
package com.formkiq.stacks.dynamodb.config;

import com.formkiq.graalvm.annotations.Reflectable;

Expand Down Expand Up @@ -51,11 +51,13 @@ public long getMaxPagesPerTransaction() {

/**
* Set Max Pages Per Transaction.
*
*
* @param maxPages long
* @return SiteConfigurationOcr
*/
public void setMaxPagesPerTransaction(final long maxPages) {
public SiteConfigurationOcr setMaxPagesPerTransaction(final long maxPages) {
this.maxPagesPerTransaction = maxPages;
return this;
}

/**
Expand All @@ -69,10 +71,12 @@ public long getMaxTransactions() {

/**
* Set Max Transactions.
*
*
* @param maxTx long
* @return SiteConfigurationOcr
*/
public void setMaxTransactions(final long maxTx) {
public SiteConfigurationOcr setMaxTransactions(final long maxTx) {
this.maxTransactions = maxTx;
return this;
}
}
Loading

0 comments on commit 5d5a10d

Please sign in to comment.