@Override public String insertFormData( String businessFormTemplateId, Map<String, String> formData, String flowRequestId) { List<BusinessBaseTemplate> list = businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId); for (BusinessBaseTemplate baseTemplate : list) { if (baseTemplate.getBusinessBaseTemplate().endsWith("empty_detail.ftl") || baseTemplate.getBusinessBaseTemplate().endsWith("empty_main.ftl")) continue; insertFormData(baseTemplate, formData, flowRequestId); } return ""; }
@Override public String updateFormData( String businessFormtemplateId, Map<String, String> formData, String flowRequestId) { List<BusinessBaseTemplate> list = businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormtemplateId); for (BusinessBaseTemplate baseTemplate : list) { if (baseTemplate.getBusinessBaseTemplate().endsWith("empty_detail.ftl") || baseTemplate.getBusinessBaseTemplate().endsWith("empty_main.ftl")) continue; String businessBaseId = baseTemplate.getBusinessBaseId(); BusinessBase businessBase = businessBaseDao.getById(businessBaseId); // 如果是主表的话先判断是否存储数据,如果不存在执行insert操作,存在执行update操作。(解决可能人工通过plsql删除数据的情况) if (businessBase.getIsDetail() != null && businessBase.getIsDetail().intValue() == 0) { if (countFormData(baseTemplate, formData, flowRequestId) == 0) { insertFormData(baseTemplate, formData, flowRequestId); } else { updateFormData(baseTemplate, formData, flowRequestId); } } else { updateFormData(baseTemplate, formData, flowRequestId); } } return ""; }
@Override public String buildByTemplateIdAndSubix( String businessFormTemplateId, String baseSubix, String mainSubix, String detailSubix, String htmlSubix) { StringBuffer bld = new StringBuffer(); List<businessBaseVo> businessBaseFormList = new ArrayList<businessBaseVo>(); BusinessFormTemplate businsessFormTemplate = businessFormTemplateDao.getById(businessFormTemplateId); String htmlTemplatePath = businsessFormTemplate.getHtmlElementPath() + htmlSubix; List<BusinessBaseTemplate> list = businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId); for (BusinessBaseTemplate baseTemplate : list) { String businessBaseId = baseTemplate.getBusinessBaseId(); BusinessBase businessBase = this.businessBaseDao.getById(businessBaseId); Integer isDetail = businessBase.getIsDetail(); String templatePath = baseTemplate.getBusinessBaseTemplate(); if (isDetail != null && isDetail.intValue() == 0) { templatePath = templatePath + mainSubix; } else { templatePath = templatePath + detailSubix; } BaseForm baseForm = this.baseFormDao.getById(businessBase.getBaseFormId()); baseForm.setBaseFormCode(baseForm.getBaseFormCode().toLowerCase()); List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId()); List<TemplateFieldDto> templateFieldList = new ArrayList<TemplateFieldDto>(); for (TemplateField templateField : fieldList) { if (htmlSubix.equalsIgnoreCase(".read")) { templateField.setIsEdit(0); templateField.setIsRequire(0); } if (templateField != null && templateField.getIsShow() != null && templateField.getIsShow() == 1) { if (isDetail != null && isDetail.intValue() == 0) { templateFieldList.add( templateFieldService.bulidMainField(templateField, htmlTemplatePath)); } else { templateFieldList.add( templateFieldService.buildDetailField(templateField, htmlTemplatePath)); } } } Map<String, Object> param = new HashMap<String, Object>(); param.put("templateFieldList", templateFieldList); param.put("baseTemplate", baseTemplate); param.put("baseForm", baseForm); try { Template template = formTemplateService.getTemplate(templatePath); StringWriter writer = new StringWriter(); template.process(param, writer); businessBaseVo vo = new businessBaseVo(); vo.setBusinessBaseHtml(writer.toString()); vo.setBusinessBaseTemplate(baseTemplate); businessBaseFormList.add(vo); } catch (IOException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-209"); } catch (TemplateException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-210"); } } try { String baseTemplatePath = businessFormTemplateDao.getById(businessFormTemplateId).getTempatePath() + baseSubix; Map<String, Object> param = new HashMap<String, Object>(); param.put("businsessFormTemplate", businsessFormTemplate); param.put("businessBaseFormList", businessBaseFormList); Template template = formTemplateService.getTemplate(baseTemplatePath); StringWriter writer = new StringWriter(); template.process(param, writer); bld.append(writer.toString()); } catch (IOException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-209"); } catch (TemplateException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-210"); } String resultForm = bld.toString(); return resultForm; }
@SuppressWarnings({"unchecked", "rawtypes"}) private void updateFormData( BusinessBaseTemplate baseTemplate, Map<String, String> formData, String flowRequestId) { String businessBaseId = baseTemplate.getBusinessBaseId(); BusinessBase businessBase = businessBaseDao.getById(businessBaseId); String baseFormId = businessBase.getBaseFormId(); BaseForm baseForm = baseFormDao.getById(baseFormId); List<TemplateFieldDto> fieldDtoList = new ArrayList<TemplateFieldDto>(); StringBuffer insertSql = new StringBuffer(); StringBuffer paramSql = new StringBuffer(); Map<String, Object> insertParam = new HashMap<String, Object>(); insertSql.append(" insert into ").append(baseForm.getBaseFormCode()).append("(id,"); paramSql.append(" values (#{id},"); insertSql.append("FLOW_REQUEST_ID,"); paramSql.append("#{flowRequestId},"); insertParam.put("flowRequestId", flowRequestId); StringBuffer updateSql = new StringBuffer(); Map<String, Object> updateParam = new HashMap<String, Object>(); updateParam.put("flowRequestId", flowRequestId); updateSql.append("update ").append(baseForm.getBaseFormCode()).append(" set "); List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId()); for (TemplateField templateField : fieldList) { if (templateField != null && templateField.getIsShow() != null && templateField.getIsShow() == 1) { FormField formField = formFieldDao.getById(templateField.getFieldId()); TemplateFieldDto dto = new TemplateFieldDto(); dto.setFormField(formField); dto.setTemplateField(templateField); fieldDtoList.add(dto); updateSql.append(formField.getFieldCode()); updateSql.append(" = #{" + formField.getFieldCode().toLowerCase() + "},"); insertSql.append(formField.getFieldCode()).append(","); paramSql.append("#{" + formField.getFieldCode().toLowerCase() + "},"); } } String sql_update = updateSql.substring(0, updateSql.length() - 1) + " where id = #{id} and FLOW_REQUEST_ID = #{flowRequestId} "; String sql_insert = insertSql.substring(0, insertSql.length() - 1) + ") " + paramSql.substring(0, paramSql.length() - 1) + ")"; if (businessBase.getIsDetail() != null && businessBase.getIsDetail().intValue() == 0) { String id = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_id"); updateParam.put("id", id); List<Map<String, String>> formFileList = new ArrayList<Map<String, String>>(); Map<String, Object> validValueMap = new HashMap<String, Object>(); for (TemplateFieldDto dto : fieldDtoList) { TemplateField templateField = dto.getTemplateField(); if (templateField != null && templateField.getIsShow() != null && templateField.getIsShow() == 1) { FormField formField = dto.getFormField(); Object value = formData.get( baseForm.getBaseFormCode().toLowerCase() + "_" + formField.getFieldCode().toLowerCase()); if (value == null) value = ""; if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_UPLOAD && formField.getDatasourceType().intValue() == 2) { Object fileValue = formData.get( baseForm.getBaseFormCode().toLowerCase() + "_" + formField.getFieldCode().toLowerCase() + "_file"); fileValue = (fileValue == null ? "" : fileValue.toString()); if (fileValue.toString().endsWith(",")) fileValue = fileValue.toString().substring(0, fileValue.toString().length() - 1); Map<String, String> map = new HashMap<String, String>(); if (StringUtil.isNullOrEmpty(value)) { value = UUIDGenerator.getUUID(); } map.put("attachId", value.toString()); map.put("fileList", fileValue.toString()); formFileList.add(map); } if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_DATAFIELD) { if (!StringUtil.isNullOrEmpty(value)) { try { value = DateUtils.parseDate(value.toString(), new String[] {"yyyy-MM-dd"}); } catch (ParseException e) { log.error( this.getClass().getSimpleName() + " e==> updateFormData ,ParseException + businessFormtemplateId:[" + baseTemplate.getBusinessBaseTemplate() + "]", e); value = ""; } } else { value = ""; } } dto.setValue(value); validValueMap.put(formField.getFieldCode().toLowerCase(), value); updateParam.put(formField.getFieldCode().toLowerCase(), value); } } // 验证表单正确性 for (TemplateFieldDto dto : fieldDtoList) { templateFieldService.checkFormFieldValue(dto, validValueMap); } // 主表更新 businessBaseTemplateDao.updateDataBySql(sql_update, updateParam); // 更新附件信息 for (Map<String, String> formFile : formFileList) { String attachId = formFile.get("attachId"); String[] fileList = formFile.get("fileList").split(","); formFileService.updateFileList(attachId, fileList); } } else { String detailDataIds = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_DETAIL_DATAIDS"); String[] detailDataIdArr = detailDataIds.split(","); Integer maxValue = baseTemplate.getMaxValue(); if (maxValue != null && maxValue.intValue() > 0 && (detailDataIdArr.length > maxValue)) { throw new FormException("form-212", "表单数据超出范围"); } // 需要更新的明细idList 也就是删除时候的not in List<String> updateIdList = new ArrayList<String>(); // 新增加的明细参数 List<Map<String, Object>> insertParamList = new ArrayList<Map<String, Object>>(); for (int i = 0; i < detailDataIdArr.length; i++) { String idIndex = detailDataIdArr[i]; if (StringUtil.isNullOrEmpty(idIndex)) continue; Object id = formData.get(baseForm.getBaseFormCode().toLowerCase() + "_id_" + idIndex); Map<String, Object> validValueMap = new HashMap<String, Object>(); for (TemplateFieldDto dto : fieldDtoList) { TemplateField templateField = dto.getTemplateField(); if (templateField != null && templateField.getIsShow() != null && templateField.getIsShow() == 1) { FormField formField = dto.getFormField(); Object value = formData.get( baseForm.getBaseFormCode().toLowerCase() + "_" + formField.getFieldCode().toLowerCase() + "_" + idIndex); if (value == null) value = ""; if (formField.getViewType().intValue() == FormMetaValue.FORMFIELD_DATAFIELD) { if (!StringUtil.isNullOrEmpty(value)) { try { value = DateUtils.parseDate(value.toString(), new String[] {"yyyy-MM-dd"}); } catch (ParseException e) { log.error( this.getClass().getSimpleName() + " e==> updateFormData ,ParseException + businessFormtemplateId:[" + baseTemplate.getBusinessBaseTemplate() + "]", e); value = ""; } } else { value = null; } } dto.setValue(value); validValueMap.put(formField.getFieldCode().toLowerCase(), value); updateParam.put(formField.getFieldCode().toLowerCase(), value); insertParam.put(formField.getFieldCode().toLowerCase(), value); } } // 验证表单正确性 for (TemplateFieldDto dto : fieldDtoList) { templateFieldService.checkFormFieldValue(dto, validValueMap); } if (StringUtil.isNullOrEmpty(id)) { // 表示新增明细 id = UUIDGenerator.getUUID(); insertParam.put("id", id); HashMap<String, Object> insert = new HashMap<String, Object>(); insert.putAll(insertParam); insertParamList.add((HashMap) insert.clone()); } else { updateIdList.add(String.valueOf(id)); updateParam.put("id", id); businessBaseTemplateDao.updateDataBySql(sql_update, updateParam); } } // 删除not in update idList businessBaseTemplateDao.deleteDetailData( baseForm.getBaseFormCode(), flowRequestId, updateIdList); // 新增明细 for (Map<String, Object> insert : insertParamList) { businessBaseTemplateDao.insertDataBySql(sql_insert, insert); } } }
@Override public String buildByTemplateIdAndShowOrder(String businessFormTemplateId, int start, int end) { StringBuffer bld = new StringBuffer(); List<businessBaseVo> businessBaseFormList = new ArrayList<businessBaseVo>(); BusinessFormTemplate businsessFormTemplate = businessFormTemplateDao.getById(businessFormTemplateId); String htmlTemplatePath = businsessFormTemplate.getHtmlElementPath(); List<BusinessBaseTemplate> list = businessBaseTemplateDao.queryBybusinessBaseTemplate(businessFormTemplateId); for (BusinessBaseTemplate baseTemplate : list) { if (baseTemplate.getShowOrder() > end || baseTemplate.getShowOrder() < start) continue; String templatePath = baseTemplate.getBusinessBaseTemplate(); String businessBaseId = baseTemplate.getBusinessBaseId(); BusinessBase businessBase = this.businessBaseDao.getById(businessBaseId); Integer isDetail = businessBase.getIsDetail(); BaseForm baseForm = this.baseFormDao.getById(businessBase.getBaseFormId()); baseForm.setBaseFormCode(baseForm.getBaseFormCode().toLowerCase()); List<TemplateField> fieldList = templateFieldDao.queryByTemplateFormId(baseTemplate.getId()); List<TemplateFieldDto> templateFieldList = new ArrayList<TemplateFieldDto>(); for (TemplateField templateField : fieldList) { if (templateField != null && templateField.getIsShow() != null && templateField.getIsShow() == 1) { if (isDetail != null && isDetail.intValue() == 0) { templateFieldList.add( templateFieldService.bulidMainField(templateField, htmlTemplatePath)); } else { templateFieldList.add( templateFieldService.buildDetailField(templateField, htmlTemplatePath)); } } } Map<String, Object> param = new HashMap<String, Object>(); param.put("templateFieldList", templateFieldList); param.put("baseTemplate", baseTemplate); param.put("baseForm", baseForm); try { Template template = formTemplateService.getTemplate(templatePath); StringWriter writer = new StringWriter(); template.process(param, writer); bld.append(writer.toString()); } catch (IOException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,IOException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-209"); } catch (TemplateException e) { log.error( this.getClass().getSimpleName() + " e==> buildByBusinessFormTemplateId ,TemplateException + businessFormTemplateId:[" + businessFormTemplateId + "]", e); throw new FormException("form-210"); } } return bld.toString(); }