Fórumok

Finding users by birthday

thumbnail
Adam Victor Nazareth Brandizzi, módosítva 13 év-val korábban

Finding users by birthday

Junior Member Bejegyzések: 67 Csatlakozás dátuma: 2010.04.30. Legújabb bejegyzések
Hello, all!

How could I find the users who have a birthday in a specific day in a portlet created via plugin SDK?

I tried to use custom queries as described here, but it does not work for native Liferay entities such as users and contacts. (Actually, I asked about this problem here.)

So, what other approach would you all suggest?

Thanks in advance!
thumbnail
jelmer kuperus, módosítva 13 év-val korábban

RE: Finding users by birthday

Liferay Legend Bejegyzések: 1191 Csatlakozás dátuma: 2010.03.10. Legújabb bejegyzések
You can do that with the DynamicQuery api
thumbnail
Adam Victor Nazareth Brandizzi, módosítva 13 év-val korábban

RE: Finding users by birthday

Junior Member Bejegyzések: 67 Csatlakozás dátuma: 2010.04.30. Legújabb bejegyzések
We thought it too, but there are two problems. Firstly, the birthday is stored in the Contact entity, so we should find all contacts by birthday and then search users by the contact ids. It is surely possible but we don't find it very elegant...

However, the biggest problem is to find a user whose birthday is in any year. How could I create a dynamic query for finding a user whose birthday is at such day of such month, but ignores the year?

Thank you for your answer! I hope you all can help me again emoticon
thumbnail
jelmer kuperus, módosítva 13 év-val korábban

RE: Finding users by birthday (Válasz)

Liferay Legend Bejegyzések: 1191 Csatlakozás dátuma: 2010.03.10. Legújabb bejegyzések
Hmmm... the first problem would be relatively easy to solve because you can use subselects with dynamic queries like i for instance show in this post :

http://www.liferay.com/community/forums/-/message_boards/message/6450100

The second problem... not that easy. I actually don't see how you could achieve this with the dynamic query api, since you would need to use sql functions. And i dont believe those are supported

You should be able to do something like this

SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
try {
    session = sessionFactory.openSession();

    SQLQuery query = session.createSQLQuery("select {User_.*} from User_ where userId = ? ");
    query.addEntity("User_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));

    QueryPos queryPos = QueryPos.getInstance(query);
    queryPos.add(11548);

    return (List<user>) QueryUtil.list(query, getDialect(), 0, QueryUtil.ALL_POS);

} finally {
    if (session != null) {
        sessionFactory.closeSession(session);
    }
}

</user>


But you would have to call this code from a transactional servicebuilder method for it to work


Another option would be to directly go to the database, bypassing liferay by grabbing InfrastructureUtil.getDataSource()
thumbnail
Adam Victor Nazareth Brandizzi, módosítva 13 év-val korábban

RE: Finding users by birthday

Junior Member Bejegyzések: 67 Csatlakozás dátuma: 2010.04.30. Legújabb bejegyzések
jelmer kuperus:
Hmmm... the first problem would be relatively easy to solve because you can use subselects with dynamic queries like i for instance show in this post :

http://www.liferay.com/community/forums/-/message_boards/message/6450100


Actually, it will not help that much in this case given the second problem... On the other hand, we implemented a finder for retrieving only the user ids of the contacts and then used it for retrieve the users with the "in" operator.

jelmer kuperus:
The second problem... not that easy. I actually don't see how you could achieve this with the dynamic query api, since you would need to use sql functions. And i dont believe those are supported

You should be able to do something like this


Jelmer, YOU ARE A GENIUS! That is exactly what I was looking for. At first we did it:


Session session = null;
	try {
		session = openSession();


but now I tried to do what you suggest:

SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
try {
    session = sessionFactory.openSession();


and it worked flawlessly (in my example, not in the real application). Thank you! We will use it!


jelmer kuperus:
Another option would be to directly go to the database, bypassing liferay by grabbing InfrastructureUtil.getDataSource()


That seems pretty painful... and pretty unnecessary too, fortunately emoticon

Thank you again!
Nicholas Tenczar, módosítva 13 év-val korábban

RE: Finding users by birthday

Junior Member Bejegyzések: 53 Csatlakozás dátuma: 2010.07.15. Legújabb bejegyzések
Take a look at the CustomSQLParam class. This class takes an sql string and a value and allows you to create new WHERE/JOIN clauses in Liferay defined CustomSQL queries. For instance you could do something like this.

INNER JOIN
    Contact_ ON
    User_.userId = Contact_.userId
WHERE
    Contact_.birthday = ?


The only problem that I foresee with this method is that the process() method of CustomSQLParam only recognizes Long, Long[], String, and String[]. My use case required an Object[] with String and Long values, so I subclassed CustomSQLParam and overrode the process() method.

Take a look at UserFinderImpl's getWhere(), getJoin(), and setJoin() methods to see how the CustomSQLParam is used to create larger queries. I could probably write a full blog post to describe how CustomSQLParams work, so if something is unclear let me know and I can clarify any issues.
thumbnail
Adam Victor Nazareth Brandizzi, módosítva 13 év-val korábban

RE: Finding users by birthday

Junior Member Bejegyzések: 67 Csatlakozás dátuma: 2010.04.30. Legújabb bejegyzések
Hi, Nicholas.

I found the CustomSQLParam a bit complex for understanding... but actually I just take a brief look at it. Since the problem is (apparently) solved, probably I will not use it for now. However, I am very curious about it, so I will study it anyway. Also, if you write this blog post, let us know, ok? emoticon

Thanks!
dola dola, módosítva 11 év-val korábban

RE: Finding users by birthday

New Member Bejegyzések: 7 Csatlakozás dátuma: 2011.12.29. Legújabb bejegyzések
Hi ,
can i get all birthdays of all users into new portlet ?...

thanks emoticon