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