001/*
002 *  Copyright 2019 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.projects;
017
018import java.util.List;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.observation.Event;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.activities.Activity;
031import org.ametys.plugins.repository.activities.ActivityType;
032import org.ametys.plugins.repository.query.expression.Expression;
033import org.ametys.plugins.repository.query.expression.StringExpression;
034import org.ametys.plugins.repository.query.expression.Expression.Operator;
035import org.ametys.plugins.repository.query.expression.ExpressionContext;
036import org.ametys.plugins.repository.query.expression.OrExpression;
037import org.ametys.plugins.workspaces.project.objects.Project;
038import org.ametys.plugins.workspaces.wall.WallContentManager;
039import org.ametys.web.repository.content.WebContent;
040import org.ametys.web.repository.page.Page;
041
042import com.google.common.collect.Iterables;
043
044/**
045 * {@link ActivityType} implementation for content
046 */
047public class WebContentActivityType extends AbstractProjectsActivityType
048{
049    /** data name for the page id */
050    public static final String PAGE_ID = "pageId";
051    /** data name for the content type */
052    public static final String CONTENT_TYPE = "contentType";
053    /** data name for the content summary */
054    public static final String CONTENT_SUMMARY = "contentSummary";
055    /** data name for the content title */
056    public static final String CONTENT_TITLE = "contentTitle";
057    /** data name for the content id */
058    public static final String CONTENT_ID = "contentId";
059    
060    private WallContentManager _wallContentManager;
061    
062    @Override
063    public void service(ServiceManager serviceManager) throws ServiceException
064    {
065        super.service(serviceManager);
066        _wallContentManager = (WallContentManager) serviceManager.lookup(WallContentManager.ROLE);
067    }
068    
069    @Override
070    public void setAdditionalActivityData(Activity activity, Map<String, Object> parameters) throws RepositoryException
071    {
072        super.setAdditionalActivityData(activity, parameters);
073        
074        Content content = (Content) parameters.get(ObservationConstants.ARGS_CONTENT);
075        activity.setValue(CONTENT_ID, content.getId());
076        activity.setValue(CONTENT_TITLE, content.getTitle());
077        // FIXME the getExcerpt method lose the line break
078        activity.setValue(CONTENT_SUMMARY, _wallContentManager.getExcerpt(content, 150));
079        activity.setValue(CONTENT_TYPE, content.getTypes()[0]);
080        
081        if (content instanceof WebContent)
082        {
083            Page page = Iterables.getFirst(((WebContent) content).getReferencingPages(), null);
084            if (page != null)
085            {
086                activity.setValue(PAGE_ID, page.getId());
087            }
088        }
089    }
090    
091    @Override
092    public boolean isMergeable(Activity activity1, Activity activity2)
093    {
094        return false;
095    }
096    
097    @Override
098    public Project getProjectFromEvent(Event event)
099    {
100        Map<String, Object> args = event.getArguments();
101        WebContent content = (WebContent) args.get(ObservationConstants.ARGS_CONTENT);
102        // Find project
103        List<Project> projects = _projectManager.getProjectsForSite(content.getSite());
104        return projects.isEmpty() ? null : projects.get(0);
105    }
106
107    @Override
108    public AmetysObject getTargetAmetysObject(Activity activity)
109    {
110        return _resolver.resolveById(activity.getValue(WebContentActivityType.CONTENT_ID));
111    }
112
113    @Override
114    public List<String> getSubjectI18nParams(Activity activity)
115    {
116        List<String> params = super.getSubjectI18nParams(activity);         // {0} project title
117        params.add(activity.getValue(WebContentActivityType.CONTENT_TITLE));   // {1} content title
118        return params;
119    }
120    
121    @Override
122    public Expression getFilterPatternExpression(String pattern)
123    {
124        Expression titleExpr = new StringExpression(CONTENT_TITLE, Operator.WD, pattern);
125        Expression summaryExpr = new StringExpression(CONTENT_SUMMARY, Operator.WD, pattern, ExpressionContext.newInstance().withCaseInsensitive(true));
126        return new OrExpression(titleExpr, summaryExpr);
127    }
128    
129    @Override
130    public String getMailBodyURI(Activity activity)
131    {
132        return "cocoon://_plugins/workspaces/notification-mail-wallcontent";
133    }
134}