-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathClientModel.java
More file actions
93 lines (78 loc) · 2.79 KB
/
Copy pathClientModel.java
File metadata and controls
93 lines (78 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package application.model;
import java.util.Collections;
import java.util.Map;
import com.google.gson.Gson;
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.*;
import com.ibm.cloud.sdk.core.service.exception.NotFoundException;
import io.swagger.model.Client;
public class ClientModel {
private String database = null;
private Cloudant service = null;
private ModelHelper modelHelper = null;
public ClientModel(String serviceName, String database) {
this.database = database;
modelHelper = new ModelHelper();
try {
service = Cloudant.newInstance(serviceName);
} catch (Exception e) {
e.printStackTrace();
}
}
public DocumentResult save(Client client) {
Gson gson = new Gson();
Document clientDocument = new Document();
clientDocument.setProperties(gson.fromJson(gson.toJson(client), Map.class));
PostDocumentOptions createDocumentOptions = modelHelper.getPostDocumentOptions(clientDocument, database);
DocumentResult resp = service
.postDocument(createDocumentOptions)
.execute()
.getResult();
return resp;
}
public DocumentResult delete(String id) {
DocumentResult deleteDocumentResponse = null;
try {
// Set the options to get the document out of the database if it exists
GetDocumentOptions documentInfoOptions = modelHelper.getGetDocumentOptions(id,database);
// Try to get the document if it previously existed in the database
Document document = service
.getDocument(documentInfoOptions)
.execute()
.getResult();
// Delete the document from the database
DeleteDocumentOptions deleteDocumentOptions =
modelHelper.getDeleteDocumentOptions(id, document.getRev(), database);
deleteDocumentResponse = service
.deleteDocument(deleteDocumentOptions)
.execute()
.getResult();
if (deleteDocumentResponse.isOk()) {
System.out.println("You have deleted the document.");
}
} catch (NotFoundException nfe) {
System.out.println("Cannot delete because document was not found " + nfe);
}
return deleteDocumentResponse;
}
public Client read(String id) {
Gson gson = new Gson();
GetDocumentOptions getDocOptions = modelHelper.getGetDocumentOptions(id, database);
Document clientDocument = service
.getDocument(getDocOptions)
.execute()
.getResult();
Client client = clientDocument != null ? gson.fromJson(clientDocument.toString(),Client.class): null;
return client;
}
public FindResult getAllClientsOfAttorney(String attorneyId) {
Map<String, Object> selector = Collections.singletonMap(
"_id", Collections.singletonMap("$eq", attorneyId));
PostFindOptions findOptions = modelHelper.getFindOptions(selector,database);
FindResult response =
service.postFind(findOptions).execute()
.getResult();
System.out.println(response);
return response;
}
}