/* * ContentTypeDAOImpl.java * * Created on March 5, 2005, 11:57 AM */ package desktopbeautifier.server.dao.impl; import desktopbeautifier.server.hibernate.HibernateHelper; import desktopbeautifier.server.domain.ContentType; import desktopbeautifier.server.domain.ServerImage; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.hibernate.Query; import org.hibernate.Session; /** * ContentType data access object. * * @author Gili Tzabari */ public class ContentTypeDAOImpl implements desktopbeautifier.dao.ContentTypeDAO { private static ContentTypeDAOImpl instance; private transient HibernateHelper hibernate = HibernateHelper.getInstance(); /** * Creates a new ContentTypeDAOImpl. */ private ContentTypeDAOImpl() {} /** * Returns the singleton instance. */ public static ContentTypeDAOImpl getInstance() { if (instance==null) instance = new ContentTypeDAOImpl(); return instance; } public void removeOrphans() { Session session = hibernate.getSession(); Query getOrphans = session.createQuery("from " + ContentType.class.getName() + " type where type.id not in (select image.contentType from " + ServerImage.class.getName() + " image)"); Iterator orphans = getOrphans.iterate(); while (orphans.hasNext()) session.delete(orphans.next()); } public ContentType getContentType(String type, String subType) { Session session = hibernate.getSession(); assert(session!=null): "Session may not be null"; Query getCanonicalInstance = session.createQuery("from " + ContentType.class.getName() + " where type=:type and subType=:subType"); getCanonicalInstance.setString("type", type); getCanonicalInstance.setString("subType", subType); getCanonicalInstance.setMaxResults(1); ContentType result = (ContentType) getCanonicalInstance.uniqueResult(); if (result!=null) return result; else return null; } public ContentType getContentType(String value) { ContentType temp = new ContentType(); temp.setValue(value); return getContentType(temp.getType(), temp.getSubType()); } /** * Deletes the specified object. */ public void delete(ContentType contentType) { Session session = hibernate.getSession(); session.delete(contentType); } public ContentType save(ContentType contentType) { Session session = hibernate.getSession(); ContentType result = (ContentType) session.merge(contentType); // Transfer associations to new instance for (ServerImage image: contentType.getImages()) result.add(image); contentType.getImages().clear(); return result; } /** * Returns all ContentTypes. */ public Set list() { Session session = hibernate.getSession(); Query getContentTypes = session.createQuery("from " + ContentType.class.getName()); return new HashSet(getContentTypes.list()); } }