Java tutorial
/* Copyright 2006 - 2010 Under Dusken 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 no.dusken.aranea.service; import no.dusken.aranea.model.Image; import no.dusken.aranea.model.Person; import no.dusken.common.service.impl.GenericServiceImpl; import org.springframework.dao.DataAccessException; import org.springframework.dao.support.DataAccessUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @Service("imageService") @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true) public class ImageServiceImpl extends GenericServiceImpl<Image> implements ImageService { public ImageServiceImpl() { super(Image.class); } public List<Image> getImagesByPerson(Person p, int number, int offset) { List<Image> list = Collections.emptyList(); try { Map<String, Object> map = new HashMap<String, Object>(); map.put("person", p); list = genericDao.getByNamedQuery("image_byPerson", map, offset, number); } catch (DataAccessException dae) { log.info("Unable to get images", dae); } return list; } public Integer getImageNumberByPerson(Person p) { Long numberOfImages = 0L; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("person", p); numberOfImages = (Long) DataAccessUtils .uniqueResult(genericDao.getByNamedQuery("imagenumber_byPerson", map)); } catch (DataAccessException dae) { log.info("Unable to get number of images", dae); } return numberOfImages.intValue(); } public List<Image> getImages(Person p) { List<Image> list = Collections.emptyList(); Map<String, Object> map = new HashMap<String, Object>(); map.put("person", p); try { list = genericDao.getByNamedQuery("image_byPerson", map); } catch (DataAccessException dae) { log.warn("Unable to get images", dae); } return list; } public List<Image> getImages() { List<Image> list = Collections.emptyList(); try { list = genericDao.getByNamedQuery("image_all", null); } catch (DataAccessException dae) { log.info("Unable to get images", dae); } return list; } public Image getImageByHash(String hash) { // just to be safe: if (hash == null || hash.equalsIgnoreCase("")) { return null; } Image image = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("hash", hash); image = (Image) DataAccessUtils.uniqueResult(genericDao.getByNamedQuery("image_byHash", map)); } catch (DataAccessException dae) { log.info("Unable to get images", dae); } return image; } public List<Image> getImages(int offset, int number) { List<Image> list = Collections.emptyList(); try { list = genericDao.getByNamedQuery("image_all", null, offset, number); } catch (DataAccessException dae) { log.info("Unable to get pages", dae); } return list; } public Image getImageFileByHash(String hash) { Map<String, Object> map = new HashMap<String, Object>(); map.put("hash", hash); try { List<Image> list = genericDao.getByNamedQuery("image_byhash", map); return DataAccessUtils.uniqueResult(list); } catch (DataAccessException dae) { log.warn("Unable to get images", dae); } return null; } }