?? crmservicexmlimpl.java
字號:
package net.robin.crm.service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.rmi.ServerException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Text;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;
public class CRMServiceXMLImpl implements CRMService{
private static final String COMPANY_PERFIX = "company";
private static final String CATEGORY_PERFIX = "category";
private static final String CLIENT_PERFIX = "client";
private Document doc;
private String xmlfile;
private List observers = new ArrayList();
public CRMServiceXMLImpl() throws ServiceException {
super();
xmlfile = CRMServiceFactoryXMLImpl.getXmlfile();
try {
doc = load(xmlfile, CRMServiceFactoryXMLImpl.getSchema().toString());
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException(e);
}
}
public Company insertCompany(Company company) throws ServiceException{
Element companies = doc.getRootElement().element("companies");
company.setId(genNextId(COMPANY_PERFIX));
Element e = companies.addElement("company");
e.addAttribute("id", company.getId());
e.addElement("name").addText(company.getName());
e.addElement("address").addText(company.getAddress());
store(doc, xmlfile);
fireCompnayAdded(company);
return company;
}
private void store(Document doc, String xmlfile) throws ServiceException {
try{
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(format);
writer.setOutputStream(new FileOutputStream(xmlfile));
writer.write(doc);
writer.close();
}catch(Exception e){
e.printStackTrace();
throw new ServiceException(e);
}
}
private String genNextId(String prefix){
String xpath = null;
if(COMPANY_PERFIX.equals(prefix)){
xpath = "/crm/companies/company/@id";
}
if(CATEGORY_PERFIX.equals(prefix)){
xpath = "/crm/categories/category/@id";
}
if(CLIENT_PERFIX.equals(prefix)){
xpath = "/crm/categories/category/clients/client/@id";
}
int id = 0;
List list = doc.selectNodes(xpath);
for (Iterator i = list.iterator(); i.hasNext();) {
Attribute attr = (Attribute) i.next();
int value = Integer.parseInt(attr.getStringValue().substring(prefix.length()));
id = Math.max(id, value);
}
id++;
return prefix + id;
}
public Company updateCompany(Company company) throws ServiceException{
Company newValue = company;
Company oldValue = findCompanyById(company.getId());
String xpath = "/crm/companies/company[@id=\"" + company.getId() +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null){
throw new ServiceException("沒有被編輯的節點!");
}
e.element("name").setText(company.getName());
e.element("address").setText(company.getAddress());
store(doc, xmlfile);
fireCompanyChanged(newValue, oldValue ) ;
return company;
}
public Company removeCompany(Company company) throws ServiceException{
String xpath = "/crm/companies/company[@id=\"" + company.getId() +"\"]";
//String xpath = "/crm/companies[company/@id=\"" + company.getId() +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
int index = e.getParent().elements().indexOf(e);
xpath = "count(/crm/categories/category/clients/client[company=\""+ company.getId() +"\"])";
int count = doc.numberValueOf(xpath).intValue();
if(count>0)
throw new ServiceException("公司已經被使用!");
e.getParent().remove(e);
store(doc, xmlfile);
fireCompnayRemoved(company , index);
return company;
}
public Company findCompanyById(String id){
String xpath = "/crm/companies/company[@id=\"" + id +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null)
return null;
Company company = new Company();
company.setId(e.attributeValue("id"));
company.setName(e.elementTextTrim("name"));
company.setAddress(e.elementTextTrim("address"));
return company;
}
public List findCompanies(){
List list = new ArrayList();
String xpath = "/crm/companies/company";
List elements = doc.selectNodes(xpath);
if(elements==null)
return list;
for (Iterator i = elements.iterator(); i.hasNext();) {
Element e = (Element) i.next();
Company company = new Company();
company.setId(e.attributeValue("id"));
company.setName(e.elementTextTrim("name"));
company.setAddress(e.elementTextTrim("address"));
list.add(company);
}
return list;
}
public List findCompanies(Category category) throws ServiceException {
List list = new ArrayList();
String xpath = "/crm/categories/category[@id=\""+ category.getId() +"\"]/clients/client/company";
List nodes = doc.selectNodes(xpath);
Set idSet = new HashSet();
for (Iterator i = nodes.iterator(); i.hasNext();) {
Element e = (Element) i.next();
String id = e.getTextTrim();
if(!idSet.contains(id)){
list.add(findCompanyById(id));
}
idSet.add(id);
}
return list;
}
public int indexOf(Company company) throws ServiceException {
String xpath = "/crm/companies/company[@id=\"" + company.getId() +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
int index = e.getParent().elements().indexOf(e);
return index;
}
private Document load(String xmlfile, String schemaUrl) throws SAXException, FileNotFoundException, DocumentException{
Document doc = null;
SAXReader reader = new SAXReader(true);
reader.setFeature(
"http://apache.org/xml/features/validation/schema", true);
reader.setFeature(
"http://xml.org/sax/features/validation", true);
// reader.setProperty(
// "http://apache.org/xml/properties/schema/external-schemaLocation", schemaUrl);
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
schemaUrl);
reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
true);
doc = reader.read(new FileInputStream(xmlfile));
return doc;
}
public Category insertCategory(Category category) throws ServiceException{
Element categories = doc.getRootElement().element("categories");
category.setId( genNextId( CATEGORY_PERFIX ));
Element e = categories.addElement("category");
e.addAttribute("id", category.getId());
e.addElement("name").setText(category.getName());
e.addElement("clients");
store(doc, xmlfile);
fireCategoryAdded(category);
return category;
}
public Category updateCategory( Category category) throws ServiceException{
String xpath = "/crm/categories/category[@id=\"" + category.getId() + "\"]";
Category old = findCategoryById(category.getId());
Element e = (Element) doc.selectSingleNode(xpath);
e.element("name").setText(category.getName());
store(doc, xmlfile);
fireCategoryChanged(category,old);
return category;
}
public Category removeCategory( Category category) throws ServiceException{
String xpath = "/crm/categories/category[@id=\"" + category.getId() + "\"]";
Element e = (Element) doc.selectSingleNode(xpath);
int index = e.getParent().elements().indexOf(e);
xpath = "count(clients/client)";
int count = e.numberValueOf(xpath).intValue();
if(count>0)
throw new ServiceException("不能刪除包含客戶的分類!");
e.getParent().remove(e);
store(doc, xmlfile);
fireCategoryRemoved(category, index );
return category;
}
public Category findCategoryById(String id) throws ServiceException{
String xpath = "/crm/categories/category[@id=\"" + id + "\"]";
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null)
return null;
Category category = new Category();
category.setId(e.attributeValue("id"));
category.setName(e.elementTextTrim("name"));
return category;
}
public List findCategories() throws ServiceException{
List list = new ArrayList();
String xpath = "/crm/categories/category";
List elements = doc.selectNodes(xpath);
if(elements==null)
return list;
for (Iterator i = elements.iterator(); i.hasNext();) {
Element e = (Element) i.next();
Category category = new Category();
category.setId(e.attributeValue("id"));
category.setName(e.elementTextTrim("name"));
list.add(category);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -