Search in sources :

Example 1 with Model

use of com.jfinal.plugin.activerecord.Model in project jfinal by jfinal.

the class JFinalJson method otherToJson.

protected String otherToJson(Object value, int depth) {
    if (value instanceof Character) {
        return "\"" + escape(value.toString()) + "\"";
    }
    if (value instanceof Model) {
        Map map = com.jfinal.plugin.activerecord.CPI.getAttrs((Model) value);
        return mapToJson(map, depth);
    }
    if (value instanceof Record) {
        Map map = ((Record) value).getColumns();
        return mapToJson(map, depth);
    }
    if (value.getClass().isArray()) {
        int len = Array.getLength(value);
        List<Object> list = new ArrayList<Object>(len);
        for (int i = 0; i < len; i++) {
            list.add(Array.get(value, i));
        }
        return iteratorToJson(list.iterator(), depth);
    }
    if (value instanceof Iterator) {
        return iteratorToJson((Iterator) value, depth);
    }
    if (value instanceof Enumeration) {
        ArrayList<?> list = Collections.list((Enumeration<?>) value);
        return iteratorToJson(list.iterator(), depth);
    }
    if (value instanceof Enum) {
        return "\"" + ((Enum) value).toString() + "\"";
    }
    return beanToJson(value, depth);
}
Also used : Enumeration(java.util.Enumeration) Model(com.jfinal.plugin.activerecord.Model) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Record(com.jfinal.plugin.activerecord.Record) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Model

use of com.jfinal.plugin.activerecord.Model in project my_curd by qinyou.

the class PoiKit method export.

public HSSFWorkbook export() {
    Preconditions.checkNotNull(headers, "headers can not be null");
    Preconditions.checkNotNull(columns, "columns can not be null");
    Preconditions.checkArgument(cellWidth >= 0, "cellWidth < 0");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetName);
    HSSFRow row = null;
    HSSFCell cell = null;
    if (headers.length > 0) {
        row = sheet.createRow(0);
        if (headerRow <= 0) {
            headerRow = HEADER_ROW;
        }
        headerRow = Math.min(headerRow, MAX_ROWS);
        for (int h = 0, lenH = headers.length; h < lenH; h++) {
            @SuppressWarnings("deprecation") Region // 合并从第rowFrom行columnFrom列
            region = new Region(0, (short) h, (short) headerRow - 1, (short) h);
            // 到rowTo行columnTo的区域
            sheet.addMergedRegion(region);
            // 得到所有区域
            sheet.getNumMergedRegions();
            if (cellWidth > 0) {
                sheet.setColumnWidth(h, cellWidth);
            }
            cell = row.createCell(h);
            cell.setCellValue(headers[h]);
            HSSFCellStyle style = wb.createCellStyle();
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            style.setFillForegroundColor(HSSFColor.AQUA.index);
            cell.setCellStyle(style);
        }
    }
    if (data.size() == 0) {
        return wb;
    }
    for (int i = 0, len = data.size(); i < len; i++) {
        row = sheet.createRow(i + headerRow);
        Object obj = data.get(i);
        if (obj == null) {
            continue;
        }
        if (obj instanceof Map) {
            processAsMap(columns, row, obj);
        } else if (obj instanceof Model) {
            processAsModel(columns, row, obj);
        } else if (obj instanceof Record) {
            processAsRecord(columns, row, obj);
        }
    }
    return wb;
}
Also used : Model(com.jfinal.plugin.activerecord.Model) Region(org.apache.poi.hssf.util.Region) Record(com.jfinal.plugin.activerecord.Record) Map(java.util.Map)

Example 3 with Model

use of com.jfinal.plugin.activerecord.Model in project my_curd by qinyou.

the class CsvUtil method createCSV.

