📄 facade.java
字号:
account.setAccountId(sequenceDao.getNextAccountId());
account.validate();
accountDao.createAccount(account);
}
public void deleteAccount(int accountId) throws DeleteException {
accountDao.deleteAccount(accountId);
}
public void lockAccount(int accountId) throws UpdateException {
accountDao.lockAccount(accountId);
}
/**
* Update the account, but the accountId and username will not be updated.
*
* @param id The identity of the session.
* @param account The account.
*/
public void updateAccount(Identity id, Account account) {
account.validate();
if(account.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
accountDao.updateAccount(account);
}
/**
* Create a new article.
*
* @param id The identity of the session.
* @param article Must specify all attributes except "articleId" and "accountId".
*/
public void createArticle(Identity id, Article article) throws CreateException {
article.validate();
// check category:
Category category = getCategory(article.getCategoryId());
if(category==null || !category.isForArticles())
throw new IllegalArgumentException("Category is invalid.");
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
article.validate();
// set articleId:
article.setAccountId(id.getAccountId());
article.setArticleId(sequenceDao.getNextArticleId());
articleDao.createArticle(article);
// if succeeded:
searcher.index(article);
}
public void deleteArticle(Identity id, int articleId) throws DeleteException {
// check:
Article article = getArticleInfo(articleId);
if(article.getAccountId()!=id.getAccountId())
throw new AuthorizationException("Permission denied.");
// delete it:
articleDao.deleteArticle(articleId);
}
public Article getArticleInfo(int articleId) throws QueryException {
return articleDao.getArticleInfo(articleId);
}
public List getArticlesByCategory(int categoryId) throws QueryException {
return articleDao.getArticlesByCategory(categoryId);
}
public void updateArticle(Identity id, Article article) {
article.validate();
// check accountId:
if(article.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// check category:
Category category = getCategory(article.getCategoryId());
if(category==null || !category.isForArticles())
throw new IllegalArgumentException("Category is invalid.");
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// update:
articleDao.updateArticle(article);
}
public void updateArticleInfo(Identity id, Article article) throws UpdateException {
article.validate();
// check account:
if(article.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// check category:
Category category = getCategory(article.getCategoryId());
if(category==null || !category.isForArticles())
throw new IllegalArgumentException("Category is invalid.");
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// update:
articleDao.updateArticleInfo(article);
}
public void deleteFeedback(int feedbackId) throws DeleteException {
feedbackDao.deleteFeedback(feedbackId);
}
public Feedback getFeedback(int feedbackId) throws QueryException {
return feedbackDao.getFeedback(feedbackId);
}
public void deleteImage(Identity id, int imageId, String baseDir) throws DeleteException {
Image image = getImage(imageId);
// check account:
Category category = getCategory(image.getCategoryId());
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// ok, delete image file:
new File(baseDir + id.getAccountId() + "/" + image.getFilename()).delete();
new File(baseDir + id.getAccountId() + "/" + image.getPreviewFilename()).delete();
// and the record in the database:
imageDao.deleteImage(imageId);
}
public void createImage(Identity id, Image image, String baseDir, InputStream is) throws CreateException {
OutputStream os = null;
String oriFile = null;
String preFile = null;
try {
image.validate();
// check category:
Category category = getCategory(image.getCategoryId());
if(category==null || !category.isForImages())
throw new IllegalArgumentException("Gallery is invalid.");
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// ok, set id:
image.setImageId(sequenceDao.getNextImageId());
// create file:
oriFile = baseDir + id.getAccountId() + "/" + image.getFilename();
preFile = baseDir + id.getAccountId() + "/" + image.getPreviewFilename();
os = new BufferedOutputStream(new FileOutputStream(oriFile));
byte[] buffer = new byte[1024];
int n;
while((n=is.read(buffer))!=(-1)) {
os.write(buffer, 0, n);
}
// create preview image file:
PreviewImage.createPreviewImage(oriFile, preFile, image.getImageType());
// create image in database:
imageDao.createImage(image);
}
catch(Exception e) {
// if there is any error, delete the image file:
new File(oriFile).delete();
new File(preFile).delete();
throw new RuntimeException("Upload image failed: " + e.getMessage());
}
finally {
try{ is.close(); } catch(Exception e) {}
if(os!=null) try{ os.close(); } catch(Exception e) {}
}
}
public List getImages(int categoryId, int num, int page) throws QueryException {
return imageDao.getImages(categoryId, num, page);
}
public List getImages(int categoryId) throws QueryException {
return imageDao.getImages(categoryId);
}
public void createLink(Identity id, Link link) throws CreateException {
link.validate();
// check category:
Category category = getCategory(link.getCategoryId());
if(category==null || !category.isForLinks())
throw new IllegalArgumentException("Illegal argument: categoryId.");
if(category.getAccountId()!=id.getAccountId())
throw new AuthorizationException("Permission denied.");
// create:
link.setLinkId(sequenceDao.getNextLinkId());
linkDao.createLink(link);
}
public void deleteLink(Identity id, int linkId) throws DeleteException {
// check:
Link link = getLink(linkId);
if(link==null)
throw new IllegalArgumentException("The link is invalid.");
Category category = getCategory(link.getCategoryId());
if(category.getAccountId()!=id.getAccountId())
throw new AuthorizationException("Permission denied.");
// delete it:
linkDao.deleteLink(linkId);
}
public void updateLink(Identity id, Link link) throws UpdateException {
link.validate();
// check category:
Category category = getCategory(link.getCategoryId());
if(category==null || !category.isForLinks())
throw new IllegalArgumentException("Favorite of the link is invalid.");
if(category.getAccountId()!=id.getAccountId())
throw new RuntimeException("Permission denied.");
// update:
linkDao.updateLink(link);
}
public String writeRss(int accountId) throws QueryException {
Account account = accountDao.getAccount(accountId);
List articles = articleDao.getArticles(accountId, account.getMaxPerPage(), 1);
Iterator it = articles.iterator();
while(it.hasNext()) {
Article article = (Article)it.next();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -