Skip to content

Instantly share code, notes, and snippets.

@james-d
Created May 11, 2014 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save james-d/ab72b487b7b12bb7a3bc to your computer and use it in GitHub Desktop.
Save james-d/ab72b487b7b12bb7a3bc to your computer and use it in GitHub Desktop.
Business model and data integration tier for the laboratory information management system. See also https://gist.github.com/james-d/e485ac525c71e20bb453
package edu.marshall.genomics.lims.data;
import java.util.List;
import edu.marshall.genomics.lims.entities.Investigator;
import edu.marshall.genomics.lims.entities.Project;
import edu.marshall.genomics.lims.entities.Sample;
public interface Dao {
public List<Investigator> getAllInvestigators() throws DaoException ;
public Investigator getInvestigatorById(int id) throws DaoException ;
public List<Project> getAllProjects() throws DaoException ;
public List<Project> getProjectsByInvestigator(Investigator investigator) throws DaoException ;
public Project getProjectById(int id) throws DaoException ;
public List<Sample> getSamplesByProject(Project project) throws DaoException ;
public Sample getSampleById(int id) throws DaoException ;
public void addInvestigators(List<Investigator> investigators) throws DaoException ;
public void updateInvestigator(Investigator invesitgator) throws DaoException ;
public void addProjects(List<Project> projects) throws DaoException ;
public void updateProject(Project project) throws DaoException ;
public void addSamples(List<Sample> samples) throws DaoException ;
public void updateSample(Sample sample) throws DaoException ;
}
package edu.marshall.genomics.lims.data;
public class DaoException extends Exception {
private static final long serialVersionUID = -25809315466194148L;
public DaoException() {
}
public DaoException(String message, Throwable cause) {
super(message, cause);
}
public DaoException(String message) {
super(message);
}
public DaoException(Throwable cause) {
super(cause);
}
}
package edu.marshall.genomics.lims.data;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import edu.marshall.genomics.lims.entities.Investigator;
import edu.marshall.genomics.lims.entities.Project;
import edu.marshall.genomics.lims.entities.Sample;
@Named("dao")
public class JpaDao implements Dao {
private EntityManager em ;
public EntityManager getEntityManager() {
return em ;
}
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.em = em ;
}
@Override
public List<Investigator> getAllInvestigators() throws DaoException {
return execute("Investigator.findAll", Investigator.class);
}
@Override
public Investigator getInvestigatorById(int id)
throws DaoException {
try {
return em.find(Investigator.class, id);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public List<Project> getAllProjects() throws DaoException {
return execute("Project.findAll", Project.class);
}
@Override
public List<Project> getProjectsByInvestigator(Investigator investigator)
throws DaoException {
try {
return em.createNamedQuery("Project.byInvestigator", Project.class)
.setParameter("investigator", investigator)
.getResultList();
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public Project getProjectById(int id) throws DaoException {
try {
return em.find(Project.class, id);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public List<Sample> getSamplesByProject(Project project)
throws DaoException {
try {
return em.createNamedQuery("Sample.byProject", Sample.class)
.setParameter("project", project)
.getResultList();
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public Sample getSampleById(int id) throws DaoException {
try {
return em.find(Sample.class, id);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void addInvestigators(List<Investigator> investigators)
throws DaoException {
try {
for (Investigator investigator : investigators) {
em.persist(investigator);
}
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void addProjects(List<Project> projects) throws DaoException {
try {
for (Project project : projects)
em.persist(project);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void addSamples(List<Sample> samples) throws DaoException {
try {
for (Sample sample : samples)
em.persist(sample);
} catch (Exception e) {
throw new DaoException(e);
}
}
private <T> List<T> execute(String queryName, Class<T> type) throws DaoException {
try {
return em.createNamedQuery(queryName, type).getResultList();
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void updateInvestigator(Investigator investigator)
throws DaoException {
try {
em.merge(investigator);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void updateProject(Project project) throws DaoException {
try {
em.merge(project);
} catch (Exception e) {
throw new DaoException(e);
}
}
@Override
public void updateSample(Sample sample) throws DaoException {
try {
em.merge(sample);
} catch (Exception e) {
throw new DaoException(e);
}
}
}
package edu.marshall.genomics.lims.service;
import java.util.List;
import edu.marshall.genomics.lims.entities.Investigator;
import edu.marshall.genomics.lims.entities.Project;
import edu.marshall.genomics.lims.entities.Sample;
public interface LimsService {
public List<Investigator> getAllInvestigators() throws ServiceException ;
public Investigator getInvestigatorById(int id) throws ServiceException ;
public List<Project> getAllProjects() throws ServiceException ;
public List<Project> getProjectsByInvestigator(Investigator investigator) throws ServiceException ;
public Project getProjectById(int id) throws ServiceException ;
public List<Sample> getSamplesByProject(Project project) throws ServiceException ;
public Sample getSampleById(int id) throws ServiceException ;
public void addInvestigators(List<Investigator> investigators) throws ServiceException ;
public void updateInvestigator(Investigator invesitgator) throws ServiceException ;
public void addProjects(List<Project> projects) throws ServiceException ;
public void updateProject(Project project) throws ServiceException ;
public void addSamples(List<Sample> samples) throws ServiceException ;
public void updateSample(Sample sample) throws ServiceException ;
}
package edu.marshall.genomics.lims.service;
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = -5739815078288882145L;
public ServiceException() {
}
public ServiceException(String message) {
super(message);
}
public ServiceException(Throwable cause) {
super(cause);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
}
package edu.marshall.genomics.lims.service;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import edu.marshall.genomics.lims.data.Dao;
import edu.marshall.genomics.lims.data.DaoException;
import edu.marshall.genomics.lims.entities.Investigator;
import edu.marshall.genomics.lims.entities.Project;
import edu.marshall.genomics.lims.entities.Sample;
@Named("limsService")
@Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor=ServiceException.class)
public class ServiceImpl implements LimsService {
private Dao dao ;
public Dao getDao() {
return dao;
}
@Inject
public void setDao(Dao dao) {
this.dao = dao;
}
@Override
@Transactional(readOnly=true)
public List<Investigator> getAllInvestigators() throws ServiceException {
try {
return dao.getAllInvestigators();
} catch (DaoException exc) {
throw new ServiceException(exc);
}
}
@Override
@Transactional(readOnly=true)
public Investigator getInvestigatorById(int id)
throws ServiceException {
try {
return dao.getInvestigatorById(id);
} catch (DaoException exc) {
throw new ServiceException(exc);
}
}
@Override
@Transactional(readOnly=true)
public List<Project> getAllProjects() throws ServiceException {
try {
return dao.getAllProjects() ;
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
@Transactional(readOnly=true)
public List<Project> getProjectsByInvestigator(Investigator investigator)
throws ServiceException {
try {
return dao.getProjectsByInvestigator(investigator);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
@Transactional(readOnly=true)
public Project getProjectById(int id) throws ServiceException {
try {
return dao.getProjectById(id);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
@Transactional(readOnly=true)
public List<Sample> getSamplesByProject(Project project)
throws ServiceException {
try {
return dao.getSamplesByProject(project);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
@Transactional(readOnly=true)
public Sample getSampleById(int id) {
try {
return dao.getSampleById(id);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void addInvestigators(List<Investigator> investigators)
throws ServiceException {
try {
dao.addInvestigators(investigators);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void addProjects(List<Project> projects) throws ServiceException {
try {
dao.addProjects(projects);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void addSamples(List<Sample> samples) throws ServiceException {
try {
dao.addSamples(samples);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void updateInvestigator(Investigator investigator)
throws ServiceException {
try {
dao.updateInvestigator(investigator);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void updateProject(Project project) throws ServiceException {
try {
dao.updateProject(project);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
@Override
public void updateSample(Sample sample) throws ServiceException {
try {
dao.updateSample(sample);
} catch (DaoException e) {
throw new ServiceException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment