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.workspaces.WorkspacesConstants;
028
029import com.opensymphony.module.propertyset.PropertySet;
030import com.opensymphony.workflow.Condition;
031import com.opensymphony.workflow.WorkflowException;
032
033/**
034 * Condition to notify news publication
035 *
036 */
037public class SendNewsPublicationNotificationCondition extends AbstractContentWorkflowComponent implements Condition
038{
039    /** The user manager */
040    protected ContentTypesHelper _cTypeHelper;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _cTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
047    }
048    
049    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
050    {
051        WorkflowAwareContent content = getContent(transientVars);
052        
053        // check if content is a project news
054        if (!_cTypeHelper.isInstanceOf(content, WorkspacesConstants.PROJECT_NEWS_CONTENT_TYPE_ID))
055        {
056            return false;
057        }
058        
059        // check if the current user is the content's author
060        UserIdentity user = getUser(transientVars);
061        if (!content.getCreator().equals(user))
062        {
063            return false;
064        }
065        
066        // check if the news was not already be notified
067        boolean alreadyNotified = content.getInternalDataHolder().getValue(SetNotifiedFunction.NOTIFIED_PROPERTY_NAME, false);
068        return !alreadyNotified;
069    }
070}