Skip to content

Commit 60acc2f

Browse files
lor6pivovarit
authored andcommitted
linkrest ex (eugenp#2647)
1 parent 90a102e commit 60acc2f

13 files changed

Lines changed: 400 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
id="WebApp_ID" version="3.1">
6+
<display-name>linkrest</display-name>
7+
<servlet>
8+
<servlet-name>linkrest</servlet-name>
9+
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
10+
<init-param>
11+
<param-name>javax.ws.rs.Application</param-name>
12+
<param-value>com.baeldung.LinkRestApplication</param-value>
13+
</init-param>
14+
<load-on-startup>1</load-on-startup>
15+
</servlet>
16+
<servlet-mapping>
17+
<servlet-name>linkrest</servlet-name>
18+
<url-pattern>/*</url-pattern>
19+
</servlet-mapping>
20+
</web-app>

‎linkrest/pom.xml‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>linkrest</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.nhl.link.rest</groupId>
12+
<artifactId>link-rest</artifactId>
13+
<version>${linkrest.version}</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.glassfish.jersey.containers</groupId>
17+
<artifactId>jersey-container-servlet</artifactId>
18+
<version>${jersey.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.glassfish.jersey.media</groupId>
22+
<artifactId>jersey-media-moxy</artifactId>
23+
<version>${jersey.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.h2database</groupId>
27+
<artifactId>h2</artifactId>
28+
<version>${h2.version}</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.5</version>
37+
<configuration>
38+
<source>1.8</source>
39+
<target>1.8</target>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<artifactId>maven-war-plugin</artifactId>
44+
<version>2.6</version>
45+
<configuration>
46+
<warSourceDirectory>WebContent</warSourceDirectory>
47+
<failOnMissingWebXml>false</failOnMissingWebXml>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.cayenne.plugins</groupId>
52+
<artifactId>cayenne-maven-plugin</artifactId>
53+
<version>${cayenne.version}</version>
54+
55+
<configuration>
56+
<map>${project.basedir}/src/main/resources/linkrest.map.xml</map>
57+
</configuration>
58+
59+
<executions>
60+
<execution>
61+
<goals>
62+
<goal>cgen</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.cayenne.plugins</groupId>
69+
<artifactId>cayenne-modeler-maven-plugin</artifactId>
70+
<version>${cayenne.version}</version>
71+
</plugin>
72+
73+
</plugins>
74+
</build>
75+
<properties>
76+
<linkrest.version>2.9</linkrest.version>
77+
<cayenne.version>4.0.B1</cayenne.version>
78+
<h2.version>1.4.196</h2.version>
79+
<jersey.version>2.25.1</jersey.version>
80+
</properties>
81+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung;
2+
3+
4+
import javax.ws.rs.ApplicationPath;
5+
6+
import org.apache.cayenne.configuration.server.ServerRuntime;
7+
import org.glassfish.jersey.server.ResourceConfig;
8+
9+
import com.nhl.link.rest.runtime.LinkRestBuilder;
10+
import com.nhl.link.rest.runtime.LinkRestRuntime;
11+
12+
@ApplicationPath("/linkrest")
13+
public class LinkRestApplication extends ResourceConfig {
14+
15+
public LinkRestApplication() {
16+
ServerRuntime cayenneRuntime = ServerRuntime.builder()
17+
.addConfig("cayenne-linkrest-project.xml")
18+
.build();
19+
LinkRestRuntime lrRuntime = LinkRestBuilder.build(cayenneRuntime);
20+
super.register(lrRuntime);
21+
packages("com.baeldung.linkrest.apis");
22+
}
23+
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.cayenne;
2+
3+
import com.baeldung.cayenne.auto._Department;
4+
5+
public class Department extends _Department {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.cayenne;
2+
3+
import com.baeldung.cayenne.auto._Employee;
4+
5+
public class Employee extends _Employee {
6+
7+
private static final long serialVersionUID = 1L;
8+
9+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.cayenne.auto;
2+
3+
import java.util.List;
4+
5+
import org.apache.cayenne.CayenneDataObject;
6+
import org.apache.cayenne.exp.Property;
7+
8+
import com.baeldung.cayenne.Employee;
9+
10+
/**
11+
* Class _Department was generated by Cayenne.
12+
* It is probably a good idea to avoid changing this class manually,
13+
* since it may be overwritten next time code is regenerated.
14+
* If you need to make any customizations, please use subclass.
15+
*/
16+
public abstract class _Department extends CayenneDataObject {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
public static final String DEP_ID_PK_COLUMN = "dep_id";
21+
22+
public static final Property<String> NAME = Property.create("name", String.class);
23+
public static final Property<List<Employee>> EMPLOYEES = Property.create("employees", List.class);
24+
25+
public void setName(String name) {
26+
writeProperty("name", name);
27+
}
28+
public String getName() {
29+
return (String)readProperty("name");
30+
}
31+
32+
public void addToEmployees(Employee obj) {
33+
addToManyTarget("employees", obj, true);
34+
}
35+
public void removeFromEmployees(Employee obj) {
36+
removeToManyTarget("employees", obj, true);
37+
}
38+
@SuppressWarnings("unchecked")
39+
public List<Employee> getEmployees() {
40+
return (List<Employee>)readProperty("employees");
41+
}
42+
43+
44+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.cayenne.auto;
2+
3+
import org.apache.cayenne.CayenneDataObject;
4+
import org.apache.cayenne.exp.Property;
5+
6+
import com.baeldung.cayenne.Department;
7+
8+
/**
9+
* Class _Employee was generated by Cayenne.
10+
* It is probably a good idea to avoid changing this class manually,
11+
* since it may be overwritten next time code is regenerated.
12+
* If you need to make any customizations, please use subclass.
13+
*/
14+
public abstract class _Employee extends CayenneDataObject {
15+
16+
private static final long serialVersionUID = 1L;
17+
18+
public static final String EMP_ID_PK_COLUMN = "emp_id";
19+
20+
public static final Property<String> NAME = Property.create("name", String.class);
21+
public static final Property<Department> DEPARTMENT = Property.create("department", Department.class);
22+
23+
public void setName(String name) {
24+
writeProperty("name", name);
25+
}
26+
public String getName() {
27+
return (String)readProperty("name");
28+
}
29+
30+
public void setDepartment(Department department) {
31+
setToOneTarget("department", department, true);
32+
}
33+
34+
public Department getDepartment() {
35+
return (Department)readProperty("department");
36+
}
37+
38+
39+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.linkrest.apis;
2+
3+
import javax.ws.rs.DELETE;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.POST;
6+
import javax.ws.rs.PUT;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.PathParam;
9+
import javax.ws.rs.Produces;
10+
import javax.ws.rs.core.Configuration;
11+
import javax.ws.rs.core.Context;
12+
import javax.ws.rs.core.MediaType;
13+
import javax.ws.rs.core.UriInfo;
14+
15+
import com.baeldung.cayenne.Department;
16+
import com.nhl.link.rest.DataResponse;
17+
import com.nhl.link.rest.LinkRest;
18+
import com.nhl.link.rest.SimpleResponse;
19+
20+
@Path("department")
21+
@Produces(MediaType.APPLICATION_JSON)
22+
public class DepartmentResource {
23+
24+
@Context
25+
private Configuration config;
26+
27+
@GET
28+
public DataResponse<Department> getAll(@Context UriInfo uriInfo) {
29+
return LinkRest.select(Department.class, config).uri(uriInfo).get();
30+
}
31+
32+
@GET
33+
@Path("{id}")
34+
public DataResponse<Department> getOne(@PathParam("id") int id, @Context UriInfo uriInfo) {
35+
return LinkRest.select(Department.class, config).byId(id).uri(uriInfo).getOne();
36+
}
37+
38+
@POST
39+
public SimpleResponse create(String data) {
40+
return LinkRest.create(Department.class, config).sync(data);
41+
}
42+
43+
@PUT
44+
public SimpleResponse createOrUpdate(String data) {
45+
return LinkRest.createOrUpdate(Department.class, config).sync(data);
46+
}
47+
48+
@Path("{id}/employees")
49+
public EmployeeSubResource getEmployees(@PathParam("id") int id, @Context UriInfo uriInfo) {
50+
return new EmployeeSubResource(id, config);
51+
}
52+
53+
@DELETE
54+
@Path("{id}")
55+
public SimpleResponse delete(@PathParam("id") int id) {
56+
return LinkRest.delete(Department.class, config).id(id).delete();
57+
}
58+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.linkrest.apis;
2+
3+
import javax.ws.rs.DELETE;
4+
import javax.ws.rs.GET;
5+
import javax.ws.rs.POST;
6+
import javax.ws.rs.PUT;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.PathParam;
9+
import javax.ws.rs.Produces;
10+
import javax.ws.rs.core.Configuration;
11+
import javax.ws.rs.core.Context;
12+
import javax.ws.rs.core.MediaType;
13+
import javax.ws.rs.core.UriInfo;
14+
15+
import com.baeldung.cayenne.Department;
16+
import com.baeldung.cayenne.Employee;
17+
import com.nhl.link.rest.DataResponse;
18+
import com.nhl.link.rest.LinkRest;
19+
import com.nhl.link.rest.SimpleResponse;
20+
21+
@Produces(MediaType.APPLICATION_JSON)
22+
public class EmployeeSubResource {
23+
24+
private Configuration config;
25+
26+
private int departmentId;
27+
28+
public EmployeeSubResource(int departmentId, Configuration config) {
29+
this.departmentId = departmentId;
30+
this.config = config;
31+
}
32+
33+
public EmployeeSubResource() {
34+
}
35+
36+
@GET
37+
public DataResponse<Employee> getAll(@Context UriInfo uriInfo) {
38+
return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).uri(uriInfo).get();
39+
}
40+
41+
@GET
42+
@Path("{id}")
43+
public DataResponse<Employee> getOne(@PathParam("id") int id, @Context UriInfo uriInfo) {
44+
return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).byId(id).uri(uriInfo).getOne();
45+
}
46+
47+
@POST
48+
public SimpleResponse create(String data) {
49+
50+
return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data);
51+
}
52+
53+
@PUT
54+
public SimpleResponse createOrUpdate(String data) {
55+
return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data);
56+
}
57+
58+
@DELETE
59+
@Path("{id}")
60+
public SimpleResponse delete(@PathParam("id") int id) {
61+
return LinkRest.delete(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).id(id).delete();
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)