工厂模式的一次运用

最近有个需求,是解析上传的xml文件,上传的xml文件分为几种不同的类型,但是解析完成之后又要做一些相同的操作,所以就想到了用工厂的模式去实现。下面记录一下,实现的方式:

 

首先分析需求,由于解析过程中都插入过程表,所以建的一个类就是一个抽象类,而没有去建接口,这样便于写一些实现的方法

public abstract class Progress {
	@Resource
	private ConsumerFeignClient consumerFeignClient;

	private static Progress progress;

	@PostConstruct
	public void initClient(){
		progress = this;
		progress.consumerFeignClient = this.consumerFeignClient;
	}


	public abstract void insertRecord(Document document,String logId);
	public UploadData insertUploadLogData(String logId,String districtCode,String dataCode){
		UploadData uploadData = new UploadData();
		APIResponse apiResponse = progress.consumerFeignClient.queryDistrict(districtCode);
		List<Map<String,String>> info = (List<Map<String, String>>) apiResponse.getData();
		//String districtName = info.get(0).get("districtName");
		//String districtName = "东城区管理员";
		uploadData.setDataCode(dataCode);
		//uploadData.setDistrictName(districtName);
		uploadData.setDistrictCode(districtCode);
		uploadData.setUploadLogId(logId);
		uploadData.setId(UuidUtil.getUuid());
		return uploadData;
	}
}

 

然后创建一个工厂类,用于生产相应的解析类:

public class ProgressFactory {

	public Progress getProgress(String fileType){
		if (fileType == null){
			return null;
		}
        //根据不同类型创建不同的解析类
		if (FileTypeEnum.JSJD.getName().equals(fileType)){
			return new ReceiveProgress();
		}else if (FileTypeEnum.FFJD.getName().equals(fileType)){
			return new DistributionProgress();
		}else if (FileTypeEnum.LQJD.getName().equals(fileType)){
			return new TaskReceiveProgress();
		}else if (FileTypeEnum.ZJJD.getName().equals(fileType)){
			return new CheckRecordProgress();
		}else if (FileTypeEnum.ZJSHXX.getName().equals(fileType)){
			return new AuditRecordProgress();
		}else if (FileTypeEnum.ZJFKYJ.getName().equals(fileType)){
			return new CheckCommentProgress();
		}
		return null;
	}
}

接下来就是相应的解析类了,随便贴一个:

@Component
public class TaskReceiveProgress extends Progress {

	@Autowired
	private TaskreceiveRecordMapper taskreceiveRecordMapper;
	@Autowired
	private DispatchFlowsCheckMapper dispatchFlowsCheckMapper;
	@Autowired
	private TaskreceiveDataMapper taskreceiveDataMapper;
	@Autowired
	private UploadDataMapper uploadLogMapper;

	private static TaskReceiveProgress taskReceiveProgress;

	@PostConstruct
	public void init(){
		taskReceiveProgress = this;
		taskReceiveProgress.taskreceiveRecordMapper = this.taskreceiveRecordMapper;
		taskReceiveProgress.dispatchFlowsCheckMapper = this.dispatchFlowsCheckMapper;
		taskReceiveProgress.taskreceiveDataMapper = this.taskreceiveDataMapper;
		taskReceiveProgress.uploadLogMapper = this.uploadLogMapper;
	}

	@Override
	public void insertRecord(Document document, String logId) {
		List Package = document.getRootElement().element("Packages").elements("Package");
		SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		for (Iterator item = Package.iterator(); item.hasNext();) {
			Element root = (Element) item.next();
			//生成数据分发上传过程表记录
			TaskreceiveRecord taskreceiveRecord = new TaskreceiveRecord();
			String taskreceiveRecordId = UuidUtil.getUuid();
			taskreceiveRecord.setId(taskreceiveRecordId);
			taskreceiveRecord.setDataRecieveId(root.element("DATA_RECIEVE_ID").getText());
			taskreceiveRecord.setDataDistributeId(root.element("DATA_DISTRIBUTE_ID").getText());
			taskreceiveRecord.setReceiveUsername(root.element("RECEIVE_USERNAME").getText());
			taskreceiveRecord.setDistrictCode(root.element("DISTRICT_CODE").getText());
			taskreceiveRecord.setDistrictName(root.element("DISTRICT_NAME").getText());
			taskreceiveRecord.setXianCount(Integer.parseInt(root.element("XIAN_COUNT").getText()));
			try {
				taskreceiveRecord.setReceiveTime(dateFormat.parse(root.element("RECEIVE_TIME").getText()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			taskReceiveProgress.taskreceiveRecordMapper.insertSelective(taskreceiveRecord);
			//生成数据分发记录表记录
			List Items = root.element("DataItems").elements("Item");
			ArrayList<TaskreceiveData> taskreceiveDataList = new ArrayList<>();
			ArrayList<UploadData> uploadLogs = new ArrayList<>();
			for (Iterator it = Items.iterator(); it.hasNext();) {
				TaskreceiveData taskreceiveData = new TaskreceiveData();
				Element bElm = (Element) it.next();
				//生成上传文件记录表的信息
				UploadData uploadData = insertUploadLogData(logId, bElm.element("DISTRICT_CODE").getText().trim(),bElm.element("DATA_CODE").getText());
				uploadLogs.add(uploadData);
				String id = UuidUtil.getUuid();
				taskreceiveData.setId(id);
				taskreceiveData.setTaskserviceId(taskreceiveRecordId);
				taskreceiveData.setDataCode(bElm.element("DISTRICT_CODE").getText().trim());
				taskreceiveData.setDataCode(bElm.element("DATA_CODE").getText());
				taskreceiveData.setDistrictName(uploadData.getDistrictName());
				taskreceiveDataList.add(taskreceiveData);
				//更新流程表
				DispatchFlowsCheck dispatchFlows = taskReceiveProgress.dispatchFlowsCheckMapper.getByDataCode(bElm.element("DATA_CODE").getText());
				if (dispatchFlows==null){
					dispatchFlows = new DispatchFlowsCheck();
					dispatchFlows.setId(UuidUtil.getUuid());
					dispatchFlows.setDataCode(bElm.element("DATA_CODE").getText());
					dispatchFlows.setDistrictName(uploadData.getDistrictName());
					dispatchFlows.setDistrictCode(uploadData.getDistrictCode());
					dispatchFlows.setRecieveId(id);
					taskReceiveProgress.dispatchFlowsCheckMapper.insertSelective(dispatchFlows);
				}else{
					taskReceiveProgress.dispatchFlowsCheckMapper.updateCodeByDataCode("RECIEVE_ID",id,bElm.element("DATA_CODE").getText());
				}
			}
			taskReceiveProgress.taskreceiveDataMapper.bulkInsert(taskreceiveDataList);
			taskReceiveProgress.uploadLogMapper.bulkInsert(uploadLogs);
		}
	}

}

 

以上就是工厂类的一些运用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值