001/*
002 *  Copyright 2023 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;
017
018import java.time.ZonedDateTime;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.core.user.UserIdentity;
025import org.ametys.core.userpref.UserPreferencesException;
026import org.ametys.core.userpref.UserPreferencesManager;
027import org.ametys.plugins.workspaces.AbstractWorkspaceDAO;
028import org.ametys.plugins.workspaces.project.objects.Project;
029
030/**
031 * DAO for thread's user prefrerences
032 */
033public class MembersUserPreferencesDAO extends AbstractWorkspaceDAO
034{
035    /** Avalon Role */
036    public static final String ROLE = MembersUserPreferencesDAO.class.getName();
037
038    /** the user preferences context for project joined dates */
039    public static final String PROJECT_JOIN_DATE_USER_PREF_CONTEXT = "/workspaces/members/project-joined-date";
040
041    /** The user preferences */
042    protected UserPreferencesManager _userPrefsManager;
043
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        super.service(manager);
048        _userPrefsManager = (UserPreferencesManager) manager.lookup(UserPreferencesManager.ROLE);
049    }
050
051    /**
052     * Get the joined date of current user for a project.
053     * @param project the project.
054     * @return the joined date.
055     */
056    public ZonedDateTime getProjectJoinedDate(Project project)
057    {
058        ZonedDateTime joinedDate = null;
059        try
060        {
061            joinedDate = _userPrefsManager.getUserPreferenceAsDate(_currentUserProvider.getUser(), PROJECT_JOIN_DATE_USER_PREF_CONTEXT, Map.of(), project.getId());
062        }
063        catch (UserPreferencesException e)
064        {
065            getLogger().error("An error occured while getting the project joined date from user preferences.", e);
066        }
067
068        return joinedDate;
069    }
070
071    /**
072     * Set the joined date of a user for a project.
073     * @param user the user.
074     * @param project the project.
075     * @param joinedDateAsString the joined date as a string.
076     */
077    public void setJoinedDate(UserIdentity user, Project project, String joinedDateAsString)
078    {
079        try
080        {
081            _userPrefsManager.addUserPreference(user, MembersUserPreferencesDAO.PROJECT_JOIN_DATE_USER_PREF_CONTEXT, Map.of(), project.getId(), joinedDateAsString);
082        }
083        catch (UserPreferencesException e)
084        {
085            getLogger().error("Unable to store the joined date for user '" + user + "' in project '" + project + "'.", e);
086        }
087    }
088}