/**
* Copyright (C) 2008-2010, Squale Project - http://www.squale.org
*
* This file is part of Squale.
*
* Squale 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 3 of the
* License, or any later version.
*
* Squale 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Squale. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Cr�� le 8 juil. 05
*
* Pour changer le mod�le de ce fichier g�n�r�, allez � :
* Fen�tre>Pr�f�rences>Java>G�n�ration de code>Code et commentaires
*/
package org.squale.squalecommon.daolayer.tag;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.squale.jraf.commons.exception.JrafDaoException;
import org.squale.jraf.provider.persistence.hibernate.AbstractDAOImpl;
import org.squale.jraf.spi.persistence.ISession;
import org.squale.squalecommon.daolayer.DAOMessages;
import org.squale.squalecommon.enterpriselayer.businessobject.tag.TagBO;
import org.squale.squalecommon.enterpriselayer.businessobject.tag.TagCategoryBO;
/**
* @author M400843
*/
public class TagCategoryDAOImpl
extends AbstractDAOImpl
{
/**
* Instance singleton
*/
private static TagCategoryDAOImpl instance = null;
/** log */
private static Log LOG;
/** initialisation du singleton */
static
{
instance = new TagCategoryDAOImpl();
}
/**
* Constructeur prive
*
* @throws JrafDaoException
*/
private TagCategoryDAOImpl()
{
initialize( TagCategoryBO.class );
if ( null == LOG )
{
LOG = LogFactory.getLog( TagCategoryDAOImpl.class );
}
}
/**
* Retourne un singleton du DAO
*
* @return singleton du DAO
*/
public static TagCategoryDAOImpl getInstance()
{
return instance;
}
/**
* R�cup�re les categories de tags commen�ant par le nom pass� en param�tre
*
* @param pSession la session
* @param pTagCategoryName le nom de la cat�gorie de tag que l'on veut r�cup�rer
* @return une collection de Tags poss�dant le m�me d�but de nom que le tag demand�
* @throws JrafDaoException si une erreur � lieu
*/
public Collection findNamedTagCategories( ISession pSession, String pTagCategoryName )
throws JrafDaoException
{
Collection<TagCategoryBO> tagCats = new ArrayList<TagCategoryBO>();
//r�cup�ration des component poss�dant le m�me tag
String whereClause = "where ";
whereClause += getAlias() + ".name like(\'" + pTagCategoryName + "%\') order by " + getAlias() + ".name";
Iterator it = findWhere( pSession, whereClause ).iterator();
while ( it.hasNext() )
{
//pour chaque component renvoy�, on va verifier qu'il correspond bien �
//la m�me classe que la classe demand�e
TagCategoryBO tagCat = (TagCategoryBO) it.next();
tagCats.add( tagCat );
}
return tagCats;
}
/**
* R�cup�re les tags pr�sent en base
*
* @param pSession la session
* @return une collection de Tags poss�dant le m�me d�but de nom que le tag demand�
* @throws JrafDaoException si une erreur � lieu
*/
public Collection findTagCategories( ISession pSession )
throws JrafDaoException
{
Collection<TagCategoryBO> tagCats = new ArrayList<TagCategoryBO>();
//r�cup�ration des component poss�dant le m�me tag
String whereClause = "order by " + getAlias() + ".id";
Iterator it = findWhere( pSession, whereClause ).iterator();
while ( it.hasNext() )
{
//pour chaque component renvoy�, on va verifier qu'il correspond bien �
//la m�me classe que la classe demand�e
TagCategoryBO tagCat = (TagCategoryBO) it.next();
tagCats.add( tagCat );
}
return tagCats;
}
/**
* Creates the wanted tag category
*
* @param pSession the current session
* @param pTagCategoryToCreate the wanted tag that will be inserted in the database
* @return createdTagCategory the tag inserted in the database
* @throws JrafDaoException if an error occurs
*/
public TagCategoryBO createTagCategory( ISession pSession, TagCategoryBO pTagCategoryToCreate )
throws JrafDaoException
{
TagCategoryBO createdTagCategory = null;
// Verification que le nom du tag n'existe pas d�j�
int existantTagCategory = countWhereName( pSession, pTagCategoryToCreate );
if ( 0 == existantTagCategory )
{
super.create( pSession, pTagCategoryToCreate );
createdTagCategory = pTagCategoryToCreate;
}
return createdTagCategory;
}
/**
* Modifies the wanted tag category
*
* @param pSession the current session
* @param pTagCategoryToModify the wanted tag category that will be modified in the database
* @return modifiedTagCategory the tag category modified in the database
* @throws JrafDaoException if an error occurs
*/
public TagCategoryBO modifyTagCategory( ISession pSession, TagCategoryBO pTagCategoryToModify )
throws JrafDaoException
{
TagCategoryBO modifiedTagCategory = null;
super.save( pSession, pTagCategoryToModify );
modifiedTagCategory = pTagCategoryToModify;
return modifiedTagCategory;
}
/**
* deletes one or more TagCategoryBOs from the database with the naves given as a parameter
*
* @param pSession the current session
* @param pNamesToDelete a collection of Strings of the names of the tags to delete
* @return boolean if the number of removals from the database equals the number wanted
* @throws JrafDaoException if an error occurs
*/
public boolean deleteTagCategories( ISession pSession, Collection<String> pNamesToDelete )
throws JrafDaoException
{
String whereClause = "where ";
// porte le m�me nom
boolean first = true;
whereClause += getAlias() + ".name in (";
for ( String tagName : pNamesToDelete )
{
if (!first){
whereClause += ",";
}
whereClause += "'" + tagName + "'";
first = false;
}
whereClause += ")";
int nbremoves = super.removeWhere( pSession, whereClause );
return nbremoves == pNamesToDelete.size();
}
/**
* returns the number of tagCategories with the given name
*
* @param pSession the current session
* @param pTagCategoryBO the tag category to find
* @return a number of tagCategories
* @throws JrafDaoException if an error occurs
*/
public int countWhereName( ISession pSession, TagCategoryBO pTagCategoryBO )
throws JrafDaoException
{
String whereClause = "where ";
// porte le m�me nom
whereClause += getAlias() + ".name = '" + pTagCategoryBO.getName() + "'";
int ret = countWhere( pSession, whereClause ).intValue();
return ret;
}
}