From c6935eec9f785793e98412c21c2f4675b8f7df85 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 23 Jan 2014 13:07:46 +0100
Subject: [PATCH] OO-951: create a wrapper for the properties to update, it
 prevents the auto flush of the changes on the database

---
 .../org/olat/admin/user/imp/ImportStep00.java |  13 +-
 .../org/olat/admin/user/imp/ImportStep01.java |  28 ++-
 .../olat/admin/user/imp/UpdateIdentity.java   | 181 +++++++++++++++++-
 .../admin/user/imp/UserImportController.java  |  25 ++-
 .../olat/admin/user/imp/_content/step1.html   |   8 +-
 .../user/imp/_i18n/LocalStrings_de.properties |   1 +
 .../user/imp/_i18n/LocalStrings_en.properties |   1 +
 7 files changed, 231 insertions(+), 26 deletions(-)

diff --git a/src/main/java/org/olat/admin/user/imp/ImportStep00.java b/src/main/java/org/olat/admin/user/imp/ImportStep00.java
index 37f1b4e247f..abf7a30b7a3 100644
--- a/src/main/java/org/olat/admin/user/imp/ImportStep00.java
+++ b/src/main/java/org/olat/admin/user/imp/ImportStep00.java
@@ -236,14 +236,13 @@ class ImportStep00 extends BasicStep {
 				Identity ident = BaseSecurityManager.getInstance().findIdentityByName(login);
 				if (ident != null) {
 					// update existing accounts, add info message
-					idents.add(ident);
 					
-					ident.getUser().getPreferences().setLanguage(lang);
-					importDataError = updateUserProperties(ident, parts, i, columnId, tempEmailsInUse, importedEmails, true);
-					if(importDataError) break;
-					
-					UpdateIdentity uIdentity = new UpdateIdentity(ident, pwd);
+					UpdateIdentity uIdentity = new UpdateIdentity(ident, pwd, lang);
+					idents.add(uIdentity);
 					updateIdents.add(uIdentity);
+					
+					importDataError = updateUserProperties(uIdentity, parts, i, columnId, tempEmailsInUse, importedEmails, true);
+					if(importDataError) break;
 				} else {
 					// no identity/user yet, create
 					// check that no user with same login name is already in list
@@ -322,7 +321,7 @@ class ImportStep00 extends BasicStep {
 				if ( (thisKey.equals(UserConstants.INSTITUTIONALEMAIL) || thisKey.equals(UserConstants.EMAIL)) && !thisValue.isEmpty() ) {
 					// check that no user with same email is already in OLAT
 					Identity identity = UserManager.getInstance().findIdentityByEmail(thisValue);
-					if (identity != null && !identity.equals(ud)) {
+					if (identity != null && !ud.equals(identity)) {
 						textAreaElement.setErrorKey("error.email.exists", new String[] { String.valueOf(i + 1), thisValue });
 						importDataError = true;
 						break;
diff --git a/src/main/java/org/olat/admin/user/imp/ImportStep01.java b/src/main/java/org/olat/admin/user/imp/ImportStep01.java
index 6fe6989a7be..b15537abf68 100644
--- a/src/main/java/org/olat/admin/user/imp/ImportStep01.java
+++ b/src/main/java/org/olat/admin/user/imp/ImportStep01.java
@@ -79,6 +79,7 @@ class ImportStep01 extends BasicStep {
 	private final class ImportStepForm01 extends StepFormBasicController {
 
 		private FormLayoutContainer textContainer;
+		private MultipleSelectionElement updateEl;
 		private MultipleSelectionElement updatePasswordEl;
 		private List<UserPropertyHandler> userPropertyHandlers;
 
@@ -98,11 +99,17 @@ class ImportStep01 extends BasicStep {
 
 		@Override
 		protected void formOK(UserRequest ureq) {
-			Boolean update = Boolean.FALSE;
+			Boolean updateUsers = Boolean.FALSE;
+			if(updateEl != null && updateEl.isAtLeastSelected(1)) {
+				updateUsers = Boolean.TRUE; 
+			}
+			addToRunContext("updateUsers", updateUsers);
+			
+			Boolean updatePasswords = Boolean.FALSE;
 			if(updatePasswordEl != null && updatePasswordEl.isAtLeastSelected(1)) {
-				update = Boolean.TRUE; 
+				updatePasswords = Boolean.TRUE; 
 			}
-			addToRunContext("updatePasswords", update);
+			addToRunContext("updatePasswords", updatePasswords);
 			fireEvent(ureq, StepsEvent.ACTIVATE_NEXT);
 		}
 
@@ -127,10 +134,17 @@ class ImportStep01 extends BasicStep {
 			String overview = getTranslator().translate("import.confirm", new String[] { "" + cntall, "" + cntNew, "" + cntOld });
 			textContainer.contextPut("overview", overview);
 			textContainer.contextPut("updateusers", updateIdents.isEmpty());
-			if(!updateIdents.isEmpty() && canCreateOLATPassword) {
-				String[] theValues = new String[]{ translate("update.password") };
-				updatePasswordEl = uifactory
-						.addCheckboxesHorizontal("update.password", textContainer, new String[]{"on"}, theValues, null);
+			if(!updateIdents.isEmpty()) {
+				String[] updateValues = new String[]{ translate("update.user") };
+				updateEl = uifactory
+						.addCheckboxesHorizontal("update.user", textContainer, new String[]{"on"}, updateValues, null);
+				updateEl.select("on", true);
+				
+				if(canCreateOLATPassword) {
+					String[] theValues = new String[]{ translate("update.password") };
+					updatePasswordEl = uifactory
+							.addCheckboxesHorizontal("update.password", textContainer, new String[]{"on"}, theValues, null);
+				}
 			}
 
 			FlexiTableColumnModel tableColumnModel = FlexiTableDataModelFactory.createFlexiTableColumnModel();
diff --git a/src/main/java/org/olat/admin/user/imp/UpdateIdentity.java b/src/main/java/org/olat/admin/user/imp/UpdateIdentity.java
index be9a8503f3b..15dab54adee 100644
--- a/src/main/java/org/olat/admin/user/imp/UpdateIdentity.java
+++ b/src/main/java/org/olat/admin/user/imp/UpdateIdentity.java
@@ -19,29 +19,204 @@
  */
 package org.olat.admin.user.imp;
 
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
 import org.olat.core.id.Identity;
+import org.olat.core.id.Persistable;
+import org.olat.core.id.Preferences;
+import org.olat.core.id.User;
+import org.olat.core.util.StringHelper;
 
 /**
  * 
- * Initial date: 20.12.2013<br>
+ * Initial date: 23.01.2014<br>
  * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
  *
  */
-public class UpdateIdentity {
+public class UpdateIdentity implements Identity {
 	
+	private static final long serialVersionUID = 8918783456443529334L;
+
+	private final UpdateUser userWrapper;
 	private final Identity identity;
+	
 	private final String password;
+	private final String language;
 	
-	public UpdateIdentity(Identity identity, String password) {
+	public UpdateIdentity(Identity identity, String password, String language) {
 		this.identity = identity;
 		this.password = password;
+		this.language = language;
+		this.userWrapper = new UpdateUser(identity.getUser());
 	}
 	
 	public String getPassword() {
 		return password;
 	}
 
+	public String getLanguage() {
+		return language;
+	}
+
 	public Identity getIdentity() {
+		return getIdentity(false);
+	}
+	
+	public Identity getIdentity(boolean transferNewProperties) {
+		if(transferNewProperties) {
+			User user = identity.getUser();
+			if(StringHelper.containsNonWhitespace(language)) {
+				user.getPreferences().setLanguage(language);
+			}
+			
+			Map<String,String> updatedProperties = userWrapper.getUpdatedProperties();
+			for(Map.Entry<String, String> entry:updatedProperties.entrySet()) {
+				String propertyName = entry.getKey();
+				String propertyValue = entry.getValue();
+				user.setProperty(propertyName, propertyValue);
+			}
+		}
 		return identity;
 	}
+
+	@Override
+	public Long getKey() {
+		return identity.getKey();
+	}
+
+	@Override
+	public Date getCreationDate() {
+		return identity.getCreationDate();
+	}
+
+	@Override
+	public String getName() {
+		return identity.getName();
+	}
+	
+	@Override
+	public void setName(String name) {
+		//
+	}
+
+	@Override
+	public User getUser() {
+		return userWrapper;
+	}
+
+	@Override
+	public Date getLastLogin() {
+		return identity.getLastLogin();
+	}
+
+	@Override
+	public void setLastLogin(Date loginDate) {
+		//
+	}
+
+	@Override
+	public Integer getStatus() {
+		return identity.getStatus();
+	}
+
+	@Override
+	public void setStatus(Integer newStatus) {
+		//
+	}	
+	
+	@Override
+	public int hashCode() {
+		return identity.hashCode();
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		return identity.equals(obj);
+	}
+
+	@Override
+	public boolean equalsByPersistableKey(Persistable persistable) {
+		return identity.equalsByPersistableKey(persistable);
+	}
+	
+	private static class UpdateUser implements User {
+
+		private static final long serialVersionUID = -7755595504039649174L;
+		
+		private final User user;
+		private Map<String,String> updatedProperties = new HashMap<String,String>();
+		
+		public UpdateUser(User user) {
+			this.user = user;
+		}
+		
+		public Map<String,String> getUpdatedProperties() {
+			return updatedProperties;
+		}
+		
+		@Override
+		public Long getKey() {
+			return user.getKey();
+		}
+
+		@Override
+		public Date getCreationDate() {
+			return user.getCreationDate();
+		}
+
+		@Override
+		public Preferences getPreferences() {
+			return user.getPreferences();
+		}
+
+		@Override
+		public void setPreferences(Preferences prefs) {
+			//
+		}
+
+		@Override
+		public void setProperty(String propertyName, String propertyValue) {
+			String currentProperty = user.getProperty(propertyName, null);
+			if(currentProperty == null
+					|| (currentProperty != null && !currentProperty.equals(propertyValue))) {
+				updatedProperties.put(propertyName, propertyValue);
+			}
+		}
+
+		@Override
+		public String getProperty(String propertyName, Locale locale) {
+			if(updatedProperties.containsKey(propertyName)) {
+				return updatedProperties.get(propertyName);
+			}
+			return user.getProperty(propertyName, locale);
+		}
+
+		@Override
+		public void setIdentityEnvironmentAttributes(Map<String, String> identEnvAttribs) {
+			//
+		}
+
+		@Override
+		public String getPropertyOrIdentityEnvAttribute(String propertyName, Locale locale) {
+			return null;
+		}
+
+		@Override
+		public int hashCode() {
+			return user.hashCode();
+		}
+
+		@Override
+		public boolean equals(Object obj) {
+			return user.equals(obj);
+		}
+
+		@Override
+		public boolean equalsByPersistableKey(Persistable persistable) {
+			return user.equalsByPersistableKey(persistable);
+		}
+	}
 }
diff --git a/src/main/java/org/olat/admin/user/imp/UserImportController.java b/src/main/java/org/olat/admin/user/imp/UserImportController.java
index 95d72b72be7..119b79335e8 100644
--- a/src/main/java/org/olat/admin/user/imp/UserImportController.java
+++ b/src/main/java/org/olat/admin/user/imp/UserImportController.java
@@ -61,11 +61,13 @@ import org.olat.user.propertyhandlers.UserPropertyHandler;
 
 /**
  * Description:<br>
- * TODO: Felix Class Description for UserImportController
+ * Bulk import and update of users.
+ * 
  * <P>
  * Initial Date: 17.08.2005 <br>
  * 
  * @author Felix, Roman Haag
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
  */
 public class UserImportController extends BasicController {
 
@@ -150,17 +152,23 @@ public class UserImportController extends BasicController {
 		return ident;
 	}
 	
-	private Identity doUpdateIdentity(UpdateIdentity singleUser, Boolean updatePassword) {
-		String password = singleUser.getPassword();
-		Identity identity = singleUser.getIdentity();
-		um.updateUserFromIdentity(identity);
+	private Identity doUpdateIdentity(UpdateIdentity userToUpdate, Boolean updateUsers, Boolean updatePassword) {
+		Identity identity;
+		if(updateUsers != null && updateUsers.booleanValue()) {
+			identity = userToUpdate.getIdentity(true);
+			um.updateUserFromIdentity(identity);
+		} else {
+			identity = userToUpdate.getIdentity();
+		}
+		
+		String password = userToUpdate.getPassword();
 		if(StringHelper.containsNonWhitespace(password) && updatePassword != null && updatePassword.booleanValue()) {
 			Authentication auth = securityManager.findAuthentication(identity, "OLAT");
 			if(auth != null) {
 				olatAuthManager.changePassword(getIdentity(), identity, password);
 			}
 		}
-		return singleUser.getIdentity();
+		return userToUpdate.getIdentity();
 	}
 
 	/**
@@ -191,12 +199,13 @@ public class UserImportController extends BasicController {
 						for (TransientIdentity newIdent:newIdents) {
 							doCreateAndPersistIdentity(newIdent);
 						}
-						
+
+						Boolean updateUsers = (Boolean)runContext.get("updateUsers");
 						Boolean updatePasswords = (Boolean)runContext.get("updatePasswords");
 						@SuppressWarnings("unchecked")
 						List<UpdateIdentity> updateIdents = (List<UpdateIdentity>) runContext.get("updateIdents");
 						for (UpdateIdentity updateIdent:updateIdents) {
-							doUpdateIdentity(updateIdent, updatePasswords);
+							doUpdateIdentity(updateIdent, updateUsers, updatePasswords);
 						}
 
 						@SuppressWarnings("unchecked")
diff --git a/src/main/java/org/olat/admin/user/imp/_content/step1.html b/src/main/java/org/olat/admin/user/imp/_content/step1.html
index 8903c067c30..7054233a149 100644
--- a/src/main/java/org/olat/admin/user/imp/_content/step1.html
+++ b/src/main/java/org/olat/admin/user/imp/_content/step1.html
@@ -5,6 +5,12 @@
 	$r.translate("step1.nonewusers")
 #end
 
+#if($r.visible("update.user"))
+		<br/><br/>$r.render("update.user")
+#end
 #if($r.visible("update.password"))
-		<br/><br/>$r.render("update.password")
+	#if(!$r.visible("update.user"))
+		<br/>
+	#end
+	<br/>$r.render("update.password")
 #end
\ No newline at end of file
diff --git a/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_de.properties
index 884230ca776..603b13f1f05 100644
--- a/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_de.properties
@@ -48,3 +48,4 @@ table.user.login=Benutzername
 table.user.pwd=Passwort
 title=Import von Benutzern
 update.password=Passw\u00F6rter \u00E4ndern f\u00FCr existierende Benutzer
+update.user=Existierende Benutzer \u00E4ndern
diff --git a/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_en.properties
index 1f58eeecc05..280f84794fe 100644
--- a/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/admin/user/imp/_i18n/LocalStrings_en.properties
@@ -48,3 +48,4 @@ table.user.login=User name
 table.user.pwd=Password
 title=User import
 update.password=Update passwords of current users
+update.user=Update current users
-- 
GitLab