Skip to content

Commit ff85af9

Browse files
committed
Updated readme with diagram
1 parent ea26987 commit ff85af9

File tree

1 file changed

+74
-48
lines changed

1 file changed

+74
-48
lines changed

README.md

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,124 @@
11
[![Build Status](https://travis-ci.org/code-brewery/untappd.svg?branch=master)](https://travis-ci.org/code-brewery/java-ormen)
22
[![codecov.io](http://codecov.io/github/code-brewery/java-ormen/coverage.svg?branch=master)](http://codecov.io/github/code-brewery/java-ormen?branch=master)
33
# java-ormen
4-
Java-ormen is an Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database.
4+
Java-ormen is an Object relation mapping library for REST API:s in Java. It is designed to build applications that are powered by a RESTful API instead of a database. This might be be useful when developing micro service oriented software.
55

6-
Thos async library purpose is to allow Java applications to easily execute HTTP requests over an REST API, and create an abstraction on top of REST, so that only the CRUD operations are exposed.
6+
The library's purpose is to allow Java applications to easily execute HTTP requests over an REST API. This will be achieved by create an abstraction on top of the REST calls, so the developer can invoke crud methods on Java POJOS.
77

8-
The library has been designed using the Java CompletableFuture. If you you don't need a ascynchronous library you can tell the library to execute query's synchronous.
8+
Many ORM librarys restricts the user to do certain tasks (filtering etc etc). This api lets the user to write an own APIImplementation, so its possible to customize the library for different rest API:s. The library can talk with different rest api's with different behavior at the same time.
9+
10+
The library uses the Jackson ObjectMapper, so its possible to use jackson annotations to describe how objects shall be marshalled and unmarshalled. The JSONConverter are optional, and can be removed for something else, if needed ( its up to the APIImplementation ).
11+
12+
The library strives for creating immutable objects. This means that you can have control over your objects. But ofcourse, its impossible to make it immutable , if your code isn't.
913

10-
The library uses the Jackson ObjectMapper, so its possible to use jackson annotations to describe how objects shall be marshalled and unmarshalled.
1114

12-
The library strives for creating immutable objects. This means that you can have control over your objects.
1315

1416
## Usage
1517

16-
Lets say that we have a REST API that exposes CRUD Operations for fetching Dog´s. Well dogs can bark, so we will use dogs in this example.
18+
One just simply do the following:
19+
20+
define your model. Remember to use the resourceUrl annotation to specify the endpoint for the dog model. We need a default constructor for jackson json reflection.
21+
Observe that we give the DogModel an Finder. It will give the model powers to retrive all dogs,with an static invocation from the code.
1722

18-
As an developer you will probably create some POJOś as an a java representation of the Dogs. Then you will probably write some kind of handler that takes these POJO instance´s and sends them
23+
```java
1924

20-
back and forth the REST API. Well, you will not need to do that any longer.
25+
@ResourceUrl(url = "dogs")
26+
public class DogModel extends Model {
2127

28+
private String name;
2229

23-
One just simply do the following:
24-
25-
Start of by creating a normal POJO, but we will make sure it extends RestModel. This will give the POJO super powers ( almost ). Well it will give the pojo possibility interact with an rest api.
26-
27-
The Rest model gives you 4 public methods for interacting with the rest api, one for each CRUD operation. The good part here is that these methods returns a Completable future.
30+
private int age;
2831

29-
Look at the comments, it will give you some hints about what information you need to provide for successfully send requests over rest.
3032

31-
```java
33+
public DogModel(String name, int age) {
34+
this.name = name;
35+
this.age = age;
36+
}
3237

33-
public class MockRestModel extends RESTModel {
38+
DogModel() {
39+
this.name ="";
40+
this.age = 0;
41+
}
3442

35-
/**
36-
* Default constructor, this is needed by the jackson json mapper. so always include it.
37-
**/
38-
public MockRestModel() {
39-
this.name = "";
43+
public int getAge() {
44+
return age;
4045
}
4146

42-
public MockRestModel(String name) {
43-
this.name = name;
47+
public void setAge(int age) {
48+
this.age = age;
4449
}
45-
/**
46-
*
47-
* The return value of this method will indicate the resource url. The resource url points to the collection of resources that the pojo targets
48-
**/
50+
// override this method. You will return the identifying value for a specific model here. ( id or something like that )
4951
@Override
50-
String resourceUrl() {
51-
return "dogs";
52+
public String getIdentifierValue() {
53+
return name;
5254
}
53-
/**
54-
* This method returns the identifying data for this resource. for example this might be this.id or this.name or something else.
55-
*
56-
**/
57-
@Override
58-
String identifierValue() {
59-
60-
return "identifier";
6155

56+
public String getName() {
57+
return name;
6258
}
6359

60+
public void setName(String name) {
61+
this.name = name;
62+
}
6463

64+
public static final Finder find = new Finder(DogModel.class);
6565
}
66+
67+
6668
```
6769

68-
The good part with this model is that we will not need to write any marshall or unmarshalling logic. This is handled by the RestModel class. But if you get into trouble you can use jackson json annotations on your fields. For example there might be times when you want to exclude some data from de json marshalling, or perhaps rename.
70+
The good part with this model is that we will not need to write any marshall or unmarshalling logic. This is handled behind the sceens. But if you get into trouble you can use jackson json annotations on your fields. For example there might be times when you want to exclude some data from de json marshalling, or perhaps rename some data.
6971

7072

71-
So now we have created a model. Its now time to use it to fetch data. Simply write the following in your code. Remember, these operations are async so we will add a sleep in the end ( only for educational purpose ).
73+
So now we have created a model. Its now time to use it to fetch data. Simply do the following.
7274

7375

7476
```java
7577

78+
apiConfig = new ApiConfig.ConfigBuilder().apiLocation("api").port("8081").host("localhost").build();
7679

77-
// 1. create a dog
78-
DogModel pluto = new DogModel();
80+
DogModel model = new DogModel("pluto",4);
7981

80-
// 2. start to fetch the dog, take care of the completable future.
81-
CompletableFuture<RESTModel> futurePluto = pluto.fetch();
82+
DogModel modelWhenSaved = (DogModel) model.save();
8283

8384

84-
future.thenApply(result -> isDone(result)); // sync callback
85-
86-
Thread.sleep(4000);
87-
System.out.println("dying");
85+
// or why not try to delete a dog
86+
87+
model.delete();
88+
89+
// or fetch
90+
91+
fetchedModel = model.fetch();
92+
93+
94+
// or update
95+
96+
updatedModelFromBackend = model.delete();
97+
98+
// why not fetch all dogs?
99+
100+
List<Model> listOfDogs = DogModel.find.all();
88101

102+
103+
89104

90105

91106
```
92107

93-
In the code above, the dog model will call the rest api when the fetch method is invoked. A CompletableFuture is returned, and we can work with that future as an promise.
108+
In the code above, the dog model will call the rest api when the fetch method is invoked. A Dog is returned when data is expected to change.
109+
110+
94111

112+
## contribute?
95113

114+
Sure, no problem! Contributions are welcome!
115+
116+
## Library Structure
117+
118+
The library is very lightweight and only consists of a few classes, displayed here.
119+
![logo](https://raw.githubusercontent.com/code-brewery/java-ormen/pictures/diagram.png)
120+
121+
96122

97123
# LICENSE
98124
The MIT License (MIT)

0 commit comments

Comments
 (0)