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.config.SystemRepositoryChangedEvent;
 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.organizationalunit.UpdatedOrganizationalUnitEvent;
 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;
 import  org.uberfire.rpc.SessionInfo;
 
 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;
 
     @Inject
     private Event<UpdatedOrganizationalUnitEvent> updatedOrganizationalUnitEvent;
 
     private Map<String, OrganizationalUnit> registeredOrganizationalUnits = new HashMap<String, OrganizationalUnit>();
 
     private
     @Inject
     SessionInfo sessionInfo;
 
     @PostConstruct
     public void loadOrganizationalUnits() {
         Collection<ConfigGroup> groups = .getConfiguration( ConfigType.ORGANIZATIONAL_UNIT );
         if ( groups != null ) {
             for ( ConfigGroup groupConfig : groups ) {
                 // Make sure existing Organizational Units are correctly initialized with a default group id.
                 String ouName = groupConfig.getName();
                 String defaultGroupId = groupConfig.getConfigItemValue( "defaultGroupId" );
                 if ( defaultGroupId == null || defaultGroupId.trim().isEmpty() ) {
                     groupConfig.setConfigItem( .newConfigItem( "defaultGroupId"getSanitizedDefaultGroupIdouName ) ) );
                     .updateConfiguration( groupConfig );
                 }
 
                 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 String defaultGroupId ) {
        OrganizationalUnit newOrganizationalUnit = null;
        try {
            .startBatch();
            final ConfigGroup groupConfig = .newConfigGroup( ConfigType.ORGANIZATIONAL_UNIT,
                                                                                 name,
                                                                                 "" );
            groupConfig.addConfigItem( .newConfigItem( "owner",
                                                                           owner ) );
            String _defaultGroupId = defaultGroupId == null || defaultGroupId.trim().isEmpty() ? getSanitizedDefaultGroupIdname ) : defaultGroupId;
            groupConfig.addConfigItem( .newConfigItem( "defaultGroupId",
                                                                           _defaultGroupId ) );
            groupConfig.addConfigItem( .newConfigItem( "repositories",
                                                                           new ArrayList<String>() ) );
            groupConfig.addConfigItem( .newConfigItem( "security:roles",
                                                                           new ArrayList<String>() ) );
            .addConfiguration( groupConfig );
            newOrganizationalUnit = .newOrganizationalUnit( groupConfig );
            .put( newOrganizationalUnit.getName(),
                                               newOrganizationalUnit );
            return newOrganizationalUnit;
        } finally {
            .endBatch();
            if(newOrganizationalUnit != null){
                .fire(new NewOrganizationalUnitEvent(newOrganizationalUnitgetUserInfo()));
            }
        }
    }
    @Override
    public OrganizationalUnit createOrganizationalUnitfinal String name,
                                                        final String owner,
                                                        final String defaultGroupId,
                                                        final Collection<Repository> repositories ) {
        OrganizationalUnit newOrganizationalUnit = null;
        try {
            .startBatch();
            final ConfigGroup groupConfig = .newConfigGroup( ConfigType.ORGANIZATIONAL_UNIT,
                                                                                 name,
                                                                                 "" );
            groupConfig.addConfigItem( .newConfigItem( "owner",
                                                                           owner ) );
            String _defaultGroupId = defaultGroupId == null || defaultGroupId.trim().isEmpty() ? getSanitizedDefaultGroupIdname ) : defaultGroupId;
            groupConfig.addConfigItem( .newConfigItem( "defaultGroupId",
                                                                           _defaultGroupId ) );
            groupConfig.addConfigItem( .newConfigItem( "repositories",
                                                                           getRepositoryAliasesrepositories ) ) );
            groupConfig.addConfigItem( .newConfigItem( "security:roles",
                                                                           new ArrayList<String>() ) );
            .addConfiguration( groupConfig );
            newOrganizationalUnit = .newOrganizationalUnit( groupConfig );
            .put( newOrganizationalUnit.getName(),
                                               newOrganizationalUnit );
            return newOrganizationalUnit;
        } finally {
            .endBatch();
            if (newOrganizationalUnit != null) {
                .fire(new NewOrganizationalUnitEvent(newOrganizationalUnitgetUserInfo()));
            }
        }
    }
    private List<StringgetRepositoryAliasesfinal Collection<Repository> repositories ) {
        final List<StringrepositoryList = new ArrayList<String>();
        for ( Repository repo : repositories ) {
            repositoryList.addrepo.getAlias() );
        }
        return repositoryList;
    }
    @Override
    public OrganizationalUnit updateOrganizationalUnitfinal String name,
                                                        final String owner,
                                                        final String defaultGroupId ) {
        final ConfigGroup thisGroupConfig = findGroupConfigname );
        if ( thisGroupConfig != null ) {
            OrganizationalUnit updatedOrganizationalUnit = null;
            try {
                .startBatch();
                thisGroupConfig.setConfigItem( .newConfigItem( "owner",
                                                                                   owner ) );
                // As per loadOrganizationalUnits(), all Organizational Units should have the default group id value set
                String _defaultGroupId = defaultGroupId == null || defaultGroupId.trim().isEmpty() ?
                        thisGroupConfig.getConfigItemValue( "defaultGroupId" ) : defaultGroupId;
                thisGroupConfig.setConfigItem( .newConfigItem( "defaultGroupId",
                                                                                   _defaultGroupId ) );
                .updateConfiguration( thisGroupConfig );
                updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
                .put( updatedOrganizationalUnit.getName(),
                                                   updatedOrganizationalUnit );
                return updatedOrganizationalUnit;
            } finally {
                .endBatch();
                if(updatedOrganizationalUnit != null){
                    .fire(new UpdatedOrganizationalUnitEvent(updatedOrganizationalUnitgetUserInfo()));
                }
            }
        } 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 ) {
            try {
                .startBatch();
                ConfigItem<Listrepositories = thisGroupConfig.getConfigItem( "repositories" );
                repositories.getValue().add( repository.getAlias() );
                .updateConfiguration( thisGroupConfig );
                final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
                .put( updatedOrganizationalUnit.getName(),
                                                   updatedOrganizationalUnit );
            } finally {
                .endBatch();
                .fire(new RepoAddedToOrganizationaUnitEvent(organizationalUnitrepositorygetUserInfo()));
            }
        } 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 ) {
            try {
                .startBatch();
                final ConfigItem<Listrepositories = thisGroupConfig.getConfigItem( "repositories" );
                repositories.getValue().remove( repository.getAlias() );
                .updateConfiguration( thisGroupConfig );
                final OrganizationalUnit updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
                .put( updatedOrganizationalUnit.getName(),
                                                   updatedOrganizationalUnit );
            } finally {
                .endBatch();
                .fire(new RepoRemovedFromOrganizationalUnitEvent(organizationalUnitrepositorygetUserInfo()));
            }
        } 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 ) {
            OrganizationalUnit updatedOrganizationalUnit = null;
            try {
                .startBatch();
                final ConfigItem<Listroles = thisGroupConfig.getConfigItem( "security:roles" );
                roles.getValue().add( role );
                .updateConfiguration( thisGroupConfig );
                updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
                .put( updatedOrganizationalUnit.getName(),
                                                   updatedOrganizationalUnit );
            } finally {
                .endBatch();
                if (updatedOrganizationalUnit != null) {
                    .fire(new UpdatedOrganizationalUnitEvent(updatedOrganizationalUnitgetUserInfo()));
                }
            }
        } 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 ) {
            OrganizationalUnit updatedOrganizationalUnit = null;
            try {
                .startBatch();
                final ConfigItem<Listroles = thisGroupConfig.getConfigItem( "security:roles" );
                roles.getValue().remove( role );
                .updateConfiguration( thisGroupConfig );
                updatedOrganizationalUnit = .newOrganizationalUnit( thisGroupConfig );
                .put( updatedOrganizationalUnit.getName(),
                                                   updatedOrganizationalUnit );
            } finally {
                .endBatch();
                if (updatedOrganizationalUnit != null) {
                    .fire(new UpdatedOrganizationalUnitEvent(updatedOrganizationalUnitgetUserInfo()));
                }
            }
        } 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 ) {
            OrganizationalUnit ou = null;
            try {
                .startBatch();
                .removeConfiguration( thisGroupConfig );
                ou = .remove( groupName );
            } finally {
                .endBatch();
                ifou != null ){
                    .fire(new RemoveOrganizationalUnitEvent(ougetUserInfo()));
                }
            }
        }
    }
    @Override
    public OrganizationalUnit getParentOrganizationalUnitfinal Repository repository ) {
        for ( OrganizationalUnit organizationalUnit : .values() ) {
            if ( organizationalUnit.getRepositories() != null &&
                    organizationalUnit.getRepositories().contains( repository ) ) {
                return organizationalUnit;
            }
        }
        return null;
    }
    @Override
    public String getSanitizedDefaultGroupIdfinal String proposedGroupId ) {
        //Only [A-Za-z0-9_\-.] are valid so strip everything else out
        return proposedGroupId != null ? proposedGroupId.replaceAll"[^A-Za-z0-9_\\-.]""" ) : proposedGroupId;
    }
    @Override
    public Boolean isValidGroupIdfinal String proposedGroupId ) {
        if ( proposedGroupId != null && !proposedGroupId.trim().isEmpty() ) {
            if ( proposedGroupId.length() == getSanitizedDefaultGroupIdproposedGroupId ).length() ) {
                return true;
            }
        }
        return false;
    }
    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();
    }
    protected String getUserInfo( SessionInfo sessionInfo ) {
        try {
            return sessionInfo.getIdentity().getIdentifier();
        } catch ( final Exception e ) {
            return "system";
        }
    }
New to GrepCode? Check out our FAQ X