Skip to content

Commit

Permalink
Merge Space Templates - Meeds-io/MIPs#150 (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored Nov 4, 2024
2 parents 9fca0f4 + 2ef7828 commit 9d50c53
Show file tree
Hide file tree
Showing 38 changed files with 388 additions and 1,177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ public <T> T get(Object key, Callable<T> valueLoader) {

@Override
public void put(Object key, Object value) {
cacheInstance.put((Serializable) key, value);
cacheInstance.put(getSerializableKey(key), value);
}

@Override
public void evict(Object key) {
cacheInstance.remove((Serializable) key);
cacheInstance.remove(getSerializableKey(key));
}

@Override
Expand All @@ -119,9 +119,18 @@ public void clear() {

@Override
protected Object lookup(Object key) {
key = getSerializableKey(key);
return cacheInstance.get((Serializable) key);
}

private Serializable getSerializableKey(Object key) {
if (key instanceof Serializable serializable) {
return serializable;
} else {
return key.hashCode();
}
}

};
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
Expand Down Expand Up @@ -70,14 +69,15 @@ public class InMemoryUserHandler implements UserHandler {

private List<UserEventListener> userListeners = new ArrayList<>();

private static Map<String, User> usersById = new HashMap<>();
private static Map<String, User> usersById = new ConcurrentHashMap<>();

public InMemoryUserHandler(OrganizationService organizationService) {
this.organizationService = organizationService;
}

public final List<UserEventListener> getUserEventListeners() {
return Collections.unmodifiableList(userListeners);
// Clone list to avoid ConcurrentModificationException (event with Vector got it)
return userListeners.stream().toList();
}

@Override
Expand Down Expand Up @@ -225,9 +225,9 @@ public InMemoryListAccess<User> findUsersByQuery(Query query, UserStatus userSta
.map(user -> filterUserStatus(user, userStatus))
.filter(Objects::nonNull)
.filter(user -> contains(user.getEmail(), query.getEmail())
&& contains(user.getFirstName(), query.getFirstName())
&& contains(user.getLastName(), query.getLastName())
&& contains(user.getUserName(), query.getUserName()))
&& contains(user.getFirstName(), query.getFirstName())
&& contains(user.getLastName(), query.getLastName())
&& contains(user.getUserName(), query.getUserName()))
.filter(Objects::nonNull)
.toList();
return new InMemoryListAccess<>(users, new User[0]);
Expand Down Expand Up @@ -259,10 +259,10 @@ public InMemoryListAccess<User> findUsersByQuery(Query query, List<String> group
.stream()
.map(user -> filterUserStatus(user, userStatus))
.filter(user -> CollectionUtils.isEmpty(groupIds)
|| groupIds.stream()
.anyMatch(groupId -> !getMembershipHandler().findMembershipsByUserAndGroup(user.getUserName(),
groupId)
.isEmpty()))
|| groupIds.stream()
.anyMatch(groupId -> !getMembershipHandler().findMembershipsByUserAndGroup(user.getUserName(),
groupId)
.isEmpty()))
.filter(Objects::nonNull)
.toList();
return new InMemoryListAccess<>(users, new User[0]);
Expand All @@ -275,9 +275,10 @@ public User findUserByUniqueAttribute(String attributeName, String attributeValu
.stream()
.filter(Objects::nonNull)
.filter(profile -> profile.getUserInfoMap() != null
&& profile.getUserInfoMap().containsKey(attributeName)
&& StringUtils.equals(profile.getUserInfoMap().get(attributeName),
attributeValue))
&& profile.getUserInfoMap().containsKey(attributeName)
&& StringUtils.equals(profile.getUserInfoMap()
.get(attributeName),
attributeValue))
.distinct()
.toList();
User user = null;
Expand Down Expand Up @@ -341,73 +342,93 @@ private void saveUser(User user, boolean broadcast, boolean isNew) {
}

private void preSave(User user, boolean isNew) {
for (UserEventListener listener : userListeners) {
try {
listener.preSave(user, isNew);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
}
getUserEventListeners().forEach(listener -> {
try {
listener.preSave(user, isNew);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private void postSave(User user, boolean isNew) {
for (UserEventListener listener : userListeners) {
try {
listener.postSave(user, isNew);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
}
getUserEventListeners().forEach(listener -> {
try {
listener.postSave(user, isNew);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private void preDelete(User user) {
for (UserEventListener listener : userListeners) {
try {
listener.preDelete(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
}
getUserEventListeners().forEach(listener -> {
try {
listener.preDelete(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private void postDelete(User user) {
for (UserEventListener listener : userListeners) {
try {
listener.postDelete(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
}
getUserEventListeners().forEach(listener -> {
try {
listener.postDelete(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private void preSetEnabled(User user) {
for (UserEventListener listener : userListeners)
try {
listener.preSetEnabled(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
getUserEventListeners().forEach(listener -> {
try {
listener.preSetEnabled(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private void postSetEnabled(User user) {
for (UserEventListener listener : userListeners)
try {
listener.postSetEnabled(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}", listener.getClass().getName()), e);
}
getUserEventListeners().forEach(listener -> {
try {
listener.postSetEnabled(user);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(ERROR_BROADCASTING_EVENT_MESSAGE.replace("{}",
listener.getClass()
.getName()),
e);
}
});
}

private InMemoryUserProfileHandler getUserProfileHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.util.Calendar;
import java.util.Collection;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import org.exoplatform.container.ExoContainer;
import org.exoplatform.services.listener.Event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,33 @@
import java.util.ArrayList;
import java.util.Collection;

import org.exoplatform.container.PortalContainer;
import org.exoplatform.container.component.RequestLifeCycle;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.container.xml.ValueParam;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.navigation.NavigationContext;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.portal.mop.service.NavigationService;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.organization.Group;
import org.exoplatform.services.organization.GroupEventListener;
import org.exoplatform.services.organization.GroupHandler;
import org.exoplatform.services.organization.OrganizationService;

import io.meeds.common.ContainerTransactional;

/**
* This listener is used to initialize/destroy the Group Site Config of a group
* when it is created/deleted
*/
public class GroupPortalConfigListener extends GroupEventListener {

/** . */
private final UserPortalConfigService portalConfigService;

/** . */
private final LayoutService layoutService;
private UserPortalConfigService portalConfigService;

/** . */
private final OrganizationService orgService;
private LayoutService layoutService;

private String groupNamePattern;
private OrganizationService orgService;

private static Log LOG = ExoLogger.getLogger(GroupPortalConfigListener.class);
private String groupNamePattern;

public GroupPortalConfigListener(InitParams params,
UserPortalConfigService portalConfigService,
Expand All @@ -72,41 +65,28 @@ public GroupPortalConfigListener(InitParams params,
}
}

@Override
@ContainerTransactional
public void preDelete(Group group) throws Exception {
RequestLifeCycle.begin(PortalContainer.getInstance());
try {
String groupId = group.getId().trim();
String groupId = group.getId().trim();

// Remove all descendant navigations
removeGroupNavigation(group);

portalConfigService.removeUserPortalConfig(PortalConfig.GROUP_TYPE, groupId);
} finally {
RequestLifeCycle.end();
}
// Remove all descendant navigations
removeGroupNavigation(group);
portalConfigService.removeUserPortalConfig(PortalConfig.GROUP_TYPE, groupId);
}

@Override
@ContainerTransactional
public void postSave(Group group, boolean isNew) throws Exception {
if (!isNew) {
return;
}

RequestLifeCycle.begin(PortalContainer.getInstance());
try {
String groupId = group.getId();

if (layoutService.getPortalConfig(SiteKey.group(groupId)) == null) {
if (groupNamePattern != null && groupId.startsWith(groupNamePattern)) {
portalConfigService.createGroupSite(groupId);
} else {
LOG.debug("The group name doesn't match the pattern. Ignore creating from listener", groupId);
}
} else {
LOG.debug("The group site {} already exists. Ignore creating from listener", groupId);
}
} finally {
RequestLifeCycle.end();
String groupId = group.getId();
if (groupNamePattern != null
&& groupId.startsWith(groupNamePattern)
&& layoutService.getPortalConfig(SiteKey.group(groupId)) == null) {
portalConfigService.createGroupSite(groupId);
}
}

Expand All @@ -128,7 +108,7 @@ private void removeGroupNavigation(Group group) throws Exception {

private Collection<String> getDescendantGroups(Group group, GroupHandler groupHandler) throws Exception {
Collection<Group> groupCollection = groupHandler.findGroups(group);
Collection<String> col = new ArrayList<String>();
Collection<String> col = new ArrayList<>();
for (Group childGroup : groupCollection) {
col.add(childGroup.getId());
col.addAll(getDescendantGroups(childGroup, groupHandler));
Expand Down
Loading

0 comments on commit 9d50c53

Please sign in to comment.