Skip to content

Commit

Permalink
SUP-15603: REST EPs for linked projects of (micro)schema
Browse files Browse the repository at this point in the history
  • Loading branch information
plyhun committed Jan 3, 2024
1 parent 19a55ae commit e6a21c2
Show file tree
Hide file tree
Showing 21 changed files with 357 additions and 66 deletions.
5 changes: 5 additions & 0 deletions LTS-CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ include::content/docs/variables.adoc-include[]
The LTS changelog lists releases which are only accessible via a commercial subscription.
All fixes and changes in LTS releases will be released the next minor release. Changes from LTS 1.4.x will be included in release 1.5.0.

[[v1.10.24]]
== 1.10.24 (TBD)

icon:check[] REST: New endpoints have been added for retrieving referenced projects for schema/microschema.

[[v1.10.23]]
== 1.10.23 (20.12.2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.gentics.mesh.core.rest.error.Errors.error;
import static com.gentics.mesh.rest.Messages.message;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;

Expand All @@ -21,6 +22,9 @@
import com.gentics.mesh.core.data.dao.BranchDao;
import com.gentics.mesh.core.data.dao.MicroschemaDao;
import com.gentics.mesh.core.data.dao.UserDao;
import com.gentics.mesh.core.data.page.Page;
import com.gentics.mesh.core.data.page.PageTransformer;
import com.gentics.mesh.core.data.page.impl.DynamicStreamPageImpl;
import com.gentics.mesh.core.data.project.HibProject;
import com.gentics.mesh.core.data.schema.HibMicroschema;
import com.gentics.mesh.core.data.schema.HibMicroschemaVersion;
Expand All @@ -29,15 +33,19 @@
import com.gentics.mesh.core.db.Database;
import com.gentics.mesh.core.endpoint.handler.AbstractCrudHandler;
import com.gentics.mesh.core.rest.MeshEvent;
import com.gentics.mesh.core.rest.error.NotModifiedException;
import com.gentics.mesh.core.rest.microschema.impl.MicroschemaModelImpl;
import com.gentics.mesh.core.rest.microschema.impl.MicroschemaResponse;
import com.gentics.mesh.core.rest.schema.MicroschemaModel;
import com.gentics.mesh.core.rest.schema.change.impl.SchemaChangesListModel;
import com.gentics.mesh.core.result.Result;
import com.gentics.mesh.core.verticle.handler.HandlerUtilities;
import com.gentics.mesh.core.verticle.handler.WriteLock;
import com.gentics.mesh.json.JsonUtil;
import com.gentics.mesh.parameter.PagingParameters;
import com.gentics.mesh.parameter.SchemaUpdateParameters;
import com.gentics.mesh.util.UUIDUtil;
import com.gentics.mesh.util.ValidationUtil;

import dagger.Lazy;

Expand All @@ -52,13 +60,16 @@ public class MicroschemaCrudHandler extends AbstractCrudHandler<HibMicroschema,

private final ProjectMicroschemaLoadAllActionImpl projectMicroschemaLoadAllAction;

private final PageTransformer pageTransformer;

@Inject
public MicroschemaCrudHandler(Database db, MicroschemaComparatorImpl comparator, Lazy<BootstrapInitializer> boot, HandlerUtilities utils,
WriteLock writeLock, ProjectMicroschemaLoadAllActionImpl projectMicroschemaLoadAllAction, MicroschemaDAOActions microschemaActions) {
WriteLock writeLock, ProjectMicroschemaLoadAllActionImpl projectMicroschemaLoadAllAction, MicroschemaDAOActions microschemaActions, PageTransformer pageTransformer) {
super(db, utils, writeLock, microschemaActions);
this.comparator = comparator;
this.boot = boot;
this.projectMicroschemaLoadAllAction = projectMicroschemaLoadAllAction;
this.pageTransformer = pageTransformer;
}

@Override
Expand Down Expand Up @@ -251,4 +262,35 @@ public void handleRemoveMicroschemaFromProject(InternalActionContext ac, String
}
}, () -> ac.send(NO_CONTENT));
}

