001/*
002 *  Copyright 2015 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.clientside;
017
018import java.util.HashMap;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.commons.lang3.ArrayUtils;
023
024import org.ametys.core.ui.Callable;
025import org.ametys.core.ui.StaticClientSideElement;
026import org.ametys.plugins.explorer.calendars.Calendar;
027import org.ametys.plugins.explorer.calendars.CalendarEvent;
028import org.ametys.plugins.explorer.workflow.AbstractExplorerNodeWorkflowComponent;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.workflow.support.WorkflowProvider;
031
032import com.opensymphony.workflow.Workflow;
033
034/**
035 * Button client side element for calendars
036 * Used for add and edit event buttons
037 */
038public class CalendarButtonClientSideElement extends StaticClientSideElement
039{
040    /** The workflow provider */
041    protected WorkflowProvider _workflowProvider;
042    
043    /** The ametys resolver */
044    protected AmetysObjectResolver _resolver;
045    
046    @Override
047    public void service(ServiceManager serviceManager) throws ServiceException
048    {
049        super.service(serviceManager);
050        _workflowProvider = (WorkflowProvider) serviceManager.lookup(WorkflowProvider.ROLE);
051        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
052    }
053
054    
055    /**
056     * Indicates if the current user can initialize the calendar workflow
057     * @param calendarId The calendar identifier
058     * @return true if initialization is possible
059     */
060    @Callable
061    public boolean canInitWorkflow(String calendarId)
062    {
063        Calendar calendar = _resolver.resolveById(calendarId);
064        return canInitWorkflow(calendar);
065    }
066    
067    /**
068     * Indicates if the current user can initialize the calendar workflow
069     * @param calendar The calendar
070     * @return true if initialization is possible
071     */
072    public boolean canInitWorkflow(Calendar calendar)
073    {
074        Workflow workflow = _workflowProvider.getAmetysObjectWorkflow();
075        
076        int initActionId = 1;
077        
078        HashMap<String, Object> inputs = new HashMap<>();
079        inputs.put(AbstractExplorerNodeWorkflowComponent.EXPLORERNODE_KEY, calendar);
080        
081        return workflow.canInitialize(calendar.getWorkflowName(), initActionId, inputs);
082    }
083    
084    /**
085     * Indicates if the current user can edit an event
086     * @param eventId The event identifier
087     * @return true if edition is possible
088     */
089    @Callable
090    public boolean canEditWorkflow(String eventId)
091    {
092        CalendarEvent event = _resolver.resolveById(eventId);
093        return canEditWorkflow(event);
094    }
095    
096    /**
097     * Indicates if the current user can edit an event
098     * @param event The event
099     * @return true if edition is possible
100     */
101    public boolean canEditWorkflow(CalendarEvent event)
102    {
103        Workflow workflow = _workflowProvider.getAmetysObjectWorkflow(event);
104        
105        int editActionId = 2;
106        
107        HashMap<String, Object> inputs = new HashMap<>();
108        inputs.put(AbstractExplorerNodeWorkflowComponent.EXPLORERNODE_KEY, event.getParent()); // parent is the calendar
109        return ArrayUtils.contains(workflow.getAvailableActions(event.getWorkflowId(), inputs), editActionId);
110    }
111}