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