Skip to content

LUT-29906 : Optimization of sub-entities ids of an entity retrieval #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import fr.paris.lutece.portal.service.plugin.Plugin;

import java.util.List;
import java.util.Set;

/**
*
Expand Down Expand Up @@ -158,6 +159,17 @@ public interface IUnitDAO
*/
List<Unit> getSubUnits( int nIdUnit, Plugin plugin );

/**
* Retrieve Set of all children units id
*
* @param nIdUnit
* the id unit
* @param plugin
* the plugin
* @return Set of all children unit id
*/
Set<Integer> getAllSubUnitsId( int nIdUnit, Plugin plugin );

/**
* Remove a unit
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import fr.paris.lutece.util.sql.DAOUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
*
Expand Down Expand Up @@ -70,7 +72,17 @@ public class UnitDAO implements IUnitDAO
+ " FROM unittree_unit WHERE id_unit NOT IN(SELECT id_parent FROM unittree_unit) ";
private static final String SQL_QUERY_SELECT_DIRECT_CHILDREN = "SELECT id_unit, id_parent, code, label, description "
+ " FROM unittree_unit WHERE id_parent = ?";

private static final String SQL_QUERY_SELECT_ALL_CHILDREN = "WITH RECURSIVE unittree (id_unit) AS ("
+ " SELECT id_unit"
+ " FROM unittree_unit "
+ " WHERE id_unit = ?"
+ " UNION ALL "
+ " SELECT t.id_unit"
+ " FROM unittree_unit t"
+ " JOIN unittree"
+ " ON t.id_parent = unittree.id_unit"
+ " ) SELECT id_unit FROM unittree WHERE id_unit != ?";

// Table unittree_unit_user
private static final String SQL_QUERY_ADD_USER_TO_UNIT = " INSERT INTO unittree_unit_user ( id_unit, id_user ) VALUES ( ?, ? ) ";
private static final String SQL_QUERY_SELECT_IDS_USER = " SELECT id_user FROM unittree_unit_user WHERE id_unit = ? ";
Expand Down Expand Up @@ -241,6 +253,27 @@ public List<Unit> getSubUnits( int nIdUnit, Plugin plugin )
return listUnits;
}

/**
* {@inheritDoc}
*/
public Set<Integer> getAllSubUnitsId( int nIdUnit, Plugin plugin )
{
Set<Integer> idsSet = new HashSet<Integer>( );
try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_CHILDREN, plugin ) )
{
daoUtil.setInt( 1, nIdUnit );
daoUtil.setInt( 2, nIdUnit );
daoUtil.executeQuery( );

while ( daoUtil.next( ) )
{
idsSet.add( daoUtil.getInt( 1 ) );
}
}

return idsSet;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,7 @@ public static List<Unit> getDirectSubUnits( int nIdUnit )
*/
public static Set<Integer> getAllSubUnitsId( int nIdUnit )
{
Set<Integer> setResult = new HashSet<>( );
List<Unit> listUnits = _dao.getSubUnits( nIdUnit, _plugin );
for ( Unit unit : listUnits )
{
setResult.add( unit.getIdUnit( ) );
setResult.addAll( getAllSubUnitsId( unit.getIdUnit( ) ) );
}
return setResult;
return _dao.getAllSubUnitsId( nIdUnit, _plugin );
}

/**
Expand Down