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

📄 tagrepository.java

📁 非常有影响的 j道 论 坛 源码 国外很有明的专家编写的 ....对java爱好者很有参考价值
💻 JAVA
字号:
/*
 * Copyright 2007 the original author or jdon.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.jdon.jivejdon.repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.jdon.controller.model.PageIterator;
import com.jdon.jivejdon.Constants;
import com.jdon.jivejdon.dao.SequenceDao;
import com.jdon.jivejdon.dao.TagDao;
import com.jdon.jivejdon.model.ForumThread;
import com.jdon.jivejdon.model.ThreadTag;

public class TagRepository {

	private TagDao tagDao;

	private SequenceDao sequenceDao;

	public TagRepository(TagDao tagDao, SequenceDao sequenceDao) {
		this.tagDao = tagDao;
		this.sequenceDao = sequenceDao;
	}

	public Collection<Long> searchTitle(String s) {
		return tagDao.searchTitle(s);
	}

	public PageIterator getTaggedThread(Long tagID, int start, int count) {
		return tagDao.getTaggedThread(tagID, start, count);
	}

	public PageIterator getThreadTags(int start, int count) {
		return tagDao.getThreadTags(start, count);
	}

	public ThreadTag getThreadTag(Long tagID) {
		return tagDao.getThreadTag(tagID);
	}

	public void saveTagTitle(ForumThread forumThread, String[] tagTitle) throws Exception {
		if ((tagTitle == null) || (tagTitle.length == 0)) {
			return;
		}
		for (int i = 0; i < tagTitle.length; i++) {
			if ((tagTitle[i] != null) && (tagTitle[i].length() != 0))
				insert(forumThread.getThreadId(), tagTitle[i]);
		}
		forumThread.setTags(getThreadTags(forumThread));
	}

	public void insert(Long fourmThreadId, String tagTitle) throws Exception {
		tagTitle = tagTitle.replace(" ", "").toLowerCase();
		ThreadTag threadTag = tagDao.getThreadTagByTitle(tagTitle);
		if (threadTag != null) {
			threadTag.setAssonum(threadTag.getAssonum() + 1);
			updateThreadTag(threadTag);
		} else {
			threadTag = new ThreadTag();
			Long tagID = sequenceDao.getNextId(Constants.OTHERS);
			threadTag.setTagID(tagID);
			threadTag.setTitle(tagTitle);
			threadTag.setAssonum(1);
			tagDao.createThreadTag(threadTag);
		}
		tagDao.addThreadTag(threadTag.getTagID(), fourmThreadId);
	}

	// 进行比较 使用hibernate merge就一句OK
	public void mergeTagTitle(ForumThread forumThread, String[] tagTitle) throws Exception {
		if ((tagTitle == null) || (tagTitle.length == 0)) {
			return;
		}
		if ((forumThread.getTags() == null) || (forumThread.getTags().size() == 0)) {
			saveTagTitle(forumThread, tagTitle);
			return;
		} else {
			// delete all old tags
			deleteTagTitle(forumThread);
			saveTagTitle(forumThread, tagTitle);
		}
	}

	public void deleteTagTitle(ForumThread forumThread) throws Exception {
		Collection tags = getThreadTags(forumThread);
		for (Object o : tags) {
			ThreadTag tag = (ThreadTag) o;
			if (tag.getAssonum() <= 1) {
				deleteThreadTag(tag);
			} else {
				tag.setAssonum(tag.getAssonum() - 1);
				updateThreadTag(tag);
				tagDao.delThreadTag(forumThread.getThreadId());
			}
		}
	}

	public Collection getThreadTags(ForumThread forumThread) {
		Collection tags = new ArrayList();
		Collection ids = tagDao.getThreadTagIDs(forumThread.getThreadId());
		Iterator iter = ids.iterator();
		while (iter.hasNext()) {
			Long tagID = (Long) iter.next();
			tags.add(tagDao.getThreadTag(tagID));
		}
		return tags;
	}

	public String[] getThreadTagTitles(ForumThread forumThread) {
		Collection tags = getThreadTags(forumThread);
		String[] tagTitle = new String[tags.size()];
		int i = 0;
		for (Object o : tags) {
			ThreadTag tag = (ThreadTag) o;
			tagTitle[i] = tag.getTitle();
			i++;
		}
		return tagTitle;
	}

	public void updateThreadTag(ThreadTag threadTag) throws Exception {
		tagDao.updateThreadTag(threadTag);
	}

	public void deleteThreadTag(ThreadTag threadTag) throws Exception {
		threadTag = this.getThreadTag(threadTag.getTagID());
		tagDao.deleteThreadTag(threadTag.getTagID());
	}

}

⌨️ 快捷键说明

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