gt:         if (childUsers == null)
context: /*
   * Copyright (C) 2005-2013 Alfresco Software Limited.
   *
   * This file is part of Alfresco
   *
   * Alfresco is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Alfresco is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
   */
  package org.alfresco.repo.security.authority.script;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.LinkedHashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.TreeSet;
  
  import org.alfresco.query.PagingResults;
  import org.alfresco.repo.jscript.ScriptNode;
  import org.alfresco.repo.security.authority.AuthorityInfo;
  import org.alfresco.service.ServiceRegistry;
  import org.alfresco.service.cmr.repository.NodeRef;
  import org.alfresco.service.cmr.security.AuthorityService;
  import org.alfresco.service.cmr.security.AuthorityType;
  import org.alfresco.util.ModelUtil;
  import org.alfresco.util.ScriptPagingDetails;
  import org.mozilla.javascript.Scriptable;
  
  /**
   * The Script group is a GROUP authority exposed to the scripting API.
   * It provides access to the properties of the group including the children of the group which may be groups or users.
   * 
   * @author mrogers
   */
  public class ScriptGroup implements Authority, Serializable
  {
      private static final long serialVersionUID = 6073732221341647273L;
      private transient ServiceRegistry serviceRegistry;
      private transient AuthorityService authorityService;
      private ScriptAuthorityType authorityType = ScriptAuthorityType.GROUP;
      private String shortName;
      private String fullName;
      private String displayName;
      private Set<String> childAuthorityNames;
      private NodeRef groupNodeRef;
      private Scriptable scope;
      
      /**
       * New script group
       * @param fullName String
       * @param displayName String
       * @param serviceRegistry ServiceRegistry
       * @param scope Scriptable
       */
      public ScriptGroup(String fullName, String displayName, ServiceRegistry serviceRegistry, Scriptable scope)
      {
         this(fullName, displayName, serviceRegistry, serviceRegistry.getAuthorityService(), scope);
      }
      
      /**
       * New script group
       * @param fullName String
       * @param serviceRegistry ServiceRegistry
       * @param scope Scriptable
       */
      public ScriptGroup(String fullName, ServiceRegistry serviceRegistry, Scriptable scope)
      {
         this(fullName, null, serviceRegistry, serviceRegistry.getAuthorityService(), scope);
      }
      
      /**
       * New script group
       * @param fullName String
       * @param authorityService AuthorityService
       * @deprecated Use {@link #ScriptGroup(String, ServiceRegistry, Scriptable)} instead
       */
      public ScriptGroup(String fullName, AuthorityService authorityService)
      {
         this(fullName, null, null, authorityService, null);
      }
      
      /**
       * New script group
       * @param fullName String
       * @param displayName String
       * @param serviceRegistry ServiceRegistry
       * @param authorityService AuthorityService
       * @param scope Scriptable
       */
      private ScriptGroup(String fullName, String displayName, ServiceRegistry serviceRegistry, AuthorityService authorityService, Scriptable scope)
      {
         this.authorityService = authorityService;
         this.serviceRegistry = serviceRegistry;
         this.fullName = fullName;
         this.scope = scope;
         shortName = authorityService.getShortName(fullName);
         this.displayName = displayName;
      }
      
      /**
       * Delete this group
       */
      public void deleteGroup()
      {
          authorityService.deleteAuthority(fullName);
      }
  	
      public void setAuthorityType(ScriptAuthorityType authorityType) 
      {
          this.authorityType = authorityType;
      }
  
      public ScriptAuthorityType getAuthorityType() 
      {
          return authorityType;
      }
  
      public void setShortName(String shortName) 
      {
          this.shortName = shortName;
      }
  
      /**
       * Get the short name
       */
      public String getShortName() 
      {
          return shortName;
      }
  
      public void setFullName(String fullName) 
      {
          this.fullName = fullName;
      }
  
      /**
       * Get the full internal name, also known
       *  as the Authority Name
       */
      public String getFullName() 
      {
          return fullName;
      }
  
      /**
       * Change the display name for this group.    Need administrator permission to call this method to change a display name.
       * @param displayName String
       */
      public void setDisplayName(String displayName) 
      {
          getDisplayName();
          if (this.displayName != null && !this.displayName.equals(displayName))
          {
              authorityService.setAuthorityDisplayName(fullName, displayName);
          }
          this.displayName = displayName;
      }
  
      public String getDisplayName() 
      {
          if (displayName == null)
          {
              displayName = authorityService.getAuthorityDisplayName(fullName);
          }
          return displayName;
      }
  	
      /**
       * Get all users contained in this group
       * @return ScriptUser[]
       */
      public ScriptUser[] getAllUsers()
      {
          Set<String> children = authorityService.getContainedAuthorities(AuthorityType.USER, fullName, false);
          Set<ScriptUser> users = new LinkedHashSet<ScriptUser>();
          for (String authority : children)
          {
              ScriptUser user = new ScriptUser(authority, null, serviceRegistry, this.scope);
              users.add(user);
          }
          return users.toArray(new ScriptUser[users.size()]);
      }
  	
      /**
       * Get all sub groups (all decendants)
       * @return the descenants of this group
       */
      public ScriptGroup[] getAllGroups()
      {
          Set<String> children = authorityService.getContainedAuthorities(AuthorityType.GROUP, fullName, false);
          Set<ScriptGroup> groups = new LinkedHashSet<ScriptGroup>();
          for (String authority : children)
          {
              ScriptGroup group = new ScriptGroup(authority, null, serviceRegistry, authorityService, this.scope);
              groups.add(group);	
          }
          return groups.toArray(new ScriptGroup[groups.size()]);
      }
  
      /**
       * Get child groups of this group
       */
      private ScriptUser[] childUsers;
  	
      private Set<String> getChildAuthorityNames()
      {
          if (childAuthorityNames == null)
          {
              childAuthorityNames = authorityService.getContainedAuthorities(null, fullName, true);
          }
          return childAuthorityNames;
      }
  
      private Set<String> getChildNamesOfType(AuthorityType type)
      {
          Set<String> authorities = getChildAuthorityNames();
          Set<String> result = new TreeSet<String>();
          for (String authority : authorities)
          {
              if (AuthorityType.getAuthorityType(authority) == type)
              {
                  result.add(authority);
              }
          }
          return result;
      }
  
      /**
       * Get child users of this group
       */
      public ScriptUser[] getChildUsers()
      {
          return getChildUsers(new ScriptPagingDetails(), null);
      }
      
      /**
       * Get child users of this group
       * @param paging Paging object with max number to return, and items to skip
       * @param sortBy What to sort on (authorityName, shortName or displayName)
       */
      public ScriptUser[] getChildUsers(ScriptPagingDetails paging, String sortBy)
      {
  