-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PB-9090. Add feature to apply layout by name
- Loading branch information
john
committed
Mar 13, 2018
1 parent
5493bdc
commit aad9b00
Showing
7 changed files
with
179 additions
and
33 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
sdk/src/main/java/com/silanis/esl/sdk/examples/ApplyLayoutByNameExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.silanis.esl.sdk.examples; | ||
|
||
import com.silanis.esl.sdk.DocumentPackage; | ||
import com.silanis.esl.sdk.DocumentType; | ||
import com.silanis.esl.sdk.PackageId; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
import static com.silanis.esl.sdk.builder.DocumentBuilder.newDocumentWithName; | ||
import static com.silanis.esl.sdk.builder.FieldBuilder.signerCompany; | ||
import static com.silanis.esl.sdk.builder.FieldBuilder.signerTitle; | ||
import static com.silanis.esl.sdk.builder.PackageBuilder.newPackageNamed; | ||
import static com.silanis.esl.sdk.builder.SignatureBuilder.signatureFor; | ||
import static com.silanis.esl.sdk.builder.SignerBuilder.newSignerWithEmail; | ||
|
||
/** | ||
* Created by schoi on 12/03/18. | ||
*/ | ||
public class ApplyLayoutByNameExample extends SDKSample { | ||
|
||
public String layoutId; | ||
public DocumentPackage packageWithLayout; | ||
|
||
public static final String LAYOUT_PACKAGE_NAME = "Layout " + new SimpleDateFormat("HH:mm:ss").format(new Date()); | ||
public static final String LAYOUT_PACKAGE_DESCRIPTION = "This is a package with document to create layout."; | ||
public static final String LAYOUT_DOCUMENT_NAME = "First Document"; | ||
public static final String FIELD_1_NAME = "field title"; | ||
public static final String FIELD_2_NAME = "field company"; | ||
public static final String APPLY_LAYOUT_DOCUMENT_NAME = "Apply Layout Document"; | ||
public static final String APPLY_LAYOUT_DOCUMENT_ID = "docId"; | ||
public static final String APPLY_LAYOUT_DOCUMENT_DESCRIPTION = "Document with applied layout description."; | ||
|
||
public static void main(String... args) { | ||
new ApplyLayoutByNameExample().run(); | ||
} | ||
|
||
public void execute() { | ||
// Create a package with one document and one signature with two fields | ||
DocumentPackage superDuperPackage = newPackageNamed(LAYOUT_PACKAGE_NAME) | ||
.describedAs(LAYOUT_PACKAGE_DESCRIPTION) | ||
.withSigner(newSignerWithEmail(email1) | ||
.withCustomId("Client1") | ||
.withFirstName("John") | ||
.withLastName("Smith") | ||
.withTitle("Managing Director") | ||
.withCompany("Acme Inc.")) | ||
.withDocument(newDocumentWithName(LAYOUT_DOCUMENT_NAME) | ||
.withId("documentId") | ||
.withDescription("Layout document description") | ||
.fromStream(documentInputStream1, DocumentType.PDF) | ||
.withSignature(signatureFor(email1) | ||
.onPage(0) | ||
.atPosition(120, 100) | ||
.withField(signerTitle() | ||
.withName(FIELD_1_NAME) | ||
.onPage(0) | ||
.atPosition(120, 200)) | ||
.withField(signerCompany() | ||
.withName(FIELD_2_NAME) | ||
.onPage(0) | ||
.atPosition(120, 300)))) | ||
.build(); | ||
|
||
PackageId packageId1 = eslClient.createPackage(superDuperPackage); | ||
superDuperPackage.setId(packageId1); | ||
|
||
// Create layout from package | ||
layoutId = eslClient.getLayoutService().createLayout(superDuperPackage); | ||
|
||
// Create a new package to apply document layout to | ||
DocumentPackage packageFromLayout = newPackageNamed(getPackageName()) | ||
.describedAs("This is a package created using the eSignLive SDK") | ||
.withEmailMessage("This message should be delivered to all signers") | ||
.withSigner(newSignerWithEmail(email1) | ||
.withCustomId("Client1") | ||
.withFirstName("John") | ||
.withLastName("Smith") | ||
.withTitle("Managing Director") | ||
.withCompany("Acme Inc.")) | ||
.withDocument(newDocumentWithName(APPLY_LAYOUT_DOCUMENT_NAME) | ||
.withId(APPLY_LAYOUT_DOCUMENT_ID) | ||
.withDescription(APPLY_LAYOUT_DOCUMENT_DESCRIPTION) | ||
.fromStream(documentInputStream2, DocumentType.PDF)) | ||
.build(); | ||
|
||
packageId = eslClient.createPackage(packageFromLayout); | ||
|
||
// Apply the layout to document in package | ||
eslClient.getLayoutService().applyLayoutByName(packageId, APPLY_LAYOUT_DOCUMENT_ID, LAYOUT_PACKAGE_NAME); | ||
|
||
packageWithLayout = eslClient.getPackage(packageId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tester/src/test/java/com/silanis/esl/sdk/examples/ApplyLayoutByNameExampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.silanis.esl.sdk.examples; | ||
|
||
import com.silanis.esl.sdk.Document; | ||
import com.silanis.esl.sdk.DocumentPackage; | ||
import com.silanis.esl.sdk.Field; | ||
import com.silanis.esl.sdk.Signature; | ||
import org.junit.Test; | ||
|
||
import java.util.Collection; | ||
|
||
import static com.silanis.esl.sdk.FieldStyle.BOUND_COMPANY; | ||
import static com.silanis.esl.sdk.FieldStyle.BOUND_TITLE; | ||
import static com.silanis.esl.sdk.examples.DocumentLayoutExample.*; | ||
import static org.hamcrest.CoreMatchers.not; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.closeTo; | ||
import static org.hamcrest.core.Is.is; | ||
|
||
/** | ||
* Created by schoi on 13/03/18. | ||
*/ | ||
public class ApplyLayoutByNameExampleTest { | ||
|
||
private static final double TOLERANCE = 1.25; | ||
private ApplyLayoutByNameExample example; | ||
|
||
@Test | ||
public void verifyResult() { | ||
|
||
example = new ApplyLayoutByNameExample(); | ||
example.run(); | ||
|
||
DocumentPackage packageWithLayout = example.packageWithLayout; | ||
|
||
assertThat("Package name should not have changed", packageWithLayout.getName(), not(LAYOUT_PACKAGE_NAME)); | ||
assertThat("Package description should not have changed", packageWithLayout.getDescription(), not(LAYOUT_PACKAGE_DESCRIPTION)); | ||
assertThat("Package should have only 2 signers", packageWithLayout.getSigners().size(), is(2)); | ||
/* Note that default consent will be added automatically. */ | ||
assertThat("Package should have 2 documents", packageWithLayout.getDocuments().size(), is(2)); | ||
|
||
Document documentWithLayout = packageWithLayout.getDocument(APPLY_LAYOUT_DOCUMENT_NAME); | ||
assertThat(documentWithLayout.getDescription(), is(APPLY_LAYOUT_DOCUMENT_DESCRIPTION)); | ||
assertThat(documentWithLayout.getId().getId(), is(APPLY_LAYOUT_DOCUMENT_ID)); | ||
assertThat("Document should have 1 signature", documentWithLayout.getSignatures().size(), is(1)); | ||
|
||
// Validate that the signature fields were applied correctly to document. | ||
validateSignatureFields(documentWithLayout.getSignatures()); | ||
} | ||
|
||
private void validateSignatureFields(Collection<Signature> signatures) { | ||
for (Signature signature : signatures) { | ||
assertThat("Signature email was not set correctly", signature.getSignerEmail(), is(example.email1)); | ||
assertThat("Signature page number was not set correctly", signature.getPage(), is(0)); | ||
assertThat("Signature x coordinate was not set correctly", signature.getX(), closeTo(120, TOLERANCE)); | ||
assertThat("Signature y coordinate was not set correctly", signature.getY(), closeTo(100, TOLERANCE)); | ||
|
||
for (Field field : signature.getFields()) { | ||
if (field.getName().equals(FIELD_1_NAME)) { | ||
assertThat("Field style was not set correctly", field.getStyle(), is(BOUND_TITLE)); | ||
assertThat("Field page number was not set correctly", field.getPage(), is(0)); | ||
assertThat("Field x coordinate was not set correctly", field.getX(), closeTo(120, TOLERANCE)); | ||
assertThat("Field y coordinate was not set correctly", field.getY(), closeTo(200, TOLERANCE)); | ||
} | ||
if (field.getName().equals(FIELD_2_NAME)) { | ||
assertThat("Field style was not set correctly", field.getStyle(), is(BOUND_COMPANY)); | ||
assertThat("Field page number was not set correctly", field.getPage(), is(0)); | ||
assertThat("Field x coordinate was not set correctly", field.getX(), closeTo(120, TOLERANCE)); | ||
assertThat("Field y coordinate was not set correctly", field.getY(), closeTo(300, TOLERANCE)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters