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.repository.Content;
032import org.ametys.cms.repository.WorkflowAwareContent;
033import org.ametys.cms.search.SearchField;
034import org.ametys.cms.search.model.SystemProperty;
035import org.ametys.cms.search.query.Query;
036import org.ametys.cms.search.query.Query.Operator;
037import org.ametys.cms.search.query.WorkflowNameQuery;
038import org.ametys.cms.search.solr.field.WorkflowNameSearchField;
039import org.ametys.cms.workflow.WorkflowNameEnumerator;
040import org.ametys.core.model.type.ModelItemTypeHelper;
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 a content.
051 */
052public class WorkflowNameSystemProperty extends AbstractSystemProperty<String, Content>
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 Object getValue(Content content)
097    {
098        if (content instanceof WorkflowAwareContent waContent)
099        {
100            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(waContent);
101            
102            long workflowId = waContent.getWorkflowId();
103            return workflow.getWorkflowName(workflowId);
104        }
105        
106        return null;
107    }
108    
109    public Object valueToJSON(Content content, DataContext context)
110    {
111        String workflowName = (String) getValue(content);
112        return _workflowHelper.getWorkflowLabel(workflowName);
113    }
114    
115    public void valueToSAX(ContentHandler contentHandler, Content content, DataContext context) throws SAXException
116    {
117        String workflowName = (String) getValue(content);
118        if (workflowName != null)
119        {
120            AttributesImpl attr = ModelItemTypeHelper.getXMLAttributesFromDataContext(context);
121            attr.addCDATAAttribute("name", String.valueOf(workflowName));
122            XMLUtils.startElement(contentHandler, getName(), attr);
123            _workflowHelper.getWorkflowLabel(workflowName).toSAX(contentHandler);
124            XMLUtils.endElement(contentHandler, getName());
125        }
126    }
127    
128    public Enumerator<String> getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
129    {
130        DefaultConfiguration conf = new DefaultConfiguration("criteria");
131        
132        DefaultConfiguration enumConf = new DefaultConfiguration("enumeration");
133        
134        DefaultConfiguration customEnumerator = new DefaultConfiguration("custom-enumerator");
135        customEnumerator.setAttribute("class", WorkflowNameEnumerator.class.getName());
136        enumConf.addChild(customEnumerator);
137        
138        conf.addChild(enumConf);
139        
140        String role = "enumerator";
141        enumeratorManager.addComponent(getPluginName(), null, role, WorkflowNameEnumerator.class, conf);
142        
143        try
144        {
145            enumeratorManager.initialize();
146            return enumeratorManager.lookup(role);
147        }
148        catch (Exception e)
149        {
150            throw new ConfigurationException("Unable to initialize the workflow name enumerator for system property '" + getName() + "'.", conf, e);
151        }
152    }
153
154    @Override
155    protected String _getTypeId()
156    {
157        return ModelItemTypeConstants.STRING_TYPE_ID;
158    }
159}