diff --git a/sdk/src/main/java/com/silanis/esl/api/model/BasePackage.java b/sdk/src/main/java/com/silanis/esl/api/model/BasePackage.java index 2b15dd09a..4f42c19cd 100644 --- a/sdk/src/main/java/com/silanis/esl/api/model/BasePackage.java +++ b/sdk/src/main/java/com/silanis/esl/api/model/BasePackage.java @@ -65,6 +65,8 @@ public class BasePackage extends Entity public static final String FIELD_CREATED = "created"; @JsonIgnore public static final String FIELD_TIMEZONE_ID = "timezoneId"; + @JsonIgnore + private static final String FIELD_CONDITIONS = "conditions"; // Empty Constructor public BasePackage ( ) {} @@ -91,6 +93,7 @@ public BasePackage ( ) {} protected String _visibility = "ACCOUNT"; protected java.util.Date _created; protected String _timezoneId; + protected List conditions; // Accessors @@ -579,4 +582,15 @@ public BasePackage safeSetTimezoneId( String value ){ public String getTimezoneId(){ return _timezoneId; } + + public BasePackage setConditions(List value) { + + this.conditions = value; + setDirty(FIELD_CONDITIONS); + return this; + } + + public List getConditions() { + return conditions; + } } \ No newline at end of file diff --git a/sdk/src/main/java/com/silanis/esl/api/model/ConditionalField.java b/sdk/src/main/java/com/silanis/esl/api/model/ConditionalField.java new file mode 100644 index 000000000..8c34c2105 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/api/model/ConditionalField.java @@ -0,0 +1,28 @@ +package com.silanis.esl.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ConditionalField extends Field { + + @JsonIgnore + private static final String FIELD_CONDITIONS = "conditions"; + + private List conditions; + + public ConditionalField() { } + + public Field setConditions(List value) { + + this.conditions = value; + setDirty(FIELD_CONDITIONS); + return this; + } + + public List getConditions() { + return conditions; + } +} diff --git a/sdk/src/main/java/com/silanis/esl/api/model/FieldCondition.java b/sdk/src/main/java/com/silanis/esl/api/model/FieldCondition.java new file mode 100644 index 000000000..7406e96d8 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/api/model/FieldCondition.java @@ -0,0 +1,72 @@ +package com.silanis.esl.api.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.io.Serializable; + +import static com.silanis.esl.api.util.SchemaSanitizer.throwOnNull; +import static com.silanis.esl.api.util.SchemaSanitizer.trim; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class FieldCondition extends Model implements Serializable { + + @JsonIgnore + public static final String FIELD_ID = "id"; + @JsonIgnore + public static final String FIELD_CONDITION = "condition"; + @JsonIgnore + public static final String FIELD_ACTION = "action"; + + protected String id; + protected String condition; + protected String action; + + public FieldCondition() { + + } + + public FieldCondition setId(String value) { + + throwOnNull(FIELD_ID, value); + value = trim(value); + + this.id = value; + setDirty(FIELD_ID); + return this; + } + + public String getId() { + return id; + } + + public FieldCondition setCondition(String value) { + + throwOnNull(FIELD_CONDITION, value); + value = trim(value); + + this.condition = value; + setDirty(FIELD_CONDITION); + return this; + } + + public String getCondition() { + return condition; + } + + public FieldCondition setAction(String value) { + + throwOnNull(FIELD_ACTION, value); + value = trim(value); + + this.action = value; + setDirty(FIELD_ACTION); + return this; + } + + public String getAction() { + return action; + } +} + + diff --git a/sdk/src/main/java/com/silanis/esl/sdk/ConditionalField.java b/sdk/src/main/java/com/silanis/esl/sdk/ConditionalField.java new file mode 100644 index 000000000..65837a6e8 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/ConditionalField.java @@ -0,0 +1,17 @@ +package com.silanis.esl.sdk; + +import java.io.Serializable; +import java.util.List; + +public class ConditionalField extends Field implements Serializable { + + private List conditions; + + public void setConditions( List conditions ) { + this.conditions = conditions; + } + + public List getConditions() { + return conditions; + } +} diff --git a/sdk/src/main/java/com/silanis/esl/sdk/DocumentPackage.java b/sdk/src/main/java/com/silanis/esl/sdk/DocumentPackage.java index b512244de..e293761ed 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/DocumentPackage.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/DocumentPackage.java @@ -33,6 +33,7 @@ public class DocumentPackage implements Serializable { private List messages; private Visibility visibility; private String timezoneId; + private List conditions; /** * @param name the name of the package @@ -320,4 +321,8 @@ public void setVisibility(Visibility visibility) { public String getTimezoneId() { return timezoneId; } public void setTimezoneId(String timezoneId) { this.timezoneId = timezoneId; } + + public List getConditions() { return conditions; } + + public void setConditions(List conditions) { this.conditions = conditions; } } \ No newline at end of file diff --git a/sdk/src/main/java/com/silanis/esl/sdk/FieldCondition.java b/sdk/src/main/java/com/silanis/esl/sdk/FieldCondition.java new file mode 100644 index 000000000..af73eae2c --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/FieldCondition.java @@ -0,0 +1,37 @@ +package com.silanis.esl.sdk; + +import java.io.Serializable; + +public class FieldCondition implements Serializable { + + private String id; + private String condition; + private String action; + + + public FieldCondition() {} + + public String getId() { + return id; + } + + public void setId( String id ) { + this.id = id; + } + + public String getCondition() { + return condition; + } + + public void setCondition( String condition ) { + this.condition = condition; + } + + public String getAction() { + return action; + } + + public void setAction( String action ) { + this.action = action; + } +} diff --git a/sdk/src/main/java/com/silanis/esl/sdk/builder/PackageBuilder.java b/sdk/src/main/java/com/silanis/esl/sdk/builder/PackageBuilder.java index 0688932d7..546f2cfa9 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/builder/PackageBuilder.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/builder/PackageBuilder.java @@ -27,6 +27,7 @@ public class PackageBuilder { private DocumentPackageAttributes attributes; private Visibility visibility; private String timezoneId; + private final List conditions = new ArrayList(); private static final String ORIGIN_KEY = "origin"; @@ -174,6 +175,10 @@ public DocumentPackage build() { documentPackage.setTimezoneId(timezoneId); } + if (conditions != null && !conditions.isEmpty()) { + documentPackage.setConditions(conditions); + } + return documentPackage; } @@ -347,4 +352,15 @@ public PackageBuilder withOrigin( String origin ) { this.attributes.append(ORIGIN_KEY, origin); return this; } + + /** + * Sets the condition for the package field. + * + * @param condition field condition + * @return the package builder itself + */ + public PackageBuilder withCondition( FieldCondition condition) { + this.conditions.add(condition); + return this; + } } \ No newline at end of file diff --git a/sdk/src/main/java/com/silanis/esl/sdk/examples/ConditionalFieldExample.java b/sdk/src/main/java/com/silanis/esl/sdk/examples/ConditionalFieldExample.java new file mode 100644 index 000000000..b2228973b --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/examples/ConditionalFieldExample.java @@ -0,0 +1,89 @@ +package com.silanis.esl.sdk.examples; + +import com.silanis.esl.sdk.ConditionalField; +import com.silanis.esl.sdk.DocumentPackage; +import com.silanis.esl.sdk.DocumentType; +import com.silanis.esl.sdk.FieldCondition; +import com.silanis.esl.sdk.FieldId; +import com.silanis.esl.sdk.FieldStyle; +import com.silanis.esl.sdk.PackageId; +import com.silanis.esl.sdk.SignatureId; +import com.silanis.esl.sdk.builder.PackageBuilder; +import com.silanis.esl.sdk.builder.SignerBuilder; + +import java.util.ArrayList; +import java.util.List; + +import static com.silanis.esl.sdk.builder.DocumentBuilder.newDocumentWithName; +import static com.silanis.esl.sdk.builder.FieldBuilder.*; +import static com.silanis.esl.sdk.builder.SignatureBuilder.signatureFor; + +public class ConditionalFieldExample extends SDKSample { + + private final String documentId = "DocumentId"; + private final SignatureId signatureId = new SignatureId("signatureId"); + private final FieldId fieldId1 = new FieldId("fieldId1"); + private final FieldId fieldId2 = new FieldId("fieldId2"); + + public DocumentPackage retrievedPackageWithoutConditions; + public DocumentPackage retrievedPackageWithUpdatedConditions; + + public static void main(String... args) { + new ConditionalFieldExample().run(); + } + + @Override + protected void execute() { + FieldCondition condition = new FieldCondition(); + condition.setId("ConditionId"); + condition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + condition.setAction("document['DocumentId'].field['fieldId1'].enabled = true"); + + DocumentPackage superDuperPackage = PackageBuilder.newPackageNamed(getPackageName()) + .describedAs("Description") + .withSigner(SignerBuilder.newSignerWithEmail(email1) + .withFirstName("Patty") + .withLastName("Galant")) + .withDocument(newDocumentWithName("Document") + .withId(documentId) + .fromStream(documentInputStream1, DocumentType.PDF) + .withSignature(signatureFor(email1) + .withId(signatureId) + .onPage(0) + .atPosition(400, 100) + .withField(textField() + .withId(fieldId1) + .onPage(0) + .atPosition(0, 0)) + .withField(checkBox() + .withId(fieldId2) + .onPage(0) + .atPosition(0, 0)))) + .withCondition(condition) + .build(); + + PackageId packageId = eslClient.createPackageOneStep(superDuperPackage); + retrievedPackage = eslClient.getPackage(packageId); + + FieldCondition newCondition = new FieldCondition(); + newCondition.setId("ConditionId"); + newCondition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + newCondition.setAction("document['DocumentId'].field['fieldId1'].enabled = false"); + List conditions = new ArrayList(); + conditions.add(newCondition); + + ConditionalField conditionalField = new ConditionalField(); + conditionalField.setId(fieldId2); + conditionalField.setConditions(conditions); + conditionalField.setStyle(FieldStyle.UNBOUND_CHECK_BOX); + + eslClient.getApprovalService().updateConditionalField(packageId, documentId, signatureId, conditionalField); + retrievedPackageWithUpdatedConditions = eslClient.getPackage(packageId); + + conditions.clear(); + conditionalField.setConditions(conditions); + eslClient.getApprovalService().updateConditionalField(packageId, documentId, signatureId, conditionalField); + retrievedPackageWithoutConditions = eslClient.getPackage(packageId); + + } +} diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/UrlTemplate.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/UrlTemplate.java index 8babea186..843f2eae5 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/internal/UrlTemplate.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/UrlTemplate.java @@ -108,6 +108,7 @@ public class UrlTemplate { public static final String FIELD_PATH = "/packages/{packageId}/documents/{documentId}/approvals/{approvalId}/fields"; public static final String FIELD_ID_PATH = "/packages/{packageId}/documents/{documentId}/approvals/{approvalId}/fields/{fieldId}"; public static final String SIGNABLE_APPROVAL_PATH = "/packages/{packageId}/documents/{documentId}/signers/{signerId}/approvals"; + public static final String CONDITIONAL_FIELD_PATH = "/packages/{packageId}/documents/{documentId}/approvals/{approvalId}/fields/{fieldId}/conditionalFields"; // Provider Document Resource public static final String PROVIDER_DOCUMENTS = "/documents"; diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverter.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverter.java new file mode 100644 index 000000000..3d6ed9ed8 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverter.java @@ -0,0 +1,139 @@ +package com.silanis.esl.sdk.internal.converter; + +import com.silanis.esl.sdk.FieldId; +import com.silanis.esl.sdk.FieldStyle; + +import java.util.ArrayList; +import java.util.List; + +public class ConditionalFieldConverter { + + private com.silanis.esl.sdk.ConditionalField sdkField = null; + private com.silanis.esl.api.model.ConditionalField apiField = null; + + /** + * Construct with API ConditionalField object involved in conversion. + * + * @param apiField + */ + public ConditionalFieldConverter(com.silanis.esl.api.model.ConditionalField apiField) { + this.apiField = apiField; + } + + /** + * Construct with SDK ConditionalField object involved in conversion. + * @param sdkField + */ + public ConditionalFieldConverter(com.silanis.esl.sdk.ConditionalField sdkField) { + this.sdkField = sdkField; + } + + /** + * Convert from SDK conditional field to API conditional field. + * + * @return an API ConditionalField object. + */ + public com.silanis.esl.api.model.ConditionalField toAPIConditionalField() { + if (sdkField == null) { + return apiField; + } + + com.silanis.esl.api.model.ConditionalField result = new com.silanis.esl.api.model.ConditionalField(); + + result.setPage(sdkField.getPage()); + result.setExtract(sdkField.isExtraction()); + if ( sdkField.getName() != null ) { + result.setName( sdkField.getName() ); + } + + if (!sdkField.isExtraction()) { + result.setLeft( sdkField.getX() ); + result.setTop( sdkField.getY() ); + result.setWidth( sdkField.getWidth()); + result.setHeight( sdkField.getHeight()); + } + + result.setValue(sdkField.getValue()); + + if (sdkField.getStyle() == FieldStyle.BOUND_QRCODE) { + result.setType("IMAGE"); + } else { + result.setType("INPUT"); + } + + result.setSubtype( new FieldStyleAndSubTypeConverter(sdkField.getStyle()).toAPIFieldSubtype() ); + result.setBinding( sdkField.getBinding()); + + if ( sdkField.getId() != null ) { + result.setId( sdkField.getId().toString() ); + } + + + if ( sdkField.getFieldValidator() != null ) { + result.setValidation( new FieldValidatorConverter( sdkField.getFieldValidator() ).toAPIFieldValidation() ); + } + + if ( sdkField.getTextAnchor() != null ) { + result.setExtractAnchor( new TextAnchorConverter( sdkField.getTextAnchor() ).toAPIExtractAnchor() ); + } + + if(sdkField.getConditions() != null) { + List conditions = new ArrayList(); + for (com.silanis.esl.sdk.FieldCondition condition : sdkField.getConditions()) { + conditions.add(new FieldConditionConverter(condition).toAPIFieldCondition()); + } + result.setConditions(conditions); + } + + return result; + + } + + /** + * Convert from API conditional field to SDK conditional field. + * + * @return an SDK ConditionalField object. + */ + public com.silanis.esl.sdk.ConditionalField toSDKConditionalField() { + + if (apiField == null) { + return sdkField; + } + com.silanis.esl.sdk.ConditionalField result = new com.silanis.esl.sdk.ConditionalField(); + + result.setExtraction(apiField.evalExtract()); + if ( apiField.getValidation() != null ) { + result.setFieldValidator(new FieldValidatorConverter(apiField.getValidation()).toSDKFieldValidator()); + } + + result.setId(new FieldId(apiField.getId())); + result.setName(apiField.getName()); + + if ( apiField.getPage() != null ) + result.setPage(apiField.getPage()); + + result.setStyle(new FieldStyleAndSubTypeConverter(apiField.getSubtype(), apiField.getBinding()).toSDKFieldStyle()); + result.setTextAnchor(new TextAnchorConverter(apiField.getExtractAnchor()).toSDKTextAnchor()); + result.setValue(apiField.getValue()); + + if ( apiField.getLeft() != null ) + result.setX(apiField.getLeft()); + if ( apiField.getTop() != null ) + result.setY(apiField.getTop()); + if ( apiField.getWidth() != null ) + result.setWidth(apiField.getWidth()); + if ( apiField.getHeight() != null ) + result.setHeight(apiField.getHeight()); + + if(apiField.getConditions()!= null) { + List conditions = new ArrayList(); + for (com.silanis.esl.api.model.FieldCondition condition : apiField.getConditions()) { + conditions.add(new FieldConditionConverter(condition).toSDKFieldCondition()); + } + result.setConditions(conditions); + } + + return result; + } +} + diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/DocumentPackageConverter.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/DocumentPackageConverter.java index a7798cb75..ef57a0a36 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/DocumentPackageConverter.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/DocumentPackageConverter.java @@ -8,6 +8,7 @@ import com.silanis.esl.sdk.AttachmentRequirement; import com.silanis.esl.sdk.Document; import com.silanis.esl.sdk.DocumentPackage; +import com.silanis.esl.sdk.FieldCondition; import com.silanis.esl.sdk.GroupId; import com.silanis.esl.sdk.Message; import com.silanis.esl.sdk.PackageId; @@ -156,6 +157,14 @@ public Role apply(final Signer input) { result.addRole(role); } + if(sdkPackage.getConditions() != null) { + ArrayList conditions = new ArrayList(); + for (FieldCondition condition : sdkPackage.getConditions()) { + conditions.add(new FieldConditionConverter(condition).toAPIFieldCondition()); + } + result.setConditions(conditions); + } + return result; } @@ -258,6 +267,13 @@ public DocumentPackage toSDKPackage() { if (apiPackage.getCreated() != null) { documentPackage.setCreatedDate(apiPackage.getCreated()); } + if(apiPackage.getConditions() != null) { + ArrayList conditions = new ArrayList(); + for (com.silanis.esl.api.model.FieldCondition condition : apiPackage.getConditions()) { + conditions.add(new FieldConditionConverter(condition).toSDKFieldCondition()); + } + documentPackage.setConditions(conditions); + } return documentPackage; } diff --git a/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverter.java b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverter.java new file mode 100644 index 000000000..3cbb00236 --- /dev/null +++ b/sdk/src/main/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverter.java @@ -0,0 +1,50 @@ +package com.silanis.esl.sdk.internal.converter; + +public class FieldConditionConverter { + + + private com.silanis.esl.sdk.FieldCondition sdkFieldCondition = null; + private com.silanis.esl.api.model.FieldCondition apiFieldCondition = null; + + /** + * Construct with API FieldCondition object involved in conversion. + * + * @param apiFieldCondition + */ + public FieldConditionConverter(com.silanis.esl.api.model.FieldCondition apiFieldCondition) { + this.apiFieldCondition= apiFieldCondition; + } + + /** + * Construct with SDK FieldCondition object involved in conversion. + * + * @param sdkFieldCondition + */ + public FieldConditionConverter(com.silanis.esl.sdk.FieldCondition sdkFieldCondition) { + this.sdkFieldCondition = sdkFieldCondition; + } + + public com.silanis.esl.sdk.FieldCondition toSDKFieldCondition() { + if (apiFieldCondition == null) { + return sdkFieldCondition; + } + + com.silanis.esl.sdk.FieldCondition result = new com.silanis.esl.sdk.FieldCondition(); + result.setId(apiFieldCondition.getId()); + result.setAction(apiFieldCondition.getAction()); + result.setCondition(apiFieldCondition.getCondition()); + return result; + } + + public com.silanis.esl.api.model.FieldCondition toAPIFieldCondition() { + if (sdkFieldCondition == null) { + return apiFieldCondition; + } + + com.silanis.esl.api.model.FieldCondition result = new com.silanis.esl.api.model.FieldCondition(); + result.setId(sdkFieldCondition.getId()); + result.setAction(sdkFieldCondition.getAction()); + result.setCondition(sdkFieldCondition.getCondition()); + return result; + } +} diff --git a/sdk/src/main/java/com/silanis/esl/sdk/service/ApprovalService.java b/sdk/src/main/java/com/silanis/esl/sdk/service/ApprovalService.java index 29c7efc8e..cb30d1286 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/service/ApprovalService.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/service/ApprovalService.java @@ -4,6 +4,7 @@ import com.silanis.esl.api.model.Package; import com.silanis.esl.api.model.Role; import com.silanis.esl.sdk.*; +import com.silanis.esl.sdk.internal.converter.ConditionalFieldConverter; import com.silanis.esl.sdk.internal.converter.DocumentPackageConverter; import com.silanis.esl.sdk.internal.converter.FieldConverter; import com.silanis.esl.sdk.internal.converter.SignatureConverter; @@ -175,6 +176,19 @@ public void updateField(PackageId packageId, String documentId, SignatureId sign apiClient.updateField(packageId.getId(), documentId, signatureId.getId(), apiField); } + /** + * Update a conditional field from a signature + * + * @param packageId The package Id + * @param documentId The document Id + * @param signatureId The approval Id + * @param conditionalField The SDK Conditional Field to be updated + */ + public void updateConditionalField(PackageId packageId, String documentId, SignatureId signatureId, ConditionalField conditionalField) { + com.silanis.esl.api.model.ConditionalField apiField = new ConditionalFieldConverter(conditionalField).toAPIConditionalField(); + apiClient.updateConditionalField(packageId.getId(), documentId, signatureId.getId(), apiField); + } + /** * Delete a field from a signature * diff --git a/sdk/src/main/java/com/silanis/esl/sdk/service/apiclient/ApprovalApiClient.java b/sdk/src/main/java/com/silanis/esl/sdk/service/apiclient/ApprovalApiClient.java index 5463a9cfc..f4da4b75d 100644 --- a/sdk/src/main/java/com/silanis/esl/sdk/service/apiclient/ApprovalApiClient.java +++ b/sdk/src/main/java/com/silanis/esl/sdk/service/apiclient/ApprovalApiClient.java @@ -140,6 +140,24 @@ public void updateField(String packageId, String documentId, String signatureId, } } + public void updateConditionalField(String packageId, String documentId, String signatureId, com.silanis.esl.api.model.ConditionalField field) { + String path = template.urlFor(UrlTemplate.CONDITIONAL_FIELD_PATH) + .replace("{packageId}", packageId) + .replace("{documentId}", documentId) + .replace("{approvalId}", signatureId) + .replace("{fieldId}", field.getId()) + .build(); + + try { + String json = Serialization.toJson(field); + restClient.put(path, json); + } catch (RequestException e) { + throw new EslServerException("Could not update conditional field from signature.", e); + } catch (Exception e) { + throw new EslException("Could not update conditional field from signature.", e); + } + } + public void deleteField(String packageId, String documentId, String approvalId, String fieldId) { String path = template.urlFor(UrlTemplate.FIELD_ID_PATH) .replace("{packageId}", packageId) diff --git a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverterTest.java b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverterTest.java new file mode 100644 index 000000000..fec3541c1 --- /dev/null +++ b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/ConditionalFieldConverterTest.java @@ -0,0 +1,175 @@ +package com.silanis.esl.sdk.internal.converter; + +import com.silanis.esl.api.model.FieldCondition; +import com.silanis.esl.sdk.FieldId; +import com.silanis.esl.sdk.FieldStyle; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; + +public class ConditionalFieldConverterTest implements ConverterTest { + + private com.silanis.esl.sdk.ConditionalField sdkField1 = null; + private com.silanis.esl.sdk.ConditionalField sdkField2 = null; + private com.silanis.esl.api.model.ConditionalField apiField1 = null; + private com.silanis.esl.api.model.ConditionalField apiField2 = null; + private ConditionalFieldConverter converter = null; + + @Override + @Test + public void convertNullSDKToAPI() { + sdkField1 = null; + converter = new ConditionalFieldConverter(sdkField1); + assertThat( "Converter didn't return a null api object for a null sdk object", converter.toAPIConditionalField(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullAPIToSDK() { + apiField1 = null; + converter = new ConditionalFieldConverter(apiField1); + assertThat( "Converter didn't return a null sdk object for a null api object", converter.toSDKConditionalField(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullSDKToSDK() { + sdkField1 = null; + converter = new ConditionalFieldConverter(sdkField1); + assertThat( "Converter didn't return a null sdk object for a null sdk object", converter.toSDKConditionalField(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullAPIToAPI() { + apiField1 = null; + converter = new ConditionalFieldConverter(apiField1); + assertThat( "Converter didn't return a null api object for a null api object", converter.toAPIConditionalField(), is( nullValue() ) ); + } + + @Override + @Test + public void convertSDKToSDK() { + + sdkField1 = createTypicalSDKConditionalField(); + sdkField2 = new ConditionalFieldConverter(sdkField1).toSDKConditionalField(); + assertThat( "Converter returned a null sdk object for a non null sdk object", sdkField2, is( notNullValue() ) ); + assertThat( "Converter didn't return the same non-null sdk object it was given", sdkField2, is( equalTo( sdkField1 ) ) ); + } + + @Override + @Test + public void convertAPIToAPI() { + + apiField1 = createTypicalAPIConditionalField(); + apiField2 = new ConditionalFieldConverter(apiField1).toAPIConditionalField(); + + assertThat( "Converter returned a null api object for a non null api object", apiField2, is( notNullValue() ) ); + assertThat( "Converter didn't return the same non-null api object it was given", apiField2, is( equalTo( apiField1 ) ) ); + } + + @Override + @Test + public void convertAPIToSDK() { + apiField1 = createTypicalAPIConditionalField(); + sdkField1 = new ConditionalFieldConverter(apiField1).toSDKConditionalField(); + + assertThat("Converter returned a null api object for a non null sdk object", apiField1, is( notNullValue() ) ); + assertThat("Left coordinate was not correctly set", apiField1.getLeft(), is( equalTo(sdkField1.getX()) ) ); + assertThat("Top coordinate was not correctly set", apiField1.getTop(), is(equalTo(sdkField1.getY()))); + assertThat("Width was not correctly set", apiField1.getWidth(), is(equalTo(sdkField1.getWidth()))); + assertThat("Height was not correctly set",apiField1.getHeight(), is(equalTo(sdkField1.getHeight()))); + assertThat("Page was not correctly set",apiField1.getPage(), is(equalTo(sdkField1.getPage()))); + assertThat("Extract was not correctly set",apiField1.getExtract(), is(equalTo(sdkField1.isExtraction()))); + assertThat("Value was not correctly set",apiField1.getValue(), is(equalTo(sdkField1.getValue()))); + assertThat("ID was not correctly set", apiField1.getId(), is(equalTo(sdkField1.getId().toString()))); + assertThat("Name was not correctly set", apiField1.getName(), is(equalTo(sdkField1.getName()))); + assertThat("Conditions was not correctly set", apiField1.getConditions().size(), is(equalTo(sdkField1.getConditions().size()))); + } + + @Override + @Test + public void convertSDKToAPI() { + + sdkField1 = createTypicalSDKConditionalField(); + apiField1 = new ConditionalFieldConverter(sdkField1).toAPIConditionalField(); + + assertThat("Converter returned a null api object for a non null sdk object", apiField1, is( notNullValue() ) ); + assertThat("Left coordinate was not correctly set", apiField1.getLeft(), is( equalTo(sdkField1.getX()) ) ); + assertThat("Top coordinate was not correctly set", apiField1.getTop(), is(equalTo(sdkField1.getY()))); + assertThat("Width was not correctly set", apiField1.getWidth(), is(equalTo(sdkField1.getWidth()))); + assertThat("Height was not correctly set",apiField1.getHeight(), is(equalTo(sdkField1.getHeight()))); + assertThat("Page was not correctly set",apiField1.getPage(), is(equalTo(sdkField1.getPage()))); + assertThat("Extract was not correctly set",apiField1.getExtract(), is(equalTo(sdkField1.isExtraction()))); + assertThat("Value was not correctly set",apiField1.getValue(), is(equalTo(sdkField1.getValue()))); + assertThat("ID was not correctly set", apiField1.getId(), is(equalTo(sdkField1.getId().toString()))); + assertThat("Name was not correctly set", apiField1.getName(), is(equalTo(sdkField1.getName()))); + assertThat("Conditions was not correctly set", apiField1.getConditions().size(), is(equalTo(sdkField1.getConditions().size()))); + } + + /** + * Create an SDK Field. + * + * @return SDK Field. + */ + private com.silanis.esl.sdk.ConditionalField createTypicalSDKConditionalField() { + com.silanis.esl.sdk.ConditionalField sdkField = new com.silanis.esl.sdk.ConditionalField(); + + sdkField.setExtraction(false); + sdkField.setHeight(100.0); + sdkField.setX(10.0); + sdkField.setId(new FieldId("99")); + sdkField.setName("Field name"); + sdkField.setPage(1); + sdkField.setStyle(FieldStyle.BOUND_DATE); + sdkField.setY(101.0); + sdkField.setValue("field value"); + sdkField.setWidth(102.0); + com.silanis.esl.sdk.FieldCondition condition = new com.silanis.esl.sdk.FieldCondition(); + condition.setId("ConditionId"); + condition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + condition.setAction("document['DocumentId'].field['fieldId1'].enabled = false"); + List conditions = new ArrayList(); + conditions.add(condition); + sdkField.setConditions(conditions); + + return sdkField; + } + + /** + * Create an API Field. + * + * @return API Field. + */ + private com.silanis.esl.api.model.ConditionalField createTypicalAPIConditionalField() { + com.silanis.esl.api.model.ConditionalField apiField = new com.silanis.esl.api.model.ConditionalField(); + + apiField.setExtract(false); + apiField.setHeight(100.0); + apiField.setLeft(10.0); + apiField.setId("3"); + apiField.setName("Field name"); + apiField.setPage(1); + apiField.setSubtype("TEXTFIELD"); + apiField.setTop(101.0); + apiField.setType("INPUT"); + apiField.setValue("field value"); + apiField.setWidth(102.0); + com.silanis.esl.api.model.FieldCondition condition = new com.silanis.esl.api.model.FieldCondition(); + condition.setId("ConditionId"); + condition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + condition.setAction("document['DocumentId'].field['fieldId1'].enabled = false"); + List conditions = new ArrayList(); + conditions.add(condition); + apiField.setConditions(conditions); + + return apiField; + } +} diff --git a/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverterTest.java b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverterTest.java new file mode 100644 index 000000000..f5fdc4ec0 --- /dev/null +++ b/sdk/src/test/java/com/silanis/esl/sdk/internal/converter/FieldConditionConverterTest.java @@ -0,0 +1,126 @@ +package com.silanis.esl.sdk.internal.converter; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; + +public class FieldConditionConverterTest implements ConverterTest { + + private com.silanis.esl.sdk.FieldCondition sdkFieldCondition1 = null; + private com.silanis.esl.sdk.FieldCondition sdkFieldCondition2 = null; + private com.silanis.esl.api.model.FieldCondition apiFieldCondition1 = null; + private com.silanis.esl.api.model.FieldCondition apiFieldCondition2 = null; + private FieldConditionConverter converter = null; + + @Override + @Test + public void convertNullSDKToAPI() { + sdkFieldCondition1 = null; + converter = new FieldConditionConverter(sdkFieldCondition1); + assertThat( "Converter didn't return a null api object for a null sdk object", converter.toAPIFieldCondition(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullAPIToSDK() { + apiFieldCondition1 = null; + converter = new FieldConditionConverter(apiFieldCondition1); + assertThat( "Converter didn't return a null sdk object for a null api object", converter.toSDKFieldCondition(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullSDKToSDK() { + sdkFieldCondition1 = null; + converter = new FieldConditionConverter(sdkFieldCondition1); + assertThat( "Converter didn't return a null sdk object for a null sdk object", converter.toSDKFieldCondition(), is( nullValue() ) ); + } + + @Override + @Test + public void convertNullAPIToAPI() { + apiFieldCondition1 = null; + converter = new FieldConditionConverter(apiFieldCondition1); + assertThat( "Converter didn't return a null api object for a null api object", converter.toAPIFieldCondition(), is( nullValue() ) ); + } + + @Override + @Test + public void convertSDKToSDK() { + + sdkFieldCondition1 = createTypicalSDKFieldCondition(); + sdkFieldCondition2 = new FieldConditionConverter(sdkFieldCondition1).toSDKFieldCondition(); + assertThat( "Converter returned a null sdk object for a non null sdk object", sdkFieldCondition2, is( notNullValue() ) ); + assertThat( "Converter didn't return the same non-null sdk object it was given", sdkFieldCondition2, is( equalTo(sdkFieldCondition1) ) ); + } + + @Override + @Test + public void convertAPIToAPI() { + + apiFieldCondition1 = createTypicalAPIFieldCondition(); + apiFieldCondition2 = new FieldConditionConverter(apiFieldCondition1).toAPIFieldCondition(); + + assertThat( "Converter returned a null api object for a non null api object", apiFieldCondition2, is( notNullValue() ) ); + assertThat( "Converter didn't return the same non-null api object it was given", apiFieldCondition2, is( equalTo(apiFieldCondition1) ) ); + } + + @Override + @Test + public void convertAPIToSDK() { + apiFieldCondition1 = createTypicalAPIFieldCondition(); + sdkFieldCondition1 = new FieldConditionConverter(apiFieldCondition1).toSDKFieldCondition(); + + assertThat("Converter returned a null api object for a non null sdk object", apiFieldCondition1, is( notNullValue() ) ); + assertThat("Id was not correctly set", apiFieldCondition1.getId(), is( equalTo(sdkFieldCondition1.getId()) ) ); + assertThat("Condition was not correctly set", apiFieldCondition1.getCondition(), is(equalTo(sdkFieldCondition1.getCondition()))); + assertThat("Action not correctly set", apiFieldCondition1.getAction(), is(equalTo(sdkFieldCondition1.getAction()))); + } + + @Override + @Test + public void convertSDKToAPI() { + + sdkFieldCondition1 = createTypicalSDKFieldCondition(); + apiFieldCondition1 = new FieldConditionConverter(sdkFieldCondition1).toAPIFieldCondition(); + + assertThat("Converter returned a null api object for a non null sdk object", apiFieldCondition1, is( notNullValue() ) ); + assertThat("Id coordinate was not correctly set", apiFieldCondition1.getId(), is( equalTo(sdkFieldCondition1.getId()) ) ); + assertThat("Condition coordinate was not correctly set", apiFieldCondition1.getCondition(), is(equalTo(sdkFieldCondition1.getCondition()))); + assertThat("Action was not correctly set", apiFieldCondition1.getAction(), is(equalTo(sdkFieldCondition1.getAction()))); + } + + /** + * Create an SDK Field. + * + * @return SDK Field. + */ + private com.silanis.esl.sdk.FieldCondition createTypicalSDKFieldCondition() { + com.silanis.esl.sdk.FieldCondition sdkFieldCondition = new com.silanis.esl.sdk.FieldCondition(); + + sdkFieldCondition.setId("ConditionId"); + sdkFieldCondition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + sdkFieldCondition.setAction("document['DocumentId'].field['fieldId1'].enabled = false"); + + return sdkFieldCondition; + } + + /** + * Create an API Field. + * + * @return API Field. + */ + private com.silanis.esl.api.model.FieldCondition createTypicalAPIFieldCondition() { + com.silanis.esl.api.model.FieldCondition apiFieldCondition = new com.silanis.esl.api.model.FieldCondition(); + + apiFieldCondition.setId("ConditionId"); + apiFieldCondition.setCondition("document['DocumentId'].field['fieldId2'].value == 'X'"); + apiFieldCondition.setAction("document['DocumentId'].field['fieldId1'].enabled = false"); + + return apiFieldCondition; + } +} diff --git a/tester/src/test/java/com/silanis/esl/sdk/examples/ConditionalFieldExampleTest.java b/tester/src/test/java/com/silanis/esl/sdk/examples/ConditionalFieldExampleTest.java new file mode 100644 index 000000000..dd598086b --- /dev/null +++ b/tester/src/test/java/com/silanis/esl/sdk/examples/ConditionalFieldExampleTest.java @@ -0,0 +1,25 @@ +package com.silanis.esl.sdk.examples; + +import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; + +public class ConditionalFieldExampleTest { + + @Test + public void verifyResult() { + ConditionalFieldExample example = new ConditionalFieldExample(); + example.run(); + + assertThat("Package doesn't have conditional fields.", example.getRetrievedPackage().getConditions(), hasSize(1)); + + assertThat("Package doesn't have conditional fields.", example.retrievedPackageWithUpdatedConditions.getConditions(), hasSize(1)); + assertThat("Package doesn't updated properly.", example.retrievedPackageWithUpdatedConditions.getConditions().get(0).getAction(), + is(equalTo("document['DocumentId'].field['fieldId1'].enabled = false"))); + + assertThat("Package have conditional fields.", example.retrievedPackageWithoutConditions.getConditions(), empty()); + } +}