001/*
002 *  Copyright 2024 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.documents;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.component.Component;
023
024import org.ametys.cms.repository.comment.AbstractComment;
025import org.ametys.plugins.workspaces.util.WorkspaceObjectJSONHelper;
026
027/**
028 * Helper to convert documents to JSON
029 */
030public class DocumentsJSONHelper extends WorkspaceObjectJSONHelper implements Component
031{
032    /** The Avalon role */
033    public static final String ROLE = DocumentsJSONHelper.class.getName();
034    
035
036    /**
037     * return a list of comments in JSON format
038     * @param <T> type of the value to retrieve
039     * @param comments the comments to translate as JSON
040     * @param lang the current language
041     * @param siteName The current site name
042     * @return list of comments
043     */
044    public <T extends AbstractComment> List<Map<String, Object>> commentsToJson(List<T> comments, String lang, String siteName)
045    {
046        List<Map<String, Object>> json = new ArrayList<>();
047
048        for (T comment : comments)
049        {
050            Map<String, Object> commentAsJSON = _commentToJson(comment, lang, siteName, null, true);
051            // As we do not want file comments to be editable or deletable, remove rights
052            commentAsJSON.put("canHandle", false);
053            json.add(commentAsJSON);
054        }
055
056        return json;
057    }
058}