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;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.slf4j.Logger;
026
027import org.ametys.cms.ObservationConstants;
028import org.ametys.cms.content.external.ExternalizableMetadataHelper;
029import org.ametys.cms.repository.ModifiableDefaultContent;
030import org.ametys.odf.orgunit.OrgUnit;
031import org.ametys.odf.orgunit.RootOrgUnitProvider;
032import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsCollection;
033import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
034
035/**
036 * SCC for orgunit contents.
037 */
038public class OrgUnitSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsCollection
039{
040    /** SCC model id */
041    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.orgunit";
042    
043    /** Root org unit provider */
044    protected RootOrgUnitProvider _rootOrgUnitProvider;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _rootOrgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
051    }
052    
053    @Override
054    protected List<Map<String, Object>> _search(Map<String, Object> searchParams, Logger logger)
055    {
056        return _convertBigDecimal(_apogeeDAO.searchOrgUnits(getDataSourceId(), getParameterValues(), searchParams));
057    }
058
059    @Override
060    protected String getMappingName()
061    {
062        return "orgunit";
063    }
064
065    @Override
066    protected boolean handleParent(ModifiableDefaultContent currentContent, ModifiableDefaultContent parentContent, Logger logger)
067    {
068        boolean hasChanges = false;
069
070        ModifiableCompositeMetadata cm = currentContent.getMetadataHolder();
071        OrgUnit rootOrgUnit = _rootOrgUnitProvider.getRoot();
072        hasChanges = ExternalizableMetadataHelper.setMetadata(cm, OrgUnit.PARENT_ORGUNIT, rootOrgUnit);
073        hasChanges = _updateRelation(rootOrgUnit.getMetadataHolder(), OrgUnit.CHILD_ORGUNITS, currentContent) || hasChanges;
074        
075        try
076        {
077            applyChanges(rootOrgUnit, 22, ObservationConstants.EVENT_CONTENT_MODIFIED, logger);
078        }
079        catch (RepositoryException e)
080        {
081            logger.error("An error occured during updating root org unit after synchronizing the content '{}'.", currentContent.getId(), logger);
082        }
083        
084        return hasChanges;
085    }
086    
087    @Override
088    protected String getChildrenMetadataName()
089    {
090        return OrgUnit.CHILD_ORGUNITS;
091    }
092    
093    @Override
094    protected String getChildrenInvertMetadataName()
095    {
096        return OrgUnit.PARENT_ORGUNIT;
097    }
098}