Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jun 10, 2024
1 parent 8b85735 commit 90f6d11
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
*/
package com.formkiq.aws.dynamodb;

import com.formkiq.aws.dynamodb.objects.DateUtil;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -64,16 +61,6 @@ public interface DynamodbVersionRecord<T> extends DynamodbRecord<T> {
*/
String skVersion();

/**
* Get Inserted Date {@link String}.
*
* @return {@link String}
*/
default String getInsertedDate() {
SimpleDateFormat df = DateUtil.getIsoDateFormatter();
return df.format(new Date());
}

/**
* Update Attributes to Version.
*
Expand Down
20 changes: 16 additions & 4 deletions docs/openapi/openapi-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6442,6 +6442,9 @@
description: Attribute with boolean value
valueType:
$ref: '#/components/schemas/AttributeValueType'
insertedDate:
type: string
description: Inserted Timestamp
DocumentSearchMeta:
type: object
properties:
Expand Down Expand Up @@ -6994,6 +6997,9 @@
booleanValue:
type: boolean
description: Attribute with boolean value
insertedDate:
type: string
description: Inserted Timestamp
DocumentTag:
type: object
properties:
Expand Down Expand Up @@ -9878,10 +9884,16 @@
description: Message response
securitySchemes:
ApiAuthorization:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: awsSigv4
type: oauth2
flows: {}
x-amazon-apigateway-authorizer:
type: jwt
jwtConfiguration:
issuer:
Fn::Sub: https://cognito-idp.${AWS::Region}.amazonaws.com/${CognitoUserPool}
audience:
- Fn::Sub: ${CognitoUserPoolClient}
identitySource: $request.header.Authorization
x-amazon-apigateway-integrations:
lambdaApi200:
uri:
Expand Down
6 changes: 6 additions & 0 deletions docs/openapi/openapi-jwt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6442,6 +6442,9 @@
description: Attribute with boolean value
valueType:
$ref: '#/components/schemas/AttributeValueType'
insertedDate:
type: string
description: Inserted Timestamp
DocumentSearchMeta:
type: object
properties:
Expand Down Expand Up @@ -6994,6 +6997,9 @@
booleanValue:
type: boolean
description: Attribute with boolean value
insertedDate:
type: string
description: Inserted Timestamp
DocumentTag:
type: object
properties:
Expand Down
24 changes: 14 additions & 10 deletions docs/openapi/openapi-key.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6442,6 +6442,9 @@
description: Attribute with boolean value
valueType:
$ref: '#/components/schemas/AttributeValueType'
insertedDate:
type: string
description: Inserted Timestamp
DocumentSearchMeta:
type: object
properties:
Expand Down Expand Up @@ -6994,6 +6997,9 @@
booleanValue:
type: boolean
description: Attribute with boolean value
insertedDate:
type: string
description: Inserted Timestamp
DocumentTag:
type: object
properties:
Expand Down Expand Up @@ -9878,18 +9884,16 @@
description: Message response
securitySchemes:
ApiAuthorization:
type: apiKey
name: Authorization
in: header
x-amazon-apigateway-authtype: Custom API Key
type: oauth2
flows: {}
x-amazon-apigateway-authorizer:
type: jwt
jwtConfiguration:
issuer:
Fn::Sub: https://cognito-idp.${AWS::Region}.amazonaws.com/${CognitoUserPool}
audience:
- Fn::Sub: ${CognitoUserPoolClient}
identitySource: $request.header.Authorization
authorizerUri:
Fn::Sub: arn:${Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ApiKeyAuthorizer.Arn}/invocations
authorizerResultTtlInSeconds: 300
authorizerPayloadFormatVersion: "2.0"
enableSimpleResponses: true
type: request
x-amazon-apigateway-integrations:
lambdaApi200:
uri:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import com.formkiq.stacks.dynamodb.attributes.DynamicObjectToDocumentAttributeRecord;
import com.formkiq.stacks.dynamodb.schemas.Schema;
import com.formkiq.stacks.dynamodb.schemas.SchemaAttributesCompositeKey;
import com.formkiq.stacks.dynamodb.schemas.SchemaMissingRequiredAttributes;
import com.formkiq.stacks.dynamodb.schemas.SchemaService;
import com.formkiq.stacks.dynamodb.schemas.SchemaServiceDynamodb;
import com.formkiq.validation.ValidationError;
Expand Down Expand Up @@ -252,8 +253,12 @@ private void appendDocumentAttributes(final WriteRequestBuilder writeBuilder, fi

Schema schema = getSchame(siteId);

Collection<DocumentAttributeRecord> documentAttributes =
addCompositeKeys(siteId, documentId, documentAttributeRecords, schema);
Collection<DocumentAttributeRecord> documentAttributes = addCompositeAndRequiredKeys(siteId,
documentId, documentAttributeRecords, schema, validationAccess);

Date date = new Date();
documentAttributeRecords.stream().filter(a -> a.getInsertedDate() == null)
.forEach(a -> a.setInsertedDate(date));

Map<String, AttributeRecord> attributeMap = validateDocumentAttributes(schema, siteId,
documentId, documentAttributes, isUpdate, validation, validationAccess);
Expand Down Expand Up @@ -291,14 +296,33 @@ private void appendDocumentAttributes(final WriteRequestBuilder writeBuilder, fi
}
}

private Collection<DocumentAttributeRecord> addCompositeKeys(final String siteId,
private Collection<DocumentAttributeRecord> addMissingRequired(final Schema schema,
final String siteId, final String documentId,
final Collection<DocumentAttributeRecord> documentAttributes) {

return new SchemaMissingRequiredAttributes(attributeService, schema, siteId, documentId)
.apply(documentAttributes);
}

private Collection<DocumentAttributeRecord> addCompositeAndRequiredKeys(final String siteId,
final String documentId, final Collection<DocumentAttributeRecord> documentAttributeRecords,
final Schema schema) {
final Schema schema, final AttributeValidationAccess validationAccess) {

Collection<DocumentAttributeRecord> documentAttributes = documentAttributeRecords;

if (schema != null) {

if (AttributeValidationAccess.ADMIN_CREATE.equals(validationAccess)
|| AttributeValidationAccess.CREATE.equals(validationAccess)) {
Collection<DocumentAttributeRecord> missingRequired =
addMissingRequired(schema, siteId, documentId, documentAttributes);

if (!missingRequired.isEmpty()) {
documentAttributes =
Stream.concat(documentAttributes.stream(), missingRequired.stream()).toList();
}
}

Collection<DocumentAttributeRecord> compositeKeys =
generateCompositeKeys(schema, siteId, documentId, documentAttributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
import static com.formkiq.aws.dynamodb.SiteIdKeyGenerator.createDatabaseKey;
import static com.formkiq.aws.dynamodb.objects.Objects.formatDouble;
import static com.formkiq.stacks.dynamodb.attributes.AttributeRecord.ATTR;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.formkiq.aws.dynamodb.DbKeys;
import com.formkiq.aws.dynamodb.DynamodbVersionRecord;
import com.formkiq.aws.dynamodb.objects.DateUtil;
import com.formkiq.aws.dynamodb.objects.Strings;
import com.formkiq.graalvm.annotations.Reflectable;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
Expand All @@ -43,6 +48,8 @@
public class DocumentAttributeRecord
implements DynamodbVersionRecord<DocumentAttributeRecord>, DbKeys {

/** {@link SimpleDateFormat}. */
private final SimpleDateFormat df = DateUtil.getIsoDateFormatter();
/** Boolean value. */
@Reflectable
private Boolean booleanValue;
Expand All @@ -61,6 +68,9 @@ public class DocumentAttributeRecord
/** Type of Attribute. */
@Reflectable
private DocumentAttributeValueType valueType;
/** Inserted Date. */
@Reflectable
private Date insertedDate;

/**
* constructor.
Expand Down Expand Up @@ -102,6 +112,10 @@ public Map<String, AttributeValue> getDataAttributes() {
map.put("stringValue", fromS(this.stringValue));
}

if (this.insertedDate != null) {
map.put("inserteddate", AttributeValue.fromS(df.format(this.insertedDate)));
}

return map;
}

Expand All @@ -128,6 +142,15 @@ record =
if (attrs.containsKey("numberValue")) {
record.numberValue(nn(attrs, "numberValue"));
}

if (attrs.containsKey("inserteddate")) {
try {
record = record.setInsertedDate(df.parse(ss(attrs, "inserteddate")));
} catch (ParseException e) {
// ignore
}
}

}

return record;
Expand Down Expand Up @@ -343,6 +366,26 @@ public DocumentAttributeRecord updateValueType() {
return this;
}

/**
* Get Inserted Date.
*
* @return {@link Date}
*/
public Date getInsertedDate() {
return this.insertedDate;
}

/**
* Set Inserted Date.
*
* @param date {@link Date}
* @return {@link DocumentAttributeRecord}
*/
public DocumentAttributeRecord setInsertedDate(final Date date) {
this.insertedDate = date;
return this;
}

@Override
public String pkVersion(final String siteId) {
return pk(siteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.formkiq.stacks.api.transformers;
package com.formkiq.stacks.dynamodb.schemas;

import static com.formkiq.aws.dynamodb.objects.Objects.notNull;
import static com.formkiq.aws.dynamodb.objects.Strings.isEmpty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand All @@ -38,8 +37,6 @@
import com.formkiq.stacks.dynamodb.attributes.AttributeRecord;
import com.formkiq.stacks.dynamodb.attributes.AttributeService;
import com.formkiq.stacks.dynamodb.attributes.DocumentAttributeRecord;
import com.formkiq.stacks.dynamodb.schemas.Schema;
import com.formkiq.stacks.dynamodb.schemas.SchemaAttributesRequired;

/**
* Generates a {@link Collection} of missing required {@link DocumentAttributeRecord}.
Expand All @@ -48,13 +45,13 @@ public class SchemaMissingRequiredAttributes
implements Function<Collection<DocumentAttributeRecord>, Collection<DocumentAttributeRecord>> {

/** Document Id. */
private String docId;
private final String docId;
/** Schema. */
private Schema schema;
private final Schema schema;
/** {@link AttributeService}. */
private AttributeService service;
private final AttributeService service;
/** SiteId. */
private String site;
private final String site;

/**
* constructor.
Expand All @@ -78,15 +75,16 @@ public Collection<DocumentAttributeRecord> apply(final Collection<DocumentAttrib
List<DocumentAttributeRecord> missingAttributes = Collections.emptyList();

if (this.schema != null) {
Set<String> keys = c.stream().map(a -> a.getKey()).collect(Collectors.toSet());
Set<String> keys =
c.stream().map(DocumentAttributeRecord::getKey).collect(Collectors.toSet());

List<SchemaAttributesRequired> requiredAttributes = this.schema.getAttributes().getRequired()
.stream().filter(a -> !keys.contains(a.getAttributeKey())).toList();

if (!requiredAttributes.isEmpty()) {

List<String> attributeKeys =
requiredAttributes.stream().map(a -> a.getAttributeKey()).toList();
requiredAttributes.stream().map(SchemaAttributesRequired::getAttributeKey).toList();

Map<String, AttributeRecord> attributeMap =
this.service.getAttributes(this.site, attributeKeys);
Expand Down Expand Up @@ -126,7 +124,7 @@ private Collection<DocumentAttributeRecord> createRequiredAttributes(

List<String> values =
!notNull(attribute.getDefaultValues()).isEmpty() ? attribute.getDefaultValues()
: Arrays.asList(attribute.getDefaultValue());
: Collections.singletonList(attribute.getDefaultValue());

for (String value : values) {

Expand Down
Loading

0 comments on commit 90f6d11

Please sign in to comment.