001/*
002 *  Copyright 2024 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.cms.search.systemprop;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.avalon.framework.configuration.DefaultConfiguration;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang3.StringUtils;
028import org.xml.sax.ContentHandler;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
032import org.ametys.cms.search.SearchField;
033import org.ametys.cms.search.model.SystemProperty;
034import org.ametys.cms.search.query.Query;
035import org.ametys.cms.search.query.Query.Operator;
036import org.ametys.cms.search.query.WorkflowNameQuery;
037import org.ametys.cms.search.solr.field.WorkflowNameSearchField;
038import org.ametys.cms.workflow.WorkflowNameEnumerator;
039import org.ametys.core.model.type.ModelItemTypeHelper;
040import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
041import org.ametys.plugins.workflow.support.WorkflowHelper;
042import org.ametys.plugins.workflow.support.WorkflowProvider;
043import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
044import org.ametys.runtime.model.Enumerator;
045import org.ametys.runtime.model.type.DataContext;
046import org.ametys.runtime.model.type.ModelItemTypeConstants;
047import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
048
049/**
050 * {@link SystemProperty} which represents the current workflow name of an ametys object.
051 */
052public class WorkflowNameSystemProperty extends AbstractSystemProperty<String, ModelAwareDataAwareAmetysObject>
053{
054    /** The workflow provider */
055    protected WorkflowProvider _workflowProvider;
056    /** The workflow helper */
057    protected WorkflowHelper _workflowHelper;
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        super.service(manager);
063        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
064        _workflowHelper = (WorkflowHelper) manager.lookup(WorkflowHelper.ROLE);
065    }
066    
067    @Override
068    public boolean isMultiple()
069    {
070        return false;
071    }
072    
073    @Override
074    public boolean isSortable()
075    {
076        return true;
077    }
078    
079    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
080    {
081        if (StringUtils.isEmpty((String) value))
082        {
083            return new WorkflowNameQuery(Operator.EXISTS);
084        }
085        
086        return new WorkflowNameQuery(operator, (String) value);
087    }
088    
089    @Override
090    public SearchField getSearchField()
091    {
092        return new WorkflowNameSearchField();
093    }
094    
095    @Override
096    public String getRenderer()
097    {
098        return "Ametys.plugins.cms.search.SearchGridHelper.renderWorkflow";
099    }
100    
101    @Override
102    public Object getValue(ModelAwareDataAwareAmetysObject ametysObject)
103    {
104        if (ametysObject instanceof WorkflowAwareAmetysObject waAmetysObject)
105        {
106            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(waAmetysObject);
107            
108            long workflowId = waAmetysObject.getWorkflowId();
109            return workflow.getWorkflowName(workflowId);
110        }
111        
112        return null;
113    }
114    
115    public Object valueToJSON(ModelAwareDataAwareAmetysObject ametysObject, DataContext context)
116    {
117        String workflowName = (String) getValue(ametysObject);
118        if (workflowName != null)
119        {
120            return Map.of(
121                "name", workflowName,
122                "label", _workflowHelper.getWorkflowLabel(workflowName)
123            );
124        }
125        return Map.of();
126    }
127    
128    public void valueToSAX(ContentHandler contentHandler, ModelAwareDataAwareAmetysObject ametysObject, DataContext context) throws SAXException
129    {
130        String workflowName = (String) getValue(ametysObject);
131        if (workflowName != null)
132        {
133            AttributesImpl attr = ModelItemTypeHelper.getXMLAttributesFromDataContext(context);
134            attr.addCDATAAttribute("name", String.valueOf(workflowName));
135            XMLUtils.startElement(contentHandler, getName(), attr);
136            _workflowHelper.getWorkflowLabel(workflowName).toSAX(contentHandler);
137            XMLUtils.endElement(contentHandler, getName());
138        }
139    }
140    
141    public Enumerator<String> getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
142    {
143        DefaultConfiguration conf = new DefaultConfiguration("criteria");
144        
145        DefaultConfiguration enumConf = new DefaultConfiguration("enumeration");
146        
147        DefaultConfiguration customEnumerator = new DefaultConfiguration("custom-enumerator");
148        customEnumerator.setAttribute("class", WorkflowNameEnumerator.class.getName());
149        enumConf.addChild(customEnumerator);
150        
151        conf.addChild(enumConf);
152        
153        String role = "enumerator";
154        enumeratorManager.addComponent(getPluginName(), null, role, WorkflowNameEnumerator.class, conf);
155        
156        try
157        {
158            enumeratorManager.initialize();
159            return enumeratorManager.lookup(role);
160        }
161        catch (Exception e)
162        {
163            throw new ConfigurationException("Unable to initialize the workflow name enumerator for system property '" + getName() + "'.", conf, e);
164        }
165    }
166
167    @Override
168    protected String _getTypeId()
169    {
170        return ModelItemTypeConstants.STRING_TYPE_ID;
171    }
172}