Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
GTNPORTAL-3013 UserProfileDAOImpl not distinguishing creating and upd…
Browse files Browse the repository at this point in the history
…ating user profile when firing preSave/postSave events
  • Loading branch information
mposolda committed May 15, 2013
1 parent ee8c61a commit 19abebb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.poi.hslf.model.Placeholder;
import org.exoplatform.services.organization.UserProfile;
import org.exoplatform.services.organization.UserProfileEventListener;
import org.exoplatform.services.organization.UserProfileHandler;
Expand Down Expand Up @@ -81,15 +82,22 @@ public UserProfile createUserProfileInstance(String userName) {
// }

public void saveUserProfile(UserProfile profile, boolean broadcast) throws Exception {
// We need to check if userProfile exists, because organization API is limited and it doesn't have separate methods for
// "creation" and for "update" of user profile :/
boolean isNew = true;
if (broadcast) {
UserProfile found = getProfile(profile.getUserName());
isNew = found == null;
}

if (broadcast) {
preSave(profile, true);
preSave(profile, isNew);
}

setProfile(profile.getUserName(), profile);

if (broadcast) {
postSave(profile, true);
postSave(profile, isNew);
}

}
Expand All @@ -110,6 +118,7 @@ public UserProfile removeUserProfile(String userName, boolean broadcast) throws
}
return profile;
} catch (Exception exp) {
handleException("Exception occured when removing user profile", exp);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,20 +537,45 @@ public void testRemoveMembershipByUser() throws Exception {
groupHandler_.removeGroup(group3, true);
}

// public void testUserProfileListener() throws Exception
// {
// UserProfileListener l = new UserProfileListener();
// profileHandler_.addUserProfileEventListener(l);
// User user = createUser(USER);
// assertNotNull(user);
// UserProfile profile = profileHandler_.createUserProfileInstance(user.getUserName());
// profile.setAttribute("blah", "blah");
// profileHandler_.saveUserProfile(profile, true);
// assertTrue(l.preSave && l.postSave);
// profileHandler_.removeUserProfile(user.getUserName(), true);
// assertFalse(l.preDelete && l.postDelete);
// userHandler_.removeUser(user.getUserName(), false);
// }
@Test
public void testUserProfileListener() throws Exception {
System.out.println("Trigger testUserProfileListener");
UserProfileListener l = new UserProfileListener();
profileHandler_.addUserProfileEventListener(l);
User user = createUser(USER);
assertNotNull(user);
UserProfile profile = profileHandler_.createUserProfileInstance(user.getUserName());
profile.setAttribute("blah", "blah");
System.out.println("Going to save userProfiel");
profileHandler_.saveUserProfile(profile, true);
assertTrue(l.preSave && l.postSave);
assertEquals(l.preSaveCreations, 1);
assertEquals(l.postSaveCreations, 1);
assertEquals(l.preSaveUpdates, 0);
assertEquals(l.postSaveUpdates, 0);

// Upgrade userProfile
profile.setAttribute("blah", "blah2");
profileHandler_.saveUserProfile(profile, true);
assertEquals(l.preSaveCreations, 1);
assertEquals(l.postSaveCreations, 1);
assertEquals(l.preSaveUpdates, 1);
assertEquals(l.postSaveUpdates, 1);

// Another upgrade of userProfile
profile.setAttribute("blah", "blah3");
profileHandler_.saveUserProfile(profile, true);
assertEquals(l.preSaveCreations, 1);
assertEquals(l.postSaveCreations, 1);
assertEquals(l.preSaveUpdates, 2);
assertEquals(l.postSaveUpdates, 2);

// Delete profile
assertFalse(l.preDelete || l.postDelete);
profileHandler_.removeUserProfile(user.getUserName(), true);
assertTrue(l.preDelete && l.postDelete);
userHandler_.removeUser(user.getUserName(), false);
}

@Test
public void testLinkMembership() throws Exception {
Expand Down Expand Up @@ -597,6 +622,11 @@ private static class UserProfileListener extends UserProfileEventListener {

boolean postDelete;

int preSaveCreations = 0;
int preSaveUpdates = 0;
int postSaveCreations = 0;
int postSaveUpdates = 0;

@Override
public void postDelete(UserProfile profile) throws Exception {
assertEquals(USER, profile.getUserName());
Expand All @@ -607,6 +637,12 @@ public void postDelete(UserProfile profile) throws Exception {
public void postSave(UserProfile profile, boolean isNew) throws Exception {
assertEquals(USER, profile.getUserName());
postSave = true;

if (isNew) {
postSaveCreations++;
} else {
postSaveUpdates++;
}
}

@Override
Expand All @@ -619,6 +655,12 @@ public void preDelete(UserProfile profile) throws Exception {
public void preSave(UserProfile profile, boolean isNew) throws Exception {
assertEquals(USER, profile.getUserName());
preSave = true;

if (isNew) {
preSaveCreations++;
} else {
preSaveUpdates++;
}
}

}
Expand Down

0 comments on commit 19abebb

Please sign in to comment.