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.Issue; 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.*; @Service("issueService") @Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true) public class IssueServiceImpl extends GenericServiceImpl<Issue> implements IssueService { public IssueServiceImpl() { super(Issue.class); } public List<Issue> getIssues(int year) { List<Issue> i = Collections.emptyList(); try { Map<String, Object> map = new HashMap<String, Object>(); map.put("fromDate", new GregorianCalendar(year, 0, 1)); map.put("toDate", new GregorianCalendar(year, 11, 31)); i = genericDao.getByNamedQuery("issues_byyear", map); } catch (DataAccessException dae) { log.warn("Unable to get Issues", dae); } return i; } public List<Issue> getIssuesPublished(int year) { List<Issue> i = Collections.emptyList(); try { Map<String, Object> map = new HashMap<String, Object>(); map.put("fromDate", new GregorianCalendar(year, 0, 1)); GregorianCalendar endOfYear = new GregorianCalendar(year, 11, 31); GregorianCalendar today = new GregorianCalendar(); if (endOfYear.after(today)) { map.put("toDate", today); } else { map.put("toDate", endOfYear); } i = genericDao.getByNamedQuery("issues_byyear_published", map); } catch (DataAccessException dae) { log.warn("Unable to get Issues", dae); } return i; } public List<Issue> getIssues() { List<Issue> i = Collections.emptyList(); try { i = genericDao.getByNamedQuery("issue_issues", null); } catch (DataAccessException dae) { log.warn("Unable to get Issues", dae); } return i; } public Issue getIssue(int year, int issue) { Issue i = null; try { Map<String, Object> map = new HashMap<String, Object>(); map.put("fromDate", new GregorianCalendar(year, 0, 1)); map.put("toDate", new GregorianCalendar(year + 1, 0, 1)); map.put("issue", issue); List<Issue> list = genericDao.getByNamedQuery("issues_byyear_and_issue", map); Object o = DataAccessUtils.uniqueResult(list); if (o != null && o instanceof Issue) { i = (Issue) o; } } catch (DataAccessException dae) { log.warn("Unable to get Issue", dae); } return i; } public Issue getCurrentIssue() { Issue i = null; try { List<Issue> list = genericDao.getByNamedQuery("issues_latest", null); Object o = DataAccessUtils.uniqueResult(list); if (o != null && o instanceof Issue) { i = (Issue) o; } } catch (DataAccessException dae) { log.warn("Unable to get Issue", dae); } return i; } public List<Issue> getCurrentIssues(int number) { List<Issue> i = Collections.emptyList(); try { i = genericDao.getByNamedQuery("issue_issues", null, 0, number); } catch (DataAccessException dae) { log.warn("Unable to get Issues", dae); } return i; } public Issue getOldestIssue() { Issue i = null; try { List<Issue> list = genericDao.getByNamedQuery("issues_oldest", null); Object o = DataAccessUtils.uniqueResult(list); if (o != null && o instanceof Issue) { i = (Issue) o; } } catch (DataAccessException dae) { log.warn("Unable to get Issue", dae); } return i; } public List<Issue> getIssues(int offset, int number) { List<Issue> list = Collections.emptyList(); try { list = genericDao.getByNamedQuery("issue_issues", null, offset, number); } catch (DataAccessException dae) { log.info("Unable to get pages", dae); } return list; } public List<Issue> getPublishedIssues(int number) { List<Issue> list = Collections.emptyList(); try { Map map = new HashMap(); map.put("today", new GregorianCalendar()); list = genericDao.getByNamedQuery("issue_published", map, 0, number); } catch (DataAccessException dae) { log.info("Unable to get pages", dae); } return list; } }