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