Check if email address is already in use



  • Just found this in some old code. I remember first discovering this and removing it then, came across it again the other day and thought it was worth posting up here!

    The purpose of this method is for user creation - you can't create two users with the same email address. One of the original developers of our product came up with this:

    public String[] getUserEmailAddresses(LibrarySession l)
    	{
    		String []useremails=null;
    		DirectoryUser u=null;
    		try
    		{
    			Collection usercoll=l.getDirectoryUserCollection();
    			DirectoryUser[] users=(DirectoryUser[])usercoll.getItems();
    			useremails=new String[users.length];
    
    			for(int a=0;a<users.length;a++)
    			{
    				u=users[a];
    				String search=EmailUserProfile.DIRECTORYUSER_ATTRIBUTE+"='"+u.getId()+"'";
    				Selector sel=new Selector(l);
    				sel.setSearchClassname(EmailUserProfile.CLASS_NAME);
    				sel.setSearchSelection(search);
    				LibraryObject[] lo=sel.getItems();
    				if(lo==null||lo.length==0)
    				{
    					useremails[a]="NO EMAIL ADDRESS";
    				}
    				else
    				{
    					EmailUserProfile emup=(EmailUserProfile)lo[0];
    					String addy=emup.getEmailAddress();
    					useremails[a]=addy;
    				}
    			}
    		}
    		catch(Exception e)
    		{
    			(log error etc...)
    		}
    		finally
    		{
    			return useremails;
    		}
    
    	}
    </pre>
    

    The results of this function were then passed into a JSP page, which looped through the array to check if the email address already existed. Not surprisingly, when the system got a fair number of users, this was quite a hold up!

    Why they didn't think it easier to take the email address as a parameter and then search directly for it rather than getting the email for every user on the system I don't know.

    A more minor wtf is that there's a return statement in a finally block - this generates a compiler warning, so I've got warnings turned off at the moment for this project! For some reason, they seemed to do this quite a lot. Ah well.



  • Ah, good ol' community server - seems to have stripped out "a++)" from the end of the for loop (who uses 'a' as a for loop variable anyway? nothing wrong with it per se, just unusual). And the little angel smiley is, in fact, [ a ] (without the spaces).



  • @PhillS said:

    And the little angel smiley is, in fact, [ a ] (without the spaces).


    That's why I hate automatic smiley graphics. Just because forums, email, and IM deal with text doesn't mean it's natural human language peppered with smileys. Leave funny character combinations alone and let the reader interpret them in context.



  • @PhillS said:

    Ah, good ol' community server - seems to have stripped out "a++)" from the end of the for loop...

    I can't help but feel it's a great improvement.



  • WTF is up with people who BSD formatting, but then remove ALL spaces possible from each individual line of code? It drives me completely nuts. Love your whitespace or hate it, but please, at least be consistent.


Log in to reply