/** * Copyright 2011 the original author or authors. * * 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 org.bricket.plugin.genericdomain.service.impl; import java.util.List; import java.util.Locale; import java.util.Map; import org.bricket.plugin.genericdomain.domain.DomainObject; import org.bricket.plugin.genericdomain.service.GenericDomainService; import org.bricket.service.BricketServiceImpl; import org.bricket.service.UncheckedRuntimeException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.synyx.hades.dao.GenericDao; import org.synyx.hades.domain.Order; import org.synyx.hades.domain.PageRequest; /** * @author Ingo Renner * @author Henning Teek */ @Service(value = "genericDomainService") @Transactional(readOnly = true) public class GenericDomainServiceImpl extends BricketServiceImpl implements GenericDomainService { private final Logger log = LoggerFactory.getLogger(GenericDomainServiceImpl.class); @Autowired private Map<String, GenericDao<? extends DomainObject, Long>> genericDaos; @Override protected void onInit() { log.info("generic domain service initialized. bricket daos = " + (genericDaos == null ? "null" : genericDaos.keySet())); } @SuppressWarnings("unchecked") private <T extends DomainObject> GenericDao<T, Long> getDao(Class<T> type) { String simpleName = type.getSimpleName(); String key = simpleName.substring(0, 1).toLowerCase(Locale.getDefault()) + simpleName.substring(1) + "Dao"; if (genericDaos.containsKey(key)) { return (GenericDao<T, Long>) genericDaos.get(key); } throw new UncheckedRuntimeException("no dao found for type: " + type.getName()); } @Override public <T extends DomainObject> T load(Class<T> type, Long id) { return getDao(type).readByPrimaryKey(id); } @Override public <T extends DomainObject> List<T> loadAll(Class<T> type, final int from, final int count, final String sortProperty, final boolean sortAsc) { final PageRequest pageable; int page; if (from > 0) { page = from / count; } else { page = 0; } if (sortProperty == null || "".equals(sortProperty.trim())) { pageable = new PageRequest(page, count); } else { pageable = new PageRequest(page, count, sortAsc ? Order.ASCENDING : Order.DESCENDING, sortProperty); } return getDao(type).readAll(pageable).asList(); } @Override public <T extends DomainObject> List<T> loadAll(Class<T> type) { return getDao(type).readAll(); } @Override public <T extends DomainObject> int getCount(Class<T> type) { return getDao(type).count().intValue(); } @Override @Transactional public <T extends DomainObject> void save(Class<T> type, T object) { getDao(type).save(object); } @Override @Transactional public <T extends DomainObject> void delete(Class<T> type, T object) { getDao(type).delete(getDao(type).readByPrimaryKey(object.getId())); } }