/**
* Handle the request for the project, linked to the schema with the given UUID.
*
* @param ac
* @param microschemaUuid
*/
public void handleGetLinkedProjects(InternalActionContext ac, String microschemaUuid) {
validateParameter(microschemaUuid, "microschemaUuid");

PagingParameters pagingInfo = ac.getPagingParameters();
ValidationUtil.validate(pagingInfo);

utils.syncTx(ac, tx -> {
MicroschemaDao microschemaDao = tx.microschemaDao();
HibMicroschema microschema = microschemaDao.loadObjectByUuid(ac, microschemaUuid, READ_PERM);

Result<HibProject> result = microschemaDao.findLinkedProjects(microschema);
Page<? extends HibProject> page = new DynamicStreamPageImpl<>(result.stream(), pagingInfo);

// Handle etag
if (ac.getGenericParameters().getETag()) {
String etag = pageTransformer.getETag(page, ac);
ac.setEtag(etag, true);
if (ac.matches(etag, true)) {
throw new NotModifiedException();
}
}
return pageTransformer.transformToRestSync(page, ac, 0);
}, m -> ac.send(m, OK));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import com.gentics.mesh.auth.MeshAuthChainImpl;
import com.gentics.mesh.context.InternalActionContext;
import com.gentics.mesh.core.db.Database;
import com.gentics.mesh.core.endpoint.RolePermissionHandlingEndpoint;
import com.gentics.mesh.core.endpoint.admin.LocalConfigApi;
import com.gentics.mesh.etc.config.MeshOptions;
import com.gentics.mesh.core.endpoint.RolePermissionHandlingEndpoint;
import com.gentics.mesh.parameter.impl.GenericParametersImpl;
import com.gentics.mesh.parameter.impl.PagingParametersImpl;
import com.gentics.mesh.parameter.impl.VersioningParametersImpl;
import com.gentics.mesh.rest.InternalEndpointRoute;
Expand Down Expand Up @@ -62,6 +63,7 @@ public void registerEndPoints() {
addReadHandlers();
addUpdateHandler();
addDeleteHandler();
addMiscHandlers();
addRolePermissionHandler("microschemaUuid", MICROSCHEMA_UUID, "microschema", crudHandler, false);
}

Expand Down Expand Up @@ -193,4 +195,24 @@ private void addCreateHandler() {
crudHandler.handleCreate(wrap(rc));
}, isOrderedBlockingHandlers());
}

