001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Vincent Vergnolle
018 */
019package org.nuxeo.ecm.platform.picture;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.lang.StringUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.platform.picture.api.PictureConversion;
031import org.nuxeo.runtime.model.ContributionFragmentRegistry;
032
033/**
034 * Registry for the {@link org.nuxeo.ecm.platform.picture.api.PictureConversion} class (merge supported)
035 *
036 * @since 7.1
037 */
038public class PictureConversionRegistry extends ContributionFragmentRegistry<PictureConversion> {
039
040    private static final Log log = LogFactory.getLog(PictureConversionRegistry.class);
041
042    protected final Map<String, PictureConversion> pictureConversions = new HashMap<>();
043
044    public PictureConversion getPictureConversion(String id) {
045        return pictureConversions.get(id);
046    }
047
048    /**
049     * Returns picture conversion collection sorted by order.
050     */
051    public List<PictureConversion> getPictureConversions() {
052        List<PictureConversion> entries = new ArrayList<>(pictureConversions.values());
053        Collections.sort(entries);
054        return entries;
055    }
056
057    @Override
058    public String getContributionId(PictureConversion pictureConversion) {
059        return pictureConversion.getId();
060    }
061
062    @Override
063    public void contributionUpdated(String id, PictureConversion pictureConversion,
064            PictureConversion oldPictureConversion) {
065        if (pictureConversions.containsKey(id)) {
066            contributionRemoved(id, pictureConversion);
067        }
068
069        if (pictureConversion.isEnabled()) {
070            if (!StringUtils.isBlank(id)) {
071                pictureConversions.put(id, pictureConversion);
072            } else {
073                log.warn(String.format("Missing 'id' for picture conversion %s, not registering it.", pictureConversion));
074            }
075
076        }
077    }
078
079    @Override
080    public void contributionRemoved(String id, PictureConversion pictureConversion) {
081        pictureConversions.remove(id);
082    }
083
084    @Override
085    public PictureConversion clone(PictureConversion pictureConversion) {
086        return pictureConversion.clone();
087    }
088
089    @Override
090    public void merge(PictureConversion source, PictureConversion dest) {
091        dest.merge(source);
092    }
093
094}