javahashmapdocumentumdfcdocumentum-dfc

How to print a value from a Hashmap with a ArrayList


I am working on a piece of code which has a Hashmap. This hashmap has a string as key and an arraylist as value. I populate arraylist the with values I'm fetching using a DQL. I've three attributes for many users. Then putting the value into the hashmap. What I want is to iterate the Hashmap and fetch the set of 3 attritubes for 1 user. Let me provide the code below

    package com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import xtrim.util.i;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfLoginInfo;
import com.documentum.fc.common.IDfLoginInfo;

public class Adlookup {


    IDfSessionManager sessionMrg = null;
    IDfSession session=null;
    IDfClient dfclient = null;
    IDfClientX clientX = new DfClientX();
    IDfCollection total = null;
    int j;
    int flag = 0;
    WriteToExcel ex = new WriteToExcel();

    public void LookupReport(String docbaseName, String username, String password) throws DfException, IOException
    {
        dfclient = clientX.getLocalClient();
        String Docbase = docbaseName;

        IDfLoginInfo loginInfo = new DfLoginInfo();
        loginInfo.setUser(username);
        loginInfo.setPassword(password);
        sessionMrg = dfclient.newSessionManager();
        sessionMrg.setIdentity(Docbase, loginInfo);
        session = sessionMrg.getSession(Docbase);
        System.out.println("connection created for adlookup");


        String query = Getquery();
        IDfQuery dql = new DfQuery();
        dql.setDQL(query);

        total = dql.execute(session, IDfQuery.DF_EXEC_QUERY);

        System.out.println("all good for lookup");

        //String[] columnNames = new String[] { "User Name","User Login Name","Email"};
        List<String> lstValues = new ArrayList<String>();  
        Map<Integer, ArrayList<String>> myMap = new HashMap<Integer, ArrayList<String>>();  
        while (total.next())
        {

             lstValues.add(total.getString("uname")+","+total.getString("loginname")+","+total.getString("uadd"));
             myMap.put(flag, (ArrayList<String>) lstValues);

             flag++;    
             System.out.println("Flag value: " +flag);

            // lstValues.clear();

        }       

        Set setofKeys = myMap.keySet();
        Iterator itr = setofKeys.iterator();

        while(itr.hasNext())
        {
            Integer key = (Integer) itr.next();
            ArrayList<String> value = myMap.get(key);
            System.out.println("\nResult :"+value);
        }
    }

    private String Getquery() {
        // TODO Auto-generated method stub
        String query = "select user_name as uname, user_login_name as loginname, user_address as uadd  from dm_user dmu, dm_dbo.MV_V_MIDAS_MERCK_PRSN1 dma where  upper(dmu.user_login_name)=upper(dma.isid) and dmu.user_state=0 and directory_display_ind='I'";
        return query;
    }

}

I'm getting output like this

Result :

    [Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

    Result :[Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

    Result :[Sayre,Joseph,sayrej,joseph.sayre@abc.com, Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com, Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com]

but I want something like this :

Result : Sayre,Joseph,sayrej,joseph.sayre@abc.com
Result : Kapoor,Rohit,kapoorro,rohit.kapoor@abc.com
Result : Pineiros-Vallejo, Miguel,pineirom,rajendra.baxi@abc.com

Also I need to print this values in an excelsheet.

Any kind of help will appreciated.


Solution

  • If the content of the ArrayList are attributes of a user, then why don't you create a class UserAttributes having fields to store the values and overrides toString() to output them?