/**
* @#ConstantDAO.java
* Copyright (C) 2009-2010 HereOnline Corporation
*/
package cn.com.hereonline.admin.dao;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import cn.com.hereonline.admin.common.Common;
/**
* IConstantDAO的实现类
* @author HereOnLine Corporation
* @version 01 jun 28, 2009 作成
*/
public class ConstantDAO extends BaseDAO implements IConstantDAO {
File _FILEPATH = null;
/**批量查询**/
public List<Constant> searchPerson() {
List<Constant> list = new ArrayList<Constant>();
try {
Element root = initDoc().getDocumentElement();
NodeList pList = null;
pList = root.getElementsByTagName("Constant");
for (int i = 0; i < pList.getLength(); i++) {
Element pE = (Element) pList.item(i);
Constant person = new Constant();
NodeList ids = pE.getElementsByTagName("value");
if (ids.getLength() == 1) {
Element e = (Element) ids.item(0);
Text t = (Text) e.getFirstChild();
person.setValue(t.getNodeValue());
}
NodeList names = pE.getElementsByTagName("name");
if (names.getLength() == 1) {
Element e = (Element) names.item(0);
Text t = (Text) e.getFirstChild();
person.setName(t.getNodeValue());
}
NodeList desc = pE.getElementsByTagName("desc");
if(desc.getLength()==1){
Element e = (Element) desc.item(0);
Text t = (Text) e.getFirstChild();
person.setDesc(t.getNodeValue());
}
list.add(person);
}
} catch (Exception e) {
System.out.println("ERROR!");
}
return list;
}
/**添加常量**/
public void insertPerson(Constant p) {
try {
Document document = initDoc();
Element root = document.getDocumentElement();
Element person = null;
person = document.createElement("Constant");
Element id = document.createElement("value");
person.appendChild(id);
Text tId = document.createTextNode(p.getValue());
id.appendChild(tId);
Element name = document.createElement("name");
person.appendChild(name);
Text tName = document.createTextNode(p.getName());
name.appendChild(tName);
root.appendChild(person);
commitTransformer(document);
//closeIO(document);
} catch (ParserConfigurationException e) {
System.out.println("ERROR!");
} catch (SAXException e) {
System.out.println("ERROR!");
} catch (IOException e) {
System.out.println("ERROR!");
} catch (Exception e) {
System.out.println("ERROR!");
}
}
/**关闭IO**/
private void closeIO(Document document) throws IOException {
FileOutputStream outStream = new FileOutputStream("constant.xml");
OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
outWriter.close();
outStream.close();
}
/**删除常量**/
public void deletePerson(Constant p) {
try {
Document document = initDoc();
NodeList pList = null ;
Node del;
pList = document.getElementsByTagName("Constant");
for (int i = 0; i < pList.getLength(); i++) {
Element element = (Element) pList.item(i);
NodeList ids = element.getElementsByTagName("name");
if (ids.getLength() == 1) {
Element e = (Element) ids.item(0);
Text t = (Text) e.getFirstChild();
if (t.getNodeValue().equals(p.getName())) {
del = (Node) element;
element.getParentNode().removeChild(del);
}
}
}
commitTransformer(document);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException");
} catch (SAXException e) {
System.out.println("SAXException");
} catch (IOException e) {
System.out.println("IOException");
} catch (TransformerException e) {
System.out.println("TransformerException");
} catch (Exception e) {
System.out.println("Exception");
}
}
/**更新常量**/
public void updatePerson(Constant p) {
try {
Document document = initDoc();
NodeList pList = null;
pList = document.getElementsByTagName("Constant");
Element e;
Text t;
for (int i = 0; i < pList.getLength(); i++) {
Element element = (Element) pList.item(i);
NodeList names = element.getElementsByTagName("name");
if (names.getLength() == 1) {
e = (Element) names.item(0);
t = (Text) e.getFirstChild();
if (t.getNodeValue().equals(p.getName())) {
NodeList values = element.getElementsByTagName("value");
if (values.getLength() == 1) {
e = (Element) values.item(0);
t = (Text) e.getFirstChild();
t.setNodeValue(p.getValue());
}
}
}
}
commitTransformer(document);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException");
} catch (SAXException e) {
System.out.println("SAXException");
} catch (IOException e) {
System.out.println("IOException");
} catch (TransformerException e) {
System.out.println("TransformerException");
} catch (Exception e) {
System.out.println("Exception");
}
}
/**提交操作**/
private void commitTransformer(Document document) throws Exception {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(this._FILEPATH);
transformer.transform(source, result);
}
/**初使化文档**/
private Document initDoc() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = null;
if(Common.isLinux()){
String path = Thread.currentThread().getContextClassLoader().getResource("constant_server.xml").getFile();
_FILEPATH = new File(path);
}else{//
String path = Thread.currentThread().getContextClassLoader().getResource("constant_local.xml").getFile();
_FILEPATH = new File(path);
}
try{
document = builder.parse(this._FILEPATH);
}catch(Exception e)
{
e.printStackTrace();
}
return document;
}
/**取单个值**/
public String getNode(String name) {
try {
Document document = initDoc();
NodeList pList = null;
pList = document.getElementsByTagName("Constant");
for (int i = 0; i < pList.getLength(); i++) {
Element element = (Element) pList.item(i);
NodeList ids = element.getElementsByTagName("name");
if (ids.getLength() == 1) {
Element e = (Element) ids.item(0);
Text t = (Text) e.getFirstChild();
if (t.getNodeValue().trim().equals(name)) {
NodeList values = element.getElementsByTagName("value");
if (values.getLength() == 1) {
e = (Element) values.item(0);
t = (Text) e.getFirstChild();
return t.getNodeValue();
}
}
}
}
commitTransformer(document);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException");
} catch (SAXException e) {
System.out.println("SAXException");
} catch (IOException e) {
System.out.println("IOException");
} catch (TransformerException e) {
System.out.println("TransformerException");
} catch (Exception e) {
System.out.println("Exception");
}
return null;
}
}