/**
 * Copyright (C) 2011  jtalks.org Team
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * Also add information on how to contact you by electronic and paper mail.
 * Creation date: Apr 12, 2011 / 8:05:19 PM
 * The jtalks.org Project
 */
package org.jtalks.poulpe.model.dao.hibernate;

import java.util.List;

import org.jtalks.poulpe.model.dao.SectionDao;
import org.jtalks.poulpe.model.entity.Branch;
import org.jtalks.poulpe.model.entity.Section;

/**
 *  @author tanya birina
 *  @author Dmitriy Sukharev
 */
public class SectionHibernateDao extends AbstractHibernateDao<Section> implements
		SectionDao {

	/**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<Section> getAll() {
        return getSession().createQuery("from Section").list();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isSectionNameExists(String section) {
        return ((Number) getSession()
                .createQuery("select count(*) from Section s where s.name = ?")
                .setString(0, section)
                .uniqueResult()).intValue() != 0;
    }

    /** {@inheritDoc} */
    @Override
    public boolean deleteRecursively(Long id) {
        Section victim = (Section) getSession().load(Section.class, id);
        victim.getBranches().clear();
        final String querySection = "DELETE FROM Section WHERE id = :id";
        return getSession().createQuery(querySection).setLong("id", id).executeUpdate() == 1;
    }

    /** {@inheritDoc} */
    @Override
    public boolean deleteAndMoveBranchesTo(Long id, Long recipientId) {
        Section victim = (Section) getSession().load(Section.class, id);
        Section recipient = (Section) getSession().load(Section.class, recipientId);
        for (Branch branch : victim.getBranches()) {
            branch.setSection(recipient);
            getSession().save(branch);
        }
        recipient.getBranches().addAll(victim.getBranches());
        saveOrUpdate(recipient);
        victim.getBranches().clear();
        final String querySection = "DELETE FROM Section WHERE id = :id";
        return getSession().createQuery(querySection).setLong("id", id).executeUpdate() == 1;
    }

}

@@ -2171,32 +2171,645 @@
 () == 1;%0A    %7D%0A%0A
+//    Section section = new Section();%0A//    section.setName(%22section%22);%0A//    section.setBranches(new ArrayList%3CBranch%3E());%0A//    try %7B%0A//        sectionService.saveSection(section);%0A//    %7D catch (NotUniqueException e) %7B%0A//        e.printStackTrace();%0A//    %7D%0A//    Branch branch = new Branch();%0A//    branch.setName(%22branch%22);%0A//    branch.setSection(section);%0A//    section.getBranches().add(branch);%0A//    try %7B%0A//        branchService.saveBranch(branch);%0A//        sectionService.saveSection(section);    // to save indexes%0A//    %7D catch (NotUniqueException e) %7B%0A//        e.printStackTrace();%0A//    %7D%0A    %0A
     /** %7B@inheri
@@ -3057,24 +3057,254 @@
 cipientId);%0A
+//        final String updateQuery = %22UPDATE Branch SET section = :recipient WHERE section = :victim%22;%0A//        getSession().createQuery(updateQuery).setEntity(%22recipient%22, recipient).setEntity(%22victim%22, victim).executeUpdate();%0A
         for 
@@ -3522,32 +3522,32 @@
 ate(recipient);%0A
-
         victim.g
@@ -3560,32 +3560,412 @@
 ches().clear();%0A
+//        try %7B%0A//            getSession().delete(victim);%0A//            return true;%0A//        %7D catch (HibernateException e) %7B%0A//            // TODO: handle exception%0A//            return false;%0A//        %7D%0A//        recipient.getBranches().addAll(victim.getBranches());%0A//        saveOrUpdate(recipient);%0A//        victim.getBranches().clear();%0A//        saveOrUpdate(victim);%0A
         final St

Revert "POULPE-33 Redundant commented code was eliminated."