001/*
002 *  Copyright 2016 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.repository.activities;
017
018import java.time.ZonedDateTime;
019import java.time.format.DateTimeFormatter;
020import java.util.Map;
021
022import javax.jcr.RepositoryException;
023
024import org.ametys.core.user.UserIdentity;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.RepositoryConstants;
027
028/**
029 * Interface for activity holder
030 */
031public interface ActivityHolder
032{
033    /** the activity root node name */
034    public static final String ACTIVITIES_ROOT_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":activities";
035    /** Prefix used for naming the activity node */
036    public static final String ACTIVITY_NAME_PREFIX = "ametys-activity_";
037    /** formatter to store date time in jcr node name */
038    public static final DateTimeFormatter JCR_UTC_FORMAT = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_SSS");
039    
040    /**
041     * Returns the activities stored by this activity holder
042     * @return The activities
043     * @throws RepositoryException if failed to get activity nodes
044     */
045    public AmetysObjectIterable<Activity> getActivities() throws RepositoryException;
046    
047    /**
048     * Add an activity to the activity holder
049     * @param type the type
050     * @param parameters the parameters
051     * @param eventId the id of the event
052     * @return the activity
053     * @throws RepositoryException when an error occurred
054     */
055    public Activity addActivity(ActivityType type, Map<String, Object> parameters, String eventId) throws RepositoryException;
056    
057    /**
058     * Add an activity to the activity holder
059     * 
060     * This convenient method will call {@link #addActivity(ZonedDateTime, ActivityType, Map, UserIdentity, String)} with the current time.
061     * 
062     * @param type the type
063     * @param parameters the parameters
064     * @param author the author
065     * @param eventId the id of the event
066     * @return the activity
067     * @throws RepositoryException when an error occurred
068     */
069    public default Activity addActivity(ActivityType type, Map<String, Object> parameters, UserIdentity author, String eventId) throws RepositoryException
070    {
071        return addActivity(ZonedDateTime.now(), type, parameters, author, eventId);
072    }
073    
074    /**
075     * Add an activity to the activity holder
076     * @param date the date
077     * @param type the type
078     * @param parameters the parameters
079     * @param author the author
080     * @param eventId the id of the event
081     * @return the activity
082     * @throws RepositoryException when an error occurred
083     */
084    public Activity addActivity(ZonedDateTime date, ActivityType type, Map<String, Object> parameters, UserIdentity author, String eventId) throws RepositoryException;
085}