linkrest ex (#2647)
This commit is contained in:
parent
90a102ec47
commit
60acc2fd1d
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
id="WebApp_ID" version="3.1">
|
||||
<display-name>linkrest</display-name>
|
||||
<servlet>
|
||||
<servlet-name>linkrest</servlet-name>
|
||||
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
|
||||
<init-param>
|
||||
<param-name>javax.ws.rs.Application</param-name>
|
||||
<param-value>com.baeldung.LinkRestApplication</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>linkrest</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
|
@ -0,0 +1,81 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>linkrest</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.nhl.link.rest</groupId>
|
||||
<artifactId>link-rest</artifactId>
|
||||
<version>${linkrest.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
<artifactId>jersey-container-servlet</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.media</groupId>
|
||||
<artifactId>jersey-media-moxy</artifactId>
|
||||
<version>${jersey.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>WebContent</warSourceDirectory>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.cayenne.plugins</groupId>
|
||||
<artifactId>cayenne-maven-plugin</artifactId>
|
||||
<version>${cayenne.version}</version>
|
||||
|
||||
<configuration>
|
||||
<map>${project.basedir}/src/main/resources/linkrest.map.xml</map>
|
||||
</configuration>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>cgen</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.cayenne.plugins</groupId>
|
||||
<artifactId>cayenne-modeler-maven-plugin</artifactId>
|
||||
<version>${cayenne.version}</version>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<linkrest.version>2.9</linkrest.version>
|
||||
<cayenne.version>4.0.B1</cayenne.version>
|
||||
<h2.version>1.4.196</h2.version>
|
||||
<jersey.version>2.25.1</jersey.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung;
|
||||
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
|
||||
import org.apache.cayenne.configuration.server.ServerRuntime;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
|
||||
import com.nhl.link.rest.runtime.LinkRestBuilder;
|
||||
import com.nhl.link.rest.runtime.LinkRestRuntime;
|
||||
|
||||
@ApplicationPath("/linkrest")
|
||||
public class LinkRestApplication extends ResourceConfig {
|
||||
|
||||
public LinkRestApplication() {
|
||||
ServerRuntime cayenneRuntime = ServerRuntime.builder()
|
||||
.addConfig("cayenne-linkrest-project.xml")
|
||||
.build();
|
||||
LinkRestRuntime lrRuntime = LinkRestBuilder.build(cayenneRuntime);
|
||||
super.register(lrRuntime);
|
||||
packages("com.baeldung.linkrest.apis");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.cayenne;
|
||||
|
||||
import com.baeldung.cayenne.auto._Department;
|
||||
|
||||
public class Department extends _Department {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.cayenne;
|
||||
|
||||
import com.baeldung.cayenne.auto._Employee;
|
||||
|
||||
public class Employee extends _Employee {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.cayenne.auto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.cayenne.CayenneDataObject;
|
||||
import org.apache.cayenne.exp.Property;
|
||||
|
||||
import com.baeldung.cayenne.Employee;
|
||||
|
||||
/**
|
||||
* Class _Department was generated by Cayenne.
|
||||
* It is probably a good idea to avoid changing this class manually,
|
||||
* since it may be overwritten next time code is regenerated.
|
||||
* If you need to make any customizations, please use subclass.
|
||||
*/
|
||||
public abstract class _Department extends CayenneDataObject {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String DEP_ID_PK_COLUMN = "dep_id";
|
||||
|
||||
public static final Property<String> NAME = Property.create("name", String.class);
|
||||
public static final Property<List<Employee>> EMPLOYEES = Property.create("employees", List.class);
|
||||
|
||||
public void setName(String name) {
|
||||
writeProperty("name", name);
|
||||
}
|
||||
public String getName() {
|
||||
return (String)readProperty("name");
|
||||
}
|
||||
|
||||
public void addToEmployees(Employee obj) {
|
||||
addToManyTarget("employees", obj, true);
|
||||
}
|
||||
public void removeFromEmployees(Employee obj) {
|
||||
removeToManyTarget("employees", obj, true);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Employee> getEmployees() {
|
||||
return (List<Employee>)readProperty("employees");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.cayenne.auto;
|
||||
|
||||
import org.apache.cayenne.CayenneDataObject;
|
||||
import org.apache.cayenne.exp.Property;
|
||||
|
||||
import com.baeldung.cayenne.Department;
|
||||
|
||||
/**
|
||||
* Class _Employee was generated by Cayenne.
|
||||
* It is probably a good idea to avoid changing this class manually,
|
||||
* since it may be overwritten next time code is regenerated.
|
||||
* If you need to make any customizations, please use subclass.
|
||||
*/
|
||||
public abstract class _Employee extends CayenneDataObject {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final String EMP_ID_PK_COLUMN = "emp_id";
|
||||
|
||||
public static final Property<String> NAME = Property.create("name", String.class);
|
||||
public static final Property<Department> DEPARTMENT = Property.create("department", Department.class);
|
||||
|
||||
public void setName(String name) {
|
||||
writeProperty("name", name);
|
||||
}
|
||||
public String getName() {
|
||||
return (String)readProperty("name");
|
||||
}
|
||||
|
||||
public void setDepartment(Department department) {
|
||||
setToOneTarget("department", department, true);
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return (Department)readProperty("department");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.linkrest.apis;
|
||||
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Configuration;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import com.baeldung.cayenne.Department;
|
||||
import com.nhl.link.rest.DataResponse;
|
||||
import com.nhl.link.rest.LinkRest;
|
||||
import com.nhl.link.rest.SimpleResponse;
|
||||
|
||||
@Path("department")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class DepartmentResource {
|
||||
|
||||
@Context
|
||||
private Configuration config;
|
||||
|
||||
@GET
|
||||
public DataResponse<Department> getAll(@Context UriInfo uriInfo) {
|
||||
return LinkRest.select(Department.class, config).uri(uriInfo).get();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
public DataResponse<Department> getOne(@PathParam("id") int id, @Context UriInfo uriInfo) {
|
||||
return LinkRest.select(Department.class, config).byId(id).uri(uriInfo).getOne();
|
||||
}
|
||||
|
||||
@POST
|
||||
public SimpleResponse create(String data) {
|
||||
return LinkRest.create(Department.class, config).sync(data);
|
||||
}
|
||||
|
||||
@PUT
|
||||
public SimpleResponse createOrUpdate(String data) {
|
||||
return LinkRest.createOrUpdate(Department.class, config).sync(data);
|
||||
}
|
||||
|
||||
@Path("{id}/employees")
|
||||
public EmployeeSubResource getEmployees(@PathParam("id") int id, @Context UriInfo uriInfo) {
|
||||
return new EmployeeSubResource(id, config);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
public SimpleResponse delete(@PathParam("id") int id) {
|
||||
return LinkRest.delete(Department.class, config).id(id).delete();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.linkrest.apis;
|
||||
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Configuration;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import com.baeldung.cayenne.Department;
|
||||
import com.baeldung.cayenne.Employee;
|
||||
import com.nhl.link.rest.DataResponse;
|
||||
import com.nhl.link.rest.LinkRest;
|
||||
import com.nhl.link.rest.SimpleResponse;
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class EmployeeSubResource {
|
||||
|
||||
private Configuration config;
|
||||
|
||||
private int departmentId;
|
||||
|
||||
public EmployeeSubResource(int departmentId, Configuration config) {
|
||||
this.departmentId = departmentId;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public EmployeeSubResource() {
|
||||
}
|
||||
|
||||
@GET
|
||||
public DataResponse<Employee> getAll(@Context UriInfo uriInfo) {
|
||||
return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).uri(uriInfo).get();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("{id}")
|
||||
public DataResponse<Employee> getOne(@PathParam("id") int id, @Context UriInfo uriInfo) {
|
||||
return LinkRest.select(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).byId(id).uri(uriInfo).getOne();
|
||||
}
|
||||
|
||||
@POST
|
||||
public SimpleResponse create(String data) {
|
||||
|
||||
return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data);
|
||||
}
|
||||
|
||||
@PUT
|
||||
public SimpleResponse createOrUpdate(String data) {
|
||||
return LinkRest.create(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).sync(data);
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
public SimpleResponse delete(@PathParam("id") int id) {
|
||||
return LinkRest.delete(Employee.class, config).toManyParent(Department.class, departmentId, Department.EMPLOYEES).id(id).delete();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<domain project-version="9">
|
||||
<map name="linkrest"/>
|
||||
|
||||
<node name="datanode"
|
||||
factory="org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory"
|
||||
schema-update-strategy="org.apache.cayenne.access.dbsync.CreateIfNoSchemaStrategy"
|
||||
>
|
||||
<map-ref name="linkrest"/>
|
||||
<data-source>
|
||||
<driver value="org.h2.Driver"/>
|
||||
<url value="jdbc:h2:mem:myDb;MVCC=TRUE"/>
|
||||
<connectionPool min="1" max="1"/>
|
||||
<login userName="sa" password="sa"/>
|
||||
</data-source>
|
||||
</node>
|
||||
</domain>
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<data-map xmlns="http://cayenne.apache.org/schema/9/modelMap"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://cayenne.apache.org/schema/9/modelMap http://cayenne.apache.org/schema/9/modelMap.xsd"
|
||||
project-version="9">
|
||||
<property name="defaultPackage" value="com.baeldung.cayenne"/>
|
||||
<db-entity name="departments">
|
||||
<db-attribute name="dep_id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
|
||||
<db-attribute name="name" type="NVARCHAR" length="50"/>
|
||||
</db-entity>
|
||||
<db-entity name="employees">
|
||||
<db-attribute name="emp_id" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
|
||||
<db-attribute name="name" type="NVARCHAR" length="50"/>
|
||||
</db-entity>
|
||||
<obj-entity name="Department" className="com.baeldung.cayenne.Department" dbEntityName="departments">
|
||||
<obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
|
||||
</obj-entity>
|
||||
<obj-entity name="Employee" className="com.baeldung.cayenne.Employee" dbEntityName="employees">
|
||||
<obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
|
||||
</obj-entity>
|
||||
<db-relationship name="employees" source="departments" target="employees" toMany="true">
|
||||
<db-attribute-pair source="dep_id" target="emp_id"/>
|
||||
</db-relationship>
|
||||
<db-relationship name="department" source="employees" target="departments" toDependentPK="true" toMany="false">
|
||||
<db-attribute-pair source="emp_id" target="dep_id"/>
|
||||
</db-relationship>
|
||||
<obj-relationship name="employees" source="Department" target="Employee" deleteRule="Deny" db-relationship-path="employees"/>
|
||||
<obj-relationship name="department" source="Employee" target="Department" deleteRule="Nullify" db-relationship-path="department"/>
|
||||
</data-map>
|
Loading…
Reference in New Issue