bookserviceimpl.java
来自「一个基本的图书馆管理系统」· Java 代码 · 共 532 行 · 第 1/2 页
JAVA
532 行
package c18.service;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.upload.FormFile;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import c18.dao.CategoryDao;
import c18.dao.CategoryDaoImpl;
import c18.dao.HibernateSessionFactory;
import c18.dao.BookDao;
import c18.dao.BookDaoImpl;
import c18.entity.Book;
import c18.entity.BookCategory;
import c18.entity.Category;
import c18.helper.PageBean;
import c18.helper.PageResult;
import c18.struts.action.exception.ActionException;
import c18.struts.action.exception.ExistChildActionException;
import c18.struts.action.exception.ExistNameActionException;
import c18.struts.action.exception.ExistReferActionException;
import c18.struts.action.exception.NotFindActionException;
import c18.struts.action.exception.SaveBookImageFailActionException;
public class BookServiceImpl implements BookService {
/**
* 日志操作对象
*/
private static final Log log = LogFactory.getLog(BookServiceImpl.class);
public PageResult getBooks(PageBean pageBean) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
//组建BookDao对象
BookDao bookDao = new BookDaoImpl(session);
//查询
return bookDao.getBooks(pageBean);
}
public void addBook(Book book, FormFile imageFile, String appPath, int[] categoryNos)
throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//组建BookDao对象
BookDao bookDao = new BookDaoImpl(session);
//设置余量为总数量
book.setRemain(book.getAmount());
bookDao.save(book);
if(imageFile != null
&& imageFile.getFileName() != null
&& !imageFile.getFileName().trim().equals("")){
//保存图片到"/images/book/" + bookNo目录下
//去掉appPath结尾的/或\
if (appPath != null) {
if (appPath.endsWith(File.separator)) {
appPath = appPath.substring(0, appPath.length() - 1);
}
}
String path = appPath + File.separator + "images" + File.separator
+ "book" + File.separator + book.getBookNo();
if (saveFile(imageFile, path) < 0) { //保存失败
log.error("保存图书图片失败");
//回滚
session.getTransaction().rollback();
throw new SaveBookImageFailActionException("保存图书图片失败");
}
}
//增加分类关系
if(categoryNos != null){
for(int i=0; i<categoryNos.length; i++){
BookCategory bookcategory = new BookCategory();
bookcategory.setBookNo(book.getBookNo());
bookcategory.setCategoryNo(categoryNos[i]);
bookDao.save(bookcategory);
}
}
//提交
session.getTransaction().commit();
}
private int saveFile(FormFile imageFile, String path) {
//判断路径是否存在
File pathfile = new File(path);
if (pathfile.exists()) { //已存在同名的
if (!pathfile.isDirectory()) { //不是目录
log.error("存在同名,但不是目录");
return -1;
}
} else {//不存在,创建目录
if (!pathfile.mkdirs()) { //创建目录不成功
log.error("创建目录[" + path + "]不成功");
return -2;
}
}
//判断是否存在同名文件
pathfile = new File(path + File.separator + imageFile.getFileName());
if (pathfile.exists()) { //已存在同名文件,删除
if (!pathfile.delete()) {
log.error("已存在同名文件[" + path + File.separator
+ imageFile.getFileName() + "],删除不成功");
return -3;
}
}
//保存文件
try {
InputStream stream = imageFile.getInputStream();//把文件读入
//建立一个输出流,将文件存入目录path。
OutputStream bos = new FileOutputStream(path + File.separator
+ imageFile.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
} catch (FileNotFoundException e) {
log.error(e);
return -4;
} catch (IOException e) {
log.error(e);
return -5;
}
return 0;
}
public List getCategorys() throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//查询
return categoryDao.getCategorys();
}
public List getCategorys(int categoryNo) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//查询
return categoryDao.getCategorys(categoryNo);
}
public Category getCategory(int categoryNo) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//查询
return categoryDao.get(categoryNo);
}
public void addCategory(Category category) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//判断是否有同名分类
List list = categoryDao.findByName(category.getName());
if (list != null && list.size() > 0) {//存在同名分类
//回滚
session.getTransaction().rollback();
log.debug("存在同名分类,addCategory()失败");
throw new ExistNameActionException("已存在同名分类,请输入其它分类名");
}
categoryDao.save(category);
//提交
session.getTransaction().commit();
}
public void delCategory(int categoryNo) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//判断是否存在分类
Category category = categoryDao.get(categoryNo);
if (category == null) { //不存在分类
//回滚
session.getTransaction().rollback();
log.debug("不存在此分类,delCategory()失败");
throw new NotFindActionException("不存在此分类");
}
//判断是否存在子分类
List list = categoryDao.getCategorys(category.getCategoryNo());
if (list != null && list.size() > 0) {
//存在子分类
//回滚
session.getTransaction().rollback();
log.debug("存在子分类,delCategory()失败");
throw new ExistChildActionException("此分类存在子分类,请先删除子分类");
}
try {
categoryDao.remove(category);
} catch (HibernateException e) { //删除错误,回滚
//回滚
session.getTransaction().rollback();
log.debug("可能存在其它引用,delCategory()失败");
throw new ExistReferActionException("此分类可能存在引用,请先删除相关引用");
}
//提交
session.getTransaction().commit();
}
public void modCategory(Category category) throws ActionException {
//得到当前Session
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//组建CategoryDao对象
CategoryDao categoryDao = new CategoryDaoImpl(session);
//判断是否存在分类
Category modcategory = categoryDao.get(category.getCategoryNo());
if (modcategory == null) { //不存在分类
//回滚
session.getTransaction().rollback();
log.debug("不存在此分类,modCategory()失败");
throw new NotFindActionException("不存在此分类");
}
if (modcategory.getName() != null
&& !modcategory.getName().equals(category.getName())) {
//判断是否有同名分类
List list = categoryDao.findByName(category.getName());
if (list != null && list.size() > 0) {
//回滚
session.getTransaction().rollback();
log.debug("存在同名分类,modCategory()失败");
throw new ExistNameActionException("已存在同名其它分类,请输入其它名称");
}
}
modcategory.setName(category.getName());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?