001/*
002 *  Copyright 2026 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.workspaces.members.observers;
017
018import java.util.Date;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.core.observation.Event;
025import org.ametys.core.observation.Observer;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.core.util.DateUtils;
028import org.ametys.plugins.workspaces.ObservationConstants;
029import org.ametys.plugins.workspaces.members.MembersUserPreferencesDAO;
030import org.ametys.plugins.workspaces.project.objects.Project;
031
032/**
033 * {@link Observer} to invalidate cache on front-office of a user content when a member was added or removed from a project
034 *
035 */
036public class SetJoinedDateOnMemberAddedObserver extends AbstractMemberObserver
037{
038    /** The current user provider. */
039    protected MembersUserPreferencesDAO _membersUserPrefDAO;
040    
041    @Override
042    public void service(ServiceManager smanager) throws ServiceException
043    {
044        super.service(smanager);
045        _membersUserPrefDAO = (MembersUserPreferencesDAO) smanager.lookup(MembersUserPreferencesDAO.ROLE);
046    }
047
048    @Override
049    public boolean supports(Event event)
050    {
051        String eventId = event.getId();
052        return eventId.equals(ObservationConstants.EVENT_MEMBER_ADDED) || eventId.equals(ObservationConstants.EVENT_MEMBER_JOINED);
053    }
054
055    @Override
056    protected void _internalObserve(Event event, Set<UserIdentity> users) throws Exception
057    {
058        Project project = (Project) event.getArguments().get(ObservationConstants.ARGS_PROJECT);
059        if (project == null || users == null || users.isEmpty())
060        {
061            return;
062        }
063        
064        String joinedDateAsString = DateUtils.dateToString(new Date());
065        for (UserIdentity user : users)
066        {
067            _membersUserPrefDAO.setJoinedDate(user, project, joinedDateAsString);
068        }
069    }
070}