Commit 8a72ad86 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Implemented basic hook functionality

parent 3aac16a7
Loading
Loading
Loading
Loading
+6 −0
Original line number Original line Diff line number Diff line
@@ -28,6 +28,12 @@ The first super admin user must be added manually, then he/she will be able to a


The value `user_id` is the RAP user id.
The value `user_id` is the RAP user id.


## Hooks

It is possible to load external jar files in order to extend the GMS with custom functionalities (implementing the *Hook interfaces and annotating the class with `@org.springframework.stereotype.Component`). Currently only the `GroupsHook` is available.

External jar files need to be specified at application startup using the `LOADER_PATH` environment variable (this is a Spring feature).

## Developer notes
## Developer notes


Backend and frontend are 2 separate applications:
Backend and frontend are 2 separate applications:
+22 −0
Original line number Original line Diff line number Diff line
@@ -114,6 +114,26 @@
                    </execution>
                    </execution>
                </executions>
                </executions>
            </plugin>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <!-- creates a gms-lib jar that can be included as dependency in other projects -->
                            <classifier>lib</classifier>
                            <includes>
                                <include>**/**</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <artifactId>dockerfile-maven-plugin</artifactId>
@@ -130,6 +150,8 @@
                <artifactId>spring-boot-maven-plugin</artifactId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                <configuration>
                    <executable>true</executable>
                    <executable>true</executable>
                    <!-- layout ZIP is necessary for loading external jar using PropertiesLauncher (used for hooks) -->
                    <layout>ZIP</layout>
                </configuration>
                </configuration>
            </plugin>
            </plugin>
            <plugin>
            <plugin>
+1 −1
Original line number Original line Diff line number Diff line
@@ -193,7 +193,7 @@ public class JWTWebServiceController {
    @DeleteMapping(value = "/{group:.+}", produces = MediaType.TEXT_PLAIN_VALUE)
    @DeleteMapping(value = "/{group:.+}", produces = MediaType.TEXT_PLAIN_VALUE)
    public void deleteGroup(@PathVariable("group") String groupParam, HttpServletResponse response) {
    public void deleteGroup(@PathVariable("group") String groupParam, HttpServletResponse response) {
        GroupEntity group = getGroupFromNames(extractGroupNames(groupParam));
        GroupEntity group = getGroupFromNames(extractGroupNames(groupParam));
        groupsDAO.deleteGroupById(group.getId());
        groupsDAO.deleteGroup(group);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
    }


+19 −2
Original line number Original line Diff line number Diff line
@@ -2,6 +2,7 @@ package it.inaf.ia2.gms.persistence;


import it.inaf.ia2.gms.model.GroupBreadcrumb;
import it.inaf.ia2.gms.model.GroupBreadcrumb;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.service.hook.GroupsHook;
import java.sql.PreparedStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLException;
@@ -21,6 +22,9 @@ import org.springframework.stereotype.Component;
@Component
@Component
public class GroupsDAO {
public class GroupsDAO {


    @Autowired(required = false)
    protected GroupsHook groupsHook;

    private final JdbcTemplate jdbcTemplate;
    private final JdbcTemplate jdbcTemplate;


    @Autowired
    @Autowired
@@ -30,6 +34,10 @@ public class GroupsDAO {


    public GroupEntity createGroup(GroupEntity group) {
    public GroupEntity createGroup(GroupEntity group) {


        if (groupsHook != null) {
            groupsHook.beforeCreate(group);
        }

        String sql = "INSERT INTO gms_group (id, name, path, is_leaf) VALUES (?, ?, ?, ?)";
        String sql = "INSERT INTO gms_group (id, name, path, is_leaf) VALUES (?, ?, ?, ?)";


        jdbcTemplate.update(conn -> {
        jdbcTemplate.update(conn -> {
@@ -46,6 +54,10 @@ public class GroupsDAO {


    public GroupEntity updateGroup(GroupEntity group) {
    public GroupEntity updateGroup(GroupEntity group) {


        if (groupsHook != null) {
            groupsHook.beforeUpdate(group);
        }

        String sql = "UPDATE gms_group SET name = ?, path = ?, is_leaf = ? WHERE id = ?";
        String sql = "UPDATE gms_group SET name = ?, path = ?, is_leaf = ? WHERE id = ?";


        jdbcTemplate.update(conn -> {
        jdbcTemplate.update(conn -> {
@@ -60,9 +72,14 @@ public class GroupsDAO {
        return group;
        return group;
    }
    }


    public void deleteGroupById(String groupId) {
    public void deleteGroup(GroupEntity group) {

        if (groupsHook != null) {
            groupsHook.beforeDelete(group);
        }

        String sql = "DELETE FROM gms_group WHERE id = ?";
        String sql = "DELETE FROM gms_group WHERE id = ?";
        jdbcTemplate.update(sql, groupId);
        jdbcTemplate.update(sql, group.getId());
    }
    }


    public Optional<GroupEntity> findGroupById(String groupId) {
    public Optional<GroupEntity> findGroupById(String groupId) {
+5 −0
Original line number Original line Diff line number Diff line
@@ -3,6 +3,7 @@ package it.inaf.ia2.gms.service;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HashSet;
import java.util.List;
import java.util.List;
@@ -26,6 +27,10 @@ public class GroupNameService {
        return getGroupsNames(groupsDAO.findGroupsByIds(groupIdentifiers));
        return getGroupsNames(groupsDAO.findGroupsByIds(groupIdentifiers));
    }
    }


    public String getGroupCompleteName(GroupEntity group) {
        return getGroupsNames(Collections.singletonList(group)).get(0);
    }

    /**
    /**
     * Returns the list of the group complete names, given a list of GroupEntity
     * Returns the list of the group complete names, given a list of GroupEntity
     * objects.
     * objects.
Loading