001/*
002 *  Copyright 2021 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.tasks.json;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.stream.Collectors;
023
024import org.apache.avalon.framework.component.Component;
025
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.workspaces.tasks.Task;
028import org.ametys.plugins.workspaces.tasks.Task.CheckItem;
029import org.ametys.plugins.workspaces.tasks.jcr.JCRTask;
030import org.ametys.plugins.workspaces.util.WorkspaceObjectJSONHelper;
031
032/**
033 * Helper to convert task to JSON
034 */
035public class TaskJSONHelper extends WorkspaceObjectJSONHelper implements Component
036{
037    /** The Avalon role */
038    public static final String ROLE = TaskJSONHelper.class.getName();
039    
040    /**
041     * Task as JSON
042     * @param task the task
043     * @param lang the language
044     * @param siteName the site name
045     * @return the task as JSON
046     */
047    public Map<String, Object> taskAsJSON(Task task, String lang, String siteName)
048    {
049        Map<String, Object> json = new HashMap<>();
050        
051        json.put(JCRTask.ATTRIBUTE_TASK_ID, task.getId());
052        json.put(JCRTask.ATTRIBUTE_LABEL, task.getLabel());
053        json.put(JCRTask.ATTRIBUTE_DESCRIPTION, task.getDescription());
054        json.put(JCRTask.ATTRIBUTE_TASKSLISTID, task.getTaskListId());
055        json.put("position", task.getPosition());
056        json.put(JCRTask.ATTRIBUTE_CREATIONDATE, task.getCreationDate());
057        json.put("closeInfo", _closeInfoAsJson(task));
058        
059        json.put(JCRTask.ATTRIBUTE_ASSIGNMENTS, _assignmentsToJson(task));
060        
061        json.put(JCRTask.ATTRIBUTE_TAGS, _getTags(task, siteName));
062        
063        List<Map<String, Object>> attachments = task.getAttachments()
064            .stream()
065            .map(this::_binaryToJson)
066            .collect(Collectors.toList());
067        json.put(JCRTask.ATTRIBUTE_ATTACHMENTS, attachments);
068        
069        json.put(JCRTask.ATTRIBUTE_CHECKLIST, _checkListToJson(task));
070
071        json.put("comments", _commentsToJson(task.getComments(true, true), lang, siteName));
072        json.put(JCRTask.ATTRIBUTE_DUEDATE, task.getDueDate());
073        json.put("datePassed", task.isClosed());
074        
075        return json;
076    }
077    
078    private Map<String, Object> _closeInfoAsJson(Task task)
079    {
080        UserIdentity closeAuthor = task.getCloseAuthor();
081        
082        return task.isClosed() 
083                ? Map.of(JCRTask.ATTRIBUTE_CLOSEAUTHOR, _userHelper.user2json(closeAuthor), JCRTask.ATTRIBUTE_CLOSEDATE, task.getCloseDate()) 
084                : null;
085    }
086
087    private List<Map<String, Object>> _assignmentsToJson(Task task)
088    {
089        return task.getAssignments()
090            .stream()
091            .map(u -> _userHelper.user2json(u))
092            .collect(Collectors.toList());
093    }
094    
095    private List<Map<String, Object>> _checkListToJson(Task task)
096    {
097        List<Map<String, Object>> json = new ArrayList<>();
098        for (CheckItem checkItem : task.getCheckList())
099        {
100            json.add(Map.of(JCRTask.ATTRIBUTE_CHECKLIST_LABEL, checkItem.getLabel(), JCRTask.ATTRIBUTE_CHECKLIST_ISCHECKED, checkItem.isChecked()));
101        }
102        return json;
103    }
104    
105}