001/*
002 *  Copyright 2016 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.activities.calendars;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.jcr.RepositoryException;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027
028import org.ametys.core.observation.Event;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.activities.Activity;
031import org.ametys.plugins.repository.activities.ActivityType;
032import org.ametys.plugins.repository.query.expression.Expression;
033import org.ametys.plugins.repository.query.expression.Expression.Operator;
034import org.ametys.plugins.repository.query.expression.ExpressionContext;
035import org.ametys.plugins.repository.query.expression.OrExpression;
036import org.ametys.plugins.repository.query.expression.StringExpression;
037import org.ametys.plugins.workspaces.activities.AbstractWorkspacesActivityType;
038import org.ametys.plugins.workspaces.calendars.Calendar;
039import org.ametys.plugins.workspaces.calendars.CalendarWorkspaceModule;
040import org.ametys.plugins.workspaces.calendars.ObservationConstants;
041import org.ametys.plugins.workspaces.calendars.events.CalendarEvent;
042import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
043import org.ametys.plugins.workspaces.project.objects.Project;
044
045/**
046 * {@link ActivityType} implementation for the creation of a calendar event
047 */
048public class CalendarEventActivityType extends AbstractWorkspacesActivityType
049{
050    /** Constant for event's category */
051    public static final String ACTIVITY_CATEGORY_CALENDARS = "calendars";
052    
053    /** data name for the event title */
054    public static final String CALENDAR_EVENT_TITLE = "eventTitle";
055    /** data name for the event id */
056    public static final String CALENDAR_EVENT_ID = "eventId";
057    /** data name for the calendar title */
058    public static final String CALENDAR_TITLE = "calendarTitle";
059    /** data name for the calendar id */
060    public static final String CALENDAR_ID = "calendarId";
061    
062    private CalendarWorkspaceModule _calendarModule;
063    @Override
064    public void service(ServiceManager serviceManager) throws ServiceException
065    {
066        super.service(serviceManager);
067        WorkspaceModuleExtensionPoint moduleManagerEP = (WorkspaceModuleExtensionPoint) serviceManager.lookup(WorkspaceModuleExtensionPoint.ROLE);
068        _calendarModule = moduleManagerEP.getModule(CalendarWorkspaceModule.CALENDAR_MODULE_ID);
069    }
070    
071    @Override
072    public void setAdditionalActivityData(Activity activity, Map<String, Object> parameters) throws RepositoryException
073    {
074        super.setAdditionalActivityData(activity, parameters);
075        activity.setValue(PROJECT_CATEGORY, ACTIVITY_CATEGORY_CALENDARS);
076        
077        Calendar calendar = (Calendar) parameters.get(ObservationConstants.ARGS_CALENDAR);
078        CalendarEvent calendarEvent = (CalendarEvent) parameters.get(ObservationConstants.ARGS_CALENDAR_EVENT);
079        
080        // Calendar
081        activity.setValue(CALENDAR_TITLE, calendar.getName());
082        activity.setValue(CALENDAR_ID, calendar.getId());
083        
084        // Event
085        activity.setValue(CALENDAR_EVENT_TITLE, calendarEvent.getTitle());
086        activity.setValue(CALENDAR_EVENT_ID, calendarEvent.getId());
087    }
088    
089    @Override
090    public Map<String, Object> additionnalDataToJSONForClient(Activity activity)
091    {
092        Map<String, Object> json = super.additionnalDataToJSONForClient(activity);
093        
094        String calendarId = activity.getValue(CALENDAR_ID);
095        String calendarEventId = activity.getValue(CALENDAR_EVENT_ID);
096        
097        String projectName = activity.getValue(PROJECT_NAME);
098        Project project = _projectManager.getProject(projectName);
099        if (project != null)
100        {
101            json.put("eventUrl", _calendarModule.getEventUri(project, calendarId, calendarEventId));
102        }
103
104        return json;
105    }
106    
107    @Override
108    public Expression getFilterPatternExpression(String pattern)
109    {
110        Expression eventExpr = new StringExpression(CALENDAR_EVENT_TITLE, Operator.WD, pattern, ExpressionContext.newInstance().withCaseInsensitive(true));
111        Expression calExpr = new StringExpression(CALENDAR_TITLE, Operator.WD, pattern, ExpressionContext.newInstance().withCaseInsensitive(true));
112        return new OrExpression(eventExpr, calExpr);
113    }
114    
115    @Override
116    public boolean isMergeable(Activity activity1, Activity activity2)
117    {
118        if (!super.isMergeable(activity1, activity2))
119        {
120            return false;
121        }
122        String calendarId1 = activity1.getValue(CALENDAR_ID);
123        String calendarId2 = activity2.getValue(CALENDAR_ID);
124        
125        // FIXME GG pourquoi on ne prends pas l'event en compte ?
126        
127        return calendarId1 != null && calendarId2 != null && calendarId1.equals(calendarId2);
128    }
129    
130    @Override
131    public Map<String, Object> mergeActivities(List<Activity> activities)
132    {
133        Map<String, Object> mergedActivities = super.mergeActivities(activities);
134        
135        List<Map<String, Object>> mergedEvents = new ArrayList<>();
136        
137        List<String> knownEvents = new ArrayList<>();
138        
139        for (Activity activity : activities)
140        {
141            Map<String, Object> eventInfo = new HashMap<>();
142            
143            String eventId = activity.getValue(CALENDAR_EVENT_ID);
144            if (!knownEvents.contains(eventId))
145            {
146                knownEvents.add(eventId);
147                Map<String, Object> jsonActivity = activity.toJSONForClient();
148                
149                eventInfo.put(CALENDAR_EVENT_ID, eventId);
150                eventInfo.put(CALENDAR_EVENT_TITLE, jsonActivity.get(CALENDAR_EVENT_TITLE));
151                eventInfo.put("eventUrl", jsonActivity.get("eventUrl"));
152                
153                mergedEvents.add(eventInfo);
154            }
155        }
156        
157        mergedActivities.put("events", mergedEvents);
158        mergedActivities.put("amount", mergedEvents.size());
159        
160        return mergedActivities;
161    }
162
163    @Override
164    public Project getProjectFromEvent(Event event)
165    {
166        Map<String, Object> args = event.getArguments();
167        
168        Calendar calendar = (Calendar) args.get(ObservationConstants.ARGS_CALENDAR);
169        
170        return getParentProject(calendar);
171    }
172
173    @Override
174    public AmetysObject getTargetAmetysObject(Activity activity)
175    {
176        String calendarEventId = activity.getValue(CALENDAR_EVENT_ID);
177        return _resolver.resolveById(calendarEventId);
178    }
179
180    @Override
181    public List<String> getSubjectI18nParams(Activity activity)
182    {
183        List<String> i18nParams = super.getSubjectI18nParams(activity);                                     // {0} project title
184        
185        i18nParams.add(activity.getValue(CalendarEventActivityType.CALENDAR_EVENT_TITLE));  // {1} event title
186        i18nParams.add(activity.getValue(CalendarEventActivityType.CALENDAR_TITLE));        // {2} calendar title
187        return i18nParams;
188    }
189
190    @Override
191    public String getMailBodyURI(Activity activity)
192    {
193        return "cocoon://_plugins/workspaces/notification-mail-calendar-event";
194    }
195}