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

📄 facade.java

📁 java写的blog
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Created on 2004-10-2
 * Author: Xuefeng, Copyright (C) 2004, Xuefeng.
 */
package org.crystalblog.logic;

import java.io.*;
import java.util.*;

import org.crystalblog.dao.*;
import org.crystalblog.domain.*;
import org.crystalblog.exception.*;
import org.crystalblog.web.Identity;
import org.crystalblog.search.*;

/**
 * The implementation of Facade.
 * 
 * @author Xuefeng
 */
public class Facade {

    private LuceneSearcher searcher;

    private AccountDao  accountDao;
    private ArticleDao  articleDao;
    private CategoryDao categoryDao;
    private FeedbackDao feedbackDao;
    private ImageDao    imageDao;
    private LinkDao     linkDao;
    private SequenceDao sequenceDao;

    // for dependency injection:
    public void setLuceneSearcher(LuceneSearcher searcher) { this.searcher = searcher; }

    public void setAccountDao(AccountDao accountDao)    { this.accountDao = accountDao; }
    public void setArticleDao(ArticleDao articleDao)    { this.articleDao = articleDao; }
    public void setCategoryDao(CategoryDao categoryDao) { this.categoryDao = categoryDao; }
    public void setFeedbackDao(FeedbackDao feedbackDao) { this.feedbackDao = feedbackDao; }
    public void setImageDao(ImageDao imageDao)          { this.imageDao = imageDao; }
    public void setLinkDao(LinkDao linkDao)             { this.linkDao = linkDao; }
    public void setSequenceDao(SequenceDao sequenceDao) { this.sequenceDao = sequenceDao; }

    // for abstract method in Facade:
    public Identity login(String username, String password) throws AuthorizationException {
        int accountId = accountDao.loginAccount(username, password);
        return new Identity(accountId, username);
    }

    public Account getAccount(String username) throws QueryException {
        return accountDao.getAccount(username);
    }

    public List getRecentAccounts(int num) throws QueryException {
        return accountDao.getRecentAccounts(num);
    }

    public void changePassword(Identity id, String oldPassword, String newPassword, String newPassword2) {
        Account account = getAccount(id.getAccountId());
        // validate old password:
        if(!account.getPassword().equals(oldPassword))
            throw new RuntimeException("The old password is invalid.");
        // validate new password:
        if(newPassword==null || newPassword2==null)
            throw new RuntimeException("The new password is invalid.");
        if(!newPassword.equals(newPassword2))
            throw new RuntimeException("The 2 new passwords are not match.");
        if(!newPassword.matches("[a-zA-Z0-9][a-zA-Z0-9]{6,20}"))
            throw new RuntimeException("The new password contains illegal characters.");
        accountDao.changePassword(id.getAccountId(), newPassword);
    }

    public Account getAccount(int accountId) throws QueryException {
        return accountDao.getAccount(accountId);
    }

    public Account getAccountByCategory(int categoryId) throws QueryException {
        return accountDao.getAccountByCategory(categoryId);
    }

    public Account getAccountByArticle(int articleId) throws QueryException {
        return accountDao.getAccountByArticle(articleId);
    }

    public Article getArticle(int articleId) throws QueryException {
        return articleDao.getArticle(articleId);
    }

    public List getArticles(int accountId, int num, int page) throws QueryException {
        return articleDao.getArticles(accountId, num, page);
    }

    public List getRecentArticlesInfo(int num) throws QueryException {
        return articleDao.getRecentArticlesInfo(num);
    }

    public List getArticlesByCategory(int categoryId, int num, int page) throws QueryException {
        return articleDao.getArticlesByCategory(categoryId, num, page);
    }

    public int getArticlesCount(int categoryId) throws QueryException {
        return articleDao.getArticlesCount(categoryId);
    }

    public SearchResult searchArticle(String q, int num, int page) throws QueryException {
        return searcher.search(q, num, page);
    }

    public Category getCategory(int categoryId) throws QueryException {
        return categoryDao.getCategory(categoryId);
    }

    public List getCategories(int accountId) throws QueryException {
        return categoryDao.getCategories(accountId);
    }

    public List getCategoriesOfArticle(int accountId) throws QueryException {
        return categoryDao.getCategoriesOfArticle(accountId);
    }

    public List getCategoriesOfType(int accountId, int type) throws QueryException {
        return categoryDao.getCategoriesOfType(accountId, type);
    }

    /**
     * Create a new category. 
     * 
     * @param id The Identity from session.
     * @param category Must specify "title", "type", "visible", "description".
     * @throws CreateException If creation failed.
     */
    public void createCategory(Identity id, Category category) throws CreateException {
        category.validate();
        // set category:
        category.setAccountId(id.getAccountId());
        category.setCategoryId(sequenceDao.getNextCategoryId());
        categoryDao.createCategory(category);
    }

    public void deleteCategory(Identity id, int categoryId) throws DeleteException {
        // check:
        Category category = getCategory(categoryId);
        if(category==null)
            throw new RuntimeException("Category is not existed.");
        if(category.getAccountId()!=id.getAccountId())
            throw new AuthorizationException("Permission denied.");
        // make sure it is empty:
        boolean empty = false;
        switch(category.getType()) {
        case Category.TYPE_ARTICLES:
            empty = (getArticlesCount(categoryId)==0);
        	break;
        case Category.TYPE_IMAGES:
            empty = (getImagesCount(categoryId)==0);
        	break;
        case Category.TYPE_LINKS:
            empty = (getLinksCount(categoryId)==0);
        	break;
        }
        if(!empty)
            throw new RuntimeException("Cannot delete this " + category.getName() + " because it is not empty.");

        // delete it:
        categoryDao.deleteCategory(categoryId);
    }

    public void updateCategory(Identity id, Category category) throws UpdateException {
        category.validate();
        // check account:
        if(id.getAccountId()!=category.getAccountId())
            throw new RuntimeException("Permission denied.");
        // update:
        categoryDao.updateCategory(category);
    }

    public List getAllLinks(int accountId) throws QueryException {
        return linkDao.getAllLinks(accountId);
    }

    public Link getLink(int linkId) throws QueryException {
        return linkDao.getLink(linkId);
    }

    public int getLinksCount(int categoryId) throws QueryException {
        return linkDao.getLinksCount(categoryId);
    }

    public List getFeedbacks(int articleId) throws QueryException {
        return feedbackDao.getFeedbacks(articleId);
    }

    public void createFeedback(Feedback feedback) throws CreateException {
        feedback.validate();
        feedbackDao.createFeedback(feedback);
    }

    public Image getImage(int imageId) throws QueryException {
        return imageDao.getImage(imageId);
    }

    public int getImagesCount(int categoryId) throws QueryException {
        return imageDao.getImagesCount(categoryId);
    }

    public void sendMessage(Message message) {
        // check:
        message.validate();
        // complete the fields:
        message.setSentDate(new Date());
        //message.setMessageId(getNextMessageId());
        //messageDao.sendMessage(message);
    }

    public void createAccount(Account account) throws CreateException {

⌨️ 快捷键说明

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