|
1 | 1 | [](https://travis-ci.org/code-brewery/java-ormen)
|
2 | 2 | [](http://codecov.io/github/code-brewery/java-ormen?branch=master)
|
3 | 3 | # 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. |
5 | 5 |
|
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. |
7 | 7 |
|
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. |
9 | 13 |
|
10 |
| -The library uses the Jackson ObjectMapper, so its possible to use jackson annotations to describe how objects shall be marshalled and unmarshalled. |
11 | 14 |
|
12 |
| -The library strives for creating immutable objects. This means that you can have control over your objects. |
13 | 15 |
|
14 | 16 | ## Usage
|
15 | 17 |
|
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. |
17 | 22 |
|
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 |
19 | 24 |
|
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 { |
21 | 27 |
|
| 28 | + private String name; |
22 | 29 |
|
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; |
28 | 31 |
|
29 |
| -Look at the comments, it will give you some hints about what information you need to provide for successfully send requests over rest. |
30 | 32 |
|
31 |
| -```java |
| 33 | + public DogModel(String name, int age) { |
| 34 | + this.name = name; |
| 35 | + this.age = age; |
| 36 | + } |
32 | 37 |
|
33 |
| -public class MockRestModel extends RESTModel { |
| 38 | + DogModel() { |
| 39 | + this.name =""; |
| 40 | + this.age = 0; |
| 41 | + } |
34 | 42 |
|
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; |
40 | 45 | }
|
41 | 46 |
|
42 |
| - public MockRestModel(String name) { |
43 |
| - this.name = name; |
| 47 | + public void setAge(int age) { |
| 48 | + this.age = age; |
44 | 49 | }
|
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 ) |
49 | 51 | @Override
|
50 |
| - String resourceUrl() { |
51 |
| - return "dogs"; |
| 52 | + public String getIdentifierValue() { |
| 53 | + return name; |
52 | 54 | }
|
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"; |
61 | 55 |
|
| 56 | + public String getName() { |
| 57 | + return name; |
62 | 58 | }
|
63 | 59 |
|
| 60 | + public void setName(String name) { |
| 61 | + this.name = name; |
| 62 | + } |
64 | 63 |
|
| 64 | + public static final Finder find = new Finder(DogModel.class); |
65 | 65 | }
|
| 66 | + |
| 67 | + |
66 | 68 | ```
|
67 | 69 |
|
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. |
69 | 71 |
|
70 | 72 |
|
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. |
72 | 74 |
|
73 | 75 |
|
74 | 76 | ```java
|
75 | 77 |
|
| 78 | + apiConfig = new ApiConfig.ConfigBuilder().apiLocation("api").port("8081").host("localhost").build(); |
76 | 79 |
|
77 |
| - // 1. create a dog |
78 |
| - DogModel pluto = new DogModel(); |
| 80 | + DogModel model = new DogModel("pluto",4); |
79 | 81 |
|
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(); |
82 | 83 |
|
83 | 84 |
|
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(); |
88 | 101 |
|
| 102 | + |
| 103 | + |
89 | 104 |
|
90 | 105 |
|
91 | 106 | ```
|
92 | 107 |
|
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 | + |
94 | 111 |
|
| 112 | +## contribute? |
95 | 113 |
|
| 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 | + |
| 120 | + |
| 121 | + |
96 | 122 |
|
97 | 123 | # LICENSE
|
98 | 124 | The MIT License (MIT)
|
0 commit comments