/* * BioJava development code * * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public Licence. This should * be distributed with the code. If you do not have a copy, * see: * * http://www.gnu.org/copyleft/lesser.html * * Copyright for this code is held jointly by the individual * authors. These should be listed in @author doc comments. * * For more information on the BioJava project and its aims, * or to join the biojava-l mailing list, visit the home page * at: * * http://www.biojava.org/ * * * */ import java.util.List; import org.biojava.bio.structure.PDBHeader; import org.biojava.bio.structure.Structure; import org.biojava.bio.structure.hibernate.HibernateSession; import org.biojava.bio.structure.hibernate.StructureFile; import org.hibernate.Session; import org.hibernate.Query; /** An example that loads all PDB files from the database and prints them to stdout. * * @author Andreas Prlic * */ public class DemoLoadAllStructures { public static void main(String[] args){ // since the data is lazy-loaded we keep the session open Object ownership = HibernateSession.createSession(); Session session = HibernateSession.getSession(); // init the example class DemoLoadAllStructures demo = new DemoLoadAllStructures(); // and get a listing of all availabel PDB files System.out.println("load all structures"); List structures = demo.loadAll(); System.out.println("got " + structures.size() + " structures"); // now let's iterate over the structures in the database and print the header info int pos = 0; for (PDBHeader header: structures){ pos++; System.out.println(pos + " " + header); System.out.println(demo.getStructureFile(header.getIdCode()).getPath()); Structure s = demo.getStructure(header.getIdCode()); System.out.println(s); // prevent the session cache from getting too big and cause a MemoryException session.flush(); } // at this point we can close the Hibernate session HibernateSession.closeSession(ownership); } public DemoLoadAllStructures(){ } public Structure getStructure(String pdbCode){ // taking control over session, in order to ensure thread -safety Object ownership = HibernateSession.createSession(); Session session = HibernateSession.getSession(); Query q = session.getNamedQuery("structure.byPDBCode"); q.setParameter("pdbCode",pdbCode.toUpperCase()); List lst = q.list(); HibernateSession.closeSession(ownership); if ( lst.size() != 1){ return null; } return lst.get(0); } public StructureFile getStructureFile(String pdbCode){ Object ownership = HibernateSession.createSession(); Session session = HibernateSession.getSession(); Query q = session.getNamedQuery("structureFile.byPDBCode"); q.setParameter("pdbCode", pdbCode.toUpperCase()); List lst = q.list(); HibernateSession.closeSession(ownership); if ( lst.size() != 1) return null; return lst.get(0); } public List loadAll(){ Object ownership = HibernateSession.createSession(); Session session = HibernateSession.getSession(); Query q = session.getNamedQuery("header.getAll"); List lst = q.list(); HibernateSession.closeSession(ownership); return lst; } }