private void addMiscHandlers() {
InternalEndpointRoute readOne = createRoute();
readOne.path("/:microschemaUuid/projects");
readOne.addUriParameter("microschemaUuid", "Uuid of the microschema.", MICROSCHEMA_UUID);
readOne.method(GET);
readOne.description("Load the projects, attached to the microschema with the given uuid.");
readOne.exampleResponse(OK, projectExamples.getProjectListResponse(), "Loaded projects.");
readOne.addQueryParameters(GenericParametersImpl.class);
readOne.produces(APPLICATION_JSON);
readOne.blockingHandler(rc -> {
String uuid = rc.request().params().get("microschemaUuid");
if (StringUtils.isEmpty(uuid)) {
rc.next();
} else {
InternalActionContext ac = wrap(rc);
crudHandler.handleGetLinkedProjects(ac, uuid);
}
}, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import com.gentics.mesh.core.data.dao.PersistingSchemaDao;
import com.gentics.mesh.core.data.dao.SchemaDao;
import com.gentics.mesh.core.data.dao.UserDao;
import com.gentics.mesh.core.data.page.Page;
import com.gentics.mesh.core.data.page.PageTransformer;
import com.gentics.mesh.core.data.page.impl.DynamicStreamPageImpl;
import com.gentics.mesh.core.data.perm.InternalPermission;
import com.gentics.mesh.core.data.project.HibProject;
import com.gentics.mesh.core.data.schema.HibMicroschema;
Expand All @@ -34,18 +37,22 @@
import com.gentics.mesh.core.db.Database;
import com.gentics.mesh.core.endpoint.handler.AbstractCrudHandler;
import com.gentics.mesh.core.rest.MeshEvent;
import com.gentics.mesh.core.rest.error.NotModifiedException;
import com.gentics.mesh.core.rest.schema.FieldSchema;
import com.gentics.mesh.core.rest.schema.MicronodeFieldSchema;
import com.gentics.mesh.core.rest.schema.SchemaModel;
import com.gentics.mesh.core.rest.schema.change.impl.SchemaChangesListModel;
import com.gentics.mesh.core.rest.schema.impl.SchemaResponse;
import com.gentics.mesh.core.rest.schema.impl.SchemaUpdateRequest;
import com.gentics.mesh.core.result.Result;
import com.gentics.mesh.core.verticle.handler.HandlerUtilities;
import com.gentics.mesh.core.verticle.handler.WriteLock;
import com.gentics.mesh.json.JsonUtil;
import com.gentics.mesh.parameter.PagingParameters;
import com.gentics.mesh.parameter.SchemaUpdateParameters;
import com.gentics.mesh.search.index.node.NodeIndexHandlerImpl;
import com.gentics.mesh.util.UUIDUtil;
import com.gentics.mesh.util.ValidationUtil;

import dagger.Lazy;

Expand All @@ -62,15 +69,18 @@ public class SchemaCrudHandler extends AbstractCrudHandler<HibSchema, SchemaResp

private final ProjectSchemaLoadAllActionImpl projectSchemaDAOActions;

private final PageTransformer pageTransformer;

@Inject
public SchemaCrudHandler(Database db, SchemaComparatorImpl comparator, Lazy<BootstrapInitializer> boot,
HandlerUtilities utils, NodeIndexHandlerImpl nodeIndexHandler, WriteLock writeLock, ProjectSchemaLoadAllActionImpl projectSchemaDAOActions,
SchemaDAOActions schemaActions) {
SchemaDAOActions schemaActions, PageTransformer pageTransformer) {
super(db, utils, writeLock, schemaActions);
this.comparator = comparator;
this.boot = boot;
this.nodeIndexHandler = nodeIndexHandler;
this.projectSchemaDAOActions = projectSchemaDAOActions;
this.pageTransformer = pageTransformer;
}

/**
Expand Down Expand Up @@ -329,4 +339,34 @@ public void handleApplySchemaChanges(InternalActionContext ac, String schemaUuid

}

/**
* Handle the request for the project, linked to the schema with the given UUID.
*
* @param ac
* @param schemaUuid
*/
public void handleGetLinkedProjects(InternalActionContext ac, String schemaUuid) {
validateParameter(schemaUuid, "schemaUuid");

PagingParameters pagingInfo = ac.getPagingParameters();
ValidationUtil.validate(pagingInfo);

utils.syncTx(ac, tx -> {
SchemaDao schemaDao = tx.schemaDao();
HibSchema schema = schemaDao.loadObjectByUuid(ac, schemaUuid, READ_PERM);

Result<HibProject> result = schemaDao.findLinkedProjects(schema);
Page<? extends HibProject> page = new DynamicStreamPageImpl<>(result.stream(), pagingInfo);

// Handle etag
if (ac.getGenericParameters().getETag()) {
String etag = pageTransformer.getETag(page, ac);
ac.setEtag(etag, true);
if (ac.matches(etag, true)) {
throw new NotModifiedException();
}
}
return pageTransformer.transformToRestSync(page, ac, 0);
}, m -> ac.send(m, OK));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void registerEndPoints() {
addCreateHandler();
addUpdateHandler();
addDeleteHandler();
addMiscHandlers();

addRolePermissionHandler("schemaUuid", SCHEMA_VEHICLE_UUID, "schema", crudHandler, false);
}
Expand Down Expand Up @@ -202,6 +203,25 @@ private void addReadHandlers() {
InternalActionContext ac = wrap(rc);
crudHandler.handleReadList(ac);
}, false);
}

private void addMiscHandlers() {
InternalEndpointRoute readOne = createRoute();
readOne.path("/:schemaUuid/projects");
readOne.addUriParameter("schemaUuid", "Uuid of the schema.", SCHEMA_VEHICLE_UUID);
readOne.method(GET);
readOne.description("Load the projects, attached to the schema with the given uuid.");
readOne.exampleResponse(OK, projectExamples.getProjectListResponse(), "Loaded projects.");
readOne.addQueryParameters(GenericParametersImpl.class);
readOne.produces(APPLICATION_JSON);
readOne.blockingHandler(rc -> {
String uuid = rc.request().params().get("schemaUuid");
if (StringUtils.isEmpty(uuid)) {
rc.next();
} else {
InternalActionContext ac = wrap(rc);
crudHandler.handleGetLinkedProjects(ac, uuid);
}
}, false);
}
}
16 changes: 16 additions & 0 deletions core/src/main/java/com/gentics/mesh/rest/MeshLocalClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,22 @@ public MeshRequest<SchemaListResponse> findSchemas(String projectName, Parameter
return new MeshLocalRequestImpl<>(ac.getFuture());
}

