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

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

There is a newer version: 1.0.62
Show 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 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(String query) {
        Map>> results = new HashMap<>();
        JSONObject jsonObject = new JSONObject(query);
        for (String key : jsonObject.keySet()) {
            List> value = query(StructureQuery.fromJson(jsonObject.getJSONObject(key).toString()).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");

        JSONObject query = new JSONObject("{\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" +
                "}");

        final List jsonValues = new ArrayList<>();

        JSONArray availableQueues = new JSONArray(query(StructureQuery.fromJson(query.toString()).build()));
        for (Object availableQueue : availableQueues) {
            JSONObject queue = (JSONObject) availableQueue;
            final String queueId = queue.getString("id");
            JSONObject queueEntryQuery = new JSONObject("{\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" +
                    "}");
            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;
    }


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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy