AbstractDealLinkRepository.java
package com.tradecloud.repository.impl;
import com.tradecloud.domain.model.DealLink;
import com.tradecloud.domain.model.deal.Deal;
import com.tradecloud.repository.DealLinkRepository;
import com.tradecloud.repository.base.impl.RepositoryBaseImpl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* Abstract repository for {@code DealLink} subclasses. This class contains
* common methods that can be reused by all {@code DealLink} subclasses.
*
*
*
* @param <T>
* Any subclass of {@code DealLink}
* @see FECRequestDealLinkRepositoryImpl
* @see FECDealLinkRepositoryImpl
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public abstract class AbstractDealLinkRepository<T extends DealLink> extends RepositoryBaseImpl<T, Object> implements DealLinkRepository<T> {
private static final String FIND_ALL_BY_DEAL_QUERY = "from ENTITY where deal_id = :dealId";
private static final String FIND_ALL_QUERY = "from ENTITY";
private static final String DELETE_ALL_BY_DEAL_QUERY = "delete from ENTITY where deal_id = :dealId";
@Override
public void store(T link) {
saveOrUpdate(link);
flush();
}
@Override
public void delete(T link) {
super.delete(link);
flush();
}
/**
* To create the correct query we need to replace the place-holder "ENTITY"
* from the {@link #DELETE_ALL_BY_DEAL_QUERY} with the {@code entityName}
* supplied.
*
* @param deal
* @param entityName
*/
public void deleteAllByDeal(Deal deal, String entityName) {
getSessionCustom().createQuery(DELETE_ALL_BY_DEAL_QUERY.replaceFirst("ENTITY", entityName))
.setParameter("dealId", deal.getId()).executeUpdate();
flush();
}
/**
* Finds all entities for the entity name supplied. This method allows us to
* use the same query for each subclass of {@code DealLink}. To create the
* correct query we need to replace the place-holder "ENTITY" from the
* {@link #FIND_ALL_QUERY} with the {@code entityName} supplied.
*
* @param entityName
* The entity to search by. Must be a the simple name of a
* superclass of {@code DealLink}.
* @return A list of objects extending from {@code DealLink} or an empty
* list if none found
*/
protected List<T> findAll(String entityName) {
@SuppressWarnings("unchecked")
List<T> results = (List<T>)find(FIND_ALL_QUERY.replaceFirst("ENTITY", entityName));
return results;
}
/**
* Finds all entities for the {@code Deal} and entity name supplied. This
* method allows us to use the same query for each subclass of
* {@code DealLink}. To create the correct query we need to replace the
* place-holder "ENTITY" from the {@link #FIND_ALL_BY_DEAL_QUERY} with the
* {@code entityName} supplied.
*
* @param deal
* The {@link Deal} to match on.
* @param entityName
* The entity to search by. Must be a the simple name of a
* superclass of {@code DealLink}.
* @return
*/
protected List<T> findAllByDeal(Deal deal, String entityName) {
List<T> results = new ArrayList<T>();
if (deal.getId() != null) {
results = (List<T>)findByNamedParam(FIND_ALL_BY_DEAL_QUERY.replaceFirst("ENTITY", entityName), "dealId", deal.getId());
}
return results;
}
}