/**
 * 将文本头与数据共同转成csv字符串
 *
 * @param headers 列属性
 * @param data    数据
 * @param columns 需要显示列的key值
 * @return csv字符串
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static String createCSV(List headers, List data, List columns) {
    StringBuffer strOut = new StringBuffer("");
    if (null != headers && !headers.isEmpty()) {
        // 如果文本不为空则添加到csv字符串中
        listToCSV(strOut, headers);
    }
    if (null == data || data.isEmpty()) {
        return strOut.toString();
    }
    Iterator itr = data.iterator();
    while (itr.hasNext()) {
        // 将数据添加到csv字符串
        Object obj = itr.next();
        Class cls = obj.getClass();
        if (cls != null && cls.isArray()) {
            if (obj != null) {
                Object[] objs = (Object[]) obj;
                if (objs != null) {
                    for (short i = 0; i < objs.length; i++) {
                        createCol(strOut, objs[i]);
                        strOut.append(",");
                    }
                    // 去点多余逗号
                    strOut = strOut.deleteCharAt(strOut.length() - 1);
                    strOut.append("\n");
                }
            }
        } else if (obj instanceof List) {
            List objlist = (List) obj;
            if (null == columns || columns.isEmpty()) {
                // 如果没有限制,默认全部显示
                listToCSV(strOut, objlist);
            } else {
                for (int i = 0; i < columns.size(); i++) {
                    createCol(strOut, objlist.get((Integer) columns.get(i)));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            }
        } else if (obj instanceof Map) {
            Map objmap = (Map) obj;
            if (null == columns || columns.isEmpty()) {
                // 如果没有限制,默认全部显示
                Set keyset = objmap.keySet();
                for (Object key : keyset) {
                    createCol(strOut, objmap.get(key));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            } else {
                for (int i = 0; i < columns.size(); i++) {
                    createCol(strOut, objmap.get(columns.get(i)));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            }
        } else if (obj instanceof Model) {
            Model objmodel = (Model) obj;
            if (null == columns || columns.isEmpty()) {
                // 如果没有限制,默认全部显示
                Set<Entry<String, Object>> entries = objmodel._getAttrsEntrySet();
                for (Entry entry : entries) {
                    createCol(strOut, entry.getValue());
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            } else {
                for (int i = 0; i < columns.size(); i++) {
                    createCol(strOut, objmodel.get(columns.get(i) + ""));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            }
        } else if (obj instanceof Record) {
            Record objrecord = (Record) obj;
            Map<String, Object> map = objrecord.getColumns();
            if (null == columns || columns.isEmpty()) {
                // 如果没有限制,默认全部显示
                Set<String> keys = map.keySet();
                for (String key : keys) {
                    createCol(strOut, objrecord.get(key));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            } else {
                for (int i = 0; i < columns.size(); i++) {
                    createCol(strOut, objrecord.get(columns.get(i) + ""));
                    strOut.append(",");
                }
                strOut = strOut.deleteCharAt(strOut.length() - 1);
                strOut.append("\n");
            }
        } else {
            while (itr.hasNext()) {
                Object objs = itr.next();
                if (objs != null) {
                    createCol(strOut, objs);
                    strOut.append("\n");
                }
            }
        }
        obj = null;
    }
    return strOut.toString();
}
Also used : Entry(java.util.Map.Entry) Model(com.jfinal.plugin.activerecord.Model) Record(com.jfinal.plugin.activerecord.Record)

Example 4 with Model

use of com.jfinal.plugin.activerecord.Model in project jfinal by jfinal.

the class Injector method injectModel.

@SuppressWarnings("unchecked")
public static <T> T injectModel(Class<T> modelClass, String modelName, HttpServletRequest request, boolean skipConvertError) {
    Object temp = createInstance(modelClass);
    if (temp instanceof Model == false) {
        throw new IllegalArgumentException("getModel only support class of Model, using getBean for other class.");
    }
    Model<?> model = (Model<?>) temp;
    Table table = TableMapping.me().getTable(model.getClass());
    if (table == null) {
        throw new ActiveRecordException("The Table mapping of model: " + modelClass.getName() + " not exists or the ActiveRecordPlugin not start.");
    }
    String modelNameAndDot = StrKit.notBlank(modelName) ? modelName + "." : null;
    Map<String, String[]> parasMap = request.getParameterMap();
    TypeConverter converter = TypeConverter.me();
    // 以及支持界面的 attrName有误时可以感知并抛出异常避免出错
    for (Entry<String, String[]> entry : parasMap.entrySet()) {
        String paraName = entry.getKey();
        String attrName;
        if (modelNameAndDot != null) {
            if (paraName.startsWith(modelNameAndDot)) {
                attrName = paraName.substring(modelNameAndDot.length());
            } else {
                continue;
            }
        } else {
            attrName = paraName;
        }
        Class<?> colType = table.getColumnType(attrName);
        if (colType == null) {
            if (skipConvertError) {
                continue;
            } else {
                throw new ActiveRecordException("The model attribute " + attrName + " is not exists.");
            }
        }
        try {
            String[] paraValueArray = entry.getValue();
            String paraValue = (paraValueArray != null && paraValueArray.length > 0) ? paraValueArray[0] : null;
            Object value = paraValue != null ? converter.convert(colType, paraValue) : null;
            model.set(attrName, value);
        } catch (Exception e) {
            if (skipConvertError == false) {
                throw new RuntimeException("Can not convert parameter: " + paraName, e);
            }
        }
    }
    return (T) model;
}
Also used : Table(com.jfinal.plugin.activerecord.Table) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) ActiveRecordException(com.jfinal.plugin.activerecord.ActiveRecordException) TypeConverter(com.jfinal.core.converter.TypeConverter) Model(com.jfinal.plugin.activerecord.Model)

Aggregations

Model (com.jfinal.plugin.activerecord.Model)4 Record (com.jfinal.plugin.activerecord.Record)3 Map (java.util.Map)2 TypeConverter (com.jfinal.core.converter.TypeConverter)1 ActiveRecordException (com.jfinal.plugin.activerecord.ActiveRecordException)1 Table (com.jfinal.plugin.activerecord.Table)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 Region (org.apache.poi.hssf.util.Region)1