Example usage for org.apache.commons.beanutils BeanMap BeanMap

List of usage examples for org.apache.commons.beanutils BeanMap BeanMap

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanMap BeanMap.

Prototype

public BeanMap(Object bean) 

Source Link

Document

构造一个新的BeanMap�作指定的bean, 如果给定的bean是null, 那么 这个map将会是empty。

Usage

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private void prepareEnsureUnique(String id, JsonObject instance, RBatch redissonBatch, ArrayList<String> pList,
        String parent) throws RepositoryException {
    try {//  ww  w  .j av  a  2s  .  c  o m
        BeanMap pMap = new BeanMap(cls.newInstance());
        pMap.keySet().forEach(e -> {
            if ("class".equals(e) || instance.getValue(e.toString()) == null) {
                return;
            }
            Class type = pMap.getType(e.toString());
            String fieldName = (parent == null ? "" : parent.concat(".")).concat(e.toString());
            if (isRedisEntity(type)) {
                try {
                    RedisRepositoryImpl innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    innerRepo.prepareEnsureUnique(id, instance.getJsonObject(e.toString()), redissonBatch,
                            pList, fieldName);
                } catch (RepositoryException ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                try {
                    if (cls.getDeclaredField(e.toString()).isAnnotationPresent(RedissonUnique.class)) {
                        redissonBatch.getMap(getUniqueKey(e.toString()))
                                .putIfAbsentAsync(instance.getValue(e.toString()), id);
                        pList.add(fieldName);
                    }
                } catch (NoSuchFieldException | SecurityException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private void prepareUndoUnique(String id, JsonObject instance, RBatch redissonBatch, ArrayList<String> pList,
        String parent) throws RepositoryException {
    try {/*from  w ww .  ja  va  2 s. com*/
        BeanMap pMap = new BeanMap(cls.newInstance());
        pMap.keySet().forEach(e -> {
            if ("class".equals(e) || instance.getValue(e.toString()) == null) {
                return;
            }
            Class type = pMap.getType(e.toString());
            String fieldName = (parent == null ? "" : parent.concat(".")).concat(e.toString());
            if (isRedisEntity(type)) {
                try {
                    RedisRepositoryImpl innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    innerRepo.prepareUndoUnique(id, instance.getJsonObject(e.toString()), redissonBatch, pList,
                            fieldName);
                } catch (RepositoryException ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                try {
                    if (cls.getDeclaredField(e.toString()).isAnnotationPresent(RedissonUnique.class)
                            && pList.contains(fieldName)) {
                        redissonBatch.getMap(getUniqueKey(e.toString()))
                                .removeAsync(instance.getValue(e.toString()), id);
                    }
                } catch (NoSuchFieldException | SecurityException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private void prepareEnsuerIndex(String id, T r, JsonObject instance, Boolean isNew, RBatch redissonBatch,
        ArrayList<String> pList, String parent) throws RepositoryException {
    try {//w  w  w  . j  a  v a 2 s.c om
        BeanMap pMap = new BeanMap(r == null ? cls.newInstance() : r);
        pMap.keySet().forEach(e -> {
            if ("class".equals(e) || instance.getValue(e.toString()) == null) {
                return;
            }
            Class type = pMap.getType(e.toString());
            String fieldName = (parent == null ? "" : parent.concat(".")).concat(e.toString());
            if (isRedisEntity(type)) {
                try {
                    RedisRepositoryImpl innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    innerRepo.prepareEnsuerIndex(id, pMap.get(e), instance.getJsonObject(e.toString()), isNew,
                            redissonBatch, pList, fieldName);
                } catch (RepositoryException ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                try {
                    if (cls.getDeclaredField(e.toString()).isAnnotationPresent(RedissonIndex.class)) {
                        RedissonIndex index = cls.getDeclaredField(e.toString())
                                .getAnnotation(RedissonIndex.class);
                        if (index.indexOnSave()) {
                            prepareEnsuerIndex(id, r, instance, redissonBatch, index, e.toString(),
                                    pMap.get(e));
                        }
                    } else if (cls.getDeclaredField(e.toString())
                            .isAnnotationPresent(RedissonIndex.List.class)) {
                        RedissonIndex.List indexList = cls.getDeclaredField(e.toString())
                                .getAnnotation(RedissonIndex.List.class);
                        Arrays.stream(indexList.value()).filter(f -> f.indexOnSave())
                                .forEach(index -> prepareEnsuerIndex(id, r, instance, redissonBatch, index,
                                        e.toString(), pMap.get(e)));
                    }
                    if (cls.getDeclaredField(e.toString()).isAnnotationPresent(RedissonCounter.class)) {
                        RedissonCounter counter = cls.getDeclaredField(e.toString())
                                .getAnnotation(RedissonCounter.class);
                        prepareEnsuerCounter(id, r, instance, isNew, redissonBatch, counter, e.toString(),
                                pMap.get(e));
                    } else if (cls.getDeclaredField(e.toString())
                            .isAnnotationPresent(RedissonCounter.List.class)) {
                        RedissonCounter.List counterList = cls.getDeclaredField(e.toString())
                                .getAnnotation(RedissonCounter.List.class);
                        Arrays.stream(counterList.value()).forEach(index -> prepareEnsuerCounter(id, r,
                                instance, isNew, redissonBatch, index, e.toString(), pMap.get(e)));
                    }
                } catch (NoSuchFieldException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        throw new RepositoryException(ex);
    }
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private T prepareInstanceAndFetchList(String id, RBatch redissonBatch, ArrayList<String> pList, String parent,
        Boolean deepFetch) throws InstantiationException, IllegalAccessException {
    BeanMap pMap = new BeanMap(cls.newInstance());
    try {/*  w w  w  .ja va2s.  co  m*/
        pMap.keySet().forEach(e -> {
            if ("class".equals(e)) {
                return;
            }
            Class type = pMap.getType(e.toString());
            String fieldName = (parent == null ? "" : parent.concat(".")).concat(e.toString());
            if (isRedisEntity(type)) {
                if (deepFetch) {
                    try {
                        RedisRepositoryImpl innerRepo = (RedisRepositoryImpl) factory.instance(type);
                        pMap.put(e.toString(), innerRepo.prepareInstanceAndFetchList(id, redissonBatch, pList,
                                fieldName, deepFetch));
                    } catch (InstantiationException | IllegalAccessException | RepositoryException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            } else {
                if ("id".equals(e)) {
                    redissonBatch.getMap(getStorageKey(), StringCodec.INSTANCE).getAsync(id);
                } else {
                    redissonBatch.getMap(getStorageKey(e.toString())).getAsync(id);
                }
                pList.add(fieldName);
            }
        });
    } catch (RuntimeException ex) {
        throw new InstantiationException();
    }
    return (T) pMap.getBean();
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private T mapResults(T instance, List results, ArrayList<String> pList)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException {
    pList.parallelStream().forEach(e -> {
        Object o = instance;/*from  ww  w  .jav a 2 s  .  c om*/
        for (String fieldName : e.split("\\.")) {
            BeanMap m = new BeanMap(o);
            Class type = m.getType(fieldName);
            if (isRedisEntity(type)) {
                o = m.get(fieldName);
            } else {
                Object value;
                Object result = results.get(pList.indexOf(e));
                if (result == null) {
                    value = null;
                } else if (String.class.isAssignableFrom(type)) {
                    value = result.toString();
                } else if (type.isEnum()) {
                    try {
                        value = type.getMethod("valueOf", String.class).invoke(null,
                                results.get(pList.indexOf(e)));
                    } catch (NoSuchMethodException | SecurityException | IllegalAccessException
                            | IllegalArgumentException | InvocationTargetException ex) {
                        throw new IllegalArgumentException(ex);
                    }
                } else if (result.getClass().isAssignableFrom(type)) {
                    value = type.cast(result);
                } else {
                    try {
                        value = type.getConstructor(result.getClass()).newInstance(result);
                    } catch (NoSuchMethodException | SecurityException | InstantiationException
                            | IllegalAccessException | IllegalArgumentException
                            | InvocationTargetException ex) {
                        throw new IllegalArgumentException(ex);
                    }
                }
                m.put(fieldName, value);
            }
        }
    });
    return instance;
}

From source file:com.github.jackygurui.vertxredissonrepository.repository.Impl.RedisRepositoryImpl.java

private void persistBlocking(String id, JsonObject data, RBatch redissonBatch,
        Handler<AsyncResult<Boolean>> resultHandler) {
    RBatch batch = redissonBatch == null ? redissonWrite.createBatch() : redissonBatch;
    AtomicBoolean failed = new AtomicBoolean(false);
    try {// w  w  w  . j a  v  a 2s  .c om
        BeanMap pMap = new BeanMap(cls.newInstance());
        //remove the indexes;
        if (isRedisEntity()) {
            AtomicBoolean finished = new AtomicBoolean(false);
            AtomicBoolean hasNested = new AtomicBoolean(false);
            AtomicLong stack = new AtomicLong();
            pMap.forEach((k, v) -> {
                if ("class".equals(k)) {
                    return;
                }
                Class<?> type = pMap.getType((String) k);
                if (!isRedisEntity(type)) {
                    //recreate the indexes;
                    if ("id".equals(k)) {
                        batch.getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id, id);
                    } else {
                        batch.getMap(getStorageKey((String) k)).fastPutAsync(id, data.getValue((String) k));
                    }
                } else {
                    hasNested.set(true);
                    stack.incrementAndGet();
                    RedisRepositoryImpl<?> innerRepo;
                    try {
                        innerRepo = (RedisRepositoryImpl) factory.instance(type);
                    } catch (RepositoryException e) {
                        throw new RuntimeException(e);
                    }
                    JsonObject value = data.getJsonObject((String) k);
                    final boolean newOne = !value.containsKey("id") || value.getString("id") == null
                            || "null".equals(value.getString("id"));
                    final String ID = newOne ? id : value.getString("id");
                    innerRepo.persist(ID, value, batch, c -> {//making the nested entity shares the same id as the parent when its 1:1 relation. This makes fetch a lot faster since it doesn't not need to resolve the reference when fetching 1:1 nested objects.
                        if (c.succeeded()) {
                            long s = stack.decrementAndGet();
                            if (newOne) {
                                batch.getMap(getStorageKey((String) k)).fastPutAsync(id, ID);//different to the update, create needs to add the reference field to batch
                            }
                            if (s == 0 && finished.get() && !failed.get()) { //finished iterating and no outstanding processes. 
                                if (redissonBatch == null) {//if it's not inside a nested process.
                                    finishPersist(id, data, batch, resultHandler);
                                } else {//if it is inside a nested process.
                                    resultHandler.handle(Future.succeededFuture(true));
                                }
                            }
                            //else wait for others to complete
                        } else {
                            boolean firstToFail = failed.compareAndSet(false, true);
                            if (firstToFail) {
                                resultHandler.handle(Future.failedFuture(c.cause()));
                            }
                        }
                    });
                }
            });
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            finished.set(true);
            if (!hasNested.get()) {//does not have nested RedissonEntity within
                if (redissonBatch == null) {//if it's not inside a nested process.
                    finishPersist(id, data, batch, resultHandler);
                } else {//if it is inside a nested process.
                    resultHandler.handle(Future.succeededFuture(true));
                }
            }
        } else {//not a RedissonEntity class, persist as json string.
            //recreate the indexes;
            batch.<String, String>getMap(getStorageKey(), StringCodec.INSTANCE).fastPutAsync(id,
                    Json.encode(data));
            batch.getAtomicLongAsync(getCounterKey()).incrementAndGetAsync();
            if (redissonBatch == null) {//if it's not inside a nested process.
                finishPersist(id, data, batch, resultHandler);
            } else {//if it is inside a nested process.
                resultHandler.handle(Future.succeededFuture(true));
            }
        }
    } catch (InstantiationException | IllegalAccessException | RuntimeException ex) {
        failed.set(true);
        resultHandler.handle(Future.failedFuture(ex));
    }
}

From source file:com.datatorrent.stram.plan.logical.LogicalPlanConfiguration.java

public static BeanMap getObjectProperties(Object obj) {
    return new BeanMap(obj);
}

From source file:org.agorava.core.utils.URLUtils.java

/**
 * Create a {@link Multimap} from a simple bean.
 * <p/>//from w  ww. j a va  2 s  . c  om
 * All standard properties will become keys in the target map
 * <p/>
 * Collection properties will become multi-entries
 * <p/>
 * Map properties will trigger an exception
 *
 * @param pojo the bean to convert
 * @return the multimap
 * @throws AgoravaException if one property is a Map
 */
@SuppressWarnings({ "unchecked", "MismatchedQueryAndUpdateOfCollection" })
private static Multimap<String, Object> copyIntoMultiMap(Object pojo) {
    Map<String, ?> pojoMap = new BeanMap(pojo);
    Multimap<String, Object> res = HashMultimap.create();
    for (String key : pojoMap.keySet()) {
        if (!"class".equals(key)) {
            Object value = pojoMap.get(key);
            if (value instanceof Map)
                throw new AgoravaException("Cannot convert Pojo containing a Map to a Multimap");
            if (value instanceof Collection) {
                for (Object elt : (Collection<Object>) value) {
                    res.put(key, elt);
                }
            } else
                res.put(key, value);
        }
    }
    return res;
}

From source file:org.agorava.LinkedInBaseService.java

public String buildUri(String url, Object pojo) {
    Map beanMap = new BeanMap(pojo);
    return params.addMap(beanMap).asUrl(url);
}

From source file:org.apache.metron.profiler.bolt.ProfileHBaseMapper.java

/**
 * Executes each of the 'groupBy' expressions.  The result of each
 * expression are the groups used to sort the data as part of the
 * row key./* w  w w. j ava 2  s  .c  om*/
 * @param m The profile measurement.
 * @return The result of executing the 'groupBy' expressions.
 */
private List<Object> executeGroupBy(ProfileMeasurement m) {
    List<Object> groups = new ArrayList<>();

    if (!isEmpty(m.getGroupBy())) {
        try {
            // allows each 'groupBy' expression to refer to the fields of the ProfileMeasurement
            BeanMap measureAsMap = new BeanMap(m);

            for (String expr : m.getGroupBy()) {
                Object result = executor.execute(expr, measureAsMap, Object.class);
                groups.add(result);
            }

        } catch (Throwable e) {
            String msg = format("Bad 'groupBy' expression: %s, profile=%s, entity=%s", e.getMessage(),
                    m.getProfileName(), m.getEntity());
            throw new ParseException(msg, e);
        }
    }

    return groups;
}