package net.openl10n.flies.dao;

import java.util.List;

import net.openl10n.flies.common.ContentState;
import net.openl10n.flies.common.LocaleId;
import net.openl10n.flies.model.HDocument;
import net.openl10n.flies.model.HTextFlow;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("textFlowDAO")
@AutoCreate
@Scope(ScopeType.STATELESS)
public class TextFlowDAO extends AbstractDAOImpl<HTextFlow, Long>
{

   public TextFlowDAO()
   {
      super(HTextFlow.class);
   }

   public TextFlowDAO(Session session)
   {
      super(HTextFlow.class, session);
   }

   /**
    * @param document
    * @param id
    * @return
    */
   public HTextFlow getById(HDocument document, String id)
   {
      return (HTextFlow) getSession().createCriteria(HTextFlow.class).add(Restrictions.naturalId().set("resId", id).set("document", document)).setCacheable(true).setComment("TextFlowDAO.getById").uniqueResult();
   }

   @SuppressWarnings("unchecked")
   public List<HTextFlow> findByIdList(List<Long> idList)
   {
      Query query = getSession().createQuery("FROM HTextFlow WHERE id in (:idList)");
      query.setParameterList("idList", idList);
      query.setComment("TextFlowDAO.getByIdList");
      return query.list();
   }

   public HTextFlow getObsoleteById(HDocument document, String id)
   {
      return (HTextFlow) getSession().createCriteria(HTextFlow.class).add(Restrictions.naturalId().set("resId", id).set("document", document)).add(Restrictions.eq("obsolete", true)).setCacheable(true).setComment("TextFlowDAO.getObsoleteById").uniqueResult();
   }

   @SuppressWarnings("unchecked")
   public List<Long> getIdsByTargetState(LocaleId locale, ContentState state)
   {
      Query q = getSession().createQuery("select tft.textFlow.id from HTextFlowTarget tft where tft.locale.localeId=:locale and tft.state=:state");
      q.setParameter("locale", locale);
      q.setParameter("state", state);
      q.setComment("TextFlowDAO.getIdsByTargetState");
      return q.list();
   }

   @SuppressWarnings("unchecked")
   public List<HTextFlow> getNavigationByDocumentId(Long documentId, int offset, boolean reverse)
   {
      Query q;
      if (reverse)
      {
         q = getSession().createQuery("from HTextFlow tf where tf.document.id = :id and tf.pos < :offset order by tf.pos desc");
      }
      else
      {
         q = getSession().createQuery("from HTextFlow tf where tf.document.id = :id and tf.pos  > :offset order by tf.pos");
      }
      q.setParameter("id", documentId);
      q.setParameter("offset", offset);
      q.setComment("TextFlowDAO.getNavigationByDocumentId");
      return q.list();

   }

}

@@ -1,8 +1,989 @@
+/*%0A * Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the%0A * @author tags. See the copyright.txt file in the distribution for a full%0A * listing of individual contributors.%0A * %0A * This is free software; you can redistribute it and/or modify it under the%0A * terms of the GNU Lesser General Public License as published by the Free%0A * Software Foundation; either version 2.1 of the License, or (at your option)%0A * any later version.%0A * %0A * This software is distributed in the hope that it will be useful, but WITHOUT%0A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS%0A * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more%0A * details.%0A * %0A * You should have received a copy of the GNU Lesser General Public License%0A * along with this software; if not, write to the Free Software Foundation,%0A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF%0A * site: http://www.fsf.org.%0A */%0A
 package 
@@ -1208,16 +1208,47 @@
 tFlow;%0A%0A
+import org.hibernate.Criteria;%0A
 import o
@@ -1297,16 +1297,54 @@
 ession;%0A
+import org.hibernate.criterion.Order;%0A
 import o
@@ -3399,191 +3399,226 @@
     
-Query q;%0A      if (reverse)%0A      %7B%0A         q = getSession().createQuery(%22from HTextFlow tf where tf.document.id = :id and tf.pos %3C :offset order by tf.pos desc%22);%0A      %7D%0A      el
+Criteria c = getSession().createCriteria(HTextFlow.class).add(Restrictions.eq(%22document.id%22, documentId)).add(Restrictions.eq(%22obsolete%22, false)).setComment(%22TextFlowDAO.getNavigationByDocumentId%22);%0A%0A      if (rever
 se
+)
 %0A   
@@ -3635,122 +3635,73 @@
     
-q = getSession().createQuery(%22from HTextFlow tf where tf.document.id = :id and tf.pos  %3E :offset order by tf.
+c.add(Restrictions.lt(%22pos%22, offset)).addOrder(Order.desc(%22
 pos%22)
+)
 ;%0A  
@@ -3716,142 +3716,104 @@
     
-q.setParameter(%22id%22, documentId);%0A      q.setParameter(%22offset%22, offset);%0A      q.setComment(%22TextFlowDAO.getNavigationByDocumentId%22);
+else%0A      %7B%0A         c.add(Restrictions.gt(%22pos%22, offset)).addOrder(Order.asc(%22pos%22));%0A      %7D%0A
 %0A   
@@ -3814,33 +3814,33 @@
 %7D%0A%0A      return 
-q
+c
 .list();%0A%0A   %7D%0A%0A

fix the bug with obsolete textflow