Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.guvnor.structure.backend.organizationalunit;
  
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
 
 import  org.guvnor.structure.organizationalunit.NewOrganizationalUnitEvent;
 import  org.guvnor.structure.organizationalunit.OrganizationalUnit;
 import  org.guvnor.structure.organizationalunit.OrganizationalUnitService;
 import  org.guvnor.structure.organizationalunit.RemoveOrganizationalUnitEvent;
 import  org.guvnor.structure.organizationalunit.RepoAddedToOrganizationaUnitEvent;
 import  org.guvnor.structure.organizationalunit.RepoRemovedFromOrganizationalUnitEvent;
 import  org.guvnor.structure.repositories.Repository;
 import  org.guvnor.structure.repositories.RepositoryUpdatedEvent;
 import  org.guvnor.structure.server.config.ConfigGroup;
 import  org.guvnor.structure.server.config.ConfigItem;
 import  org.guvnor.structure.server.config.ConfigType;
 import  org.guvnor.structure.server.config.ConfigurationFactory;
 import  org.guvnor.structure.server.config.ConfigurationService;
 import  org.guvnor.structure.server.organizationalunit.OrganizationalUnitFactory;
 
 public class OrganizationalUnitServiceImpl implements OrganizationalUnitService {
 
     @Inject
     private ConfigurationService configurationService;
 
     @Inject
     private ConfigurationFactory configurationFactory;
 
     @Inject
     private OrganizationalUnitFactory organizationalUnitFactory;
 
     @Inject
     private Event<NewOrganizationalUnitEvent> newOrganizationalUnitEvent;
 
     @Inject
     private Event<RemoveOrganizationalUnitEvent> removeOrganizationalUnitEvent;
 
     @Inject
     private Event<RepoAddedToOrganizationaUnitEvent> repoAddedToOrgUnitEvent;
 
     @Inject
     private Event<RepoRemovedFromOrganizationalUnitEvent> repoRemovedFromOrgUnitEvent;
 
     private Map<String, OrganizationalUnit> registeredOrganizationalUnits = new HashMap<String, OrganizationalUnit>();
 
     @PostConstruct
     public void loadOrganizationalUnits() {
         Collection<ConfigGroup> groups = .getConfiguration( ConfigType.ORGANIZATIONAL_UNIT );
         if ( groups != null ) {
             for ( ConfigGroup groupConfig : groups ) {
                 OrganizationalUnit ou = .newOrganizationalUnit( groupConfig );
                 .put( ou.getName(),
                                                    ou );
             }
         }
     }
 
     @Override
     public OrganizationalUnit getOrganizationalUnitfinal String name ) {
         return .get( name );
     }
 
     @Override
     public Collection<OrganizationalUnit> getOrganizationalUnits() {
         return new ArrayList<OrganizationalUnit>( .values() );
     }
 
     @Override
     public OrganizationalUnit createOrganizationalUnitfinal String name,
                                                         final String owner ) {
         final ConfigGroup groupConfig = .newConfigGroup( ConfigType.ORGANIZATIONAL_UNIT,
                                                                              name,
                                                                              "" );
         groupConfig.addConfigItem( .newConfigItem( "owner",
                                                                        owner ) );
         groupConfig.addConfigItem( .newConfigItem( "repositories",
                                                                        new ArrayList<String>() ) );
         groupConfig.addConfigItem( .newConfigItem( "security:roles",
                                                                        new ArrayList<String>() ) );
         .addConfiguration( groupConfig );
 
         final OrganizationalUnit newOrganizationalUnit = .newOrganizationalUnit( groupConfig );
         .put( newOrganizationalUnit.getName(),
                                            newOrganizationalUnit );
 
         .fire( new NewOrganizationalUnitEvent( newOrganizationalUnit ) );
        return newOrganizationalUnit;
    }
    @Override
    public OrganizationalUnit createOrganizationalUnitfinal String name,
                                                        final String owner,
                                                        final Collection<Repository> repositories ) {
        final ConfigGroup groupConfig = .newConfigGroup( ConfigType.ORGANIZATIONAL_UNIT,
                                                                             name,
                                                                             "" );
        groupConfig.addConfigItem( .newConfigItem( "owner",
                                                                       owner ) );
        groupConfig.addConfigItem( .newConfigItem( "repositories",
                                                                       getRepositoryAliasesrepositories ) ) );
        groupConfig.addConfigItem( .newConfigItem( "security:roles",
                                                                       new ArrayList<String>() ) );
        .addConfiguration( groupConfig );
        final OrganizationalUnit newOrganizationalUnit = .newOrganizationalUnit( groupConfig );
        .put( newOrganizationalUnit.getName(),
                                           newOrganizationalUnit );
        .fire( new NewOrganizationalUnitEvent( newOrganizationalUnit ) );
        return newOrganizationalUnit;
    }
    private List<StringgetRepositoryAliasesfinal Collection<Repository> repositories ) {
        final List<StringrepositoryList = new ArrayList<String>();
        for ( Repository repo : repositories ) {
            repositoryList.addrepo.getAlias() );
        }
        return repositoryList;
    }
    @Override
    public void updateOrganizationalUnitOwnerfinal String name,
                                               final String owner ) {
        final ConfigGroup thisGroupConfig = findGroupConfigname );
        if ( thisGroupConfig != null ) {
            thisGroupConfig.setConfigItem( .newConfigItem( "owner",
                                                                               owner ) );
            .updateConfiguration( thisGroupConfig );
            final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
            .put( updatedOrganizationalUnit.getName(),
                                               updatedOrganizationalUnit );
        } else {
            throw new IllegalArgumentException"OrganizationalUnit " + name + " not found" );
        }
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    @Override
    public void addRepositoryfinal OrganizationalUnit organizationalUnit,
                               final Repository repository ) {
        final ConfigGroup thisGroupConfig = findGroupConfigorganizationalUnit.getName() );
        if ( thisGroupConfig != null ) {
            ConfigItem<Listrepositories = thisGroupConfig.getConfigItem( "repositories" );
            repositories.getValue().add( repository.getAlias() );
            .updateConfiguration( thisGroupConfig );
            final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
            .put( updatedOrganizationalUnit.getName(),
                                               updatedOrganizationalUnit );
            .fire(new RepoAddedToOrganizationaUnitEvent(organizationalUnitrepository));
        } else {
            throw new IllegalArgumentException"OrganizationalUnit " + organizationalUnit.getName() + " not found" );
        }
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    @Override
    public void removeRepositoryfinal OrganizationalUnit organizationalUnit,
                                  final Repository repository ) {
        final ConfigGroup thisGroupConfig = findGroupConfigorganizationalUnit.getName() );
        if ( thisGroupConfig != null ) {
            final ConfigItem<Listrepositories = thisGroupConfig.getConfigItem( "repositories" );
            repositories.getValue().remove( repository.getAlias() );
            .updateConfiguration( thisGroupConfig );
            final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
            .put( updatedOrganizationalUnit.getName(),
                                               updatedOrganizationalUnit );
            .fire(new RepoRemovedFromOrganizationalUnitEvent(organizationalUnitrepository));
        } else {
            throw new IllegalArgumentException"OrganizationalUnit " + organizationalUnit.getName() + " not found" );
        }
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    @Override
    public void addRolefinal OrganizationalUnit organizationalUnit,
                         final String role ) {
        final ConfigGroup thisGroupConfig = findGroupConfigorganizationalUnit.getName() );
        if ( thisGroupConfig != null ) {
            final ConfigItem<Listroles = thisGroupConfig.getConfigItem( "security:roles" );
            roles.getValue().add( role );
            .updateConfiguration( thisGroupConfig );
            final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
            .put( updatedOrganizationalUnit.getName(),
                                               updatedOrganizationalUnit );
        } else {
            throw new IllegalArgumentException"OrganizationalUnit " + organizationalUnit.getName() + " not found" );
        }
    }
    @SuppressWarnings({ "unchecked""rawtypes" })
    @Override
    public void removeRolefinal OrganizationalUnit organizationalUnit,
                            final String role ) {
        final ConfigGroup thisGroupConfig = findGroupConfigorganizationalUnit.getName() );
        if ( thisGroupConfig != null ) {
            final ConfigItem<Listroles = thisGroupConfig.getConfigItem( "security:roles" );
            roles.getValue().remove( role );
            .updateConfiguration( thisGroupConfig );
            final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
            .put( updatedOrganizationalUnit.getName(),
                                               updatedOrganizationalUnit );
        } else {
            throw new IllegalArgumentException"OrganizationalUnit " + organizationalUnit.getName() + " not found" );
        }
    }
    protected ConfigGroup findGroupConfigfinal String name ) {
        final Collection<ConfigGroup> groups = .getConfiguration( ConfigType.ORGANIZATIONAL_UNIT );
        if ( groups != null ) {
            for ( ConfigGroup groupConfig : groups ) {
                if ( groupConfig.getName().equals( name ) ) {
                    return groupConfig;
                }
            }
        }
        return null;
    }
    @Override
    public void removeOrganizationalUnitString groupName ) {
        final ConfigGroup thisGroupConfig = findGroupConfiggroupName );
        if ( thisGroupConfig != null ) {
            .removeConfiguration( thisGroupConfig );
            final OrganizationalUnit ou = .remove( groupName );
            .fire( new RemoveOrganizationalUnitEvent( ou ) );
        }
    }
    public void updateRegisteredOU( @Observes @OrgUnit SystemRepositoryChangedEvent changedEvent ) {
        .clear();
        loadOrganizationalUnits();
    }
    // refresh org unit in case repository changed otherwise it will have outdated information
    public void updateRegisteredOUonRepoChange( @Observes RepositoryUpdatedEvent changedEvent ) {
        .clear();
        loadOrganizationalUnits();
    }
New to GrepCode? Check out our FAQ X