Document
- Document
- new Document(collection_name)
- .setField(field, value)
- .saveDocument(callback)
- .getDocumentById(documentId, callback)
- .getField(field)
- .updateDocument()
- .uploadFile(fieldName, fileName, contentToUploadInBase64, callback)
- .getFileLink(fieldName, fileName)
- .removeFile( fieldName, fileName, callback)
- .removeDocument(callback)
new Document(collection_name)
Document initialisation
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
collection_name | String |
Mandatory | Name of the collection where the document is added | "Things" |
Example
ScorocodeSdk.initWith("db8a1b41b8543397a798a181d9891b4c", "563452bbc611d8106d5da767365897de", "28f06b89b62165c33de55265166d8781", null, null, null);
Document exampleItem = new Document("Items");
.setField(field, value)
Method for setting data to document's field
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
field | String |
Mandatory | Field name | "testcoll" |
value | Object |
Mandatory | Field value | "huNr3L7QDh" |
Example
ScorocodeSdk.initWith("db8a1b41b8543397a798a181d9891b4c", "563452bbc611d8106d5da767365897de", "28f06b89b62165c33de55265166d8781", null, null, null);
final Document order = new Document(“orders”);
order.setField(“orderId”, “Ku128A439ads”);
.saveDocument(callback)
The method saves the document in the database or updates an object that already exists there
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
callback | CallbackDocumentSaved |
Mandatory | Callback for the request being executed. |
Example
Creating new document
ScorocodeSdk.initWith("db8a1b41b8543397a798a181d9891b4c", "563452bbc611d8106d5da767365897de", "28f06b89b62165c33de55265166d8781", null, null, null);
Document newDocument = new Document(“orderInfo”);
newDocument.setField("isOrderSend", false);
newDocument.setField("buyerName", “Any username”);
newDocument.saveDocument(new CallbackDocumentSaved() {
@Override
public void onDocumentSaved() {
//document saved successful (New document uploaded on server).
}
@Override
public void onDocumentSaveFailed(String errorCode, String errorMessage) {
//document save failed.
}
});
Updating the existing document
final Document document = new Document(“orderInfo”);
document.getDocumentById("KH3JCojAyT", new CallbackFindDocument() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//we found document and we can make changes in it and save it
//...change document fields...
//save
document.saveDocument(new CallbackDocumentSaved() {
@Override
public void onDocumentSaved() {
//document save succeed. Document updated on server
}
@Override
public void onDocumentSaveFailed(String errorCode, String errorMessage) {
//document save failed.
}
});
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document not found. If we try to save document here
//new document will be upload to server as in example 2.1
}
});
.getDocumentById(documentId, callback)
Method for retrieving a collection object from DB by its _id.
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
documentId | String |
Mandatory | Object identifier | "nV0p50CDKq" |
callback | CallbackGetDocumentById |
Mandatory | Callback for the request being executed. |
Example
ScorocodeSdk.initWith("db8a1b41b8543397a798a181d9891b4c", "563452bbc611d8106d5da767365897de", "28f06b89b62165c33de55265166d8781", null, null, null);
final Document document = new Document("ordersCollection");
document.getDocumentById("nV0p50CDKq", new CallbackGetDocumentById() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//document found
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document not found or error occured
}
});
.getField(field)
Method for retrieving data from a specified document field.
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
field | String |
Mandatory | Field name | "name" |
Example
final Document document = new Document(“ordersCollection”);
String orderId = document.getField(“orderId”);
.updateDocument()
Method for updating the document using Update class.
.uploadFile(fieldName, fileName, contentToUploadInBase64, callback)
File upload method
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
fieldName | String |
Mandatory | Field name | "attachment" |
fileName | String |
Mandatory | Filename with extension | "file.txt" |
contentToUploadInBase64 | String |
Mandatory | File content in base64 encoded format | "VEhJUyBJUyBGSUxFLUUtRS1FLUUtRS1FIQ==" |
callback | CallbackUploadFile |
Mandatory | Callback for the request being executed. |
Example
final Document document = new Document(“ordersCollection”);
document.getDocumentById("nV0p50CDKq", new CallbackFindDocument() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//document found and we can upload document in this file
document.uploadFile("file_field", "any_filename.txt",
Base64.encodeToString("hello world".getBytes(), Base64.DEFAULT), new CallbackUploadFile() {
@Override
public void onDocumentUploaded() {
//document upload succeed
}
@Override
public void onDocumentUploadFailed(String errorCode, String errorMessage) {
//document upload failed
}
});
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document not found
}
});
.getFileLink(fieldName, fileName)
Method for retreiving file link
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
fieldName | String |
Mandatory | Field name | "attachment" |
fileName | String |
Mandatory | Filename with extension | "file.txt" |
Example
final Document documentWithFile = new Document(“ordersCollection”);
documentWithFile.getDocumentById("nV0p50CDKq", new CallbackFindDocument() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//document found. We can try to get link on file in this document
String fileLink = documentWithFile.getFileLink("test", "file.txt");
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document not found
}
});
.removeFile(fieldName, fileName, callback)
Method for removing the file
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
fieldName | String |
Mandatory | Collection field name | "attachment" |
fileName | String |
Mandatory | Filename with extension | "file.txt" |
callback | CallbackDeleteFile |
Mandatory | Callback for the request being executed. |
Example
final Document document = new Document(“ordersCollection”);
document.getDocumentById("nV0p50CDKq", new CallbackFindDocument() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//document found we can try to delete file from this doc
document.removeFile("file", "anyname", new CallbackDeleteFile() {
@Override
public void onDocumentDeleted() {
//file deletion succeed
}
@Override
public void onDetelionFailed(String errorCodes, String errorMessage) {
//file deletion failed
}
});
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document not found
}
});
.removeDocument(callback)
Method for removing the selected document
Parameter | Type | Properties | Description | Value example |
---|---|---|---|---|
callback | CallbackRemoveDocument |
Mandatory | Callback for the request being executed. |
Example
final Document document = new Document(“ordersCollection”);
document.getDocumentById("7BOlVr1Acp", new CallbackFindDocument() {
@Override
public void onDocumentFound(DocumentInfo documentInfo) {
//we found document in collection and can remove it
document.removeDocument(new CallbackRemoveDocument() {
@Override
public void onRemoveSucceed(ResponseRemove responseRemove) {
//document removed
}
@Override
public void onRemoveFailed(String errorCode, String errorMessage) {
//remove operation failed
}
});
}
@Override
public void onDocumentNotFound(String errorCode, String errorMessage) {
//document wasn’t found
}
});