001/*
002 *  Copyright 2014 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.calendars.jcr;
017
018import java.time.ZonedDateTime;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.Node;
024
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.cms.data.holder.ModifiableIndexableDataHolder;
028import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder;
029import org.ametys.plugins.explorer.ExplorerNode;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
034import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
035import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
036import org.ametys.plugins.workspaces.calendars.Calendar;
037import org.ametys.plugins.workspaces.calendars.ModifiableCalendar;
038import org.ametys.plugins.workspaces.calendars.events.CalendarEvent;
039import org.ametys.plugins.workspaces.calendars.events.CalendarEventOccurrence;
040
041/**
042 * Default implementation of an {@link Calendar}, backed by a JCR node.<br>
043 */
044public class JCRCalendar extends DefaultTraversableAmetysObject<JCRCalendarFactory> implements ModifiableCalendar
045{
046    /** application id for resources collections. */
047    public static final String APPLICATION_ID = "Ametys.plugins.explorer.applications.Calendar";
048    
049    /** Constants for title Calendar* */
050    public static final String CALENDAR_TITLE = "title";
051    
052    /** Constants for description Calendar* */
053    public static final String CALENDAR_DESC = "description";
054    
055    /** Constants for color Calendar* */
056    public static final String CALENDAR_COLOR = "color";
057    
058    /** Constants for color Visibility* */
059    public static final String CALENDAR_VISIBILITY = "visibility";
060    
061    /** Constants for workflowName Calendar* */
062    public static final String CALENDAR_WORKFLOW_NAME = "workflowName";
063    
064    /** Constants for template description Calendar* */
065    public static final String CALENDAR_TEMPLATE_DESC = "template-description";
066    
067    /** Constants for Calendar ICS token */
068    public static final String CALENDAR_ICS_TOKEN = "ics-token";
069    
070    /**
071     * Creates an {@link JCRCalendar}.
072     * @param node the node backing this {@link AmetysObject}
073     * @param parentPath the parentPath in the Ametys hierarchy
074     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
075     */
076    public JCRCalendar(Node node, String parentPath, JCRCalendarFactory factory)
077    {
078        super(node, parentPath, factory);
079    }
080
081    public String getIconCls()
082    {
083        return "calendar";
084    }
085
086    public String getApplicationId()
087    {
088        return APPLICATION_ID;
089    }
090
091    public String getExplorerPath()
092    {
093        AmetysObject parent = getParent();
094        
095        if (parent instanceof ExplorerNode)
096        {
097            return ((ExplorerNode) parent).getExplorerPath() + "/" + getName();
098        }
099        else
100        {
101            return "";
102        }
103    }
104    
105    public String getDescription()
106    {
107        return getValue(CALENDAR_DESC);
108    }
109
110    public String getColor()
111    {
112        return getValue(CALENDAR_COLOR);
113    }
114    
115    public CalendarVisibility getVisibility()
116    {
117        if (hasValue(CALENDAR_VISIBILITY))
118        {
119            return CalendarVisibility.valueOf(getValue(CALENDAR_VISIBILITY));
120        }
121        else
122        {
123            return CalendarVisibility.PRIVATE;
124        }
125    }
126    
127    public String getWorkflowName()
128    {
129        return getValue(CALENDAR_WORKFLOW_NAME);
130    }
131    
132    public String getTemplateDescription()
133    {
134        return getValue(CALENDAR_TEMPLATE_DESC, false, StringUtils.EMPTY);
135    }
136    
137    public Map<CalendarEvent, List<CalendarEventOccurrence>> getEvents(ZonedDateTime startDate, ZonedDateTime endDate)
138    {
139        Map<CalendarEvent, List<CalendarEventOccurrence>> events = new HashMap<>();
140        
141        AmetysObjectIterable<AmetysObject> childrens = this.getChildren();
142        for (AmetysObject child : childrens)
143        {
144            if (child instanceof CalendarEvent)
145            {
146                CalendarEvent event = (CalendarEvent) child;
147                
148                ZonedDateTime startDateEvent = startDate;
149                ZonedDateTime endDateEvent = endDate;
150                
151                List<CalendarEventOccurrence> occurences = event.getOccurrences(startDateEvent, endDateEvent);
152                if (!occurences.isEmpty())
153                {
154                    events.put(event, occurences);
155                }
156            }
157        }
158        
159        return events;
160    }
161
162    public void setDescription(String desc)
163    {
164        setValue(CALENDAR_DESC, desc);
165    }
166
167    public void setColor(String color)
168    {
169        setValue(CALENDAR_COLOR, color); 
170    }
171    
172    public void setVisibility(CalendarVisibility visibility)
173    {
174        setValue(CALENDAR_VISIBILITY, visibility.name());
175    }
176
177    public void setWorkflowName(String workflowName)
178    {
179        setValue(CALENDAR_WORKFLOW_NAME, workflowName); 
180    }
181    
182    public void setTemplateDescription(String templateDesc)
183    {
184        setValue(CALENDAR_TEMPLATE_DESC, templateDesc);
185    }
186    
187    public boolean hasChildExplorerNodes() throws AmetysRepositoryException
188    {
189        try (AmetysObjectIterable<AmetysObject> children = getChildren())
190        {
191            for (AmetysObject child : children)
192            {
193                if (child instanceof ExplorerNode)
194                {
195                    return true;
196                }
197            }
198    
199            return false;
200        }
201    }
202    
203    public String getIcsUrlToken()
204    {
205        return getValue(CALENDAR_ICS_TOKEN);
206    }
207    
208    public void setIcsUrlToken(String token)
209    {
210        if (token != null)
211        {
212            setValue(CALENDAR_ICS_TOKEN, token);
213        }
214        else
215        {
216            removeValue(CALENDAR_ICS_TOKEN);
217        }
218    }
219
220    public ModifiableIndexableDataHolder getDataHolder()
221    {
222        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
223        return new DefaultModifiableModelAwareDataHolder(repositoryData, _getFactory().getCalendarEventModel());
224    }
225}