@Override
public MeshRequest<ProjectListResponse> findSchemaProjects(String uuid, ParameterProvider... parameters) {
LocalActionContextImpl<ProjectListResponse> ac = createContext(ProjectListResponse.class, parameters);
ac.setParameter("schemaUuid", uuid);
schemaCrudHandler.handleGetLinkedProjects(ac, uuid);
return new MeshLocalRequestImpl<>(ac.getFuture());
}

@Override
public MeshRequest<ProjectListResponse> findMicroschemaProjects(String uuid, ParameterProvider... parameters) {
LocalActionContextImpl<ProjectListResponse> ac = createContext(ProjectListResponse.class, parameters);
ac.setParameter("microschemaUuid", uuid);
microschemaCrudHandler.handleGetLinkedProjects(ac, uuid);
return new MeshLocalRequestImpl<>(ac.getFuture());
}

@Override
public MeshRequest<MicroschemaResponse> assignMicroschemaToProject(String projectName, String microschemaUuid) {
LocalActionContextImpl<MicroschemaResponse> ac = createContext(MicroschemaResponse.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,12 @@ public interface ContainerDao<
* @param batch
*/
void unassign(SC schema, HibProject project, EventQueueBatch batch);

/**
* Find all projects which reference the schema.
*
* @param schema
* @return
*/
Result<HibProject> findLinkedProjects(SC schema);
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ default HibSchema create(SchemaVersionModel schema, HibUser creator) throws Mesh
*/
Result<? extends HibNode> getNodes(HibSchema schema);

/**
* Find all projects which reference the schema.
*
* @param schema
* @return
*/
Result<HibProject> findLinkedProjects(HibSchema schema);

/**
* Load all nodes, accessible in the given branch with Read Published permission.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ default HibMicroschemaVersion fromReference(HibProject project, MicroschemaRefer
return fromReference(project, reference, null);
}

/**
* Find all projects which reference the schema.
*
* @param schema
* @return
*/
@Override
default Result<HibProject> findLinkedProjects(HibMicroschema schema) {
return new TraversalResult<>(Tx.get().projectDao()
.findAll().stream().filter(project -> isLinkedToProject(schema, project)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,7 @@ default HibSchema create(SchemaVersionModel schema, HibUser creator, String uuid
return mergeIntoPersisted(container);
}

/**
* Find all projects which reference the schema.
*
* @param schema
* @return
*/
@Override
default Result<HibProject> findLinkedProjects(HibSchema schema) {
return new TraversalResult<>(Tx.get().projectDao()
.findAll().stream().filter(project -> isLinkedToProject(schema, project)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.gentics.mesh.core.data.dao;

import com.gentics.mesh.core.data.root.MicroschemaRoot;
import com.gentics.mesh.core.data.schema.HibMicroschema;
import com.gentics.mesh.core.result.Result;

/**
* DAO for {@link HibMicroschema} operations.
*/
public interface MicroschemaDaoWrapper extends PersistingMicroschemaDao {

/**
* Return a list of all microschema container roots to which the microschema container was added.
*
* @return
*/
Result<? extends MicroschemaRoot> getRoots(HibMicroschema schema);

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gentics.mesh.core.data.root;

import com.gentics.mesh.core.data.project.HibProject;
import com.gentics.mesh.core.data.schema.HibMicroschema;
import com.gentics.mesh.core.data.schema.Microschema;
import com.gentics.mesh.core.result.Result;
import com.gentics.mesh.event.EventQueueBatch;

/**
Expand All @@ -24,4 +26,18 @@ public interface MicroschemaRoot extends RootVertex<Microschema> {
* @return
*/
boolean contains(HibMicroschema microschema);

/**
* Get the project.
*
* @return project
*/
HibProject getProject();

/**
* Return a list of all microschema container roots to which the microschema container was added.
*
* @return
*/
Result<? extends MicroschemaRoot> getRoots(Microschema schema);
}
Loading

0 comments on commit e6a21c2

Please sign in to comment.