class HibernateSystemMessageDAO extends HibernatePersistentObjectDAO<SystemMessage> implements SystemMessageDAO {

	@Override
	public List<SystemMessage> findByRecipient(String recipient, int type, Integer read) {
		String sql = "select ld_lastmodified, ld_author, ld_messagetext, ld_subject, ld_sentdate, ld_datescope, ld_prio, ld_confirmation, ld_lastnotified, ld_status, ld_trials, ld_type, ld_id, ld_html, ld_author, ld_tenantid "
				+ " from ld_systemmessage where ld_deleted = 0 and ld_type = " + type
				+ " and exists (select Q.ld_messageid from ld_recipient Q where Q.ld_name = '"
				+ SqlUtil.doubleQuotes(recipient) + "' and Q.ld_messageid=ld_id)";
		if (read != null)
			sql = sql + " and " + (read == 1 ? "exists" : "not exists")
					+ "  (select R.ld_messageid from ld_recipient R where R.ld_name = '"
					+ SqlUtil.doubleQuotes(recipient) + "' and R.ld_read=1 and R.ld_messageid=ld_id)";
		sql = sql + " order by ld_sentdate desc";

		List<SystemMessage> messages;
		try {
			messages = (List<SystemMessage>) query(sql, null, new SystemMessageMapper(), null);
			return messages;
		} catch (PersistenceException e) {
			log.error(e.getMessage(), e);
			return new ArrayList<SystemMessage>();
		}
	}

	public  HibernateSystemMessageDAO();

}