}
int importedCount = 0;
InputStream is = null;
CSVReader reader = null;
try {
is = new BufferedInputStream(new FileInputStream(subscribersCSVFile));
char separator = ',';
reader = new CSVReader(new InputStreamReader(is, "UTF-8"), separator);
String[] columns = reader.readNext();
if (columns == null) {
logger.warn("No data for importing subscriptions is found" + " or the file is not well-formed");
return;
}
if (columns.length == 1 && columns[0].contains(";")) {
// semicolon is used as a separator
reader.close();
IOUtils.closeQuietly(is);
is = new BufferedInputStream(new FileInputStream(subscribersCSVFile));
separator = ';';
reader = new CSVReader(new InputStreamReader(is, "UTF-8"), separator);
columns = reader.readNext();
}
int usernamePosition = ArrayUtils.indexOf(columns, "j:nodename");
int emailPosition = ArrayUtils.indexOf(columns, J_EMAIL);
if (usernamePosition == -1 && emailPosition == -1) {
logger.warn("No data for importing subscriptions is found" + " or the file is not well-formed");
return;
}
Map<String, Map<String, Object>> subscribers = new HashMap<String, Map<String, Object>>();
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
String username = usernamePosition != -1 ? nextLine[usernamePosition] : null;
String email = emailPosition != -1 ? nextLine[emailPosition] : null;
boolean registered = true;
if (StringUtils.isNotEmpty(username)) {
// registered Jahia user is provided
JahiaUser user = username.charAt(0) == '{' ? userManagerService.lookupUserByKey(username) :
userManagerService.lookupUser(username);
if (user != null) {
if (username.charAt(0) != '{') {
username = "{" + user.getProviderName() + "}" + username;
}
} else {
logger.warn("No user can be found for the specified username '" + username +
"'. Skipping subscription: " + StringUtils.join(nextLine, separator));
continue;
}
} else if (StringUtils.isNotEmpty(email)) {
username = email;
registered = false;
} else {
logger.warn("Neither a j:nodename nor j:email is provided." + "Skipping subscription: " +
StringUtils.join(nextLine, separator));
continue;
}
Map<String, Object> props = new HashMap<String, Object>(columns.length);
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
if ("j:nodename".equals(column) || !registered && J_EMAIL.equals(column)) {
continue;
}
props.put(column, nextLine[i]);
}
if (logger.isDebugEnabled()) {
logger.debug("Subscribing '" + username + "' with properties: " + props);
}
subscribers.put(username, props);
if (subscribers.size() > 1000) {
// flush
subscribe(subscribableIdentifier, subscribers, session);
importedCount += subscribers.size();
subscribers = new HashMap<String, Map<String, Object>>();
}
}
if (!subscribers.isEmpty()) {
// subscribe the rest
importedCount += subscribers.size();
subscribe(subscribableIdentifier, subscribers, session);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// ignore
}
}
IOUtils.closeQuietly(is);