package no.dusken.aranea.service;
import no.dusken.aranea.model.Page;
import no.dusken.aranea.model.Section;
import no.dusken.aranea.model.SectionPage;
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 javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
@Service("sectionPageService")
@Transactional(propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT, readOnly = true)
public class SectionPageServiceImpl extends GenericServiceImpl<SectionPage> implements SectionPageService{
public SectionPageServiceImpl() {
super(SectionPage.class);
}
public List<SectionPage> getSectionPageByOwner(Section owner) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("section", owner);
List<SectionPage> list = Collections.emptyList();
try {
list = genericDao.getByNamedQuery("sectionpage_by_owner", map);
} catch (DataAccessException e) {
log.error("Unable to get sectionpages by section", e);
}
return list;
}
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public void saveOrUpdateSectionPages(List<SectionPage> sectionPages) {
for(SectionPage sp : sectionPages){
genericDao.saveOrUpdate(sp);
}
}
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public void cascadeAndSaveOrUpdateSectionPages(List<SectionPage> sectionPages) {
saveOrUpdateSectionPages(cascadeChanges(sectionPages));
}
private List<SectionPage> cascadeChanges(List<SectionPage> sps) {
for (int i = 0; i < sps.size(); i++){
sps.get(i).setOrdering(i + 1);
}
return sps;
}
public SectionPage getSectionPageBySectionAndOrdering(Section s, Integer ordering) {
SectionPage sectionPage = null;
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("section", s);
map.put("ordering", ordering);
List<SectionPage> list = genericDao
.getByNamedQuery("sectionpage_by_section_ordering", map);
sectionPage = DataAccessUtils.uniqueResult(list);
} catch (DataAccessException dae) {
log.warn("Unable to get SectionOrdering", dae);
}
return sectionPage;
}
public SectionPage getSectionPageBySectionAndPage(Section s, Page p) {
SectionPage sectionPage = null;
try {
Map<String, Object> map = new HashMap<String, Object>();
map.put("section", s);
map.put("page", p);
List<SectionPage> list = genericDao
.getByNamedQuery("sectionpage_by_section_page", map);
sectionPage = DataAccessUtils.uniqueResult(list);
} catch (DataAccessException dae) {
log.warn("Unable to get SectionOrdering", dae);
}
return sectionPage;
}
@Override
public List<SectionPage> getSectionPagesByPage(Page page) {
List<SectionPage> sectionPages = Collections.emptyList();
CriteriaBuilder cb = genericDao.getCriteriaBuilder();
CriteriaQuery<SectionPage> cq = cb.createQuery(SectionPage.class);
Root<SectionPage> root = cq.from(SectionPage.class);
Predicate whereuser = cb.equal(root.<Page>get("page"), page);
CriteriaQuery<SectionPage> where = cq.select(root).where(whereuser);
TypedQuery<SectionPage> query = genericDao.getTypedQuery(where);
sectionPages = query.getResultList();
return sectionPages;
}
}