Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.green.modules.cms.utils; import java.util.List; import java.util.Map; import com.green.common.config.Global; import com.green.common.mapper.JsonMapper; import com.green.common.persistence.Page; import com.green.common.utils.CacheUtils; import com.green.common.utils.SpringContextHolder; import com.green.modules.cms.entity.Article; import com.green.modules.cms.entity.Category; import com.green.modules.cms.entity.Link; import com.green.modules.cms.entity.Site; import com.green.modules.cms.service.ArticleService; import com.green.modules.cms.service.CategoryService; import com.green.modules.cms.service.LinkService; import com.green.modules.cms.service.SiteService; import org.apache.commons.lang.StringUtils; import javax.servlet.ServletContext; /** * ? * @author ThinkGem * @version 2013-5-29 */ public class CmsUtils { private static SiteService siteService = SpringContextHolder.getBean(SiteService.class); private static CategoryService categoryService = SpringContextHolder.getBean(CategoryService.class); private static ArticleService articleService = SpringContextHolder.getBean(ArticleService.class); private static LinkService linkService = SpringContextHolder.getBean(LinkService.class); private static ServletContext context = SpringContextHolder.getBean(ServletContext.class); private static final String CMS_CACHE = "cmsCache"; /** * */ public static List<Site> getSiteList() { @SuppressWarnings("unchecked") List<Site> siteList = (List<Site>) CacheUtils.get(CMS_CACHE, "siteList"); if (siteList == null) { Page<Site> page = new Page<Site>(1, -1); page = siteService.find(page, new Site()); siteList = page.getList(); CacheUtils.put(CMS_CACHE, "siteList", siteList); } return siteList; } /** * ? * @param siteId ? */ public static Site getSite(String siteId) { String id = "1"; if (StringUtils.isNotBlank(siteId)) { id = siteId; } for (Site site : getSiteList()) { if (site.getId().equals(id)) { return site; } } return new Site(id); } /** * * @param siteId ? */ public static List<Category> getMainNavList(String siteId) { @SuppressWarnings("unchecked") List<Category> mainNavList = (List<Category>) CacheUtils.get(CMS_CACHE, "mainNavList_" + siteId); if (mainNavList == null) { Category category = new Category(); category.setSite(new Site(siteId)); category.setParent(new Category("1")); category.setInMenu(Category.SHOW); Page<Category> page = new Page<Category>(1, -1); page = categoryService.find(page, category); mainNavList = page.getList(); CacheUtils.put(CMS_CACHE, "mainNavList_" + siteId, mainNavList); } return mainNavList; } /** * ?? * @param categoryId ?? * @return */ public static Category getCategory(String categoryId) { return categoryService.get(categoryId); } /** * ? * @param siteId ? * @param parentId ? * @param number ? * @param param ? key1:'value1', key2:'value2' ... */ public static List<Category> getCategoryList(String siteId, String parentId, int number, String param) { Page<Category> page = new Page<Category>(1, number, -1); Category category = new Category(); category.setSite(new Site(siteId)); category.setParent(new Category(parentId)); if (StringUtils.isNotBlank(param)) { @SuppressWarnings({ "unused", "rawtypes" }) Map map = JsonMapper.getInstance().fromJson("{" + param + "}", Map.class); } page = categoryService.find(page, category); return page.getList(); } /** * ?? * @param categoryIds ?? * @return */ public static List<Category> getCategoryListByIds(String categoryIds) { return categoryService.findByIds(categoryIds); } /** * ? * @param articleId ? * @return */ public static Article getArticle(String articleId) { return articleService.get(articleId); } /** * ? * @param siteId ? * @param categoryId ? * @param number ? * @param param ? key1:'value1', key2:'value2' ... * posid ???12??? * image 1 * orderBy ? * @return */ public static List<Article> getArticleList(String siteId, String categoryId, int number, String param) { Page<Article> page = new Page<Article>(1, number, -1); Article article = new Article(new Category(categoryId, new Site(siteId))); if (StringUtils.isNotBlank(param)) { @SuppressWarnings({ "rawtypes" }) Map map = JsonMapper.getInstance().fromJson("{" + param + "}", Map.class); if (new Integer(1).equals(map.get("posid")) || new Integer(2).equals(map.get("posid"))) { article.setPosid(String.valueOf(map.get("posid"))); } if (new Integer(1).equals(map.get("image"))) { article.setImage(Article.YES); } if (StringUtils.isNotBlank((String) map.get("orderBy"))) { page.setOrderBy((String) map.get("orderBy")); } } article.setDelFlag(Article.DEL_FLAG_NORMAL); page = articleService.find(page, article, false); return page.getList(); } /** * ? * @param linkId ? * @return */ public static Link getLink(String linkId) { return linkService.get(linkId); } /** * ? * @param siteId ? * @param categoryId ? * @param number ? * @param param ? key1:'value1', key2:'value2' ... * @return */ public static List<Link> getLinkList(String siteId, String categoryId, int number, String param) { System.out.println("?"); Page<Link> page = new Page<Link>(1, number, -1); Link link = new Link(new Category(categoryId, new Site(siteId))); if (StringUtils.isNotBlank(param)) { @SuppressWarnings({ "unused", "rawtypes" }) Map map = JsonMapper.getInstance().fromJson("{" + param + "}", Map.class); } link.setDelFlag(Link.DEL_FLAG_NORMAL); page = linkService.find(page, link, false); return page.getList(); } // ============== Cms Cache ============== public static Object getCache(String key) { return CacheUtils.get(CMS_CACHE, key); } public static void putCache(String key, Object value) { CacheUtils.put(CMS_CACHE, key, value); } public static void removeCache(String key) { CacheUtils.remove(CMS_CACHE, key); } /** * ?URL? * @param article * @return url */ public static String getUrlDynamic(Article article) { if (StringUtils.isNotBlank(article.getLink())) { return article.getLink(); } StringBuilder str = new StringBuilder(); str.append(context.getContextPath()).append(Global.getFrontPath()); str.append("/view-").append(article.getCategory().getId()).append("-").append(article.getId()) .append(Global.getUrlSuffix()); return str.toString(); } /** * ??URL? * @param category * @return url */ public static String getUrlDynamic(Category category) { if (StringUtils.isNotBlank(category.getHref())) { if (!category.getHref().contains("://")) { return context.getContextPath() + Global.getFrontPath() + category.getHref(); } else { return category.getHref(); } } StringBuilder str = new StringBuilder(); str.append(context.getContextPath()).append(Global.getFrontPath()); str.append("/list-").append(category.getId()).append(Global.getUrlSuffix()); return str.toString(); } /** * ?ContextPath? * @param src * @return src */ public static String formatImageSrcToDb(String src) { if (StringUtils.isBlank(src)) return src; if (src.startsWith(context.getContextPath() + "/userfiles")) { return src.substring(context.getContextPath().length()); } else { return src; } } /** * ?ContextPath? * @param src * @return src */ public static String formatImageSrcToWeb(String src) { if (StringUtils.isBlank(src)) return src; if (src.startsWith(context.getContextPath() + "/userfiles")) { return src; } else { return context.getContextPath() + src; } } }