⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catalogserviceimpl.java

📁 Jaoso新闻文章发布系统 0.9.1final 程序架构: Struts+Spring+Hibernate 主要功能:   ·新闻采用在线编辑器,可以象使用word一样编辑新闻,可简繁
💻 JAVA
字号:
package jaoso.news.service.impl;

import jaoso.framework.service.impl.BaseService;

import jaoso.framework.util.MyUtils;

import jaoso.news.dao.CatalogDAO;

import jaoso.news.domain.Catalog;

import jaoso.news.exception.CatalogExistException;

import jaoso.news.service.CatalogService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * catalog manager service
 * 
 * @author Edgeloner
 * @version 0.9.1
 */
public class CatalogServiceImpl extends BaseService implements CatalogService {
	/** DOCUMENT ME! */
	private static Log log = LogFactory.getLog(CatalogServiceImpl.class);

	/** DOCUMENT ME! */
	private CatalogDAO catalogDAO;

	/** catalog tree temp store */
	private ThreadLocal localMap = new ThreadLocal();

	/**
	 * Creates a new CatalogServiceImpl object.
	 */
	public CatalogServiceImpl() {
	}

	/**
	 * Load a by id from Database
	 * 
	 * @param id
	 *            catalog id
	 * @return a instance
	 */
	public final Catalog getCatalog(final Serializable id) {
		return catalogDAO.getCatalog(id);
	}

	/**
	 * @param dao
	 *            catalogDAO
	 */
	public final void setCatalogDAO(final CatalogDAO dao) {
		this.catalogDAO = dao;
	}

	/**
	 * get a catalog tree, return collection as this.returnRS,elemment is
	 * HashMap, it look like {"122dfd2","catalog title"},
	 * 
	 * @param hierarchy
	 *            String,
	 * @param catalog
	 * @return collection
	 */
	public final Collection getCatalogTree(final int hierarchy,
			final Catalog catalog) {
		Collection tempTree = new ArrayList();
		createCatalogTree(hierarchy, catalog, tempTree);

		return tempTree;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see jaoso.news.service.CatalogService#getParents(java.io.Serializable)
	 */
	public final Map[] getParents(final Serializable id) {
		Catalog parent = getCatalog(id);
		List parents = new ArrayList();
		Map[] parentMap = null;

		if (parent != null) {
			while (true) {
				Map objs = new HashMap();
				objs.put("catalogId", parent.getCatalogId());
				objs.put("catalogTitle", parent.getCatalogTitle());
				parents.add(objs);
				parent = parent.getParent();

				if ((parent == null) || (parent.getCatalogId() == null)) {
					break;
				}
			}

			parentMap = new Map[parents.size()];

			for (int i = 0, n = parents.size(); i < n; i++) {
				parentMap[(n - 1) - i] = (Map) parents.get(i);
			}
		}

		return parentMap;
	}

	/**
	 * Add a new to Database
	 * 
	 * @param catalog
	 *            catalog
	 * @param parentid
	 *            parent catalog id
	 * @exception CatalogExistException
	 *                if catalog title exist
	 */
	public final void createCatalog(final Catalog catalog,
			final Serializable parentid) throws CatalogExistException {
		if (catalogDAO.isExist(catalog.getCatalogTitle())) {
			throw new CatalogExistException("catalog title already exist!");
		}

		if ((parentid != null) && !((String) parentid).trim().equals("")) {
			Catalog parent = catalogDAO.getCatalog(parentid);
			catalog.setParent(parent);
			parent.getChildren().add(catalog);
		}

		catalogDAO.createCatalog(catalog);
	}

	/**
	 * Load all from Database
	 * 
	 * @return catalog array
	 */
	public final Catalog[] findAllCatalog() {
		return catalogDAO.findAllCatalog();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see jaoso.news.service.CatalogService#getCatalogByParent(java.io.Serializable)
	 */
	public final Catalog[] findCatalogByParent(final Serializable id) {
		if (MyUtils.isBlank(id)) {
			return catalogDAO
					.findCatalog("from Catalog  a where a.parent is null");
		}

		return catalogDAO.findCatalog("from Catalog  a where a.parent ='" + id
				+ "'");
	}

	/**
	 * Remove a from Database
	 * 
	 * @param catalogId
	 *            catalog id
	 */
	public final void removeCatalog(final Serializable catalogId) {
		catalogDAO.removeCatalog(catalogId);
	}

	/**
	 * Update a to Database
	 * 
	 * @param catalog
	 *            catalog
	 * @throws CatalogExistException
	 *             if catalog title exist
	 */
	public final void updateCatalog(final Catalog catalog)
			throws CatalogExistException {
		if (catalogDAO.isExist(catalog.getCatalogTitle(), catalog
				.getCatalogId())) {
			throw new CatalogExistException("catalog title already exist!");
		}

		Catalog entity = getCatalog(catalog.getCatalogId());
		entity.setCatalogTitle(catalog.getCatalogTitle());
		catalogDAO.updateCatalog(entity);
	}

	/**
	 * DOCUMENT ME!
	 * 
	 * @param hierarchy
	 *            DOCUMENT ME!
	 * @param catalog
	 *            DOCUMENT ME!
	 * @param tempTree
	 *            DOCUMENT ME!
	 */
	private void createCatalogTree(final int hierarchy, final Catalog catalog,
			final Collection tempTree) {
		Catalog acatalog;
		Catalog[] it = null;

		if (catalog == null) {
			it = catalogDAO
					.findCatalog("from Catalog catalog where catalog.parent is null");

			if (it == null) {
				return;
			}
		} else {
			it = new Catalog[catalog.getChildren().size()];
			System.arraycopy(catalog.getChildren().toArray(), 0, it, 0, catalog
					.getChildren().size());
		}

		for (int i = 0, n = it.length; i < n; i++) {
			acatalog = it[i];

			int k = 0;
			String catalogtitle = "";
			Map catalog2 = new HashMap();

			while (k < hierarchy) {
				//|
				catalogtitle += "\u2502";
				k++;
			}

			if (i == (n - 1)) {
				//|-
				catalogtitle += "\u2514";
			} else {
				//|_
				catalogtitle += "\u251c";
			}

			if (acatalog.getChildren().size() > 0) {
				catalogtitle += ("[" + acatalog.getCatalogTitle() + "]");
			} else {
				catalogtitle += acatalog.getCatalogTitle();
			}

			catalog2.put("catalogId", acatalog.getCatalogId());
			catalog2.put("catalogTitle", catalogtitle);
			tempTree.add(catalog2);

			if (acatalog.getChildren().size() > 0) {
				//call self
				createCatalogTree(hierarchy + 1, acatalog, tempTree);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -