How to check if a page being saved or modified has a certain object type in an event listener

I have written a very simple event listener that is supposed to set user profile documents to hidden at creation or modification due to some business/security concerns we have around user profiles.

The event listener was working just fine in the test bed but on our production server it doesn’t seem to be marking the pages as hidden when an admin creates the user.

Here’s the relevant code I have:
GetEvents:
import org.xwiki.bridge.event.* xcontext.method.output.value = [new DocumentCreatingEvent(), new DocumentUpdatingEvent()]

OnEvent:
if (docSource.getParent() == "xwiki:Main.UserDirectory") { docSource.setHidden(true) }

Now, I’m not sure if what I’m trying to do is supposed to even work with how I’m doing it but I have some other needs to create an event listener that will listen for new documents or modifications to documents that have a particular xclass object associated with them and send emails or do other things such as create a sub-page. So I’m a bit stumped as to how to determine if the doc has an object or not.

For example: the instance provided above I can define a variable like this:
#set($obj = $doc.getObject(‘XWiki.XWikiUsers’))

but how do I then determine if the object is present or not?

Thanks!

If getObject return you something then there is an object.

ok, so… I’m unsure how to do that exactly. would something like
def docSource = xcontext.method.input.get(1) if (docSource.getParent() == "xwiki:Main.UserDirectory" && docSource.getObject('XWiki.XWikiUsers')) { docSource.setHidden(true) }

work?

I know in the .net world checking for null is pretty easy with an if(x!=null) or on some classes there is a x.IsNullOrEmpty() but I’m not exactly familiar enough with velocity or groovy syntax to know this off the top of my head.

OK I was not really understanding what was you issue in practice.

So the Velocity #if have the same behavior with null than it would with false.

#if (docSource.getObject('XWiki.XWikiUsers'))

will indeed tell you if docSource is a user.

That being said I don’t understand why you are checking the document parent.

it was just the hackish way I found to filter things initially, I’ll remove it on once things work. does groovy have the same behavior for checking null?

I think you can do the same in Groovy but it also support != null to have something more precise.