001/*
002 *  Copyright 2022 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.documents;
017
018import java.util.Map;
019
020import javax.jcr.RepositoryException;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.core.util.DateUtils;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.explorer.threads.actions.ThreadDAO;
030import org.ametys.plugins.explorer.threads.jcr.JCRPost;
031import org.ametys.plugins.repository.activities.Activity;
032import org.ametys.plugins.repository.activities.ActivityType;
033
034/**
035 * {@link ActivityType} implementation for the commenting of document
036 */
037public class DocumentCommentedActivityType extends ResourceCreatedOrUpdatedActivityType
038{
039    /** data name for storing the last modified date of the document */
040    public static final String LAST_MODIFIED = "lastModified";
041    /** data name for storing the last contributor of the document */
042    public static final String LAST_CONTRIBUTOR = "lastContributor";
043    /** data name for storing the comment */
044    public static final String COMMENT_SUMMARY = "commentSummary";
045    
046    private ThreadDAO _threadDAO;
047    @Override
048    public void service(ServiceManager serviceManager) throws ServiceException
049    {
050        super.service(serviceManager);
051        _threadDAO = (ThreadDAO) serviceManager.lookup(ThreadDAO.ROLE);
052    }
053    @Override
054    public void setAdditionalActivityData(Activity activity, Map<String, Object> parameters) throws RepositoryException
055    {
056        super.setAdditionalActivityData(activity, parameters);
057        
058        String resourceId = (String) parameters.get(ObservationConstants.ARGS_ID);
059        Resource resource = _resolver.resolveById(resourceId);
060        activity.setValue(LAST_CONTRIBUTOR, resource.getLastContributor(), "user");
061        activity.setValue(LAST_MODIFIED, DateUtils.asZonedDateTime(resource.getLastModified()), "datetime");
062        
063        JCRPost comment = (JCRPost) parameters.get(ObservationConstants.ARGS_POST);
064        
065        comment.getContent();
066        activity.setValue(COMMENT_SUMMARY, StringUtils.abbreviate(_threadDAO.convertPostToString(comment), "…", 150));
067    }
068    
069    
070    @Override
071    public boolean isMergeable(Activity activity1, Activity activity2)
072    {
073        // We don't want to merge for the moment as it would require to merge between different author
074        // to recreate a conversation otherwise it would just be useless.
075        return false;
076    }
077}