Skip to content

Commit

Permalink
Merge pull request #20 from Prygan/dev
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
Dnomyar authored Oct 31, 2016
2 parents 641837e + 669d9f0 commit eff2be4
Show file tree
Hide file tree
Showing 83 changed files with 6,680 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,4 @@ local.properties

# Code Recommenders
.recommenders/
/target/
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: java
jdk:
- oraclejdk8
script:
- mvn test
168 changes: 166 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,166 @@
# EventManager-JEE
A webapp to manage public events
# EventManager-JEE [![Build Status](https://travis-ci.com/Prygan/EventManager-JEE.svg?token=cocJpTsskx3dZagw8Jqi&branch=dev)](https://travis-ci.com/Prygan/EventManager-JEE)

Dino planner is a school project we did for the Ecole des Mines de Nantes. The aims are to use what we learned in JEE class but also to improve our ergonomic skills.


Table of contents :
- Architecture
- Router
- Flash
- CRUD database


## Architecture
```
src/
|--main/
|--java/
|--fr.lidadi.jee.eventmanager/
|--app/ (contains business part (controllers, services, daos and entity) organised by entity)
|--conf/
|--Router.java (Router configuration)
|--framework/
|--dao/ (manage database connection, and interaction)
|--sqldsl/ (A SQL DSL used by findBy())
|--...
|--jsptags (custom jsp tags)
|--router (router core code)
|--utils (some util tools)
|--resources/META-INF/persistence.xml (database information)
|--webapp/
|--assets/
|--css/ (contains all CSS)
|--js/ (contains all JavaScript)
|--WEB-INF/
|--jsp/ (contains all JSP of the app organized by entity)
|--app.tld (needed to use custom tags and funtions in JSP)
|--web.xml (define routes (not used))
|--test
```


## Router
The usage of the router is very simple. The first step is to configure a route. And then, you just have to implement the method. The following example will guide you.

This is router is powerfull :
- it is able to **extract parameters from URL** and to **check there type** (UUID, STRING or INT). And then, give them to controller methods without require any cast !
- it is able to filter according to controllers needs :
- `HttpServletRequest` is default request (no filter)
- `UserAwareRequest` user can be logged, but it is not required. Then it is possible to get an `Optional<Person>` thanks to `req.getUser()`.
- `SecuredRequest` user have to be logged (error if not). Then it is to possible get the logged `Person` thanks to `req.getUser()`.

See examples below.

### Route configuration
```java
public class Router implements HttpRouter {

@Override
public Config.HttpConfig route(Config.EmptyHttpConfig config) {
return config
.get("/")
.to("fr.lidadi.jee.eventmanager.EventServlet.welcome()")
.get("/events")
.to("fr.lidadi.jee.eventmanager.EventServlet.fetchAll()")
.get("/events/{id}")
.to("fr.lidadi.jee.eventmanager.EventServlet.fetch(UUID id)")
.get("/events/{eventId}/group/{groupId}/members")
.to("fr.lidadi.jee.eventmanager.EventServlet.test(UUID eventId, INT groupId)")
.post("/events")
.to("fr.lidadi.jee.eventmanager.EventServlet.add()")
.put("/events")
.to("fr.lidadi.jee.eventmanager.EventServlet.update()")
.delete("/events/{id}")
.to("fr.lidadi.jee.eventmanager.EventServlet.delete(UUID id)");
}
}
```

### Controller and method definition
```java
public class EventServlet implements HttpErrorResponse {

public void fetchAll(HttpServlet servlet, SecuredRequest req, HttpServletResponse resp) throws ServletException, IOException {
ok(resp, "fetchAll");
}

public void welcome(HttpServlet servlet, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
okJsp(servlet, req, resp, "/welcome.jsp");
}


public void fetch(HttpServlet servlet, UserAwareRequest req, HttpServletResponse resp, UUID id) throws ServletException, IOException {
ok(resp, "fetch(" + id + ")");
}
}
```

## Flash
Flash messages are :
- set in a controller
- display in view **only once** (the message is deleted after)

For the moment this is implemented using http sessions.

### Set a message
Associate message "Wrong credentials" to key "error" :
```java
private Flashing flashing = new Flashing();
// ...
flashing.flashing(req.getSession(), "error", "Wrong credentials");
```


### Test if a key is defined & display a message
Get the message associated to key "error" :
```java
<c:if test="${app:flashExist(sessionScope, \"error\")}">
<div class="alert alert-error">${app:consumeFlash(sessionScope, "error")}</div>
</c:if>
```

## CRUD database
In order to communicate with database, you have to :
- Create an entity that extends `fr.lidadi.jee.eventmanager.framework.dao.Entity`
- Create a Dao that extends `fr.lidadi.jee.eventmanager.framework.dao.Dao`

Then, you can use defined methods such as :
- getAll()
- get(PK id) : get an entity from it primary key (PK is the type of the primary key)
- findBy(SQLClauses sqlClauses) : equivalent to getAll with conditions
- add(T entity)
- update(T entity)
- delete(PK id)


### SQL DSL
We have defined a DSL to easily request entities with conditions. The two examples below show how it works.

#### Where
To get all event that is today, at Nantes or at Angers :
```java
findBy(
where(
and(
equal("date", today),
or(
equal("location", "Nantes"),
equal("location", "Angers"),
)
)
)
)
```

#### Like (not implemented yet)
To get all event that location starts with "P".
```java
findBy(
like(
equal("location", "P%")
)
)
```

16 changes: 16 additions & 0 deletions clevercloud/war.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"build": {
"type": "maven",
"goal": "package"
},
"deploy": {
"container": "TOMCAT7",
"war": [
{
"file": "target/eventmanager-1.0-SNAPSHOT.war",
"context": "/",
"checkMe": "/"
}
]
}
}
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,52 @@


<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.11.1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>7.0.47</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/fr/lidadi/jee/eventmanager/Application.java

This file was deleted.

Loading

0 comments on commit eff2be4

Please sign in to comment.