package com.sitinpsring.persistence;
import java.io.File;
import java.io.FileWriter;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.sitinpsring.domain.Member;
import com.sitinpsring.exception.XmlFileReadException;
import com.sitinpsring.exception.XmlFileWriteException;
public class MemberPersistence {
private String xmlFilePos;
private Document document;
public MemberPersistence(String xmlFilePos)
throws XmlFileReadException,XmlFileWriteException{
this.xmlFilePos = xmlFilePos;
if (isFileExist(this.xmlFilePos) == false) {
// Create document when file not exist
createDocument();
return;
} else {
// Get Docunent when file exist
SAXReader reader = new SAXReader();
try {
document = reader.read(new File(this.xmlFilePos));
} catch (Exception ex) {
throw new XmlFileReadException(ex.getMessage());
}
}
}
private void createDocument() throws XmlFileWriteException{
String text = "<members></members>";
try {
document = DocumentHelper.parseText(text);
writeDocumentToFile();
} catch (XmlFileWriteException ex) {
throw ex;
}catch (Exception ex) {
ex.printStackTrace();
}
}
private void writeDocumentToFile() throws XmlFileWriteException{
try {
XMLWriter writer = new XMLWriter(new FileWriter(this.xmlFilePos));
writer.write(document);
writer.close();
} catch (Exception ex) {
throw new XmlFileWriteException(ex.getMessage());
}
}
public Hashtable<String, Member> loadMemberFromFile() {
Hashtable<String, Member> retval=new Hashtable<String, Member>();
List nodes = document.getRootElement().elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
Member member = new Member(elm.attributeValue("id"),elm.elementText("name"),
Integer.parseInt(elm.elementText("age")));
retval.put(member.getId(), member);
}
return retval;
}
public boolean add(Member member) throws XmlFileWriteException{
try {
Element rootElm = document.getRootElement();
Element newMemberElm = rootElm.addElement("member");
newMemberElm.addAttribute("id", member.getId());
Element nameElm=newMemberElm.addElement("name");
nameElm.setText(member.getName());
Element ageElm=newMemberElm.addElement("age");
ageElm.setText(String.valueOf(member.getAge()));
writeDocumentToFile();
return true;
} catch (XmlFileWriteException ex) {
throw ex;
}
}
public boolean update(Member member) throws XmlFileWriteException{
try {
Element rootElm = document.getRootElement();
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
if(elm.attributeValue("id").equals(member.getId())){
elm.element("name").setText(member.getName());
elm.element("age").setText(String.valueOf(member.getAge()));
break;
}
}
writeDocumentToFile();
return false;
} catch (XmlFileWriteException ex) {
throw ex;
}
}
public boolean delete(String id) throws XmlFileWriteException{
try {
Element rootElm = document.getRootElement();
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
Member member = new Member(elm.attributeValue("id"),elm.elementText("name"),
Integer.parseInt(elm.elementText("age")));
if(member.getId().equals(id)){
rootElm.remove(elm);
break;
}
}
writeDocumentToFile();
return false;
} catch (XmlFileWriteException ex) {
throw ex;
}
}
public static boolean isFileExist(String filePath) {
boolean exists = (new File(filePath)).exists();
return exists;
}
} |