001/*
002 *  Copyright 2024 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.pagesubscription;
017
018import java.time.ZonedDateTime;
019import java.util.List;
020import java.util.Optional;
021import java.util.stream.Stream;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.core.group.GroupIdentity;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.pagesubscription.BroadcastChannelHelper.BroadcastChannel;
030import org.ametys.plugins.pagesubscription.FrequencyHelper.Frequency;
031import org.ametys.plugins.pagesubscription.type.SubscriptionType;
032import org.ametys.plugins.pagesubscription.type.SubscriptionType.FrequencyTiming;
033import org.ametys.plugins.repository.AmetysObject;
034import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
035import org.ametys.web.repository.site.Site;
036
037/**
038 * Object representing an subscription
039 */
040public class Subscription extends SimpleAmetysObject<SubscriptionFactory>
041{
042    /** key used to store the activity id when serialized */
043    public static final String ACTIVITY_ID_KEY = "id";
044    /** key used to store the activity name when serialized */
045    public static final String ACTIVITY_NAME_KEY = "name";
046
047    /**
048     * Creates an {@link Subscription}.
049     * @param node the node backing this {@link Subscription}
050     * @param parentPath the parentPath in the Ametys hierarchy
051     * @param factory the factory which created the activity
052     */
053    public Subscription(Node node, String parentPath, SubscriptionFactory factory)
054    {
055        super(node, parentPath, factory);
056    }
057
058    /**
059     * Get the type of the subscription
060     * @return the subscription type
061     */
062    public SubscriptionType getSubscriptionType()
063    {
064        return _getFactory().getSubscriptionType(getValue(SubscriptionFactory.SUBSCRIPTION_TYPE_ID));
065    }
066    
067    /**
068     * Get the subscriber of the subscription
069     * @return the subscriber subscription
070     */
071    public Optional<UserIdentity> getSubscriber()
072    {
073        return Optional.ofNullable(getValue(SubscriptionFactory.SUBSCRIBER));
074    }
075    
076    /**
077     * Get the subscribers group of the subscription
078     * @return the subscribers group subscription
079     */
080    public Optional<GroupIdentity> getSubscribersGroup()
081    {
082        String value = getValue(SubscriptionFactory.SUBSCRIBERS_GROUP);
083        return StringUtils.isNoneBlank(value)
084                ? Optional.of(GroupIdentity.stringToGroupIdentity(value))
085                : Optional.empty();
086    }
087    
088    /**
089     * Get the list of broadcast channel of the subscription
090     * @return the list of broadcast channel
091     */
092    public List<BroadcastChannel> getBroadcastChannels()
093    {
094        return Stream.of(getValueOrDefault(SubscriptionFactory.BROADCAST_CHANNEL, new String[0]))
095            .map(c -> BroadcastChannel.valueOf(c))
096            .toList();
097    }
098    
099    /**
100     * Get the frequency of the subscription
101     * @return the frequency
102     */
103    public Frequency getFrequency()
104    {
105        String frequencyType = getValue(SubscriptionFactory.FREQUENCY);
106        return Frequency.valueOf(frequencyType);
107    }
108    
109    /**
110     * <code>true</code> if the subscription is forced
111     * @return <code>true</code> if the subscription is forced
112     */
113    public boolean isForced()
114    {
115        return getValueOrDefault(SubscriptionFactory.IS_FORCED, false);
116    }
117    
118    /**
119     * Get the frequency timing hour if the subscription is forced. 
120     * @return the frequency timing
121     */
122    public FrequencyTiming getForceFrequencyTiming()
123    {
124        return new FrequencyTiming(
125            getValue(SubscriptionFactory.FORCED_DAY),
126            getValue(SubscriptionFactory.FORCED_HOUR)
127        );
128    }
129    
130    /**
131     * Get the date of the subscription.
132     * @return the date of the subscription.
133     */
134    public ZonedDateTime getDate()
135    {
136        return getValue(SubscriptionFactory.DATE);
137    }
138    
139    /**
140     * Get the site of the subscription
141     * @return the site
142     */
143    public Site getSite()
144    {
145        AmetysObject parent = getParent();
146        while (!(parent instanceof Site))
147        {
148            parent = parent.getParent();
149        }
150
151        return (Site) parent;
152    }
153}