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.project.notification;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.collections.ListUtils;
024
025import org.ametys.cms.contenttype.ContentTypesHelper;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.workflow.EnhancedCondition;
030import org.ametys.plugins.workspaces.WorkspacesConstants;
031import org.ametys.runtime.i18n.I18nizableText;
032
033import com.opensymphony.module.propertyset.PropertySet;
034import com.opensymphony.workflow.WorkflowException;
035
036/**
037 * Condition to notify news publication
038 *
039 */
040public class SendNewsPublicationNotificationCondition extends AbstractContentWorkflowComponent implements EnhancedCondition
041{
042    /** The user manager */
043    protected ContentTypesHelper _cTypeHelper;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _cTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
050    }
051    
052    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
053    {
054        WorkflowAwareContent content = getContent(transientVars);
055        
056        // check if content is a project news
057        if (!_cTypeHelper.isInstanceOf(content, WorkspacesConstants.PROJECT_NEWS_CONTENT_TYPE_ID))
058        {
059            return false;
060        }
061        
062        // check if the current user is the content's author
063        UserIdentity user = getUser(transientVars);
064        if (!content.getCreator().equals(user))
065        {
066            return false;
067        }
068        
069        // check if the news was not already be notified
070        boolean alreadyNotified = content.getInternalDataHolder().getValue(SetNotifiedFunction.NOTIFIED_PROPERTY_NAME, false);
071        return !alreadyNotified;
072    }
073
074    @Override
075    public List<ConditionArgument> getArguments()
076    {
077        return ListUtils.EMPTY_LIST;
078    }
079
080    @Override
081    public I18nizableText getDescription(Map<String, String> argumentsValues)
082    {
083        return new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_SEND_NEWS_PUBLICATION_NOTIFICATION_CONDITION_DESCRIPTION");
084    }
085}