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:org.ms123.common.data.BeanMapWrapper.java

public BeanMapWrapper(Object o) {
    m_beanmap = new BeanMap(o);
}

From source file:org.ms123.common.data.JdoLayerImpl.java

public void populate(SessionContext sessionContext, Map from, Object to, Map hintsMap) {
    PersistenceManager pm = sessionContext.getPM();
    if (hintsMap == null) {
        hintsMap = new HashMap();
    }//  w w w  . j av a2  s .  c  om
    Map<String, String> expressions = (Map) hintsMap.get("__expressions");
    if (expressions == null)
        expressions = new HashMap();
    BeanMap beanMap = new BeanMap(to);
    String entityName = m_inflector.getEntityName(to.getClass().getSimpleName());
    debug("populate.from:" + from + ",to:" + to + ",BeanMap:" + beanMap + "/hintsMap:" + hintsMap
            + "/entityName:" + entityName);
    if (from == null) {
        return;
    }
    Map permittedFields = sessionContext.getPermittedFields(entityName, "write");
    Iterator<String> it = from.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        Object oldValue = beanMap.get(key);
        boolean permitted = m_permissionService.hasAdminRole() || "team".equals(entityName)
                || sessionContext.isFieldPermitted(key, entityName, "write");
        if (!key.startsWith("_") && !permitted) {
            debug("---->populate:field(" + key + ") no write permission");
            continue;
        } else {
            debug("++++>populate:field(" + key + ") write permitted");
        }
        String datatype = null;
        String edittype = null;
        if (!key.startsWith("_")) {
            Map config = (Map) permittedFields.get(key);
            if (config != null) {
                datatype = (String) config.get("datatype");
                edittype = (String) config.get("edittype");
            }
        }

        if (key.equals(STATE_FIELD) && !m_permissionService.hasAdminRole()) {
            continue;
        }
        if ("auto".equals(edittype))
            continue;
        String mode = null;
        Map hm = (Map) hintsMap.get(key);
        if (hm != null) {
            Object m = hm.get("mode");
            if (m != null && m instanceof String) {
                mode = (String) m;
            }
            if (mode == null) {
                m = hm.get("useit");
                if (m != null && m instanceof String) {
                    mode = (String) m;
                }
            }
        }
        if (mode == null) {
            mode = "replace";
        }
        Class clazz = beanMap.getType(key);
        debug("\ttype:" + clazz + "(" + key + "=" + from.get(key) + ")");
        if ("_ignore_".equals(from.get(key))) {
            continue;
        }
        if (clazz == null) {
            debug("\t--- Warning property not found:" + key);
        } else if (clazz.equals(java.util.Date.class)) {
            String value = Utils.getString(from.get(key), beanMap.get(key), mode);
            debug("\tDate found:" + key + "=>" + value);
            Date date = null;
            if (value != null) {
                try {
                    Long val = Long.valueOf(value);
                    date = (Date) ConvertUtils.convert(val, Date.class);
                    debug("\tdate1:" + date);
                } catch (Exception e) {
                    try {
                        DateTime dt = new DateTime(value);
                        date = new Date(dt.getMillis());
                        debug("\tdate2:" + date);
                    } catch (Exception e1) {
                        try {
                            int space = value.indexOf(" ");
                            if (space != -1) {
                                value = value.substring(0, space) + "T" + value.substring(space + 1);
                                DateTime dt = new DateTime(value);
                                date = new Date(dt.getMillis());
                            }
                            debug("\tdate3:" + date);
                        } catch (Exception e2) {
                            debug("\terror setting date:" + e);
                        }
                    }
                }
            }
            debug("\tsetting date:" + date);
            beanMap.put(key, date);
        } else if (clazz.equals(java.util.Map.class)) {
            info("!!!!!!!!!!!!!!!!!!!Map not implemented");
        } else if (clazz.equals(java.util.List.class) || clazz.equals(java.util.Set.class)) {
            boolean isList = clazz.equals(java.util.List.class);
            boolean isSimple = false;
            if (datatype != null && datatype.startsWith("list_")) {
                isSimple = true;
            }
            try {
                Class type = TypeUtils.getTypeForField(to, key);
                debug("type:" + type + " fill with: " + from.get(key) + ",list:" + beanMap.get(key) + "/mode:"
                        + mode);
                Collection valList = isList ? new ArrayList() : new HashSet();
                Object fromVal = from.get(key);
                if (fromVal instanceof String && ((String) fromVal).length() > 0) {
                    info("FromVal is StringSchrott, ignore");
                    continue;
                }
                if (from.get(key) instanceof Collection) {
                    valList = (Collection) from.get(key);
                }
                if (valList == null) {
                    valList = isList ? new ArrayList() : new HashSet();
                }
                Collection toList = (Collection) PropertyUtils.getProperty(to, key);
                debug("toList:" + toList);
                debug("valList:" + valList);
                if (toList == null) {
                    toList = isList ? new ArrayList() : new HashSet();
                    PropertyUtils.setProperty(to, key, toList);
                }
                if ("replace".equals(mode)) {
                    boolean isEqual = false;
                    if (isSimple) {
                        isEqual = Utils.isCollectionEqual(toList, valList);
                        if (!isEqual) {
                            toList.clear();
                        }
                        debug("\tisEqual:" + isEqual);
                    } else {
                        List deleteList = new ArrayList();
                        String namespace = sessionContext.getStoreDesc().getNamespace();
                        for (Object o : toList) {
                            if (type.getName().endsWith(".Team")) {
                                int status = m_teamService.getTeamStatus(namespace, new BeanMap(o), null,
                                        sessionContext.getUserName());
                                debug("populate.replace.teamStatus:" + status + "/"
                                        + new HashMap(new BeanMap(o)));
                                if (status != -1) {
                                    pm.deletePersistent(o);
                                    deleteList.add(o);
                                }
                            } else {
                                pm.deletePersistent(o);
                                deleteList.add(o);
                            }
                        }
                        for (Object o : deleteList) {
                            toList.remove(o);
                        }
                    }
                    debug("populate.replace.toList:" + toList + "/" + type.getName());
                    if (isSimple) {
                        if (!isEqual) {
                            for (Object o : valList) {
                                toList.add(o);
                            }
                        }
                    } else {
                        for (Object o : valList) {
                            Map valMap = (Map) o;
                            Object n = type.newInstance();
                            if (type.getName().endsWith(".Team")) {
                                valMap.remove("id");
                                Object desc = valMap.get("description");
                                Object name = valMap.get("name");
                                Object dis = valMap.get("disabled");
                                String teamid = (String) valMap.get("teamid");
                                Object ti = Utils.getTeamintern(sessionContext, teamid);
                                if (desc == null) {
                                    valMap.put("description", PropertyUtils.getProperty(ti, "description"));
                                }
                                if (name == null) {
                                    valMap.put("name", PropertyUtils.getProperty(ti, "name"));
                                }
                                if (dis == null) {
                                    valMap.put("disabled", false);
                                }
                                pm.makePersistent(n);
                                populate(sessionContext, valMap, n, null);
                                PropertyUtils.setProperty(n, "teamintern", ti);
                            } else {
                                pm.makePersistent(n);
                                populate(sessionContext, valMap, n, null);
                            }
                            debug("populated.add:" + new HashMap(new BeanMap(n)));
                            toList.add(n);
                        }
                    }
                } else if ("remove".equals(mode)) {
                    if (isSimple) {
                        for (Object o : valList) {
                            if (toList.contains(o)) {
                                toList.remove(o);
                            }
                        }
                    } else {
                        for (Object ol : valList) {
                            Map map = (Map) ol;
                            Object o = Utils.listContainsId(toList, map, "teamid");
                            if (o != null) {
                                toList.remove(o);
                                pm.deletePersistent(o);
                            }
                        }
                    }
                } else if ("add".equals(mode)) {
                    if (isSimple) {
                        for (Object o : valList) {
                            toList.add(o);
                        }
                    } else {
                        for (Object ol : valList) {
                            Map map = (Map) ol;
                            Object o = Utils.listContainsId(toList, map, "teamid");
                            if (o != null) {
                                populate(sessionContext, map, o, null);
                            } else {
                                o = type.newInstance();
                                if (type.getName().endsWith(".Team")) {
                                    Object desc = map.get("description");
                                    Object name = map.get("name");
                                    Object dis = map.get("disabled");
                                    String teamid = (String) map.get("teamid");
                                    Object ti = Utils.getTeamintern(sessionContext, teamid);
                                    if (desc == null) {
                                        map.put("description", PropertyUtils.getProperty(ti, "description"));
                                    }
                                    if (name == null) {
                                        map.put("name", PropertyUtils.getProperty(ti, "name"));
                                    }
                                    if (dis == null) {
                                        map.put("disabled", false);
                                    }

                                    pm.makePersistent(o);
                                    populate(sessionContext, map, o, null);
                                    PropertyUtils.setProperty(o, "teamintern", ti);
                                } else {
                                    pm.makePersistent(o);
                                    populate(sessionContext, map, o, null);
                                }
                                toList.add(o);
                            }
                        }
                    }
                } else if ("assign".equals(mode)) {
                    if (!isSimple) {
                        for (Object ol : valList) {
                            Map map = (Map) ol;
                            Object o = Utils.listContainsId(toList, map);
                            if (o != null) {
                                debug("id:" + map + " already assigned");
                            } else {
                                Object id = map.get("id");
                                Boolean assign = Utils.getBoolean(map.get("assign"));
                                Object obj = pm.getObjectById(type, id);
                                if (assign) {
                                    toList.add(obj);
                                } else {
                                    toList.remove(obj);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                debug("populate.list.failed:" + key + "=>" + from.get(key) + ";" + e);
            }
        } else if (clazz.equals(java.lang.Boolean.class)) {
            try {
                beanMap.put(key, ConvertUtils.convert(from.get(key), Boolean.class));
            } catch (Exception e) {
                debug("populate.boolean.failed:" + key + "=>" + from.get(key) + ";" + e);
            }
        } else if (clazz.equals(java.lang.Double.class)) {
            String value = Utils.getString(from.get(key), beanMap.get(key), mode);
            try {
                beanMap.put(key, Double.valueOf(value));
            } catch (Exception e) {
                debug("populate.double.failed:" + key + "=>" + value + ";" + e);
            }
        } else if (clazz.equals(java.lang.Long.class)) {
            try {
                beanMap.put(key, ConvertUtils.convert(from.get(key), Long.class));
            } catch (Exception e) {
                debug("populate.long.failed:" + key + "=>" + from.get(key) + ";" + e);
            }
        } else if (clazz.equals(java.lang.Integer.class)) {
            debug("Integer:" + ConvertUtils.convert(from.get(key), Integer.class));
            try {
                beanMap.put(key, ConvertUtils.convert(from.get(key), Integer.class));
            } catch (Exception e) {
                debug("populate.integer.failed:" + key + "=>" + from.get(key) + ";" + e);
            }
        } else if ("binary".equals(datatype) || clazz.equals(byte[].class)) {
            InputStream is = null;
            InputStream is2 = null;

            try {
                if (from.get(key) instanceof FileItem) {
                    FileItem fi = (FileItem) from.get(key);
                    String name = fi.getName();
                    byte[] bytes = IOUtils.toByteArray(fi.getInputStream());
                    if (bytes != null) {
                        debug("bytes:" + bytes.length);
                    }
                    beanMap.put(key, bytes);
                    is = fi.getInputStream();
                    is2 = fi.getInputStream();
                } else if (from.get(key) instanceof Map) {
                    Map map = (Map) from.get(key);
                    String storeLocation = (String) map.get("storeLocation");
                    is = new FileInputStream(new File(storeLocation));
                    is2 = new FileInputStream(new File(storeLocation));
                    byte[] bytes = IOUtils.toByteArray(is);
                    if (bytes != null) {
                        debug("bytes2:" + bytes.length);
                    }
                    is.close();
                    beanMap.put(key, bytes);
                    is = new FileInputStream(new File(storeLocation));
                } else if (from.get(key) instanceof String) {
                    String value = (String) from.get(key);
                    if ("ignore".equals(value)) {
                        debug("ignore:");
                        return;
                    }
                    if (value.startsWith("data:")) {
                        int ind = value.indexOf(";base64,");
                        byte b[] = Base64.decode(value.substring(ind + 8));
                        beanMap.put(key, b);
                        is = new ByteArrayInputStream(b);
                        is2 = new ByteArrayInputStream(b);
                    } else {
                        byte b[] = value.getBytes();
                        beanMap.put(key, b);
                        is = new ByteArrayInputStream(b);
                        is2 = new ByteArrayInputStream(b);
                    }
                } else {
                    debug("populate.byte[].no a FileItem:" + key + "=>" + from.get(key));
                    continue;
                }
                Tika tika = new Tika();
                TikaInputStream stream = TikaInputStream.get(is);
                TikaInputStream stream2 = TikaInputStream.get(is2);
                String text = tika.parseToString(is);
                debug("Text:" + text);
                try {
                    beanMap.put("text", text);
                } catch (Exception e) {
                    beanMap.put("text", text.getBytes());
                }
                //@@@MS Hardcoded 
                try {
                    Detector detector = new DefaultDetector();
                    MediaType mime = detector.detect(stream2, new Metadata());
                    debug("Mime:" + mime.getType() + "|" + mime.getSubtype() + "|" + mime.toString());
                    beanMap.put("type", mime.toString());
                    from.put("type", mime.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
                debug("populate.byte[].failed:" + key + "=>" + from.get(key) + ";" + e);
            } finally {
                try {
                    is.close();
                    is2.close();
                } catch (Exception e) {
                }
            }
        } else {
            boolean ok = false;
            try {
                Class type = TypeUtils.getTypeForField(to, key);
                if (type != null) {
                    Object o = type.newInstance();
                    boolean hasAnn = type.isAnnotationPresent(PersistenceCapable.class);
                    debug("hasAnnotation:" + hasAnn);
                    if (o instanceof javax.jdo.spi.PersistenceCapable || hasAnn) {
                        Object id = null;
                        try {
                            Object _id = from.get(key);
                            if (_id != null) {
                                if (_id instanceof Map) {
                                    id = ((Map) _id).get("id");
                                } else {
                                    String s = String.valueOf(_id);
                                    if (s.indexOf("/") >= 0) {
                                        _id = Utils.extractId(s);
                                    }
                                    Class idClass = PropertyUtils.getPropertyType(o, "id");
                                    id = (idClass.equals(Long.class)) ? Long.valueOf(_id + "") : _id;
                                }
                            }
                        } catch (Exception e) {
                        }
                        if (id != null && !"".equals(id) && !"null".equals(id)) {
                            debug("\tId2:" + id);
                            Object relatedObject = pm.getObjectById(type, id);
                            List<Collection> candidates = TypeUtils.getCandidateLists(relatedObject, to, null);
                            if (candidates.size() == 1) {
                                Collection l = candidates.get(0);
                                debug("list.contains:" + l.contains(to));
                                if (!l.contains(to)) {
                                    l.add(to);
                                }
                            }
                            beanMap.put(key, relatedObject);
                        } else {
                            Object relatedObject = beanMap.get(key);
                            debug("\trelatedObject:" + relatedObject);
                            if (relatedObject != null) {
                                List<Collection> candidates = TypeUtils.getCandidateLists(relatedObject, to,
                                        null);
                                if (candidates.size() == 1) {
                                    Collection l = candidates.get(0);
                                    debug("list.contains:" + l.contains(to));
                                    if (l.contains(to)) {
                                        l.remove(to);
                                    }
                                }
                            }
                            beanMap.put(key, null);
                        }
                        ok = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!ok) {
                String value = Utils.getString(from.get(key), beanMap.get(key), mode);
                // debug("populate:" + key + "=>" + value); 
                // debug("String:" + ConvertUtils.convert(from.get(key), String.class)); 
                try {
                    beanMap.put(key, value);
                } catch (Exception e) {
                    debug("populate.failed:" + key + "=>" + value + ";" + e);
                }
            }
        }
        String expression = expressions.get(key);
        if (!isEmpty(expression)) {
            beanMap.put(key, oldValue);
            Map scriptCache = (Map) sessionContext.getProperty("scriptCache");
            if (scriptCache == null) {
                scriptCache = new HashMap();
                sessionContext.setProperty("scriptCache", scriptCache);
            }
            Object result = Utils.eval(expression, beanMap, scriptCache);
            try {
                if ("string".equals(datatype) && !(result instanceof String)) {
                    beanMap.put(key, ConvertUtils.convert(result, String.class));
                } else {
                    beanMap.put(key, result);
                }
            } catch (Exception e) {
                info("Cannot set value for(" + key + "):" + result + "/" + e.getMessage());
            }
        }
    }
}

From source file:org.ms123.common.data.JdoLayerImpl.java

public Map querySql(SessionContext sessionContext, StoreDesc sdesc, Map params, String sql) {
    debug("query:" + params + ",sql:" + sql);
    PersistenceManager pm = sessionContext.getPM();
    Map retMap = new HashMap();
    try {/*from www.  j  a v a 2s  .co  m*/
        debug("=========================================================================================");
        debug("sql:" + sql.replace("\n", " "));
        debug("=========================================================================================");
        debug("Imports:" + sdesc.getImports());
        Query q = pm.newQuery("javax.jdo.query.JPQL", sql.replace("\n", " "));
        q.declareImports(sdesc.getImports());
        List results = (List) q.executeWithMap(params);
        Iterator itr = results.iterator();
        int count = 0;
        int offset = getInt(params, "offset", 0);
        boolean first = true;
        Field fields[] = null;
        String colNames[] = null;
        int total = 0;
        List dataList = new ArrayList();
        int pageSize = getInt(params, "pageSize", 30);
        while (itr.hasNext()) {
            Object[] row = null;
            Object no = itr.next();
            if (pageSize == 0 || (total >= offset && count < pageSize)) {
                dataList.add(new HashMap(new BeanMap(no)));
                count++;
            }
            total++;
        }
        Map ret = new HashMap();
        ret.put("rows", dataList);
        retMap = ret;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    return retMap;
}

From source file:org.ms123.common.data.JdoLayerImpl.java

private void getBinary(SessionContext sessionContext, Object obj, boolean hasTeamSecurity, Map permittedFields,
        HttpServletResponse resp) throws Exception {
    Map<String, Object> map = new HashMap();
    BeanMap beanMap = new BeanMap(obj);
    if (beanMap.getType("_team_list") != null) {
        Set<Object> _teams = (Set<Object>) beanMap.get("_team_list");
        if (hasTeamSecurity && !m_teamService.checkTeams(sessionContext.getStoreDesc().getNamespace(),
                sessionContext.getUserName(), sessionContext.getUserProperties(), _teams)) {
            info("getPropertyMap.notAllowed:" + _teams);
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }//from   w  w w . jav a  2  s  . com
    }
    if (PropertyUtils.isReadable(obj, "content")) {
        byte[] c = (byte[]) PropertyUtils.getProperty(obj, "content");
        if (PropertyUtils.isReadable(obj, "type")) {
            String t = (String) PropertyUtils.getProperty(obj, "type");
            resp.setContentType(t);
        }
        if (PropertyUtils.isReadable(obj, "filename")) {
            String t = (String) PropertyUtils.getProperty(obj, "filename");
            resp.addHeader("Content-Disposition", "inline;filename=" + t);
        }
        if (c != null) {
            debug("getBinary:length:" + c.length);
            IOUtils.write(c, resp.getOutputStream());
        }
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getOutputStream().close();
    }
}

From source file:org.ms123.common.data.JdoLayerImpl.java

private Map<String, Object> getPropertyMap(SessionContext sessionContext, Object obj, boolean hasTeamSecurity,
        Map permittedFields, List<String> fieldsArray) throws Exception {
    Map<String, Object> map = new HashMap();
    BeanMap beanMap = new BeanMap(obj);
    if (beanMap.getType("_team_list") != null) {
        Set<Object> _teams = (Set<Object>) beanMap.get("_team_list");
        debug("getPropertyMap.hasTeamSecurity:" + hasTeamSecurity);
        if (hasTeamSecurity && !m_teamService.checkTeams(sessionContext.getStoreDesc().getNamespace(),
                sessionContext.getUserName(), sessionContext.getUserProperties(), _teams)) {
            debug("getPropertyMap.notAllowed:" + _teams);
            return null;
        }/*  w w w  .  j  ava 2s. c o  m*/
    }
    map.put("id", beanMap.get("id"));
    if (fieldsArray != null) {
        for (String field : fieldsArray) {
            if (beanMap.getType(field) == null) {
                m_logger.error("getPropertyMap.property_not_found:" + field);
                continue;
            }
            Map cm = (Map) permittedFields.get(field);
            if (cm == null) {
                continue;
            }
            try {
                Class type = beanMap.getType(field);
                Object value = beanMap.get(field);
                map.put(field, prepareValue(sessionContext, beanMap, value, (String) cm.get("datatype")));
            } catch (Exception e) {
                m_logger.error("getPropertyMap.field:" + field + "," + e);
                e.printStackTrace();
            }
        }
    } else {
        Iterator itk = beanMap.keyIterator();
        while (itk.hasNext()) {
            String field = (String) itk.next();
            Map cm = (Map) permittedFields.get(field);
            if (cm == null) {
                continue;
            }

            if (STATE_FIELD.equals(cm.get("id")) && !m_permissionService.hasAdminRole()) {
                continue;
            }
            try {
                Class type = beanMap.getType(field);
                debug("Type:" + type + "/" + cm.get("datatype") + "/field:" + field);
                Object value = beanMap.get(field);
                if ("binary".equals(cm.get("datatype"))) {
                    continue;
                }
                if ("fulltext".equals(cm.get("datatype"))) {
                    continue;
                }
                if ("set".equals(cm.get("datatype"))) {
                    continue;
                }
                if (type.equals(java.util.List.class) || type.equals(java.util.Set.class)) {
                    continue;
                }
                map.put(field, prepareValue(sessionContext, beanMap, value, (String) cm.get("datatype")));
            } catch (Exception e) {
                m_logger.error("getPropertyMap.field:" + field + "," + e);
                e.printStackTrace();
            }
        }
    }
    return map;
}

From source file:org.ms123.common.data.JdoLayerImpl.java

private void setRelatedToFields(Map targetMap, String[] colNames, BeanMap beanMap, Object value) {
    BeanMap relatedTo = new BeanMap(value);
    Iterator<String> it = relatedTo.keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();/*w ww. j a va2s.c o m*/
        if (key.startsWith("_"))
            continue;
        if (key.equals("class"))
            continue;
        if (key.equals("id"))
            continue;
        if (key.equals("name"))
            continue;
        if (colNames != null && !arrayContains(colNames, key))
            continue;
        if (beanMap != null && !beanMap.containsKey(key))
            continue;
        Class type = relatedTo.getType(key);
        if (type.equals(List.class) || type.equals(Set.class))
            continue;
        debug("\tsetRelatedToFields.key:" + key + "=" + relatedTo.get(key) + "/" + relatedTo.getType(key));
        targetMap.put(key, relatedTo.get(key));
    }
}

From source file:org.ms123.common.data.JdoLayerImpl.java

private Object prepareValue(SessionContext sc, BeanMap beanMap, Object value, String dt) throws Exception {
    if (value == null) {
        if (dt.startsWith("array/string")) {
            return new ArrayList();
        } else {/*from  w w w .  ja v  a  2 s. com*/
            return null;
        }
    }
    if (value instanceof java.util.Date) {
        return String.valueOf(((Date) value).getTime());
    } else if (value instanceof java.lang.Boolean) {
        return value;
    } else if (value instanceof java.lang.Double) {
        return value;
    } else if (value instanceof java.lang.Integer) {
        return value;
    } else if (dt.startsWith("related")) {
        Object ret = "";
        try {
            Object id = (Object) PropertyUtils.getProperty(value, "id");
            ret = id;
            String name = getRecordTitle(value);
            ret = id + "/" + name;
            ret = getTitle(sc, m_inflector.getEntityName(value.getClass().getSimpleName()), new BeanMap(value),
                    id, (String) ret);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    } else if (dt.startsWith("array/string")) {
        List arr = new ArrayList();
        String[] vals = String.valueOf(value).split(",");
        for (int i = 0; i < vals.length; i++) {
            String v = vals[i];
            arr.add(v);
        }
        return arr;
    } else if (dt.startsWith("map_")) {
        return value;
    } else if (dt.startsWith("list_")) {
        return value;
    } else if (dt.startsWith("array/")) {
        String t = dt.substring(dt.indexOf("/") + 1);
        if ("team".equals(t)) {
            t = "_team_list";
        }
        List arr = new ArrayList();
        Collection<Object> _teams = (Collection) beanMap.get(t);
        for (Object team : _teams) {
            BeanMap bm = new BeanMap(team);
            Iterator itk = bm.keyIterator();
            Map map = new HashMap();
            while (itk.hasNext()) {
                String field = (String) itk.next();
                if ("class".equals(field)) {
                    continue;
                }
                Object val = bm.get(field);
                map.put(field, prepareValue(sc, bm, val, ""));
            }
            arr.add(map);
        }
        return arr;
    } else {
        return String.valueOf(value);
    }
}

From source file:org.ms123.common.data.JdoLayerImpl.java

private Object copyObject(Object o) throws Exception {
    Map n = new HashMap();
    BeanMap beanMap = new BeanMap(o);
    Iterator itv = beanMap.keyIterator();
    while (itv.hasNext()) {
        String prop = (String) itv.next();
        if ("class".equals(prop)) {
            continue;
        }/*w  w  w. j a  v  a 2  s.c  om*/
        Object value = beanMap.get(prop);
        if ("_team_list".equals(prop)) {
            Set teamSet = new HashSet();
            Set teams = (Set) beanMap.get(prop);
            for (Object team : teams) {
                Map t = new HashMap(new BeanMap(team));
                t.remove("teamintern");
                teamSet.add(t);
            }
            value = teamSet;
        } else if (value instanceof Collection) {
            continue;
        } else {
            java.lang.reflect.Field field = o.getClass().getDeclaredField(prop);
            if (field != null) {
                if (field.isAnnotationPresent(Element.class) || field.isAnnotationPresent(Persistent.class)) {
                    continue;
                }
            }
        }
        n.put(prop, value);
    }
    return n;
}

From source file:org.ms123.common.data.lucene.LuceneServiceImpl.java

private void populate(Object obj, List<Map> searchFields, Document doc) {
    Map inMap = null;// ww w  .j a v a 2 s.co  m
    if (obj instanceof Map) {
        inMap = (Map) obj;
    } else {
        inMap = new BeanMap(obj);
    }
    String fulltext = "fulltext";
    for (Map m : searchFields) {
        String name = (String) m.get("name");
        Object v = inMap.get(name);
        if (v == null) {
            continue;
        }
        AbstractField field = null;
        AbstractField ft_field = null;
        String dt = (String) m.get("datatype");
        if ("decimal".equals(dt))
            dt = "double";
        if ("number".equals(dt))
            dt = "integer";
        if ("date".equals(dt)) {
            Date date = (Date) inMap.get(name);
            String value = DateTools.dateToString(date, DateTools.Resolution.DAY);
            field = new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED);
            ft_field = new Field(fulltext, value, Field.Store.NO, Field.Index.NOT_ANALYZED);
        }
        if ("number".equals(dt) || "double".equals(dt) || "long".equals(dt) || "integer".equals(dt)) {
            field = new NumericField(name, Field.Store.YES, true);
            ft_field = new NumericField(fulltext, Field.Store.NO, true);
            if ("long".equals(dt)) {
                Long value = (Long) inMap.get(name);
                ((NumericField) field).setLongValue(value);
                ((NumericField) ft_field).setLongValue(value);
            }
            if ("number".equals(dt)) {
                Integer value = (Integer) inMap.get(name);
                ((NumericField) field).setIntValue(value);
                ((NumericField) ft_field).setIntValue(value);
            }
            if ("integer".equals(dt)) {
                Integer value = (Integer) inMap.get(name);
                ((NumericField) field).setIntValue(value);
                ((NumericField) ft_field).setIntValue(value);
            }
            if ("double".equals(dt)) {
                Double value = (Double) inMap.get(name);
                ((NumericField) field).setDoubleValue(value);
                ((NumericField) ft_field).setDoubleValue(value);
            }
        }
        if ("string".equals(dt) || "text".equals(dt)) {
            String value = (String) inMap.get(name);
            if ("".equals(value)) {
                continue;
            }
            field = new Field(name, value, Field.Store.YES, Field.Index.ANALYZED);
            ft_field = new Field(fulltext, value, Field.Store.NO, Field.Index.ANALYZED);
        }
        if (field != null) {
            doc.add(field);
            doc.add(ft_field);
        }
    }
}

From source file:org.ms123.common.data.MultiOperations.java

private static Object getObjectByFilter(Context context, PersistenceManager pm, Class clazz, Object child,
        String queryString) throws Exception {
    String filter = expandString(context, queryString, new BeanMap(child));
    debug("getObjectByFilter:" + filter + "/clazz:" + clazz);
    Extent e = pm.getExtent(clazz, true);
    Query q = pm.newQuery(e, filter);
    try {//from  w w w . j a  v a2  s .c o  m
        Collection coll = (Collection) q.execute();
        Iterator iter = coll.iterator();
        if (iter.hasNext()) {
            Object obj = iter.next();
            debug("getObjectByFilter:found");
            return obj;
        }
    } finally {
        q.closeAll();
    }
    debug("getObjectByFilter:not found");
    return null;
}