Skip to content

Commit

Permalink
category initial
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghao-py committed Jan 22, 2014
1 parent 402594c commit 5e8fe0c
Show file tree
Hide file tree
Showing 16 changed files with 370 additions and 144 deletions.
9 changes: 9 additions & 0 deletions offline/db/main.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ BEGIN;
INSERT INTO `tb_user_role` VALUES ('1', '1', '1'), ('2', '4', '1'), ('3', '1', '2'), ('4', '5', '2'), ('5', '6', '2');
COMMIT;


DROP TABLE IF EXISTS `tb_category`;
CREATE TABLE `tb_category` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`name` varchar(255) NOT NULL COMMENT '名称',
`parent_id` bigint(20) NULL COMMENT '父类id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='分类表';

SET FOREIGN_KEY_CHECKS = 1;
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>

</dependencies>

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/thirdblock/migo/category/dao/CategoryDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.thirdblock.migo.category.dao;

import java.util.List;

import com.thirdblock.migo.core.bo.Category;

public interface CategoryDao {

void save(Category category);

void update(Category category);

Category findById(Long id);

List<Category> list();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.thirdblock.migo.category.service;

import com.thirdblock.migo.category.web.action.dto.CategoryNode;
import com.thirdblock.migo.core.bo.Category;
import com.thirdblock.migo.core.excep.ServiceException;

public interface CategoryService {

void saveOrUpdate(Category category);

Category findById(Long id) throws ServiceException;

CategoryNode getCategoryTree();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.thirdblock.migo.category.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thirdblock.migo.category.dao.CategoryDao;
import com.thirdblock.migo.category.service.CategoryService;
import com.thirdblock.migo.category.web.action.dto.CategoryNode;
import com.thirdblock.migo.core.bo.Category;
import com.thirdblock.migo.core.excep.ServiceException;

@Component
public class CategoryServiceImpl implements CategoryService {

@Autowired
private CategoryDao categoryDao;

@Override
public void saveOrUpdate(Category category) {

if (ObjectUtils.equals(category.getId(), null)) {
categoryDao.save(category);
} else {
categoryDao.update(category);
}
}

@Override
public Category findById(Long id) throws ServiceException {
if (ObjectUtils.equals(id, null) || id.equals(0L)) {
throw new ServiceException("id can't be null.");
}

return categoryDao.findById(id);
}

/**
* 获得分类树
* @return 根节点元素
*/
@Override
public CategoryNode getCategoryTree() {
List<Category> categories = categoryDao.list();
return buildCategoryTree(categories);
}

/**
* 构造分类树
* @param categories
* @return 根节点元素
*/
private CategoryNode buildCategoryTree(List<Category> categories) {

// parentId -> categories
Map<Long, List<Category>> childrenSearcher = new HashMap<Long, List<Category>>();

Iterator<Category> iter = categories.iterator();
while (iter.hasNext()) {
Category c = iter.next();

Long parentId = c.getParentId();
List<Category> lst = childrenSearcher.get(parentId);

if (lst == null) {
lst = new LinkedList<Category>();
childrenSearcher.put(parentId, lst);
}

lst.add(c);
}

// 设置根节点
CategoryNode root = new CategoryNode();
Category category = new Category();
category.setId(0L);
category.setParentId(0L);
category.setName(Category.ROOT);

buildCategoryTree(root, category, childrenSearcher);

return root;
}

@SuppressWarnings("unchecked")
private void buildCategoryTree(CategoryNode node, Category category, Map<Long, List<Category>> childrenSearcher) {

Long id = category.getId();
List<Category> categories = childrenSearcher.get(id);
categories = (List<Category>) ObjectUtils.defaultIfNull(categories, new ArrayList<Category>());

List<CategoryNode> children = (List<CategoryNode>) CollectionUtils.collect(categories, new Transformer(){
@Override
public Object transform(Object object) {
CategoryNode node = new CategoryNode();
node.setCategory((Category) object);
return node;
}
});

// 构造当前节点
node.setCategory(category);
node.setChildren(children);

// 构造孩子节点
for (int i = 0; i < children.size(); i++) {
CategoryNode child = children.get(i);
Category c = categories.get(i);
buildCategoryTree(child, c, childrenSearcher);
}

return;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.thirdblock.migo.category.web.action.dto;

import java.util.List;

import com.thirdblock.migo.core.bo.Category;

public class CategoryNode {

private Category category;
private List<CategoryNode> children;

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public List<CategoryNode> getChildren() {
return children;
}

public void setChildren(List<CategoryNode> children) {
this.children = children;
}

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.thirdblock.migo.core.web.action.BaseAction;

Expand All @@ -25,5 +26,10 @@ public ModelAndView index() {
public ModelAndView page403() {
return new ModelAndView("page403");
}

@RequestMapping(value = "", method = RequestMethod.GET)
public ModelAndView defaultPage() {
return new ModelAndView(new RedirectView("index"));
}

}
Loading

0 comments on commit 5e8fe0c

Please sign in to comment.