/*
 *
 * CODENVY CONFIDENTIAL
 * ________________
 *
 * [2012] - [2014] Codenvy, S.A.
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any. The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.analytics.metrics.accounts;

import com.codenvy.analytics.datamodel.ListValueData;
import com.codenvy.analytics.datamodel.MapValueData;
import com.codenvy.analytics.datamodel.StringValueData;
import com.codenvy.analytics.datamodel.ValueData;
import com.codenvy.analytics.metrics.Context;
import com.codenvy.analytics.metrics.MetricFilter;
import com.codenvy.analytics.metrics.MetricType;
import com.codenvy.analytics.metrics.RequiredFilter;
import com.codenvy.api.account.shared.dto.AccountMembership;
import com.codenvy.api.user.shared.dto.Member;
import com.codenvy.api.user.shared.dto.User;
import com.codenvy.api.workspace.shared.dto.Workspace;

import javax.annotation.security.RolesAllowed;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Alexander Reshetnyak
 */
@RolesAllowed(value = {"system/admin", "system/manager"})
@RequiredFilter(MetricFilter.ACCOUNT_ID)
public class AccountUsersRolesList extends AbstractAccountMetric {

    public AccountUsersRolesList() {
        super(MetricType.ACCOUNT_USERS_ROLES_LIST);
    }

    @Override
    public ValueData getValue(Context context) throws IOException {
        AccountMembership accountById = getAccountMembership(context);

        User currentUser = getCurrentUser();
        String currentUserId = currentUser.getId();

        List<ValueData> list2Return = new ArrayList<>();
        for (Workspace workspace : getWorkspaces(accountById.getId())) {

            String rolesCurrentUserInWorkspace = getUserRoleInWorkspace(currentUserId, workspace.getId());
            if (rolesCurrentUserInWorkspace.contains(ROLE_WORKSPACE_ADMIN.toLowerCase())) {

                for (Member member : getMembers(workspace.getId())) {

                    String userEmail = getUserEmail(member.getUserId());
                    for (String role : member.getRoles()) {
                        Map<String, ValueData> m = new HashMap<>();
                        m.put(ROLES, StringValueData.valueOf(role));
                        m.put(USER, StringValueData.valueOf(userEmail));
                        m.put(WS,
                              StringValueData.valueOf(
                                      workspace.getId().equals(member.getWorkspaceId()) ? workspace.getName()
                                                                                        : member.getWorkspaceId()));

                        list2Return.add(new MapValueData(m));
                    }
                }

            } else {
                Map<String, ValueData> m = new HashMap<>();
                m.put(ROLES, StringValueData.valueOf(ROLE_WORKSPACE_DEVELOPER));
                m.put(USER, StringValueData.valueOf(currentUser.getEmail()));
                m.put(WS, StringValueData.valueOf(workspace.getName()));

                list2Return.add(new MapValueData(m));
            }
        }

        list2Return = keepSpecificPage(list2Return, context);
        return new ListValueData(list2Return);
    }

    @Override
    public String getDescription() {
        return "Users roles in workspaces";
    }

    @Override
    public Class<? extends ValueData> getValueDataClass() {
        return ListValueData.class;
    }
}

@@ -2148,33 +2148,32 @@
 yId.getId())) %7B%0A
-%0A
             Stri
@@ -2279,12 +2279,32 @@
     
-if (
+boolean hasAdminRoles = 
 role
@@ -2371,25 +2371,19 @@
 rCase())
-) %7B%0A%0A    
+;%0A%0A
         
@@ -2440,20 +2440,16 @@
 d())) %7B%0A
-%0A   
         
@@ -2452,25 +2452,24 @@
             
- 
 String userE
@@ -2505,28 +2505,25 @@
 tUserId());%0A
-    
+%0A
             
@@ -2530,52 +2530,73 @@
     
-for (String role : memb
+if (hasAdminRoles %7C%7C userEmail.equals(currentUs
 er.get
-Roles
+Email
 ())
+)
  %7B%0A
-    
     
@@ -2667,36 +2667,32 @@
                 
-    
 m.put(ROLES, Str
@@ -2716,20 +2716,40 @@
 eOf(
-role));%0A    
+member.getRoles().toString()));%0A
     
@@ -2825,36 +2825,32 @@
                 
-    
 m.put(WS,%0A      
@@ -2846,296 +2846,57 @@
 (WS,
-%0A                              StringValueData.valueOf(%0A                                      workspace.getId().equals(member.getWorkspaceId()) ? workspace.getName()%0A                                                                                        : member.getWorkspaceId
+ StringValueData.valueOf(workspace.getName
 ()));%0A%0A
-    
     
@@ -2969,400 +2969,9 @@
     
-    %7D%0A                %7D%0A%0A            %7D else %7B%0A                Map%3CString, ValueData%3E m = new HashMap%3C%3E();%0A                m.put(ROLES, StringValueData.valueOf(ROLE_WORKSPACE_DEVELOPER));%0A                m.put(USER, StringValueData.valueOf(currentUser.getEmail()));%0A                m.put(WS, StringValueData.valueOf(workspace.getName()));%0A%0A                list2Return.add(new MapValueData(m));
+%7D
 %0A   

Improve the list of users' roles in workspaces