try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream,characterSet));
CsvParser parser = new CsvParser(delimiter);
line = reader.readLine(); // caption row
if(line == null){
throw new InputException(messages.getString("RowWithCaptionsIsInvalid"));
}
// ignore non-latin-1 characters in caption row => avoid problems with Little/BigEndian markers
StringBuffer latin1line = new StringBuffer(line.length());
for(int i=0; i < line.length(); i++){
char c = line.charAt(i);
if (c >= 0x20 && c <= 0xFE){
latin1line.append(c);
}
}
line = latin1line.toString();
if(line.length() == 0){
throw new InputException(messages.getString("RowWithCaptionsIsInvalid"));
}
int lineNumber = 1;
if(LOGGER.isDebugEnabled()){
LOGGER.debug("trying to parse following caption row: " + line);
}
ArrayList<String> captionRow = parser.parse(line);
ArrayList<String> methods = validateColumnMapping(captionRow);
UserImportResult importResult = new UserImportResult();
List<String> errorMessages = new ArrayList<String>();
importResult.setErrorMessages(errorMessages);
CsvUser csvUser = new CsvUser(locale);
while((line = reader.readLine()) != null){
batchTransactionCounter++;
if (line.trim().length() == 0){
continue;
}
lineNumber++;
if(LOGGER.isDebugEnabled()){
LOGGER.debug("trying to parse line " + lineNumber + ": " + line);
}
ArrayList<String> row = parser.parse(line);
if(row.size() != captionRow.size()){
errorMessages.add(messages.getString("ErrorInLine") +" " + lineNumber + ": " + messages.getString("InvalidNumberOfColumns"));
LOGGER.info(messages.getString("ErrorInLine") +" " + lineNumber + ": " + messages.getString("InvalidNumberOfColumns"));
continue;
}
csvUser.reset();
csvUser.setLineNumber(lineNumber);
Iterator<String> iterator = row.iterator();
int i = 0;
while (iterator.hasNext()) {
String element = iterator.next();
Object[] arguments = new Object[] {element};
Class[] parameterTypes = new Class[] {String.class};
LOGGER.debug(i + ": trying to call the method " + methods.get(i));
Method method = CsvUser.class.getMethod(methods.get(i), parameterTypes);
method.invoke(csvUser, arguments);
i++;
}
User userToImport = csvUser.getUser(errorMessages);
if(userToImport == null){
// error during parsing the user
importResult.incUsersIgnored();
continue;
}
// users with too less data will be ignored
if((userToImport.getUsername() == null || userToImport.getUsername().trim().equals("")) &&
(userToImport.getFirstName() == null || userToImport.getFirstName().trim().equals("")) &&
(userToImport.getLastName() == null || userToImport.getLastName().trim().equals("")) &&
(userToImport.getMsisdn() == null) &&
(userToImport.getEmail() == null || userToImport.getEmail().trim().equals("")) &&
(userToImport.getPersonalNumber() == null || userToImport.getPersonalNumber().equals(""))){
importResult.incUsersIgnored();
continue;
}
User alreadyExistingUser = null;
if(mergeAccordingToUsername){
if(userToImport.getUsername() != null && userToImport.getUsername().length() > 0){
alreadyExistingUser = getUserDetails(userToImport.getUsername(),userSet.getGroup());
}
} else if(mergeAccordingToMsisdn){
if(userToImport.getMsisdn() != null){
alreadyExistingUser = getUserDetails(userToImport.getMsisdn(),userSet.getGroup());
}
} else if(mergeAccordingToPersonelNumber){
if(userToImport.getPersonalNumber() != null && userToImport.getPersonalNumber().length() > 0){
alreadyExistingUser = getUserDetailsByPersonalNumber(userToImport.getPersonalNumber(),userSet.getGroup());
}
}
User userInDb = null;
if(alreadyExistingUser == null){
LOGGER.debug("create new User data objet");
if(userToImport.getUsername() != null && !userToImport.getUsername().equals("")){ // a default role will be assigned
Role role = getRole(USER);
Set<Role> roles = new HashSet<Role>(1);
roles.add(role);
userToImport.setRoles(roles);
}
userToImport.setGroup(userSet.getGroup());
userInDb = createUser(userToImport, userToImport.getPassword());
importResult.incUsersCreated();
} else{
LOGGER.debug("merge existing user data object");
User user = mergeUser(userToImport,alreadyExistingUser);
modifyUser(user);
importResult.incUsersUpdated();
userInDb = user;
}
UserSetManager usersetManager = new UserSetManager(locale,session);
usersetManager.add(userSet, userInDb);
// the transaction will be committed after BATCH_TRANSACTION_SIZE
// records are written to the database to enable other users to get a session
if ((batchTransactionCounter % BATCH_TRANSACTION_SIZE) == 0) {
HibernateSessions.commitTransaction(session);
LOGGER.debug("Batch transaction committed - "+batchTransactionCounter+" record(s) written.");
HibernateSessions.beginTransaction(session);
batchTransactionCounter = 0;
}
}
if (batchTransactionCounter > 0) {
HibernateSessions.commitTransaction(session);
LOGGER.debug("Batch transaction committed - "+batchTransactionCounter+" record(s) written, batch processing finished");
// In case AnonymousLocalizedAction expects a open transaction...
HibernateSessions.beginTransaction(session);
}
return importResult;
} catch (IOException e) {
throw new InputException(messages.getString("ErrorDuringReadingTheFile"),e);
} catch (NoSuchMethodException e) {
throw new BugException("a caption to method mapping is invalid",e);
} catch (IllegalAccessException e) {
throw new BugException("a caption to method mapping is invalid",e);
} catch (InvocationTargetException e) {