Hi,
Just a mini-post concerning an utility class to communicate with DCTM DCTMUtil:
001 | package com.huo.lu.db.extract.dctm; |
002 |
003 | import java.io.IOException; |
004 |
005 | import org.apache.commons.lang.exception.ExceptionUtils; |
006 | import org.apache.log4j.Logger; |
007 |
008 | import com.documentum.com.DfClientX; |
009 | import com.documentum.com.IDfClientX; |
010 | import com.documentum.fc.client.DfClient; |
011 | import com.documentum.fc.client.DfQuery; |
012 | import com.documentum.fc.client.IDfClient; |
013 | import com.documentum.fc.client.IDfQuery; |
014 | import com.documentum.fc.client.IDfSession; |
015 | import com.documentum.fc.client.IDfSessionManager; |
016 | import com.documentum.fc.client.IDfType; |
017 | import com.documentum.fc.client.IDfTypedObject; |
018 | import com.documentum.fc.client.IDfValidator; |
019 | import com.documentum.fc.client.IDfValueAssistance; |
020 | import com.documentum.fc.common.DfException; |
021 | import com.documentum.fc.common.DfLoginInfo; |
022 | import com.documentum.fc.common.IDfList; |
023 | import com.documentum.fc.common.IDfLoginInfo; |
024 |
025 | /** |
026 | * Utility class to communicate with DCTM |
027 | */ |
028 | public class DCTMUtil { |
029 | // logger |
030 | private static Logger LOG = Logger.getLogger(DCTMUtil. class ); |
031 | |
032 | public DCTMUtil(){} |
033 |
034 | /** |
035 | * Connect to Documentum docbase |
036 | * @param userName : User to connect |
037 | * @param passWd : password |
038 | * @return |
039 | * @throws DfException |
040 | */ |
041 | public IDfSessionManager connectToDocumentum(String dfcPrimaryHost, String dfcPrimaryPort, |
042 | String dfcGlobalRegistryRepository, |
043 | String dfcGlobalRegistryUsername, |
044 | String dfcGlobalRegistryPassword, |
045 | String dfcSessionSecureConnectDefault, |
046 | String userName, String passWd) throws DfException { |
047 | |
048 | IDfClientX cx = new DfClientX(); |
049 | LOG.info( "Get DfClient : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + "..." ); |
050 | IDfClient client = cx.getLocalClient(); |
051 | LOG.info( "Get DfClient : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + " : done" ); |
052 |
053 | // dfc.properties |
054 | LOG.info( "Get DfClientConfig : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + "..." ); |
055 | IDfTypedObject cfg = client.getClientConfig(); |
056 | LOG.info( "Get DfClientConfig : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + " : done" ); |
057 | LOG.info( "Set DfClientConfig.primary_host=" + dfcPrimaryHost); |
058 | cfg.setString( "primary_host" , dfcPrimaryHost); |
059 | LOG.info( "Set DfClientConfig.primary_port=" + dfcPrimaryPort); |
060 | cfg.setString( "primary_port" , dfcPrimaryPort); |
061 | LOG.info( "Set DfClientConfig.dfc.globalregistry.repository=" + dfcGlobalRegistryRepository); |
062 | cfg.setString( "dfc.globalregistry.repository" , dfcGlobalRegistryRepository); |
063 | LOG.info( "Set DfClientConfig.dfc.globalregistry.username=" + dfcGlobalRegistryUsername); |
064 | cfg.setString( "dfc.globalregistry.username" , dfcGlobalRegistryUsername); |
065 | LOG.info( "Set DfClientConfig.dfc.globalregistry.password=" + dfcGlobalRegistryPassword); |
066 | cfg.setString( "dfc.globalregistry.password" , dfcGlobalRegistryPassword); |
067 | LOG.info( "Set DfClientConfig.dfc.session.secure_connect_default=" + dfcSessionSecureConnectDefault); |
068 | cfg.setString( "dfc.session.secure_connect_default" , dfcSessionSecureConnectDefault); |
069 |
070 | // documentum session manager |
071 | LOG.info( "Get SessionManager : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + "..." ); |
072 | IDfSessionManager sessionMgr = client.newSessionManager(); |
073 | LOG.info( "Get SessionManager : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + " : done" ); |
074 | //sessionMgr.setLocale("fr"); |
075 | //sessionMgr.setIdentity(docbaseName, login); |
076 | // |
077 | // Setup login details. |
078 | IDfLoginInfo login = new DfLoginInfo(); |
079 | login.setUser(userName); |
080 | login.setPassword(passWd); |
081 | //login.setDomain(null); |
082 | // |
083 | LOG.info( "Get SessionManager.setIdentity(ALL_DOCBASES) : " + dfcPrimaryHost+ ":" + dfcPrimaryPort + " / " + userName + "..." ); |
084 | sessionMgr.setIdentity(IDfSessionManager.ALL_DOCBASES, login); |
085 | |
086 | return sessionMgr; |
087 | } |
088 |
089 | /** |
090 | * Disconnect from documentum docbase |
091 | */ |
092 | public void disconnect(IDfSessionManager sessionMgr, IDfSession session) { |
093 | if (session!= null && sessionMgr!= null ) { |
094 | LOG.info( "[disconnect] Closing session ..." ); |
095 | sessionMgr.release(session); |
096 | LOG.info( "[disconnect] Session closed" ); |
097 | LOG.info( "[disconnect] SessionMgr clearing identities ..." ); |
098 | sessionMgr.clearIdentities(); |
099 | LOG.info( "[disconnect] SessionMgr clear identities done" ); |
100 | } |
101 | } |
102 | |
103 | /** |
104 | * Execute une requête DQL et retourne son résultat. |
105 | * |
106 | * @throws DfException |
107 | * @throws IOException |
108 | */ |
109 | public IDfTypedObject runDQLQuery(IDfSession session, String dqlQuery) throws Throwable { |
110 | LOG.info( "Starting runDQLQuery : " + dqlQuery); |
111 | IDfTypedObject objectToReturn = null ; |
112 | |
113 | try { |
114 | IDfQuery query = new DfQuery(); |
115 | query.setDQL(dqlQuery); |
116 | objectToReturn = query.execute(session, IDfQuery.DF_READ_QUERY); |
117 | } catch (Throwable th) { |
118 | LOG.fatal( "ERROR: " + ExceptionUtils.getFullStackTrace(th)); |
119 | throw th; |
120 | } |
121 | LOG.info( "Exiting runDQLQuery : " + dqlQuery); |
122 | return objectToReturn; |
123 | } |
124 | |
125 | public String getValueMapping(IDfType type, String attrName, String attrValue) throws DfException { |
126 | String display = attrValue; |
127 | IDfValidator validator = type.getTypeValidator( null , "" ); |
128 | IDfValueAssistance va = validator.getValueAssistance(attrName, null ); |
129 | if (va != null ) { |
130 | IDfList list = va.getActualValues(); |
131 | for ( int i = 0 ; i < list.getCount(); i++) { |
132 | String value = list.getString(i); |
133 | if (value.equals(display)) { |
134 | if (LOG.isDebugEnabled()) |
135 | LOG.info( "Value: " +value); |
136 | display = va.getDisplayValue(value); |
137 | if (LOG.isDebugEnabled()) |
138 | LOG.info( "Display: " +display); |
139 | break ; |
140 | } |
141 | } |
142 | } |
143 | return display.replace( " - " , "_" ); |
144 | } |
145 |
146 | /** |
147 | * Affiche les informations de connection |
148 | */ |
149 | public void getSessionInfo(IDfSession session) throws Throwable { |
150 | try { |
151 | LOG.info( "" ); |
152 | LOG.info( "DM Session Properties: " ); |
153 | LOG.info( "DM Server Version: " + session.getServerVersion()); |
154 | LOG.info( "DM Session ID: " + session.getSessionId()); |
155 | LOG.info( "DFC Version: " + DfClient.getDFCVersion()); |
156 | LOG.info( "" ); |
157 | } catch (Throwable th) { |
158 | LOG.error( "[getSessionInfo] Unable to retrieve DM server info: " + ExceptionUtils.getFullStackTrace(th)); |
159 | throw th; |
160 | } |
161 | } |
162 |
163 | } |
That’s all!!!!
Huseyin OZVEREN