📄 catalogserviceimpl.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 {
//~ Static fields/initializers =============================================
/** DOCUMENT ME! */
private static Log log = LogFactory.getLog(CatalogServiceImpl.class);
//~ Instance fields ========================================================
/** DOCUMENT ME! */
private CatalogDAO catalogDAO;
/** catalog tree temp store */
private ThreadLocal localMap = new ThreadLocal();
//~ Constructors ===========================================================
/**
* Creates a new CatalogServiceImpl object.
*/
public CatalogServiceImpl() {
}
//~ Methods ================================================================
/**
* 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);
}
log.info("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
catalogDAO.createCatalog(catalog);
log.info("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}
/**
* 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 += "┃";
k++;
}
if (i == (n - 1)) {
//"?"|-
catalogtitle += "┗";
} else {
//"?"|_┗
catalogtitle += "┣";
}
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 + -