Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mc.printer.model.panel.client.paper; import com.mc.printer.model.AutoPrinterApp; import com.mc.printer.model.constants.ClientConstants; import com.mc.printer.model.constants.Constants; import com.mc.printer.model.entity.SavedDataEntity; import com.mc.printer.model.entity.TbSavedata; import com.mc.printer.model.layout.BaseTask; import com.mc.printer.model.layout.ws.CommFindEntity; import com.mc.printer.model.utils.DateHelper; import com.mc.printer.model.utils.XMLHelper; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; /** * * @author woderchen@163.com */ public abstract class ReadSavedTask extends BaseTask { private static final Logger logger = LoggerFactory.getLogger(ReadSavedTask.class); private final String formValueKey; public ReadSavedTask(String formValueKey) { logger.debug("find cache data:" + formValueKey); this.formValueKey = formValueKey; } public HashMap<String, List<SaveOject>> getRemoteServer() { HashMap<String, List<SaveOject>> map = new HashMap(); try { String URI = ClientConstants.WS_HTTP; String urlParam = "key=" + formValueKey; logger.debug("URL parameter:" + urlParam); CommFindEntity<SavedDataEntity> response = restTemplate.exchange(URI + "/data/" + "?" + urlParam, HttpMethod.GET, null, new ParameterizedTypeReference<CommFindEntity<SavedDataEntity>>() { }).getBody(); logger.debug("return data from server." + response); if (response != null && response.getCount() > 0) { logger.info("return data total." + response.getCount()); List<SavedDataEntity> alldata = response.getResult(); for (SavedDataEntity d : alldata) { SaveOject obj = new SaveOject(); obj.setElementName(d.getElementname()); obj.setValue(d.getValue()); obj.setBranchName(d.getBranch() != null ? d.getBranch().getName() : ""); String dateStr = "key"; if (d.getSubmitdate() != null) { dateStr = DateHelper.format(d.getSubmitdate(), "yyyy/MM/dd HH:mm"); } if (!map.containsKey(dateStr)) { List<SaveOject> secitonLs = new ArrayList(); secitonLs.add(obj); map.put(dateStr, secitonLs); } else { map.get(dateStr).add(obj); } } } } catch (Exception e) { } return map; } public HashMap<String, List<SaveOject>> getLocalData() { String saveDir = Constants.SYSTEM_SAVE_PATH_DEFAULT; String path = System.getProperty(Constants.SYSTEM_SAVE_PATH_OPTION); if (path != null) { saveDir = path + File.separator; } logger.debug("find path:" + saveDir); File file = new File(saveDir); if (file.exists()) { File[] files = file.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(Constants.SYSTEM_SAVE_SPLIT); } }); for (File f : files) { // String fileName = f.getName(); if (fileName.equalsIgnoreCase(formValueKey)) { //??? String[] formandvalues = fileName.split("\\" + Constants.SYSTEM_SAVE_SPLIT); if (formandvalues.length > 1) { //1?form+ ? logger.debug("collect data:" + fileName); try { SaveBean saveBean = new SaveBean(); XMLHelper<SaveBean> helper = new XMLHelper<SaveBean>(saveDir + fileName, saveBean); saveBean = helper.read(); List<SaveOject> saveobjs = saveBean.getSaveObject(); if (saveobjs != null) { logger.debug("get saved data,total:" + saveobjs.size()); } HashMap<String, List<SaveOject>> map = new HashMap(); map.put("key", saveobjs); return map; } catch (Exception e) { logger.error(e.getMessage()); } } } } } else { logger.debug("no exist:" + saveDir); } return null; } @Override public Object doBackgrounp() { /*?*/ HashMap<String, List<SaveOject>> datas = getLocalData(); //?? if (AutoPrinterApp.currentOpenMode.equals(AutoPrinterApp.MODE_CLIENT)) { if (datas == null || datas.size() <= 0) { datas = getRemoteServer(); } } return datas; } @Override public void onSucceeded(Object object) { } }