Skip to content

Commit

Permalink
PB-9090. Add feature to apply layout by name
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Mar 13, 2018
1 parent 5493bdc commit aad9b00
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 33 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public class DocumentLayoutExample extends SDKSample {
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 final String APPLY_LAYOUT_BY_NAME_DOCUMENT_NAME = "Apply Layout Document by Name";
public static final String APPLY_LAYOUT_BY_NAME_DOCUMENT_ID = "docId2";
public static final String APPLY_LAYOUT_BY_NAME_DOCUMENT_DESCRIPTION = "Document with applied layout by name description.";

public static void main(String... args) {
new DocumentLayoutExample().run();
Expand Down Expand Up @@ -92,19 +89,12 @@ public void execute() {
.withId(APPLY_LAYOUT_DOCUMENT_ID)
.withDescription(APPLY_LAYOUT_DOCUMENT_DESCRIPTION)
.fromStream(documentInputStream2, DocumentType.PDF))
.withDocument(newDocumentWithName(APPLY_LAYOUT_BY_NAME_DOCUMENT_NAME)
.withId(APPLY_LAYOUT_BY_NAME_DOCUMENT_ID)
.withDescription(APPLY_LAYOUT_BY_NAME_DOCUMENT_DESCRIPTION)
.fromStream(documentInputStream3, DocumentType.PDF))
.build();

packageId = eslClient.createPackage(packageFromLayout);

// Apply the layout to document in package
eslClient.getLayoutService().applyLayout(packageId, APPLY_LAYOUT_DOCUMENT_ID, layoutId);

// Apply the layout by name to document in package
eslClient.getLayoutService().applyLayout(packageId, APPLY_LAYOUT_BY_NAME_DOCUMENT_ID, LAYOUT_PACKAGE_NAME);

packageWithLayout = eslClient.getPackage(packageId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract public class SDKSample {
protected PackageId packageId;
protected String packageName;
protected DocumentPackage retrievedPackage;
protected InputStream documentInputStream1, documentInputStream2, documentInputStream3;
protected InputStream documentInputStream1, documentInputStream2;

protected Properties props = Props.get();

Expand Down Expand Up @@ -68,7 +68,6 @@ private void setProperties() {

documentInputStream1 = this.getClass().getClassLoader().getResourceAsStream( "document.pdf" );
documentInputStream2 = this.getClass().getClassLoader().getResourceAsStream( "document.pdf" );
documentInputStream3 = this.getClass().getClassLoader().getResourceAsStream( "document.pdf" );
}

protected EslClient getEslClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class UrlTemplate {
public static final String LAYOUT_PATH = "/layouts";
public static final String LAYOUT_LIST_PATH = "/layouts?to={to}&from={from}&dir={dir}";
public static final String APPLY_LAYOUT_PATH = "/packages/{packageId}/documents/{documentId}/layout?layoutId={layoutId}";
public static final String APPLY_LAYOUT_PATH_BY_NAME = "/packages/{packageId}/documents/{documentId}/layout?layoutName={layoutName}";
public static final String APPLY_LAYOUT_BY_NAME_PATH = "/packages/{packageId}/documents/{documentId}/layout?layoutName={layoutName}";

// QR Code Service
public static final String QRCODE_PATH = "/packages/{packageId}/documents/{documentId}/fields";
Expand Down
15 changes: 10 additions & 5 deletions sdk/src/main/java/com/silanis/esl/sdk/service/LayoutService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.silanis.esl.sdk.internal.converter.DocumentConverter;
import com.silanis.esl.sdk.internal.converter.DocumentPackageConverter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -154,14 +156,17 @@ public void applyLayout(PackageId packageId, String documentId, String layoutId)
* @param layoutName The layout name of the layout to apply.
*/
public void applyLayoutByName(PackageId packageId, String documentId, String layoutName) {
String path = template.urlFor(UrlTemplate.APPLY_LAYOUT_PATH_BY_NAME)
.replace("{packageId}", packageId.getId())
.replace("{documentId}", documentId)
.replace("{layoutName}", layoutName)
.build();

try {
String path = template.urlFor(UrlTemplate.APPLY_LAYOUT_BY_NAME_PATH)
.replace("{packageId}", packageId.getId())
.replace("{documentId}", documentId)
.replace("{layoutName}", URLEncoder.encode(layoutName, "UTF-8"))
.build();

client.post(path, "");
} catch (UnsupportedEncodingException e) {
throw new EslException("Layout name : '" + layoutName + "' can not be url encoded.", e);
} catch (RequestException e) {
throw new EslServerException("Could not apply layout.", e);
} catch (Exception e) {
Expand Down
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));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,6 @@ private void validateSignatureFields(Collection<Signature> signatures) {
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));

// *** IMPORTANT NOTES ****
//
// Do not use "both" if we still need to support JDK 1.6. It is a known bug:
// https://code.google.com/p/hamcrest/issues/detail?id=82
//
// So instead of
//
// assertThat("Field x coordinate was not set correctly", field.getX(), is(both(greaterThan(99.0)).and(lessThan(101.0))));
//
// do:
//
// assertThat("Field x coordinate was not set correctly", field.getX(), is(greaterThan(99.0)));
// assertThat("Field x coordinate was not set correctly", field.getX(), is(lessThan(101.0)));

assertThat("Field x coordinate was not set correctly", field.getX(), closeTo(100, TOLERANCE));
assertThat("Field y coordinate was not set correctly", field.getY(), closeTo(200, TOLERANCE));
}
Expand Down

0 comments on commit aad9b00

Please sign in to comment.