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    /**
063     * Creates an {@link JCRCalendar}.
064     * @param node the node backing this {@link AmetysObject}
065     * @param parentPath the parentPath in the Ametys hierarchy
066     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
067     */
068    public JCRCalendar(Node node, String parentPath, JCRCalendarFactory factory)
069    {
070        super(node, parentPath, factory);
071    }
072
073    public String getIconCls()
074    {
075        return "calendar";
076    }
077
078    public String getApplicationId()
079    {
080        return APPLICATION_ID;
081    }
082
083    public String getExplorerPath()
084    {
085        AmetysObject parent = getParent();
086        
087        if (parent instanceof ExplorerNode)
088        {
089            return ((ExplorerNode) parent).getExplorerPath() + "/" + getName();
090        }
091        else
092        {
093            return "";
094        }
095    }
096    
097    public String getDescription()
098    {
099        return getMetadataHolder().getString(CALENDAR_DESC);
100    }
101
102    public String getColor()
103    {
104        return getMetadataHolder().getString(CALENDAR_COLOR);
105    }
106    
107    public CalendarVisibility getVisibility()
108    {
109        ModifiableCompositeMetadata metadataHolder = getMetadataHolder();
110        if (metadataHolder.hasMetadata(CALENDAR_VISIBILITY))
111        {
112            return CalendarVisibility.valueOf(metadataHolder.getString(CALENDAR_VISIBILITY));
113        }
114        else
115        {
116            return CalendarVisibility.PRIVATE;
117        }
118    }
119    
120    public String getWorkflowName()
121    {
122        return getMetadataHolder().getString(CALENDAR_WORKFLOW_NAME);
123    }
124    
125    public String getTemplateDescription()
126    {
127        String templateDescription = "";
128        if (getMetadataHolder().hasMetadata(CALENDAR_TEMPLATE_DESC))
129        {
130            templateDescription = getMetadataHolder().getString(CALENDAR_TEMPLATE_DESC);
131        }
132        return templateDescription;
133    }
134    
135    public Map<CalendarEvent, List<Date>> getEvents(Date startDate, Date endDate)
136    {
137        Map<CalendarEvent, List<Date>> events = new HashMap<>();
138        
139        AmetysObjectIterable<AmetysObject> childrens = this.getChildren();
140        for (AmetysObject child : childrens)
141        {
142            if (child instanceof CalendarEvent)
143            {
144                CalendarEvent event = (CalendarEvent) child;
145                
146                Date startDateEvent = event.getStartDate();
147                Date endDateEvent = event.getEndDate();
148                
149                if (event.getFullDay())
150                {
151                    GregorianCalendar gcStart = new GregorianCalendar();
152                    gcStart.setTime(startDateEvent);
153                    gcStart.set(java.util.Calendar.HOUR_OF_DAY, 0);
154                    gcStart.set(java.util.Calendar.MINUTE, 0);
155                    gcStart.set(java.util.Calendar.SECOND, 0);
156                    gcStart.set(java.util.Calendar.MILLISECOND, 0);
157             
158                    startDateEvent = gcStart.getTime();
159                    
160                    GregorianCalendar gcEnd = new GregorianCalendar();
161                    gcEnd.setTime(endDateEvent);
162                    gcEnd.set(java.util.Calendar.HOUR_OF_DAY, 23);
163                    gcEnd.set(java.util.Calendar.MINUTE, 59);
164                    gcEnd.set(java.util.Calendar.SECOND, 59);
165                    gcEnd.set(java.util.Calendar.MILLISECOND, 999);
166             
167                    endDateEvent = gcEnd.getTime();
168                }
169                
170                List<Date> occurences = event.getOccurrences(startDate, endDate);
171                if (!occurences.isEmpty())
172                {
173                    events.put(event, occurences);
174                }
175            }
176        }
177        
178        return events;
179    }
180
181    public void setDescription(String desc)
182    {
183        getMetadataHolder().setMetadata(CALENDAR_DESC, desc);
184    }
185
186    public void setColor(String color)
187    {
188        getMetadataHolder().setMetadata(CALENDAR_COLOR, color); 
189    }
190    
191    public void setVisibility(CalendarVisibility visibility)
192    {
193        getMetadataHolder().setMetadata(CALENDAR_VISIBILITY, visibility.name());
194    }
195
196    public void setWorkflowName(String workflowName)
197    {
198        getMetadataHolder().setMetadata(CALENDAR_WORKFLOW_NAME, workflowName); 
199    }
200    
201    public void setTemplateDescription(String templateDesc)
202    {
203        getMetadataHolder().setMetadata(CALENDAR_TEMPLATE_DESC, templateDesc);
204    }
205    
206    public boolean hasChildExplorerNodes() throws AmetysRepositoryException
207    {
208        for (AmetysObject child : getChildren())
209        {
210            if (child instanceof ExplorerNode)
211            {
212                return true;
213            }
214        }
215
216        return false;
217    }
218}