Skip to content

Commit 493cc59

Browse files
committed
Addressing comments
1 parent 02ee977 commit 493cc59

File tree

6 files changed

+171
-135
lines changed

6 files changed

+171
-135
lines changed

vault/vault-hold-migration-api/src/main/java/com/google/vault/chatmigration/DirectoryService.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,45 @@
1717
public class DirectoryService {
1818
private static final Logger logger = Logger.getLogger(DirectoryService.class.getName());
1919

20-
private Directory directoryService;
21-
private Map<String, OrgUnit> orgUnits;
22-
private Map<String, User> users = new HashMap<>();
23-
24-
public DirectoryService(Directory directoryService) {
25-
this.directoryService = directoryService;
26-
getOrgUnits();
27-
}
28-
29-
public String getEmail(String accountId) {
30-
User user = users.get(accountId);
31-
if(user == null) {
32-
try {
33-
user = RetryableTemplate
34-
.callWithRetry(() -> directoryService.users().get(accountId).execute());
35-
users.put(accountId, user);
36-
} catch (ExecutionException e) {
37-
logger.log(Level.WARNING, "Unable to get email address for account Id " + accountId, e);
38-
} catch (RetryException e) {
39-
logger.log(Level.WARNING, "Unable to get email address for account Id " + accountId, e);
40-
}
41-
}
42-
return user!=null?user.getPrimaryEmail():accountId;
20+
private Directory directoryService;
21+
private Map<String, OrgUnit> orgUnits;
22+
private Map<String, User> users = new HashMap<>();
23+
24+
public DirectoryService(Directory directoryService) {
25+
this.directoryService = directoryService;
26+
getOrgUnits();
27+
}
28+
29+
public String getEmail(String accountId) {
30+
User user = users.get(accountId);
31+
if (user == null) {
32+
try {
33+
user =
34+
RetryableTemplate.callWithRetry(
35+
() -> directoryService.users().get(accountId).execute());
36+
users.put(accountId, user);
37+
} catch (ExecutionException e) {
38+
logger.log(Level.WARNING, "Unable to get email address for account Id " + accountId, e);
39+
} catch (RetryException e) {
40+
logger.log(Level.WARNING, "Unable to get email address for account Id " + accountId, e);
41+
}
4342
}
44-
45-
public OrgUnit getOrgUnit(String orgUnitId) {
46-
return orgUnits.get(orgUnitId);
43+
return user != null ? user.getPrimaryEmail() : accountId;
44+
}
45+
46+
public OrgUnit getOrgUnit(String orgUnitId) {
47+
return orgUnits.get(orgUnitId);
48+
}
49+
50+
private void getOrgUnits() {
51+
OrgUnits response = null;
52+
try {
53+
response = directoryService.orgunits().list("my_customer").setType("all").execute();
54+
orgUnits =
55+
response.getOrganizationUnits().stream()
56+
.collect(Collectors.toMap(OrgUnit::getOrgUnitId, Function.identity()));
57+
} catch (IOException e) {
58+
logger.log(Level.WARNING, "Unable to get org units", e);
4759
}
48-
49-
private void getOrgUnits() {
50-
OrgUnits response = null;
51-
try {
52-
response = directoryService.orgunits().list("my_customer").setType("all").execute();
53-
orgUnits = response.getOrganizationUnits().stream().collect(Collectors.toMap(OrgUnit::getOrgUnitId, Function.identity()));
54-
} catch (IOException e) {
55-
logger.log(Level.WARNING, "Unable to get org units", e);
56-
}
57-
}
58-
60+
}
5961
}

vault/vault-hold-migration-api/src/main/java/com/google/vault/chatmigration/DuplicateHold.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.google.api.services.vault.v1.model.HeldOrgUnit;
99
import com.google.api.services.vault.v1.model.Hold;
1010
import java.io.IOException;
11-
import java.io.PrintWriter;
12-
import java.io.StringWriter;
1311
import java.util.Arrays;
1412
import java.util.List;
1513
import java.util.concurrent.ExecutionException;
@@ -48,23 +46,25 @@ public int duplicateHolds() throws Exception {
4846
String orgUnitId = record.get(HoldsReport.ORG_UNIT_ID);
4947
String accounts = record.get(HoldsReport.ACCOUNT_IDS);
5048

51-
List<HeldAccount> accountList = (accounts.equals("")) ? null :Arrays.stream(accounts.split(","))
52-
.map(account -> new HeldAccount().setAccountId(account))
53-
.collect(Collectors.toList());
49+
List<HeldAccount> accountList =
50+
(accounts.equals(""))
51+
? null
52+
: Arrays.stream(accounts.split(","))
53+
.map(account -> new HeldAccount().setAccountId(account))
54+
.collect(Collectors.toList());
5455
boolean exceedsAccountLimit = false;
5556

5657
Hold hold = new Hold().setName(name + HOLD_NAME_SUFFIX).setCorpus("HANGOUTS_CHAT");
5758

58-
if (!"".equals(orgUnitId)) {
59+
if (!orgUnitId.equals("")) {
5960
hold.setOrgUnit(new HeldOrgUnit().setOrgUnitId(orgUnitId));
6061
} else if (!"".equals(accounts)) {
61-
if(accountList.size() > MAX_ACCOUNTS_FOR_HOLD){
62+
if (accountList.size() > MAX_ACCOUNTS_FOR_HOLD) {
6263
exceedsAccountLimit = true;
63-
hold.setAccounts(accountList.subList(0,MAX_ACCOUNTS_FOR_HOLD));
64+
hold.setAccounts(accountList.subList(0, MAX_ACCOUNTS_FOR_HOLD));
6465
} else {
6566
hold.setAccounts(accountList);
6667
}
67-
6868
}
6969
hold.setQuery(
7070
new CorpusQuery()
@@ -74,11 +74,13 @@ public int duplicateHolds() throws Exception {
7474
RetryableTemplate.callWithRetry(
7575
() -> vaultService.matters().holds().create(matterId, hold).execute());
7676

77-
if(exceedsAccountLimit){
78-
addAccountsToHold(matterId,response.getHoldId(),accountList.subList(MAX_ACCOUNTS_FOR_HOLD,accountList.size()));
77+
if (exceedsAccountLimit) {
78+
addAccountsToHold(
79+
matterId,
80+
response.getHoldId(),
81+
accountList.subList(MAX_ACCOUNTS_FOR_HOLD, accountList.size()));
7982
}
8083

81-
8284
numberOfHolds++;
8385
System.out.println("Created hold: '" + hold.getName() + "'");
8486
logger.log(Level.INFO, "Copied '" + hold.getName() + "'");
@@ -104,7 +106,8 @@ private void writeHeader() throws IOException {
104106
}
105107

106108
private void writeError(CSVRecord record, Exception ex) throws IOException {
107-
System.out.println("Hold: '" + record.get(HoldsReport.HOLD_NAME) + "' not copied to Hangouts chat.");
109+
System.out.println(
110+
"Hold: '" + record.get(HoldsReport.HOLD_NAME) + "' not copied to Hangouts chat.");
108111

109112
errorReport.printRecord(
110113
record.get(HoldsReport.MATTER_ID),
@@ -114,12 +117,21 @@ private void writeError(CSVRecord record, Exception ex) throws IOException {
114117
ex.getMessage());
115118
}
116119

117-
private void addAccountsToHold(String matterId,String holdId, List<HeldAccount> accounts) throws ExecutionException, RetryException {
118-
logger.log(Level.INFO,"There are more than 100 users on hold: " + holdId + " in matter: " + matterId + ".");
120+
private void addAccountsToHold(String matterId, String holdId, List<HeldAccount> accounts)
121+
throws ExecutionException, RetryException {
122+
logger.log(
123+
Level.INFO,
124+
"There are more than 100 users on hold: " + holdId + " in matter: " + matterId + ".");
119125
for (HeldAccount account : accounts) {
120126
logger.log(Level.INFO, "Adding account: " + account.getAccountId() + " to hold: " + holdId);
121127
RetryableTemplate.callWithRetry(
122-
() -> vaultService.matters().holds().accounts().create(matterId, holdId, account).execute());
128+
() ->
129+
vaultService
130+
.matters()
131+
.holds()
132+
.accounts()
133+
.create(matterId, holdId, account)
134+
.execute());
123135
}
124136
}
125137
}

vault/vault-hold-migration-api/src/main/java/com/google/vault/chatmigration/HoldsReport.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ private void iterateMatters(String nextPageToken) throws Exception {
5555
ListMattersResponse response =
5656
RetryableTemplate.callWithRetry(
5757
vaultService
58-
.matters()
59-
.list()
60-
.setState("OPEN")
61-
.setPageSize(100)
62-
.setPageToken(nextPageToken)::execute);
58+
.matters()
59+
.list()
60+
.setState("OPEN")
61+
.setPageSize(100)
62+
.setPageToken(nextPageToken)
63+
::execute);
6364
List<Matter> matters = response.getMatters();
6465

6566
if (matters != null && matters.size() > 0) {
@@ -75,21 +76,23 @@ private void iterateMatters(String nextPageToken) throws Exception {
7576
private void iterateHolds(Matter matter, String nextPageToken) throws Exception {
7677
ListHoldsResponse response =
7778
RetryableTemplate.callWithRetry(
78-
() -> vaultService
79-
.matters()
80-
.holds()
81-
.list(matter.getMatterId())
82-
.setPageSize(100)
83-
.setPageToken(nextPageToken)
84-
.execute());
79+
() ->
80+
vaultService
81+
.matters()
82+
.holds()
83+
.list(matter.getMatterId())
84+
.setPageSize(100)
85+
.setPageToken(nextPageToken)
86+
.execute());
8587
List<Hold> holds = response.getHolds();
8688
if (holds != null && holds.size() > 0) {
8789
for (Hold hold : holds) {
8890
if ("MAIL".equals(hold.getCorpus())) {
8991
writeHold(matter, hold);
9092
++numberOfHolds;
9193
System.out.println("Completed " + numberOfHolds + " holds. In progress...");
92-
LOGGER.log(Level.INFO,"Completed " + numberOfHolds + " holds. Report generation in progress.");
94+
LOGGER.log(
95+
Level.INFO, "Completed " + numberOfHolds + " holds. Report generation in progress.");
9396
}
9497
}
9598
}
@@ -141,14 +144,11 @@ private void writeHold(Matter matter, Hold hold) throws IOException {
141144
null,
142145
null,
143146
null,
144-
hold.getAccounts()
145-
.stream()
147+
hold.getAccounts().stream()
146148
.map(account -> account.getAccountId())
147149
.collect(Collectors.joining(",")),
148-
hold.getAccounts()
149-
.stream()
150-
.map(
151-
account -> directoryService.getEmail(account.getAccountId()))
150+
hold.getAccounts().stream()
151+
.map(account -> directoryService.getEmail(account.getAccountId()))
152152
.collect(Collectors.joining(",")),
153153
hold.getCorpus(),
154154
hold.getQuery().getMailQuery().getTerms(),

vault/vault-hold-migration-api/src/main/java/com/google/vault/chatmigration/MigrationHelper.java

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
public class MigrationHelper {
2929

30-
3130
enum MigrationOptions {
3231
GENERATE_REPORT("a", "genholdreport", "Generate Hold Report"),
3332
DUPLICATE_HOLDS("b", "duplicateholds", "Duplicate Gmail Holds to Hangouts Chat"),
@@ -80,8 +79,8 @@ public String getOption() {
8079

8180
static {
8281
try {
83-
InputStream stream = QuickStart.class.getClassLoader().
84-
getResourceAsStream("logging.properties");
82+
InputStream stream =
83+
QuickStart.class.getClassLoader().getResourceAsStream("logging.properties");
8584
LogManager.getLogManager().readConfiguration(stream);
8685
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
8786
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
@@ -91,53 +90,59 @@ public String getOption() {
9190
}
9291
}
9392

94-
95-
static final Option helpOption = Option.builder(MigrationOptions.HELP.option)
96-
.longOpt(MigrationOptions.HELP.longOpt)
97-
.argName("help").desc(MigrationOptions.HELP.description).build();
93+
static final Option helpOption =
94+
Option.builder(MigrationOptions.HELP.option)
95+
.longOpt(MigrationOptions.HELP.longOpt)
96+
.argName("help")
97+
.desc(MigrationOptions.HELP.description)
98+
.build();
9899

99100
static Options buildOptions() {
100101
Options options = new Options();
101102

102-
Option generateReport = Option.builder(MigrationOptions.GENERATE_REPORT.option)
103-
.longOpt(MigrationOptions.GENERATE_REPORT.longOpt)
104-
.desc(MigrationOptions.GENERATE_REPORT.description).build();
103+
Option generateReport =
104+
Option.builder(MigrationOptions.GENERATE_REPORT.option)
105+
.longOpt(MigrationOptions.GENERATE_REPORT.longOpt)
106+
.desc(MigrationOptions.GENERATE_REPORT.description)
107+
.build();
105108

106-
Option duplicateHolds = Option.builder(MigrationOptions.DUPLICATE_HOLDS.option)
107-
.longOpt(MigrationOptions.DUPLICATE_HOLDS.longOpt)
108-
.desc(MigrationOptions.DUPLICATE_HOLDS.description)
109-
.build();
109+
Option duplicateHolds =
110+
Option.builder(MigrationOptions.DUPLICATE_HOLDS.option)
111+
.longOpt(MigrationOptions.DUPLICATE_HOLDS.longOpt)
112+
.desc(MigrationOptions.DUPLICATE_HOLDS.description)
113+
.build();
110114

111-
Option reportFile = Option.builder(MigrationOptions.REPORT_FILE.option)
112-
.required()
113-
.hasArg()
114-
.longOpt(MigrationOptions.REPORT_FILE.longOpt)
115-
.argName("reportfile")
116-
.desc(MigrationOptions.REPORT_FILE.description)
117-
.build();
115+
Option reportFile =
116+
Option.builder(MigrationOptions.REPORT_FILE.option)
117+
.required()
118+
.hasArg()
119+
.longOpt(MigrationOptions.REPORT_FILE.longOpt)
120+
.argName("reportfile")
121+
.desc(MigrationOptions.REPORT_FILE.description)
122+
.build();
118123

119-
Option errorFile = Option.builder(MigrationOptions.ERROR_FILE.option)
120-
.required()
121-
.hasArg()
122-
.longOpt(MigrationOptions.ERROR_FILE.longOpt)
123-
.argName("errorfile")
124-
.desc(MigrationOptions.ERROR_FILE.description)
125-
.build();
124+
Option errorFile =
125+
Option.builder(MigrationOptions.ERROR_FILE.option)
126+
.required()
127+
.hasArg()
128+
.longOpt(MigrationOptions.ERROR_FILE.longOpt)
129+
.argName("errorfile")
130+
.desc(MigrationOptions.ERROR_FILE.description)
131+
.build();
126132

127-
Option includeRoom = Option.builder(MigrationOptions.INCLUDE_ROOMS.option)
128-
.longOpt(MigrationOptions.INCLUDE_ROOMS.longOpt)
129-
.argName(MigrationOptions.INCLUDE_ROOMS.description)
130-
.desc(MigrationOptions.INCLUDE_ROOMS.description)
131-
.build();
133+
Option includeRoom =
134+
Option.builder(MigrationOptions.INCLUDE_ROOMS.option)
135+
.longOpt(MigrationOptions.INCLUDE_ROOMS.longOpt)
136+
.argName(MigrationOptions.INCLUDE_ROOMS.description)
137+
.desc(MigrationOptions.INCLUDE_ROOMS.description)
138+
.build();
132139

133140
options.addOption(reportFile);
134141
options.addOption(errorFile);
135142
options.addOption(includeRoom);
136143

137144
OptionGroup optionGroup = new OptionGroup();
138-
optionGroup.addOption(generateReport)
139-
.addOption(duplicateHolds)
140-
.addOption(helpOption);
145+
optionGroup.addOption(generateReport).addOption(duplicateHolds).addOption(helpOption);
141146
optionGroup.setRequired(true);
142147
options.addOptionGroup(optionGroup);
143148

0 commit comments

Comments
 (0)