001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j;
017:
018: import java.util.Iterator;
019: import java.util.List;
020:
021: import dlog4j.formbean.CategoryForm;
022: import dlog4j.formbean.SiteForm;
023: import dlog4j.formbean.UserForm;
024: import dlog4j.security.DlogRole;
025:
026: import net.sf.hibernate.Criteria;
027: import net.sf.hibernate.HibernateException;
028: import net.sf.hibernate.Session;
029: import net.sf.hibernate.expression.Expression;
030: import net.sf.hibernate.expression.Order;
031:
032: /**
033: * 日记分类管理器
034: * @author Liudong
035: */
036: public class CategoryManager {
037:
038: /**
039: * 读取日记分类的详细信息(管理级别的方法)
040: * DlogCategoryAction的删除和更新分类方法用到该方法
041: * @param ssn
042: * @param site
043: * @param loginUser
044: * @param cat_id
045: * @return
046: * @throws HibernateException
047: */
048: public static CategoryForm getCategory(Session ssn, SiteForm site,
049: int cat_id) throws HibernateException {
050: Criteria crit = ssn.createCriteria(CategoryForm.class);
051: crit = crit.add(Expression.eq("site.id", new Integer(site
052: .getId())));
053: crit = crit.add(Expression.eq("id", new Integer(cat_id)));
054: try {
055: List cats = crit.list();
056: if (cats.size() > 0)
057: return (CategoryForm) cats.get(0);
058: } catch (HibernateException e) {
059: }
060: return null;
061: }
062:
063: /**
064: * 查询指定站点的所有日记分类(管理级别的方法)
065: * DlogCategoryAction: doCreateCategory, doMoveDown, doMoveUp
066: * @param ssn
067: * @param site
068: * @return
069: * @throws SQLException
070: * @throws HibernateException
071: */
072: public static List listCategories(Session ssn, SiteForm site)
073: throws HibernateException {
074: Criteria crit = ssn.createCriteria(CategoryForm.class);
075: crit = crit.add(Expression.eq("site.id", new Integer(site
076: .getId())));
077: crit = crit.addOrder(Order.asc("order"));
078: return crit.list();
079: }
080:
081: /**
082: * 查询某个用户可见的所有日记分类,用于浏览日记而非添加日记
083: * CategoryTag.java (一般用途)
084: * @param ssn
085: * @param site
086: * @return
087: * @throws SQLException
088: * @throws HibernateException
089: */
090: public static List listCategories(Session ssn, SiteForm site,
091: UserForm loginUser) throws HibernateException {
092: Criteria crit = ssn.createCriteria(CategoryForm.class);
093: crit = crit.add(Expression.eq("site.id", new Integer(site
094: .getId())));
095: crit = crit.addOrder(Order.asc("order"));
096: List cats = crit.list();
097: Iterator icats = cats.iterator();
098: int role = (loginUser != null) ? loginUser.getUserRole()
099: : DlogRole.ROLE_GUEST;
100: int[] ncats = (loginUser != null) ? loginUser.getOwnerCatids()
101: : new int[0];
102:
103: while (role != DlogRole.ROLE_MANAGER && icats.hasNext()) {
104: CategoryForm cat = (CategoryForm) icats.next();
105: if (role == DlogRole.ROLE_BUDDY) {
106: if (cat.getType() == CategoryForm.TYPE_OWNER) {
107: int i = 0;
108: for (; i < ncats.length; i++) {
109: if (cat.getId() == ncats[i])
110: break;
111: }
112: if (i >= ncats.length)
113: icats.remove();
114: }
115: } else {
116: if (cat.getType() == CategoryForm.TYPE_OWNER)
117: icats.remove();
118: }
119: }
120: return cats;
121: }
122:
123: /**
124: * 查询某个用户可见的所有日记分类,用于浏览日记而非添加日记
125: * CategoryTag.java (一般用途)
126: * @param ssn
127: * @param site
128: * @return
129: * @throws SQLException
130: * @throws HibernateException
131: */
132: public static List listCategoriesForModify(Session ssn,
133: SiteForm site, UserForm loginUser)
134: throws HibernateException {
135: Criteria crit = ssn.createCriteria(CategoryForm.class);
136: crit = crit.add(Expression.eq("site.id", new Integer(site
137: .getId())));
138: crit = crit.addOrder(Order.asc("order"));
139: List cats = crit.list();
140: Iterator icats = cats.iterator();
141: int role = (loginUser != null) ? loginUser.getRole().getId()
142: : DlogRole.ROLE_GUEST;
143: int[] ncats = (loginUser != null) ? loginUser.getOwnerCatids()
144: : new int[0];
145:
146: while (role != DlogRole.ROLE_MANAGER && icats.hasNext()) {
147: CategoryForm cat = (CategoryForm) icats.next();
148: if (role == DlogRole.ROLE_BUDDY) {
149: if (cat.getType() == CategoryForm.TYPE_OWNER) {
150: int i = 0;
151: for (; i < ncats.length; i++) {
152: if (cat.getId() == ncats[i])
153: break;
154: }
155: if (i < ncats.length)
156: continue;
157: } else if (cat.getType() == CategoryForm.TYPE_COMMON)
158: continue;
159: }
160: if (role == DlogRole.ROLE_FRIEND) {
161: if (cat.getType() == CategoryForm.TYPE_COMMON) {
162: int i = 0;
163: for (; i < ncats.length; i++) {
164: if (cat.getId() == ncats[i])
165: break;
166: }
167: if (i < ncats.length)
168: continue;
169: }
170: }
171: icats.remove();
172: }
173: return cats;
174: }
175: }
|