linkrest ex (#2647)
This commit is contained in:
@@ -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>
|
||||
Reference in New Issue
Block a user