Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package service; import entity.Article; import entity.ArticleFile; import java.io.IOException; import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import persistence.ArticleDao; import service.parent.PrimService; import support.ServiceResult; import support.StringAdapter; /** * * @author Rice Pavel */ @Service @Transactional public class ArticleService extends PrimService { @Autowired private ArticleDao dao; public ServiceResult save(Article obj) { return validateSave(obj, dao); } public ServiceResult save(Article obj, MultipartFile file) throws IOException { ServiceResult result = new ServiceResult(); validateSave(obj, dao, result); if (result.hasErrors()) { if (file != null && !file.isEmpty()) { ArticleFile articleFile = new ArticleFile(); articleFile.setArticle(obj); articleFile.setRusname(file.getOriginalFilename()); saveFile(articleFile, file); } } return result; } public ServiceResult delete(Article obj) { return _delete(obj, dao); } public ServiceResult update(Article obj, MultipartFile file, boolean deleteFile) throws IOException { ServiceResult res = new ServiceResult(); if (deleteFile) { delFile(obj.getArticleId()); } res = validateUpdate(obj, dao); if (file != null && !file.isEmpty()) { delFile(obj.getArticleId()); ArticleFile newFileEnt = new ArticleFile(); newFileEnt.setArticle(obj); newFileEnt.setRusname(file.getOriginalFilename()); saveFile(newFileEnt, file); } return res; } private void delFile(Long articleId) { Article obj = dao.find(articleId); log.warn("articleId " + articleId); ArticleFile oldFileEnt = obj.getArticleFile(); log.warn("delFile"); if (oldFileEnt != null) { log.warn("oldFileEnt"); fileDao.delete(oldFileEnt); } } public List<Article> getAll() { return _getAll(dao); } public Article find(Long id) { return _find(id, dao); } /* public List<HashMap<String,Object>> getByAlias(String alias){ List<HashMap<String,Object>> res = new ArrayList(); if(alias!=null&&!alias.isEmpty()){ List<Object[]>rawRes = dao.getByAlias(alias); for(Object[]o:rawRes){ HashMap articleInfo = new HashMap(); Long articleId = getLong(o[0]); String title = StringAdapter.getString(o[1]); articleInfo.put("articleId", articleId); articleInfo.put("title", title); res.add(articleInfo); } } return res; }*/ public List<Article> getByAlias(String alias) { List<Article> res = new ArrayList(); if (alias != null && !alias.isEmpty()) { res = dao.getByAlias(alias); ; } return res; } private Long getLong(Object value) { if (value != null) { if (value instanceof Long) { return (Long) value; } else if (value instanceof BigInteger) { BigInteger bi = (BigInteger) value; return bi.longValue(); } else if (value instanceof Integer) { Integer i = (Integer) value; return i.longValue(); } } return null; } }