⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entrydatamanager.java

📁 Eclipse RCP下编写的工作管理软件代码
💻 JAVA
字号:
package net.sf.util.persistence;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.sf.util.ConfigHelper;
import net.sf.util.StringUtil;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * XML的实现,不支持对象之间的关联
 * 仅支持LocalProperty
 */
public class EntryDataManager implements IDataManager {
    //存储路径
    private static String store = ConfigHelper.getDataHome()+"data/";
    //可能为主键的字段名列表

    static {
        File f = new File(store);
        if (!f.exists())
            f.mkdirs();
    }

    //要处理的数据类
    private Class dataClass;
    //实体名
    private String entityName;
    //实体列表名
    private String entityListName;
    //实体保存的文件
    private String dataFile;

    //字段列表,用于判断是否存在该字段
    private Set fields;
    
    //必须实现的构造器
    public EntryDataManager(Class dataClass) throws DataException {
    	try {
			if(!(dataClass.newInstance() instanceof IEntry))
				throw new DataException("仅支持IEntry类型实体");
	        String[] s=((IEntry)dataClass.newInstance()).getFieldNames();
	        fields=new HashSet(Arrays.asList(s));
		}catch (Exception e) {
			e.printStackTrace();
		}
		
        this.dataClass = dataClass;
        this.entityName = dataClass.getSimpleName().toLowerCase();
        this.entityListName = entityName + "list";
        this.dataFile = store + entityName + ".xml";
    }

    //取所有对象的集合
    public List<IEntry> readList() throws DataException {
        if (!fileExists())
            return new ArrayList<IEntry>(0);

        ArrayList al = new ArrayList();
        try {
            //遍历构造所需对象
            Document doc = getDocument();
            Node object = doc.getFirstChild().getFirstChild();
            while (object != null) {
                //每一个对象
                if (object.getNodeType() == Document.ELEMENT_NODE)
                    al.add(populate(object));
                object = object.getNextSibling();
            }
        } catch (Exception e) {
            throw new DataException(e);
        }
        return al;
    }
    //取一组对象的集合
    public List<IEntry> readList(Serializable id) throws DataException {
    	this.dataFile = store + entityName +id+ ".xml";
    	return readList();
    }
    
	public Serializable[] createList(List list, Serializable id) throws DataException {
		this.dataFile = store + entityName +id+ ".xml";
		return createList(list);
	}
    

    // 依父对象取子对象集合,在many-to-one关系中,用one(parent)取many 或依某一属性取对象集合
    public List<IEntry> readList(String propertyName, Object propertyValue) throws DataException {
        List<IEntry> list = readList();
        if(StringUtil.isNull(propertyName) || StringUtil.isNull(propertyValue))
        	return list;
        propertyName=propertyName.trim();
        if(propertyValue instanceof String)
        	propertyValue=((String)propertyValue).trim();
        List<IEntry> al = new ArrayList<IEntry>();
        for (IEntry o:list) {
            try {
                if (propertyValue.equals(o.getProperties().get(propertyName)))
                    al.add(o);
            } catch (Exception e) {
                System.out.println("读列表时取值出错");
            }
        }
        list.clear();
        list = null;
        return al;
    }

    //创建一组对象,返回ID数组
    public Serializable[] createList(List list) throws DataException {
    	if(list.isEmpty())
    		return null;
    	//重写文件
        //构造一个dummy,然后继续
        try {
            FileWriter fw = new FileWriter(dataFile);
            fw.write("<?xml version=\"1.0\" encoding=\"gbk\"?>");
            fw.write("<" + entityListName +" ry=\""+ ConfigHelper.getRy() + "\" bm=\"" + ConfigHelper.getBm() + "\">\n");
            for(Object o:list)
            	fw.write("\t"+((IEntry)o).toXML()+"\n");
            fw.write("</" + entityListName + ">");
            fw.flush();
            fw.close();
        } catch (IOException e) {
            throw new DataException("生成存储文件出错");
        }
        return null;
    }

    //依ID取对象
    public IData readData(Serializable id) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //创建对象,返回ID
    public Serializable createData(IData data) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //更新对象
    public void updateData(IData data) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //更新一组对象
    public void updateList(List list) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //删除对象
    public void deleteData(IData data) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //删除一组对象
    public void deleteList(List list) throws DataException {
    	throw new DataException("未实现此方法");
    }

    //取Document
    private Document getDocument() throws DataException {
        //取DOM对象
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try {
            db = dbf.newDocumentBuilder();
            return db.parse(new File(dataFile));
        } catch (Exception e) {
            throw new DataException(e);
        }
    }

    //判断存储文件是否存在
    private boolean fileExists() {
        return new File(dataFile).exists();
    }

    //将一个合法节点转化为IData
    private IData populate(Node node) throws InstantiationException, IllegalAccessException {
        IEntry data = (IEntry) dataClass.newInstance();
        NamedNodeMap nnm = node.getAttributes();
        for(int i=0;i<nnm.getLength();i++){
        	if(fields.contains(nnm.item(i).getNodeName())){
        		data.setProperty(nnm.item(i).getNodeName(), nnm.item(i).getNodeValue());
        	}
        }
        return (IData) data;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -