package com.topsun.posclient.common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.topsun.posclient.common.core.CommonCoreActivator;
import com.topsun.posclient.datamodel.Item;
import com.topsun.posclient.datamodel.ItemIndex;
import com.topsun.posclient.datamodel.dto.ItemDTO;
import com.topsun.posclient.datamodel.dto.ItemIndexDTO;
/**
*
* @author Dong
*
*/
public class LocalDataProcessor {
String id;
public LocalDataProcessor() { }
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public LocalDataProcessor(String id) {
this.id = id;
}
/**
* 删除数据文件
* @param backFile
*/
public void delateDataFile(File dataFile){
if(dataFile.exists()){
dataFile.delete();
}
}
/**
* 拷贝文件到备份目录
* @param file
* @param backPath
* @param needDel
* @return
* @throws IOException
*/
public boolean copyFile(File file,String targetPath, boolean needDel) throws IOException{
File backFile = new File(targetPath);
copyFile(file, backFile);
if(needDel){
file.delete();
}
return true;
}
/**
* 备份单品数据索引文件和数据文件
* @throws IOException
*
*/
public void backItemDataAndIndexFile() throws Exception{
String runtimePath = ProjectUtil.getRuntimeClassPath();
File indexFile = new File(runtimePath + AppConstants.DATA_ITEM_INDEX_FILENAME);
String targetIndexPath = runtimePath + AppConstants.DATA_ITEM_INDEX_FILENAME_BACK;
ItemIndexDTO oldIndex = (ItemIndexDTO)getObjectFromXml(getDataFileContent(new File(ProjectUtil.getRuntimeClassPath()+AppConstants.DATA_ITEM_INDEX_FILENAME)), ItemIndexDTO.class);
List<ItemIndex> oldIndexs = oldIndex.getItemIndexList();
StringBuffer oldBarCodes = new StringBuffer();
for(int i=0; i<oldIndexs.size(); i++){
oldBarCodes.append(oldIndexs.get(i).getBarCodes());
String dataFileName = getItemDataFileNameWithIndex(oldBarCodes, AppConstants.DATA_ITEM_FILENAME_NONEXT);
String dataBackFileName = getItemDataFileNameWithIndex(oldBarCodes, AppConstants.DATA_ITEM_FILENAME_BACK_NOEXT);
File dataFile = new File(runtimePath + dataFileName);
String targetDataPath = runtimePath +dataBackFileName;
this.copyFile(dataFile, targetDataPath, true);
}
copyFile(indexFile, targetIndexPath, true);
}
private String getItemDataFileNameWithIndex(StringBuffer idFlagList,
String name) {
String ids = idFlagList.toString();
String[] idArray = ids.split(",");
String lastId = idArray[idArray.length - 1];
return name + "#" + lastId + ".xml";
}
// 复制文件
private static void copyFile(File sourceFile, File targetFile) throws IOException {
if(!sourceFile.exists()){
return;
}
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;
try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
// 缓冲数组
byte[] b = new byte[inBuff.available()];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} finally {
// 关闭流
if (inBuff != null)
inBuff.close();
if (outBuff != null)
outBuff.close();
}
}
/**
* 获取文件字符内容
* @param file
* @return
*/
public String getDataFileContent(File file){
if(null == file || !file.exists()){
return "";
}
StringBuffer fileData = new StringBuffer();
BufferedReader reader = null;
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
reader = new BufferedReader(read);
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
fileData.append(tempString);
line++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return fileData.toString();
}
/**
* 从XML数据获取JAXB Object对象
* @param data
* @param classe
* @return
* @throws JAXBException
*/
@SuppressWarnings("rawtypes")
public Object getObjectFromXml(String data, Class classe)
throws JAXBException {
if(null == data || "".equals(data)){
return null;
}
JAXBContext context = JAXBContext.newInstance(classe);
Unmarshaller unMarshaller = context.createUnmarshaller();
return unMarshaller.unmarshal(new StringReader(data));
}
/**
* 根据Object对象创建JAXB XML文件
* @param data
* @param fileName
* @param dataPath
* @throws JAXBException
* @throws IOException
*/
public File createXmlFileFromObject(Object data, String fileName,
String dataPath) throws JAXBException, IOException {
String filePath = ProjectUtil.getRuntimeClassPath();
File file = new File(filePath + dataPath + fileName + "_"
+ System.currentTimeMillis() + ".xml");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
JAXBContext context = JAXBContext.newInstance(data.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, fos);
return file;
}
/**
* 根据Object对象创建JAXB XML文件
* @param data
* @param fullpath
* @return
* @throws JAXBException
* @throws IOException
*/
public File createXmlFileFromObject(Object data, String fullpath) throws JAXBException, IOException {
String filePath = ProjectUtil.getRuntimeClassPath();
File file = new File(filePath + fullpath);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
JAXBContext context = JAXBContext.newInstance(data.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, fos);
return file;
}
/**
* 更新本地数据文件
* @param data
* @param backPath 文件名,包含文件路径
* @param fileName 文件名,包含文件路径
* @throws IOException
* @throws JAXBException
*/
public void updateLocalDataFile(Object data, String backFilePath, String fileName) throws IOException, JAXBException {
String filePath = ProjectUtil.getRuntimeClassPath();
File file = new File(filePath + fileName);
if (file.exists()) {
// 将旧文件拷贝到备份目录,删除原文件
this.copyFile(file, filePath+backFilePath, true);
}
//创建新的本地数据文件
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile());
JAXBContext context = JAXBContext.newInstance(data.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, fos);
}
/**
* 移动到his历史目录
* @param data
* @param hisFilePath
* @param fileName
*/
public void moveToHis(String hisFilePath, String fileName){
String filePath = ProjectUtil.getRuntimeClassPath();
File file = new File(filePath + fileName);
if (file.exists()) {
// 将旧文件拷贝到His目录,删除原文件
try {
this.copyFile(file, filePath+hisFilePath, true);
} catch (IOException e) {
}
}
}
/**
* 移动到his历史目录
* @param data
* @param hisFilePath
* @param fileName
*/
public void moveFileToHis(String hisFilePath, File file){
String filePath = ProjectUtil.getRuntimeClassPath();
if (file.exists()) {
try {
this.copyFile(file, filePath+hisFilePath+file.getName(), true);
} catch (IOException e) {
LoggerUtil.logError(CommonCoreActivator.PLUGIN_ID, "备份数据文件到历史目录出错", e);
}
}
}
/**
* 删除数据文件
* @param dataFilePath 数据文件路径
*/
public void deleteDataFile(String dataFilePath) {
String filePath = ProjectUtil.getRuntimeClassPath();
File fileDel = new File(filePath + dataFilePath);
if(fileDel.isDirectory()){
File[] files = fileDel.listFiles();
for(File file : files){
file.delete();
}
}else{
fileDel.delete();
}
}
/**
* @param data
* @return
* @throws JAXBException
* @throws IOException
*/
public String getStringFromObject(Object data) throws JAXBException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JAXBContext context = JAXBContext.newInstance(data.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(data, baos);
return baos.toString();
}
public static void main(String[] args) {
File file = new File("D:/devtools/eclipse-rcp-helios-SR2-win32/runtime-New_configuration/data/download/data_item.xml");
StringBuffer fileData = new StringBuffer();
BufferedReader reader = null;
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
reader = new BufferedReader(read);
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
fileData.append(tempString);
line++;
}
JAXBContext context = JAXBContext.newInstance(ItemDTO.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
ItemDTO itemDto = (ItemDTO)unMarshaller.unmarshal(new StringReader(fileData.toString()));
for(Item item : itemDto.getItemList()){
if(item.getItemName().equals("测试069999")){
System.out.println(item.getItemName());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}