001/*
002 *  Copyright 2017 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.odfsync.apogee.scc.impl;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.slf4j.Logger;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.odf.orgunit.OrgUnit;
028import org.ametys.odf.orgunit.RootOrgUnitProvider;
029import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsCollection;
030
031/**
032 * SCC for orgunit contents.
033 */
034public class OrgUnitSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsCollection
035{
036    /** SCC model id */
037    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.orgunit";
038    
039    /** Root org unit provider */
040    protected RootOrgUnitProvider _rootOrgUnitProvider;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
047    }
048    
049    @Override
050    protected List<Map<String, Object>> _search(Map<String, Object> searchParameters, Logger logger)
051    {
052        return _convertBigDecimal(_apogeeDAO.searchOrgUnits(getDataSourceId(), getParameterValues(), searchParameters));
053    }
054
055    @Override
056    protected String getMappingName()
057    {
058        return "orgunit";
059    }
060    
061    @Override
062    protected Set<String> getNotSynchronizedRelatedContentIds(Content content, Map<String, Object> contentValues, Map<String, Object> additionalParameters, String lang, Logger logger)
063    {
064        Set<String> contentIds = super.getNotSynchronizedRelatedContentIds(content, contentValues, additionalParameters, lang, logger);
065        
066        OrgUnit rootOrgUnit = _rootOrgUnitProvider.getRoot();
067        contentIds.add(rootOrgUnit.getId());
068        
069        return contentIds;
070    }
071    
072    @Override
073    protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger)
074    {
075        Map<String, Object> result = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger);
076        
077        OrgUnit rootOrgUnit = _rootOrgUnitProvider.getRoot();
078        result.put(OrgUnit.PARENT_ORGUNIT, rootOrgUnit);
079        
080        return result;
081    }
082    
083    @Override
084    protected String getChildrenAttributeName()
085    {
086        return OrgUnit.CHILD_ORGUNITS;
087    }
088}