Skip to content

Instantly share code, notes, and snippets.

@junojisan
Created April 22, 2016 08:49
Show Gist options
  • Save junojisan/8734fa9541c63ba8e539bbec076b9905 to your computer and use it in GitHub Desktop.
Save junojisan/8734fa9541c63ba8e539bbec076b9905 to your computer and use it in GitHub Desktop.
package app;
import java.io.FileWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.QuoteMode;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.LoginEvent;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class LoginEventApp {
static final String USERNAME = "hoge@hoge.com";
static final String PASSWORD = "password";
static EnterpriseConnection connection;
static final String SOQL =
"SELECT "
+ "AdditionalInfo, "
+ "ApiType, "
+ "ApiVersion, "
+ "Application, "
+ "AuthServiceId, "
+ "Browser, "
+ "ClientVersion, "
+ "EventDate, "
+ "LoginGeoId, "
+ "LoginHistoryId, "
+ "LoginType, "
+ "LoginUrl, "
+ "Platform, "
+ "SourceIp, "
+ "Status, "
+ "UserId, "
+ "Username "
+ "FROM "
+ "LoginEvent";
public static void main(String[] args) {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
try{
connection = Connector.newConnection(config);
List<LoginEvent> loginEvents = queryLoginEvent();
printCsv(loginEvents);
}catch(ConnectionException e){
e.printStackTrace();
}
}
private static List<LoginEvent> queryLoginEvent() {
List<LoginEvent> loginEvents = new ArrayList<LoginEvent>();
try {
Boolean done = false;
QueryResult qr = connection.query(SOQL);
if(qr.getSize() > 0){
while(!done){
for(SObject sobj : qr.getRecords()){
loginEvents.add((LoginEvent)sobj);
}
if(qr.isDone()){
done = true;
}else{
qr = connection.queryMore(qr.getQueryLocator());
}
}
}
} catch (ConnectionException e) {
e.printStackTrace();
}
return loginEvents;
}
private static void printCsv(List<LoginEvent> loginEvents){
CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuoteMode(QuoteMode.ALL);
CSVPrinter printer = null;
try{
Path path = Paths.get("test.csv");
printer = new CSVPrinter(new FileWriter(path.toString()), format);
for(LoginEvent le: loginEvents){
printer.print(le.getAdditionalInfo());
printer.print(le.getApiType());
printer.print(le.getApiVersion());
printer.print(le.getApplication());
printer.print(le.getAuthServiceId());
printer.print(le.getBrowser());
printer.print(le.getClientVersion());
printer.print(new SimpleDateFormat("yyyy/MM/dd HH:mm:dd.ss").format(le.getEventDate().getTime()));
printer.print(le.getLoginGeoId());
printer.print(le.getLoginHistoryId());
printer.print(le.getLoginType());
printer.print(le.getLoginUrl());
printer.print(le.getSourceIp());
printer.print(le.getPlatform());
printer.print(le.getStatus());
printer.print(le.getUser());
printer.print(le.getUsername());
printer.println();
}
printer.flush();
}catch(Exception e){
e.printStackTrace();
}finally {
if(printer != null){
try{
printer.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment