?? crmservicexmlimpl.java
字號:
return list;
}
public Client insertClient( Client client) throws ServiceException{
String xpath = "/crm/categories/category[@id=\""+ client.getCategory().getId() +"\"]/clients";
Element clients = (Element) doc.selectSingleNode(xpath);
client.setId(genNextId(CLIENT_PERFIX));
Element e = clients.addElement("client");
e.addAttribute("id", client.getId());
e.addElement("name").setText(client.getName());
e.addElement("sex").setText(client.getSex());
e.addElement("phone").setText(client.getPhone());
e.addElement("email").setText(client.getEmail());
e.addElement("mobile").setText(client.getMobile());
e.addElement("qq").setText(client.getQq());
e.addElement("company").setText(client.getCompany().getId());
e.addElement("birthday").setText(client.getBirthdayString());
e.addElement("photo").setText(client.getPhoto());
store(doc, xmlfile);
fireClientAdded(client);
return client;
}
public Client updateClient( Client client) throws ServiceException{
String xpath = "/crm/categories/category[@id=\""+ client.getCategory().getId() +"\"]/clients/client[@id=\""+ client.getId() +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
Client old = findClientById(client.getId());
e.element("name").setText(client.getName());
e.element("sex").setText(client.getSex());
e.element("phone").setText(client.getPhone());
e.element("email").setText(client.getEmail());
e.element("mobile").setText(client.getMobile());
e.element("qq").setText(client.getQq());
e.element("company").setText(client.getCompany().getId());
e.element("birthday").setText(client.getBirthdayString());
e.element("photo").setText(client.getPhoto());
store(doc, xmlfile);
fireClientChanged(client, old);
return client;
}
public Client removeClient( Client client) throws ServiceException{
String xpath = "/crm/categories/category[@id=\""+ client.getCategory().getId() +"\"]/clients/client[@id=\""+ client.getId() +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
int index = e.getParent().elements().indexOf(e);
e.getParent().remove(e);
store(doc, xmlfile);
fireClientRemoved(client, index);
return client;
}
public Client findClientById(String id) throws ServiceException{
Client client = null;
String xpath = "/crm/categories/category/clients/client[@id=\""+ id +"\"]";
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null)
return null;
client = new Client();
client.setId(e.attributeValue("id"));
client.setName(e.elementTextTrim("name"));
client.setPhone(e.elementTextTrim("phone"));
client.setEmail(e.elementTextTrim("email"));
client.setMobile(e.elementTextTrim("mobile"));
client.setSex(e.elementTextTrim("sex"));
client.setQq(e.elementTextTrim("qq"));
client.setCompany(findCompanyById(e.elementTextTrim("company")));
try {
client.setBirthday(e.elementTextTrim("birthday"));
} catch (ParseException e1) {
e1.printStackTrace();
throw new ServiceException(e1);
}
client.setPhoto(e.elementTextTrim("photo"));
String categoryId = e.getParent().getParent().attributeValue("id");
Category category = findCategoryById(categoryId);
client.setCategory(category);
return client;
}
public List findClientByFamilyName(String familyName){
return null;
}
public Client findClient(int index) throws ServiceException {
String xpath = "(/crm/categories/category/clients/client)[position()="+(index+1)+"]";
Client client = null;
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null)
return null;
client = new Client();
client.setId(e.attributeValue("id"));
client.setName(e.elementTextTrim("name"));
client.setPhone(e.elementTextTrim("phone"));
client.setEmail(e.elementTextTrim("email"));
client.setMobile(e.elementTextTrim("mobile"));
client.setSex(e.elementTextTrim("sex"));
client.setQq(e.elementTextTrim("qq"));
client.setCompany(findCompanyById(e.elementTextTrim("company")));
try {
client.setBirthday(e.elementTextTrim("birthday"));
} catch (ParseException e1) {
e1.printStackTrace();
throw new ServiceException(e1);
}
client.setPhoto(e.elementTextTrim("photo"));
String categoryId = e.getParent().getParent().attributeValue("id");
Category category = findCategoryById(categoryId);
client.setCategory(category);
return client;
}
public int getClientCount() throws ServiceException {
String xpath = "count(/crm/categories/category/clients/client)";
int count = doc.numberValueOf(xpath).intValue();
return count;
}
public int getCompanyCount() throws ServiceException {
String xpath = "count(/crm/companies/company)";
int count = doc.numberValueOf(xpath).intValue();
//throw new ServiceException("test");
return count;
}
/**
* @return 返回對應索引號的公司
* @param index 從0開始的索引號!
*/
public Company findCompany(int index) throws ServiceException {
String xpath = "/crm/companies/company[position()="+(index+1)+"]";
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 int getClientCount(Category category, Company company) throws ServiceException {
String xpath = "count(/crm/categories/category/clients/client)";
if(category != null && company != null){
xpath = "count(/crm/categories/category[@id=\""+ category.getId() +"\"]"+
"/clients/client[company=\""+ company.getId() +"\"])";
}
if(company == null && category != null){
xpath = "count(/crm/categories/category[@id=\""+ category.getId() +"\"]"+
"/clients/client)";
}
if(company != null && category == null){
xpath = "count(/crm/categories/category"+
"/clients/client[company=\""+ company.getId() +"\"])";
}
int count = doc.numberValueOf(xpath).intValue();
return count;
}
public Client findClient(Category category, Company company, int index) throws ServiceException {
Client client = null;
String xpath = "(/crm/categories/category/clients/client)[position()="+ (index+1) +"]";
if(category != null && company != null){
xpath = "/crm/categories/category[@id=\""+ category.getId() +"\"]"+
"/clients/client[company=\""+ company.getId() +"\"][position()="+ (index+1) +"]";
}
if(company == null && category != null){
xpath = "/crm/categories/category[@id=\""+ category.getId() +"\"]"+
"/clients/client[position()="+ (index+1) +"]";
}
if(company != null && category == null){
xpath = "(/crm/categories/category"+
"/clients/client[company=\""+ company.getId() +"\"])[position()="+ (index+1) +"]";
}
Element e = (Element) doc.selectSingleNode(xpath);
if(e==null)
return null;
client = new Client();
client.setId(e.attributeValue("id"));
client.setName(e.elementTextTrim("name"));
client.setPhone(e.elementTextTrim("phone"));
client.setEmail(e.elementTextTrim("email"));
client.setMobile(e.elementTextTrim("mobile"));
client.setSex(e.elementTextTrim("sex"));
client.setQq(e.elementTextTrim("qq"));
client.setCompany(findCompanyById(e.elementTextTrim("company")));
try {
client.setBirthday(e.elementTextTrim("birthday"));
} catch (ParseException e1) {
e1.printStackTrace();
throw new ServiceException(e1);
}
client.setPhoto(e.elementTextTrim("photo"));
String categoryId = e.getParent().getParent().attributeValue("id");
Category c = findCategoryById(categoryId);
client.setCategory(c);
return client;
}
public void addCRMServiceObserver(CRMServiceObserver observer) {
observers.add(observer);
}
public void removeCRMServiceObserver(CRMServiceObserver observer) {
observers.remove(observer);
}
private void fireCompnayAdded(Company company){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.companyAdded(new CRMServiceEvent(this, company));
}
}
private void fireCompanyChanged(Company newValue, Company oldValue){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.companyChanged(new CRMServiceEvent(this, newValue, oldValue));
}
}
private void fireCompnayRemoved(Company company, int removeIndex){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.companyRemoved(new CRMServiceEvent(this, company, removeIndex));
}
}
private void fireCategoryAdded(Category category){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.categoryAdded(new CRMServiceEvent(this, category));
}
}
private void fireCategoryChanged(Category newValue, Category oldValue){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.categoryChanged(new CRMServiceEvent(this, newValue, oldValue));
}
}
private void fireCategoryRemoved(Category category, int removeIndex){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.categoryRemoved(new CRMServiceEvent(this, category, removeIndex));
}
}
private void fireClientAdded(Client value){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.clientAdded(new CRMServiceEvent(this, value));
}
}
private void fireClientChanged(Client newValue, Client oldValue){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.clientChanged(new CRMServiceEvent(this, newValue, oldValue));
}
}
private void fireClientRemoved(Client client, int removeIndex){
for (Iterator i = observers.iterator(); i.hasNext();) {
CRMServiceObserver observer = (CRMServiceObserver) i.next();
observer.clientRemoved(new CRMServiceEvent(this, client, removeIndex));
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -