Search in sources :

Example 1 with ModelQueryImpl

use of angularBeans.util.ModelQueryImpl in project AngularBeans by bessemHmidi.

the class InvocationHandler method genericInvoke.

private void genericInvoke(Object service, String methodName, JsonObject params, Map<String, Object> returns, long reqID, String UID, HttpServletRequest request) throws SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
    Object mainReturn = null;
    Method m = null;
    JsonElement argsElem = params.get("args");
    if (reqID > 0) {
        returns.put("reqId", reqID);
    }
    if (argsElem != null) {
        JsonArray args = params.get("args").getAsJsonArray();
        m = CommonUtils.getMethod(service.getClass(), methodName, args.size());
        Pair<Boolean, Map<Integer, String>> ngCast = CommonUtils.getParamCastMap(m);
        Type[] parameters = m.getGenericParameterTypes();
        if (parameters.length == args.size()) {
            List<Object> argsValues = new ArrayList<>();
            for (int i = 0; i < parameters.length; i++) {
                JsonElement element = args.get(i);
                Type type = null;
                if (ngCast != null) {
                    type = CommonUtils.getParamType(service, ngCast.getValue().get(i), ngCast.getKey());
                }
                if (element.isJsonPrimitive()) {
                    Class<?> clazz = null;
                    String typeString = ((parameters[i]).toString());
                    if (typeString.startsWith("class")) {
                        clazz = Class.forName(typeString.substring(6));
                        if (clazz.equals(Object.class)) {
                            clazz = CommonUtils.getPrimitiveClass(element);
                        }
                    } else {
                        clazz = builtInMap.get(typeString);
                    }
                    String val = element.getAsString();
                    if (type == null) {
                        argsValues.add(CommonUtils.convertFromString(val, clazz));
                    } else {
                        argsValues.add(util.deserialise(type, element));
                    }
                } else if (element.isJsonArray()) {
                    JsonArray arr = element.getAsJsonArray();
                    if (type == null) {
                        argsValues.add(util.deserialise(parameters[i], arr));
                    } else {
                        argsValues.add(util.deserialise(type, arr));
                    }
                } else {
                    if (type == null) {
                        argsValues.add(util.deserialise(parameters[i], element));
                    } else {
                        argsValues.add(util.deserialise(type, element));
                    }
                }
            }
            if (!CommonUtils.isGetter(m)) {
                update(service, params);
            }
            try {
                mainReturn = m.invoke(service, argsValues.toArray());
            } catch (Exception e) {
                handleException(m, e);
                e.printStackTrace();
            }
        }
    } else {
        m = CommonUtils.getMethod(service.getClass(), methodName, 0);
        // handling methods that took HttpServletRequest as parameter
        if (!CommonUtils.isGetter(m)) {
            update(service, params);
        }
        mainReturn = m.invoke(service);
    }
    ModelQueryImpl qImpl = (ModelQueryImpl) modelQueryFactory.get(service.getClass());
    Map<String, Object> scMap = new HashMap<>(qImpl.getData());
    returns.putAll(scMap);
    qImpl.getData().clear();
    if (!modelQueryFactory.getRootScope().getRootScopeMap().isEmpty()) {
        returns.put("rootScope", new HashMap<>(modelQueryFactory.getRootScope().getRootScopeMap()));
        modelQueryFactory.getRootScope().getRootScopeMap().clear();
    }
    String[] updates = null;
    if (m != null && m.isAnnotationPresent(NGReturn.class)) {
        if (mainReturn == null)
            mainReturn = "";
        NGReturn ngReturn = m.getAnnotation(NGReturn.class);
        updates = ngReturn.updates();
        if (ngReturn.model().length() > 0) {
            returns.put(ngReturn.model(), mainReturn);
            Map<String, String> binding = new HashMap<>();
            binding.put("boundTo", ngReturn.model());
            mainReturn = binding;
        }
    }
    if (m != null && m.isAnnotationPresent(NGPostConstruct.class)) {
        NGPostConstruct ngPostConstruct = m.getAnnotation(NGPostConstruct.class);
        updates = ngPostConstruct.updates();
    }
    if (updates != null) {
        if ((updates.length == 1) && (updates[0].equals("*"))) {
            List<String> upd = new ArrayList<>();
            for (Method met : service.getClass().getDeclaredMethods()) {
                if (CommonUtils.isGetter(met)) {
                    String fieldName = (met.getName()).substring(3);
                    String firstCar = fieldName.substring(0, 1);
                    upd.add((firstCar.toLowerCase() + fieldName.substring(1)));
                }
            }
            updates = new String[upd.size()];
            for (int i = 0; i < upd.size(); i++) {
                updates[i] = upd.get(i);
            }
        }
    }
    if (updates != null) {
        for (String up : updates) {
            String getterName = GETTER_PREFIX + up.substring(0, 1).toUpperCase() + up.substring(1);
            Method getter;
            try {
                getter = service.getClass().getMethod(getterName);
            } catch (NoSuchMethodException e) {
                getter = service.getClass().getMethod((getterName.replace(GETTER_PREFIX, BOOLEAN_GETTER_PREFIX)));
            }
            Object result = getter.invoke(service);
            returns.put(up, result);
        }
    }
    returns.put("mainReturn", mainReturn);
    if (!logger.getLogPool().isEmpty()) {
        returns.put("log", logger.getLogPool().toArray());
        logger.getLogPool().clear();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NGReturn(angularBeans.api.NGReturn) ModelQueryImpl(angularBeans.util.ModelQueryImpl) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) JsonArray(com.google.gson.JsonArray) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) NGPostConstruct(angularBeans.api.NGPostConstruct) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ModelQueryImpl

use of angularBeans.util.ModelQueryImpl in project AngularBeans by bessemHmidi.

the class RealTimeClient method prepareData.

private Map<String, Object> prepareData(ModelQuery query) {
    Map<String, Object> paramsToSend = new HashMap<String, Object>();
    ModelQueryImpl modelQuery = (ModelQueryImpl) query;
    ServerEvent ngEvent = new ServerEvent();
    ngEvent.setName("modelQuery");
    ngEvent.setData(CommonUtils.getBeanName(modelQuery.getOwner()));
    paramsToSend.putAll(modelQuery.getData());
    paramsToSend.put("ngEvent", ngEvent);
    paramsToSend.put("log", logger.getLogPool());
    paramsToSend.put("isRT", true);
    return paramsToSend;
}
Also used : ServerEvent(angularBeans.events.ServerEvent) HashMap(java.util.HashMap) ModelQueryImpl(angularBeans.util.ModelQueryImpl)

Aggregations

ModelQueryImpl (angularBeans.util.ModelQueryImpl)2 HashMap (java.util.HashMap)2 NGPostConstruct (angularBeans.api.NGPostConstruct)1 NGReturn (angularBeans.api.NGReturn)1 ServerEvent (angularBeans.events.ServerEvent)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1