You are not logged in. Click here to log in.

codebeamer Application Lifecycle Management (ALM)

Search In Project

Search inClear

Tags:  not added yet

Add users as project administrators to each newly created project

This is a fully fledge example, of how to use the Listener API, to add users as project administrators to each newly created project.


package com.intland.codebeamer.event.impl.sample;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.intland.codebeamer.event.BaseEvent;
import com.intland.codebeamer.event.ProjectListener;
import com.intland.codebeamer.manager.ProjectManager;
import com.intland.codebeamer.persistence.dao.RoleDao;
import com.intland.codebeamer.persistence.dao.UserDao;
import com.intland.codebeamer.persistence.dto.ProjectDto;
import com.intland.codebeamer.persistence.dto.RoleDto;
import com.intland.codebeamer.persistence.dto.UserDto;

/**
 * Assigns a list of users with {@link RoleDto#PROJECT_ADMIN} to each newly created project.
 */
public class AssignAdminAccountToNewProjectListener implements ProjectListener {
    protected static final Logger log = Logger.getLogger(AssignAdminAccountToNewProjectListener.class);

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ProjectManager projectManager;

    private List<String> userNamesOrIds;

    public String getAccountIds() {
        return userNamesOrIds != null && userNamesOrIds.size() > 0 ? StringUtils.join(userNamesOrIds, "; ") : null;
    }

    public void setAccountIds(String accountIds) {
        this.userNamesOrIds = (accountIds = StringUtils.trimToNull(accountIds)) != null ? Arrays.asList(StringUtils.split(accountIds, "; ")) : null;
    }

    @Override
    public void projectCreated(BaseEvent<ProjectDto, Void, Void> event) {
        if (userNamesOrIds != null && userNamesOrIds.size() > 0 && !event.isPreEvent()) {
            RoleDto projectAdminRole = roleDao.findByName(RoleDto.PROJECT_ADMIN);
            Set<RoleDto> projectAdminRoles = Collections.singleton(projectAdminRole);
            Map<UserDto,Collection<?>> membersAndRoles = new HashMap<UserDto,Collection<?>>(userNamesOrIds.size());

            for (String userNameOrId : userNamesOrIds) {
                UserDto account = userDao.findByNameOrId(userNameOrId);
                if (account != null) {
                    membersAndRoles.put(account, projectAdminRoles);
                } else {
                    log.warn("Cannot find user: " + userNameOrId);
                }
            }

            if (membersAndRoles.size() > 0) {
                UserDto user = event.getUser();
                ProjectDto project = event.getSource();

                try {
                    projectManager.setProjectMemberRoles(user, project, membersAndRoles, null);
                } catch (Exception e) {
                    log.warn("Assigning project admin failed", e);
                }
            }
        }
    }

}


Since this Listener needs extra configuration, it cannot be automatically deployed by adding a @Component annotation!

Instead you must add an appropriate <bean> configuration to

~/CB-../tomcat/webapps/cb/WEB-INF/classes/my-ApplicationContext.xml, e.g.

   <bean id="assignAdminAccountToNewProjectListener" class="com.intland.codebeamer.event.impl.sample.AssignAdminAccountToNewProjectListener">
      <property name="accountId" value="1;44"/>
   </bean>