gt:         throw new IllegalArgumentException(errorMessage);
context: /*$Id: UserDataLoaderUtils.java 10945 2008-10-19 19:35:08Z jens $*/
  /*
   ****************************************************************************
   *                                                                          *
   *                   (c) Copyright 2008 ABM-utvikling                       *
   *                                                                          *
   * This program is free software; you can redistribute it and/or modify it  *
   * under the terms of the GNU General Public License as published by the    *
   * Free Software Foundation; either version 2 of the License, or (at your   *
   * option) any later version.                                               *
   *                                                                          *
   * This program 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 General *
   * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
   *                                                                          *
   ****************************************************************************
   */
  
  package no.abmu.user.util;
  
  import java.util.Collection;
  import java.util.HashSet;
  import java.util.Set;
  
  import no.abmu.organisationregister.domain.OrganisationTypeNameConst;
  import no.abmu.organisationregister.domain.OrganisationUnit;
  import no.abmu.user.domain.Role;
  import no.abmu.user.domain.UserRoleNameConst;
  import no.abmu.user.finders.role.RoleFinderSpecification;
  import no.abmu.user.service.UserService;
  import no.abmu.util.test.Assert;
  
  import org.apache.log4j.Logger;
  
  /**
   * This class has helper methods for data loading in
   * UserRegister.
   *
   * @author Jens.Vindvad@abm-utvikling.no
   * @author $Author: jens $
   * @version $Rev: 10945 $
   * @date $Date: 2008-10-19 21:35:08 +0200 (Sun, 19 Oct 2008) $
   * @copyright ABM-Utvikling
   */
  public final class UserDataLoaderUtils {
  
      private static final Logger logger = Logger.getLogger(UserDataLoaderUtils.class);
  
      // Suppress default constructor for noninstantiability.
      private UserDataLoaderUtils() {
          // This constructor will never be invoked.
          // See Item 3 on page 12 in
          // Joshua Bloch: Effective Java: Programming Language Guide, 2001
          // ISBN 0-201-31005-8
      }
      
      
      /**
       * Find and return user role specified by roleName. If a user 
       * role with the specified roleName does not exist it will be created.
       * If more than one role have the specified roleName an
       * IllegalStateExecption will be thrown.
       * 
       * @param userService the UserService for interacting with userRegister.
       * @param roleName the String (roleName) to find or create role for.
       * @return Role - the existing or newly created user role for specified roleName.
       * @throws IllegalStateException if there exist more than one role 
       * with specified roleName.
       */
      public static Role findOrCreateRoleByName(UserService userService, String roleName) {
          Assert.checkRequiredArgument("userService", userService);
          Assert.checkRequiredArgument("roleName", roleName);
  
          RoleFinderSpecification finder = new RoleFinderSpecification(roleName);
          Collection<Role> roles = userService.find(finder);
  
          // No role with specified roleName exist, create one and return this.
          if (roles.size() == 0) {
              Role role = new Role(roleName);
              userService.store(role);
              return role;
          } 
  
          // There exist one role with specified roleName, return this.
          if (roles.size() == 1) {
              return roles.iterator().next();
          }
          
          // If we reach this point there exists more than one role with 
          // specified roleName (roles.size > 1).
          // This should never happen throws an exception.
          String errorMessage = "There exist " + roles.size() + " with roleName='" + roleName + "'"; 
          logger.error(errorMessage);
          throw new IllegalStateException(errorMessage);
      }
      
  
      public static String getMainOrganisationType(OrganisationUnit organisationUnit) {
          Assert.checkRequiredArgument("organisationUnit", organisationUnit);
          
          String organisationTypeName = "";
          if (organisationUnit.hasType(OrganisationTypeNameConst.ARCHIVE)) {
              organisationTypeName = OrganisationTypeNameConst.ARCHIVE;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.FAG_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.FAG_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.REPORTINGMUSEUM)) {
              organisationTypeName = OrganisationTypeNameConst.REPORTINGMUSEUM;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.MUSEUM)) {
              organisationTypeName = OrganisationTypeNameConst.MUSEUM;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.FYLKES_LIBRARY)){
              organisationTypeName = OrganisationTypeNameConst.FYLKES_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KOMMUNE)) {
              organisationTypeName = OrganisationTypeNameConst.KOMMUNE;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.FYLKE)) {
              organisationTypeName = OrganisationTypeNameConst.FYLKE;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.PUBLIC_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.PUBLIC_LIBRARY;
          } else  if (organisationUnit.hasType(OrganisationTypeNameConst.HIGH_SCHOOL_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.HIGH_SCHOOL_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.HOSPITAL_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.HOSPITAL_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.PRISON_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.PRISON_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.MOBILE_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.MOBILE_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.PRIMARY_SCHOOL_LIBRARY)) {
              organisationTypeName = OrganisationTypeNameConst.PRIMARY_SCHOOL_LIBRARY;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_GENERAL_ARTS)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_GENERAL_ARTS;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_VISUAL_ARTS)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_VISUAL_ARTS;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_DRAMATIC_ART)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_DRAMATIC_ART;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_DRAMATIC_ART_70)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_DRAMATIC_ART_70;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_KNUTEPUNKT)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_KNUTEPUNKT;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_MUSIC)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_MUSIC;
          } else if (organisationUnit.hasType(OrganisationTypeNameConst.KKD_MUSIC_70)) {
              organisationTypeName = OrganisationTypeNameConst.KKD_MUSIC_70;
          } else {
              String errorMessage = "Unsupported organisationType for organisationUnit ==> " + organisationUnit;
              logger.error(errorMessage);
              throw new IllegalArgumentException(errorMessage);
          }
  
          return organisationTypeName;
      }
  
      
      /**
       * Return roleNames for specified organisationTypeName. Or throws 
       * IllegalArgumentException if no known organisationTypeName are specified.
       * 
       * @param organisationTypeName
       * @return set of roleNames the supported organisationType should have.
       */
      public static Set<String> getRoleNamesForMainOrganisationType(String organisationTypeName) {
          Assert.checkRequiredArgument("organisationTypeName", organisationTypeName);
  
          Set<String> roleNames = new HashSet<String>();
  
          /* All users should have this */
          roleNames.add(UserRoleNameConst.ROLE_ABMSTATISTIKK_USER);
  
          if (organisationTypeName == OrganisationTypeNameConst.ARCHIVE) {
              roleNames.add(UserRoleNameConst.ROLE_ARCHIVE_USER);
              roleNames.add(UserRoleNameConst.ROLE_ARCHIVE_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.FYLKES_LIBRARY) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FYLKESBIB_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FOLKEBIB_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_GRUNNSKOLE_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_VIDEREGAENDE_SKOLE_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_MOBIL_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_KOMMUNE_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FYLKESKOMMUNE_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.KOMMUNE) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_KOMMUNE_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_GRUNNSKOLE_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.FYLKE) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FYLKESKOMMUNE_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_VIDEREGAENDE_SKOLE_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.PUBLIC_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.FOLKEBIBLIOTEK_SVALBARD) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FOLKEBIB_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_MOBIL_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.FAG_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.NATIONAL_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.UNIVERITY_OF_OSLO 
                  || organisationTypeName == OrganisationTypeNameConst.UNIVERITY_OF_BERGEN 
                  || organisationTypeName == OrganisationTypeNameConst.UNIVERITY_OF_TRONDHEIM 
                  || organisationTypeName == OrganisationTypeNameConst.UNIVERITY_OF_TROMSOE 
                  || organisationTypeName == OrganisationTypeNameConst.SPECIALISED_UNIVERISTY_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.PUBLIC_COLLEGE_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.PRIVATE_COLLEGE_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.PUBLIC_SPECIAL_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.PRIVATE_SPECIAL_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.FAG_BIBLIOTEK_LEVEL_1 
                  || organisationTypeName == OrganisationTypeNameConst.FAG_BIBLIOTEK_LEVEL_2 
                  || organisationTypeName == OrganisationTypeNameConst.FAG_BIBLIOTEK_LEVEL_3) {
              roleNames.add(UserRoleNameConst.ROLE_FAGBIB_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.REPORTINGMUSEUM) {
              roleNames.add(UserRoleNameConst.ROLE_REPORTING_MUSEUM_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.MUSEUM) {
              roleNames.add(UserRoleNameConst.ROLE_MUSEUM_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.HIGH_SCHOOL_LIBRARY) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_VIDEREGAENDE_SKOLE_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.HOSPITAL_LIBRARY) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_PASIENT_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.PRISON_LIBRARY) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_FENGSEL_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.MOBILE_LIBRARY) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_MOBIL_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.PRIMARY_SCHOOL_LIBRARY 
                  || organisationTypeName == OrganisationTypeNameConst.GRUNNSKOLEBIBLIOTEK_SVALBARD) {
              roleNames.add(UserRoleNameConst.ROLE_FOLKEBIB_GRUNNSKOLE_REGISTRATOR);
              return roleNames;
          }
  
          if (organisationTypeName == OrganisationTypeNameConst.KKD_GENERAL_ARTS) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_GENERAL_ARTS_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_GENERAL_ARTS_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_VISUAL_ARTS) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_VISUAL_ARTS_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_VISUAL_ARTS_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_DRAMATIC_ART) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_DRAMATIC_ART_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_DRAMATIC_ART_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_DRAMATIC_ART_70) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_DRAMATIC_ART_70_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_DRAMATIC_ART_70_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_KNUTEPUNKT) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_KNUTEPUNKT_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_KNUTEPUNKT_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_MUSIC) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_MUSIC_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_MUSIC_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
          if (organisationTypeName == OrganisationTypeNameConst.KKD_MUSIC_70) {
              roleNames.add(UserRoleNameConst.ROLE_KKD_MUSIC_70_USER);
              roleNames.add(UserRoleNameConst.ROLE_KKD_MUSIC_70_REGISTRATOR);
              roleNames.add(UserRoleNameConst.ROLE_KKD_FINANCE_APPLICATION_REGISTRATOR);
              return roleNames;
          }
  
          String errorMessage = "Unsupported organisationType '" + organisationTypeName + "'";
          logger.error(errorMessage);
  