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_STARTDATE, task.getStartDate()); 073 json.put(JCRTask.ATTRIBUTE_DUEDATE, task.getDueDate()); 074 json.put("datePassed", task.isClosed()); 075 076 return json; 077 } 078 079 private Map<String, Object> _closeInfoAsJson(Task task) 080 { 081 UserIdentity closeAuthor = task.getCloseAuthor(); 082 083 return task.isClosed() 084 ? Map.of(JCRTask.ATTRIBUTE_CLOSEAUTHOR, _userHelper.user2json(closeAuthor), JCRTask.ATTRIBUTE_CLOSEDATE, task.getCloseDate()) 085 : null; 086 } 087 088 private List<Map<String, Object>> _assignmentsToJson(Task task) 089 { 090 return task.getAssignments() 091 .stream() 092 .map(u -> _userHelper.user2json(u)) 093 .collect(Collectors.toList()); 094 } 095 096 private List<Map<String, Object>> _checkListToJson(Task task) 097 { 098 List<Map<String, Object>> json = new ArrayList<>(); 099 for (CheckItem checkItem : task.getCheckList()) 100 { 101 json.add(Map.of(JCRTask.ATTRIBUTE_CHECKLIST_LABEL, checkItem.getLabel(), JCRTask.ATTRIBUTE_CHECKLIST_ISCHECKED, checkItem.isChecked())); 102 } 103 return json; 104 } 105 106}