001/*
002 *  Copyright 2018 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.userdirectory.synchronize;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.slf4j.Logger;
027
028import org.ametys.cms.contenttype.ContentType;
029import org.ametys.cms.repository.Content;
030import org.ametys.cms.repository.ContentQueryHelper;
031import org.ametys.cms.repository.ContentTypeExpression;
032import org.ametys.plugins.contentio.synchronize.impl.DefaultSynchronizingContentOperator;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.query.expression.AndExpression;
035import org.ametys.plugins.repository.query.expression.Expression.Operator;
036import org.ametys.plugins.repository.query.expression.StringExpression;
037
038/**
039 * Operator to get UDOrgUnit Type content
040 */
041public class UDOrgUnitTypeOperator extends DefaultSynchronizingContentOperator implements Serviceable
042{
043    /** The id of orgunit's type content type */
044    protected static final String UDORGUNIT_TYPE_CONTENT_TYPE = "org.ametys.plugins.userdirectory.Content.udorgunitType";
045    /** The name of the metadata holding the type of orgunit */
046    protected static final String UDORGUNIT_METADATA_TYPE = "type";
047    /** The name of the metadata holding the code of a Orgunit type */
048    protected static final String UDORGUNIT_TYPE_METADATA_CODE = "code";
049    
050    /** The Ametys resolver */
051    protected AmetysObjectResolver _resolver;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
057    }
058    
059    @Override
060    public Map<String, List<Object>> transform(ContentType cType, Map<String, List<Object>> remoteValues, Logger logger)
061    {
062        if (remoteValues.containsKey(UDORGUNIT_METADATA_TYPE))
063        {
064            Map<String, List<Object>> transformedValues = new HashMap<>(remoteValues);
065            
066            List<Object> contentIds = new ArrayList<>();
067            List<Object> typeCodes = remoteValues.get(UDORGUNIT_METADATA_TYPE);
068            for (Object typeCode : typeCodes)
069            {
070                Content udorgUnitTypeContent = _getOrgUnitTypeFromCode(String.valueOf(typeCode));
071                if (udorgUnitTypeContent != null)
072                {
073                    contentIds.add(udorgUnitTypeContent);
074                }
075                else
076                {
077                    logger.warn("The code '{}' does not have a corresponding UDOrgunit Type content. It will be ignored.", typeCode);
078                }
079            }
080            
081            transformedValues.put(UDORGUNIT_METADATA_TYPE, contentIds);
082            return transformedValues;
083        }
084        else
085        {
086            return remoteValues;
087        }
088    }
089    
090    /**
091     * Get UDOrgUnit Type content from code
092     * @param code the code
093     * @return The UDOrgUnit Type content or <code>null</code> if not found
094     */
095    protected Content _getOrgUnitTypeFromCode (String code)
096    {
097        ContentTypeExpression cTypeExpr = new ContentTypeExpression(Operator.EQ, UDORGUNIT_TYPE_CONTENT_TYPE);
098        StringExpression codeExpr = new StringExpression(UDORGUNIT_TYPE_METADATA_CODE, Operator.EQ, code);
099        
100        String xpathQuery = ContentQueryHelper.getContentXPathQuery(new AndExpression(cTypeExpr, codeExpr));
101        return _resolver.<Content>query(xpathQuery).stream()
102                .findFirst()
103                .orElse(null);
104    }
105}
106