tony_ziroff
Occasional Observer

how to get UserStatisticsAgent Implementation

Jump to solution

Hi,

I want to get the last login date of all users, but I did not find the Implementation of the UserStatisticsAgent interface.

Currently I am running the following code, but it uses the internal class de.espirit.firstspirit.server.usermanagement.UserStatisticImpl:

Connection connection = context.getConnection();

Project[] projects = connection.getProjects();

for(int i=0; i< projects.length; i++){

    context.logInfo( "" + projects[i].toString() );

    UnmodifiableRandomAccessList<User> users = projects[i].getUsers();

    Iterator<User> iterator = users.iterator();

    while(iterator.hasNext()){

        User user = iterator.next();

        context.logInfo( user.getId()  + "\t" + user.getName() + "\t" + user.getStatistic().getLastLogin());

    }

}

could anyone help me solving the following problem described as pseudocode:

Users[] users = system.getAllUsers();

for(int i=0; i<users.length;i++){

    print (UserStatisticImpl.getLastLoginAsDate(user.getId()));

}

0 Kudos
1 Solution

Accepted Solutions
hbarthel
New Responder

        ManagerBroker managerBroker = connection.getBroker().requireSpecialist(ManagerBroker.TYPE);

        UserManager userManager = managerBroker.getManager(UserManager.class);

        Long[] userIds = userManager.getUserIds();

        UserStatisticsAgent usa = connection.getBroker().requireSpecialist(UserStatisticsAgent.TYPE);

        for (Long userId : userIds) {

            String login = userManager.getUserByID(userId).getLoginName();

            LocalDate date = usa.getLastLogin(userId).orElse(null);

..............

        }

View solution in original post

0 Kudos
2 Replies
hbarthel
New Responder

        ManagerBroker managerBroker = connection.getBroker().requireSpecialist(ManagerBroker.TYPE);

        UserManager userManager = managerBroker.getManager(UserManager.class);

        Long[] userIds = userManager.getUserIds();

        UserStatisticsAgent usa = connection.getBroker().requireSpecialist(UserStatisticsAgent.TYPE);

        for (Long userId : userIds) {

            String login = userManager.getUserByID(userId).getLoginName();

            LocalDate date = usa.getLastLogin(userId).orElse(null);

..............

        }

0 Kudos
tony_ziroff
Occasional Observer

thanks! ended with the following code dumping user information, project memberships with roles and external group memberships

import java.time.LocalDate;

import java.util.Collection;

import java.util.Iterator;

import java.util.List;

import de.espirit.firstspirit.access.AdminService;

import de.espirit.firstspirit.access.Connection;

import de.espirit.firstspirit.access.admin.UserStorage;

import de.espirit.firstspirit.access.project.ExternalGroup;

import de.espirit.firstspirit.access.project.Group;

import de.espirit.firstspirit.access.project.Project;

import de.espirit.firstspirit.access.User;

import de.espirit.firstspirit.agency.ManagerBroker;

import de.espirit.firstspirit.agency.UserStatisticsAgent;

import de.espirit.firstspirit.manager.UserManager;

Connection connection = context.getConnection();

Project[] projects = connection.getProjects();

ManagerBroker managerBroker = connection.getBroker().requireSpecialist(ManagerBroker.TYPE);

UserManager userManager = managerBroker.getManager(UserManager.class);        

UserStorage userstorage=connection.getService(AdminService.class).getUserStorage();        

Long[] userIds = userManager.getUserIds();

UserStatisticsAgent usa = connection.getBroker().requireSpecialist(UserStatisticsAgent.TYPE);

for (Long userId : userIds) {

    String roles = "";

    String groupNames = "";

    User user=userstorage.getUserByID(userId);

    String userName = user.getLoginName() + "^-" + user.getRealname();

    String userInfo = userName + "^-" + user.getEMail() + "^-" + user.getLDAPSection() + "^-" + user.isActive();          

    

    Collection<ExternalGroup> extGroups = user.getExternalGroups();

    for (ExternalGroup group : extGroups) {

        groupNames += group.getName( ) + " - ";

    }

   

    String projectList = "";

    for(int i=0; i< projects.length; i++){

        List<User> users = projects[i].getUsers();

        Iterator<User> iterator = users.iterator();

     

        while(iterator.hasNext()){

            User localUser = iterator.next();

            if(user.getId() == localUser.getId()) {

                projectList += projects[i].getName() + " - ";

                Group[] groups = projects[i].getGroupsArray(localUser);

                for( int j=0; j>groups.length; j++ ) {

                    roles = groups[j].getName();

                }

            }

        }               

    }

   

    try{

        LocalDate date = usa.getLastLogin(userId).orElse(null);

        userInfo += "^-" + date.toString();

    }catch( Exception ex ){

        userInfo += "^-" + "no last login available";

    }

    context.logInfo( "^-" + userInfo  + "^-" + projectList+ "^-" + groupNames);

}

0 Kudos