All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fivefaces.structureclient.controller.AbstractStructureController Maven / Gradle / Ivy

The newest version!
package com.fivefaces.structureclient.controller;

import com.fivefaces.common.exception.ItemNotFoundException;
import com.fivefaces.structure.query.builder.StructureQuery;
import com.fivefaces.structure.service.StructureService;
import org.apache.commons.collections4.CollectionUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public abstract class AbstractStructureController {

    @Autowired
    private StructureService structureService;

    protected List> query(StructureQuery query) {
        List> result = getQueryResult(query);
        Boolean failIfNotFound = query.getUnmapped("failIfNotFound", Boolean.class);
        if (failIfNotFound != null && failIfNotFound) {
            if (CollectionUtils.isEmpty(result)) {
                throw new ItemNotFoundException("Resource not found");
            }
        }
        Boolean failIfFound = query.getUnmapped("failIfFound", Boolean.class);
        if (failIfFound != null && failIfFound) {
            if (CollectionUtils.isNotEmpty(result)) {
                throw new ItemNotFoundException("Resource found while requesting failIfFound");
            }
        }
        return result;
    }

    protected BigInteger count(StructureQuery query) {
        return structureService.count(query);
    }

    protected Map>> queries(Map queries) {
        Map>> results = new HashMap<>();
        for (String key : queries.keySet()) {
            List> value = query(StructureQuery.fromGenerics((Map) queries.get(key)).build());
            results.put(key, value);
        }
        return results;
    }

    protected List> namedQuery(String query, Map params) {
        return structureService.queryWithParams(query, params);
    }

    protected List geQueueWithService(String jsonInput) {
        // get the queues which match the required service supplied in the payload
        JSONObject payload = new JSONObject(jsonInput);
        final String service = payload.getString("service");
        final String locationId = payload.getString("locationId");

        StructureQuery query = StructureQuery.fromJson("{\n" +
                "  \"type\": \"queue\",\n" +
                "  \"failIfNotFound\" : true,\n" +
                "  \"criteria\": [\n" +
                "    {\n" +
                "      \"member\": \"$.locationId\",\n" +
                "      \"type\": \"eq\",\n" +
                "      \"string\": true,\n" +
                "      \"value\":\"" + locationId + "\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"member\": \"$.status\",\n" +
                "      \"type\": \"eq\",\n" +
                "      \"string\": true,\n" +
                "      \"value\":\"ACTIVE\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"member\": \"$.services[*]\",\n" +
                "      \"type\": \"json_contains\",\n" +
                "      \"string\": true,\n" +
                "      \"value\": \"" + service + "\"\n" +
                "    }\n" +
                "  ]\n" +
                "}").build();

        final List jsonValues = new ArrayList<>();

        JSONArray availableQueues = new JSONArray(query(query));
        for (Object availableQueue : availableQueues) {
            JSONObject queue = (JSONObject) availableQueue;
            final String queueId = queue.getString("id");
            StructureQuery queueEntryQuery = StructureQuery.fromJson("{\n" +
                    "  \"type\": \"queue_entry\",\n" +
                    "  \"count\" : true,\n" +
                    "  \"criteria\": [\n" +
                    "    {\n" +
                    "      \"member\": \"$.queueId\",\n" +
                    "      \"type\": \"eq\",\n" +
                    "      \"string\": true,\n" +
                    "      \"value\":\"" + queueId + "\"\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}").build();
            BigInteger activeCount = structureService.count(queueEntryQuery);
            queue.put("activeCount", activeCount);
            final Integer currentFreeCapacity = queue.getInt("capacity") - activeCount.intValue();
            if (currentFreeCapacity > 0) {
                queue.put("currentFreeCapacity", currentFreeCapacity);
                jsonValues.add(queue);
            }
        }

        if (jsonValues.isEmpty()) {
            throw new ItemNotFoundException("Resource not found - No Capacity");
        }

        jsonValues.sort(new Comparator<>() {
            private static final String KEY_NAME = "activeCount";

            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valA = (String) a.get(KEY_NAME);
                String valB = (String) b.get(KEY_NAME);
                return valA.compareTo(valB);
            }
        });
        return jsonValues;
    }

    protected boolean hasAnyRole(Authentication authentication, List roles) {
        for (GrantedAuthority authority : authentication.getAuthorities()) {
            if (roles.contains(authority.getAuthority())) {
                return true;
            }
        }
        return false;
    }


    private List> getQueryResult(StructureQuery query) {
        if (query.getId() != null) {
            final String type = query.getType();
            final UUID id = UUID.fromString(query.getId());
            Map result = structureService.queryById(type, id, query);
            return List.of(result);
        } else {
            return structureService.query(query);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy