class HibernateMenuDAO extends HibernatePersistentObjectDAO<Menu> implements MenuDAO {

	@Override
	public List<Menu> findByGroupId(long groupId) {
		List<Menu> coll = new ArrayList<Menu>();

		// The administrators can see all menus
		if (groupId == Group.GROUPID_ADMIN)
			return findAll();

		try {
			/*
			 * Search for menus that define its own security policies
			 */
			StringBuffer query = new StringBuffer("select distinct(_entity) from Menu _entity  ");
			query.append(" left join _entity.menuGroups as _group ");
			query.append(" where _entity.deleted=0 and _group.groupId =" + groupId);

			coll = (List<Menu>) findByQuery(query.toString(), null, null);

			/*
			 * Now search for all other menus that references the previous ones
			 */
			if (!coll.isEmpty()) {
				StringBuffer query2 = new StringBuffer("select _entity from Menu _entity where _entity.deleted=0 ");
				query2.append(" and _entity.securityRef in (");
				boolean first = true;
				for (Menu menu : coll) {
					if (!first)
						query2.append(",");
					query2.append(Long.toString(menu.getId()));
					first = false;
				}
				query2.append(")");
				List<Menu> coll2 = (List<Menu>) findByQuery(query2.toString(), null, null);
				for (Menu menu : coll2) {
					if (!coll.contains(menu))
						coll.add(menu);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return coll;
	}

	protected  HibernateMenuDAO();

}