WEB开发网
开发学院软件开发Java Flex tree基于数据库的数据源 阅读

Flex tree基于数据库的数据源

 2009-09-21 00:00:00 来源:WEB开发网   
核心提示: 3.java端服务接口 TestService.java Java代码packagenet.shopin.api;importnet.shopin.view.CatalogVO;importjava.util.List;publicinterfaceTestService{/***获取所有节点*

3.java端服务接口  TestService.java

Java代码   

package net.shopin.api; 
 
import net.shopin.view.CatalogVO; 
 
import java.util.List; 
 
public interface TestService { 
  /** 
   * 获取所有节点 
   * @return 
   */ 
  String getTree(); 
  /** 
   * 增加节点 
   * @return 
   */ 
  String addNode(String sid,String name); 
  /** 
   * 删除节点 
   * @return 
   */ 
  String delNode(String sid); 
  /** 
   * 更新节点 
   * @return 
   */ 
  String updateNode(String sid,String name); 
} 

4.java 端服务接口实现 TestServiceImpl

Java代码   

package net.shopin.business; 
 
import com.shopin.modules.orm.hibernate.SimpleHibernateDao; 
import com.shopin.modules.utils.DozerMapper; 
import java.util.List; 
import java.util.ArrayList; 
import net.shopin.entity.Catalog; 
import net.shopin.entity.ProductList; 
import net.shopin.api.TestService; 
import net.shopin.view.CatalogVO; 
import org.springframework.transaction.annotation.Transactional; 
import org.springframework.stereotype.Component; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.hibernate.SessionFactory; 
import org.hibernate.Criteria; 
import org.hibernate.criterion.Restrictions; 
import org.apache.commons.lang.ObjectUtils; 
 
@Transactional 
@Component("testService") 
public class TestServiceImpl implements TestService { 
  private SimpleHibernateDao<Catalog, Long> testDao; 
  @Autowired 
  public void setSessionFactory(SessionFactory sessionFactory) { 
    testDao = new SimpleHibernateDao<Catalog, Long>(sessionFactory, Catalog.class); 
  } 
 
  @Transactional(readOnly = true) 
  public String getTree() { 
    Catalog catalog = testDao.findByUnique("sid", Long.parseLong("1")); 
    Criteria criteria = testDao.createCriteria(); 
    criteria.add(Restrictions.eq("fatherNodeId", catalog)); 
    List<Catalog> list = criteria.list(); 
    String tree = listToxml(list); 
    return tree; 
  } 
 
  private List<Catalog> getSonNode(String sid) { 
//    System.out.println("--------getSonNode-----------"); 
    String hql = "from Catalog as o where o.fatherNodeId = ? "; 
    Catalog catalog = testDao.findByUnique("sid", Long.parseLong(sid)); 
    List<Catalog> list = testDao.find(hql, catalog); 
    return list; 
  } 
 
  //把list对象转化成XML 
  private String listToxml(List flist) { 
    ArrayList list = (ArrayList) flist; 
    String treedata = "<treeRoot>\n"; 
    treedata = CreateXmlNode(list, 1L, treedata); 
    treedata = treedata + "</treeRoot>\n"; 
//    System.out.println(treedata); 
    return treedata; 
  } 
 
  /** 
   * 递归解析list –> Xml 
   */ 
  private String CreateXmlNode(List<Catalog> list, Long nodepid, String cratedata) { 
    for (int a = 0; a < list.size(); a++) { 
      Catalog fsort = (Catalog) list.get(a); 
      if ((fsort.getFatherNodeId().getSid().toString()).equals(nodepid.toString())) { 
        List<Catalog> lists = getSonNode(fsort.getSid().toString()); 
        if (lists.size() > 0) { 
          cratedata = cratedata + "<second label=\"" + fsort.getFatherNodeId().getNodeName() + "-" + fsort.getNodeName() + "\" sid=\"" + fsort.getSid() + "\">\n"; 
          cratedata = CreateXmlNode(lists, fsort.getSid(), cratedata); 
          cratedata = cratedata + "</second>\n"; 
        } else { 
          cratedata = cratedata + "<third label=\"" + fsort.getFatherNodeId().getNodeName() + "-" + fsort.getNodeName() + "\" sid=\"" + fsort.getSid() + "\"/>\n"; 
        } 
      } 
    } 
    return cratedata; 
  } 
 
  public String addNode(String sid, String name) { 
    Catalog catalog = testDao.findByUnique("sid", Long.parseLong(sid)); 
    Catalog newCatalog = new Catalog(); 
    newCatalog.setNodeName(name); 
    newCatalog.setFatherNodeId(catalog); 
    testDao.save(newCatalog); 
    String data = getTree(); 
    return data; 
  } 
 
  public String delNode(String sid) { 
    if (getSonNode(sid).size() > 0) { 
      System.out.println("不可以删除此节点"); 
    } else { 
      String hql = "from Catalog as o where o.sid = ? "; 
      Catalog catalog = testDao.findByUnique("sid", Long.parseLong(sid)); 
      testDao.delete(catalog); 
    } 
    String data = getTree(); 
    return data; 
  } 
 
  public String updateNode(String sid, String name) { 
    String hql = "from Catalog as o where o.sid = ? "; 
    Catalog catalog = testDao.findByUnique("sid", Long.parseLong(sid)); 
    catalog.setNodeName(name); 
    testDao.save(catalog); 
    String data = getTree(); 
    return data; 
  } 
} 

上一页  1 2 3 

Tags:

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接