private void writeHeaderRow(Entity entity, HSSFSheet sheet) { HSSFRow headerRow = sheet.createRow(0); int colnum = 0; for (Map.Entry<String, Object> component : getComponents(entity)) { String componentName = component.getKey(); headerRow.createCell(colnum).setCellValue(new HSSFRichTextString(componentName)); ComponentDescriptor cd = entity.descriptor().getComponent(componentName); PrimitiveType primitiveType; if (cd.getTypeDescriptor() instanceof SimpleTypeDescriptor) primitiveType = ((SimpleTypeDescriptor) cd.getTypeDescriptor()).getPrimitiveType(); else throw new UnsupportedOperationException( "Can only export simple type attributes, " + "failed to export " + entity.type() + '.' + cd.getName()); Class<?> javaType = (primitiveType != null ? primitiveType.getJavaType() : String.class); String formatString = null; if (BeanUtil.isIntegralNumberType(javaType)) formatString = getIntegralPattern(); else if (BeanUtil.isDecimalNumberType(javaType)) formatString = getDecimalPattern(); else if (Time.class.isAssignableFrom(javaType)) formatString = getTimePattern(); else if (Timestamp.class.isAssignableFrom(javaType)) formatString = getTimestampPattern(); else if (Date.class.isAssignableFrom(javaType)) formatString = getDatePattern(); if (formatString != null) { HSSFDataFormat dataFormat = workbook.createDataFormat(); CellStyle columnStyle = workbook.createCellStyle(); columnStyle.setDataFormat(dataFormat.getFormat(formatString)); sheet.setDefaultColumnStyle(colnum, columnStyle); } colnum++; } }
@SuppressWarnings("synthetic-access") public ProductWrapper<E> generate(ProductWrapper<E> wrapper) { Entity entity = createEntity("Test"); context.setCurrentProduct(new ProductWrapper<Entity>(entity)); if (!builder.execute((BeneratorContext) context)) return null; return wrapper.wrap((E) entity.get(componentName)); }
@Override public boolean valid(Entity product) { if (!super.valid(product)) return false; String ean = (String) product.getComponent("ean_code"); if (!eanValidator.isValid(ean, null)) return false; String name = (String) product.getComponent("name"); if (name == null || name.length() == 0) return false; if (product.getComponent("name") == null) return false; Number price = (Number) product.getComponent("price"); if (price == null || price.doubleValue() < 0) return false; if (product.getComponent("manufacturer") == null) return false; return true; }
private static Object convertEntity(Entity entity, Class<?> targetType) { Object result = BeanUtil.newInstance(targetType); for (Map.Entry<String, Object> entry : entity.getComponents().entrySet()) { Object value = convertAny(entry.getValue(), typeOrComponentTypeOf(entry.getKey(), targetType)); AnyMutator.setValue(result, entry.getKey(), value, false, true); } return result; }
private HSSFSheet getOrCreateSheet(Entity entity) { // create file if (workbook == null) createWorkbook(); String sheetName = entity.type(); HSSFSheet sheet = workbook.getSheet(sheetName); if (sheet == null) { sheet = workbook.createSheet(sheetName); writeHeaderRow(entity, sheet); } return sheet; }
private Set<Entry<String, Object>> getComponents(Entity entity) { return entity.getComponents().entrySet(); }