Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic validations on Export API #942

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions learn/how-tos/validationson-exportapi-using-crudlisteners.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
title: "Dynamic Validations on Export API using CRUD Listeners"
id: "validationson-exportapi-using-crudListeners"
sidebar_label: "Validations using CRUD Listeners on Export API"
---
## Introduction

In this document, learn how we can implement dynamic validations on Export API.

The existing listeners are designed specifically for dynamic validations during CRUD operations. To perform dynamic validations on the Export API, we need to incorporate a new listener for export operations. To achieve this, include the following Java classes: EntityPreExportEvent.java and ExportMethodInvocationHandler.java. Place these classes in the 'src/main/java/com/wavemaker/runtime/data/aop' directory, ensuring you create the necessary package/folder structure under 'src/main/java'.

EntityPreExportEvent class:

```java
package com.wavemaker.runtime.data.aop;

import com.wavemaker.runtime.data.event.EntityCRUDEvent;
import com.wavemaker.runtime.data.export.DataExportOptions;

public class EntityPreExportEvent<E> extends EntityCRUDEvent<E> {
private DataExportOptions dataExportOptions;
public EntityPreExportEvent(String serviceId, Class<E> entityClass, DataExportOptions dataExportOptions) {
super(serviceId, entityClass);
this.dataExportOptions = dataExportOptions;
}

public DataExportOptions getDataExportOptions() {
return dataExportOptions;
}
}
```

ExportMethodInvocationHandler class:

```java
package com.wavemaker.runtime.data.aop;

import com.wavemaker.runtime.data.export.DataExportOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;

import java.lang.reflect.Method;
import java.util.Objects;

public class ExportMethodInvocationHandler implements CRUDMethodInvocationHandler {
private static final Logger logger = LoggerFactory.getLogger(ExportMethodInvocationHandler.class);

@Autowired
private ApplicationEventPublisher applicationEventPublisher;

@Override
public void preHandle(String serviceId, Class entityClass, Method method, Object[] args) {
DataExportOptions dataExportOptions = (DataExportOptions) args[0];
logger.info("publishing pre export event");
applicationEventPublisher.publishEvent(new EntityPreExportEvent(serviceId, entityClass, dataExportOptions));
}

@Override
public void postHandle(String serviceId, Class entityClass, Method method, Object retVal) {
}

@Override
public boolean matches(Class entityClass, Method method) {
boolean matches = "export".equals(method.getName()) && Objects.equals(method.getParameterTypes()[0].getName(), DataExportOptions.class.getName());
logger.info("export matches: {}, method name: {}, param1: {}", matches, method.getName(), method.getParameterTypes()[0].getName());
return matches;
}
}

```

Create a bean in project-user-spring.xml file for ExportMethodInvocationHandler class.

``` bean
<bean name="ExportEventHandler" class="com.wavemaker.runtime.data.aop.ExportMethodInvocationHandler"/>

```

Now you can use the EntityPreExportEvent listener in your Java services as per your requirement.

Follow the below example in which we are validating the user role and providing export access specifically to the admin role for exporting data table content.

1. Create a JavaService.

```java
package com.testcrud.agecalculator;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.wavemaker.runtime.security.SecurityService;
import com.wavemaker.runtime.service.annotations.ExposeToClient;
import com.wavemaker.runtime.service.annotations.HideFromClient;
import java.sql.Date;
import java.time.Period;
import java.time.LocalDate;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wavemaker.runtime.service.annotations.ExposeToClient;
import com.wavemaker.runtime.service.annotations.HideFromClient;
import org.springframework.context.event.EventListener;
import com.testcrud.hrdb.Employee;
import com.testcrud.hrdb.User;
import com.testcrud.hrdb.service.UserService;
import com.wavemaker.runtime.security.SecurityService;
import com.wavemaker.commons.WMRuntimeException;
import com.wavemaker.runtime.data.aop.EntityPreExportEvent;
import com.wavemaker.commons.MessageResource;
import com.wavemaker.commons.WMRuntimeException;
import com.wavemaker.commons.MessageResource;
import com.wavemaker.commons.WMRuntimeException;
import java.util.*;

@ExposeToClient
public class Exportapivalidations {
@Autowired
private UserService userService;
@Autowired
SecurityService securityService;


private static final Logger logger = LoggerFactory.getLogger(AgeCalculator.class);

@EventListener
public void beforeEmployeeCreate(EntityPreExportEvent<User> entityPreExportEvent){

logger.info("---------Pre Processing-----------");
final List userRoles = (List) Arrays.asList(securityService.getUserRoles());
if (!userRoles.contains("admin")) {
logger.error("The logged in user does not have enough permissions");
throw new WMRuntimeException(MessageResource.create("You do not have enough permissions"));
}

}

}

```
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@
"how-tos/audit-history-using-crud-listeners",
"how-tos/rowlevel-security-using-crud-event-listeners",
"how-tos/validations-using-crudListeners",
"how-tos/validationson-exportapi-using-crudListeners",
"how-tos/custom-business-logic-using-crud-event-listeners",
"how-tos/mysql-connection-using-ssl",
"how-tos/switch-mariadb-to-mysql-connector"
Expand Down