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);
}
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;
}
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();
}
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;
}
Aggregations