MicroStrategy ONE
Creating a New Connection Map
The code sample shown below illustrates how to create a new connection map. See Connection Mapping for an explanation of connection mapping.
/*
* To create a new DB Connection map we need the following components
*
* DB Instance
* User
* DB Connection
* DB Login
* Project
*
* A connection mapping is a configuration object and thus we will need an
* admin session in order to save one. No need to specify project name
*
* */
package com.microstrategy.standalone;
import java.util.List;
import com.microstrategy.web.objects.WebConnectionMap;
import com.microstrategy.web.objects.WebConnectionMapSource;
import com.microstrategy.web.objects.WebDBConnection;
import com.microstrategy.web.objects.WebDBLogin;
import com.microstrategy.web.objects.WebDBRole;
import com.microstrategy.web.objects.WebFolder;
import com.microstrategy.web.objects.WebIServerSession;
import com.microstrategy.web.objects.WebObjectSource;
import com.microstrategy.web.objects.WebObjectsFactory;
import com.microstrategy.web.objects.WebProject;
import com.microstrategy.web.objects.WebSearch;
import com.microstrategy.web.objects.admin.users.WebUser;
import com.microstrategy.web.objects.admin.users.WebUserEntity;
import com.microstrategy.webapi.EnumDSSXMLAuthModes;
import com.microstrategy.webapi.EnumDSSXMLObjectSubTypes;
import com.microstrategy.webapi.EnumDSSXMLObjectTypes;
import com.microstrategy.webapi.EnumDSSXMLSearchDomain;
public class CreateConnectionMap {
static WebObjectsFactory objectsFactory = null;
static WebObjectSource wos = null;
public static void main(String[] args) throws Exception {
//Create admin session (no project name)
WebIServerSession isess = WebObjectsFactory.getInstance().getIServerSession();
isess.setServerName("vm-jsmith");
isess.setLogin("administrator");
//isess.setProjectName("MicroStrategy Tutorial");
isess.setPassword("");
isess.setServerPort(0);
isess.setAuthMode(EnumDSSXMLAuthModes.DssXmlAuthStandard);
isess.getSessionID();
System.out.println("Session Created...");
objectsFactory = isess.getFactory();
wos = objectsFactory.getObjectSource();
WebConnectionMapSource mapSource = wos.getConnectionMapSource();
//Get a DB role Excel data source
WebDBRole dbRole = (WebDBRole)wos.getObject("47E08DA640328241A06A268428B25E4A", EnumDSSXMLObjectTypes.DssXmlTypeDBRole);
//Get user entity Jeffrey Malick
WebUser wUser = (WebUser)wos.getObject("28ECA90311D5188EC000E9ABCA1B1A4F", EnumDSSXMLObjectTypes.DssXmlTypeUser);
WebUserEntity user = (WebUserEntity)wUser;
//Get project object MicroStrategy Tutorial
WebProject proj = (WebProject)wos.getObject("B19DEDCC11D4E0EFC000EB9495D0F44F", EnumDSSXMLObjectTypes.DssXmlTypeProject);
//Search for existing DB login
WebSearch search= wos.getNewSearchObject();
search.setNamePattern("CustomDBLogin");
search.setAsync(false);
search.types().add(EnumDSSXMLObjectSubTypes.DssXmlSubTypeDBLogin);
search.setDomain(EnumDSSXMLSearchDomain.DssXmlSearchDomainConfiguration);
search.submit();
WebFolder wf = search.getResults();
WebDBLogin dbLogin;
//Does DB login exist?
if(wf.size()>0){
//Get existing login
dbLogin = (WebDBLogin) wf.get(0);
}
else{
//Get new DBLogin
dbLogin = (WebDBLogin)wos.getNewObject(EnumDSSXMLObjectTypes.DssXmlTypeDBLogin);
dbLogin.setLogin("DBLogin");
dbLogin.setPassword("DBPass");
//Folder in which to save db login object
WebFolder folder = (WebFolder)wos.getObject("73F7482911D3596C60001B8F67019608", EnumDSSXMLObjectTypes.DssXmlTypeFolder);
wos.save(dbLogin, "CustomDBLogin", folder);
}
//Retrieve an existing DB connection, Excel_WH, for connection map
WebDBConnection dbconn = (WebDBConnection)wos.getObject("5FC116B6434FCA8DBCE4B2AA6A480910", EnumDSSXMLObjectTypes.DssXmlTypeDBConnection);
List mapList = mapSource.getMatchingConnectionMaps(dbRole, user, proj, null);
//Check if map already exists
if(mapList.size()==0){
//If there is no connection map, create it
WebConnectionMap wcm = mapSource.createConnectionMap(dbRole, user, proj, null, dbconn, dbLogin);
wcm.save();
}
System.out.println("Created Connection Map for: " + user.getDisplayName());
isess.closeSession();
}
}