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();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user