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        if (source.isEnabledSet() && source.isEnabled() != dest.isEnabled()) {
092            dest.setEnabled(source.isEnabled());
093        }
094
095        if (source.isDefaultSet() && source.isDefault()) {
096            dest.setDefault(source.isDefault());
097        }
098
099        // cannot disable default picture conversion
100        if (!dest.isEnabled() && dest.isDefault()) {
101            dest.setEnabled(true);
102            if (log.isWarnEnabled()) {
103                String message = String.format("The picture conversion '%s' is marked as default, enabling it.",
104                        dest.getId());
105                log.warn(message);
106            }
107        }
108
109        String chainId = source.getChainId();
110        if (!StringUtils.isBlank(chainId)) {
111            dest.setChainId(chainId);
112        }
113
114        String tag = source.getTag();
115        if (!StringUtils.isBlank(tag)) {
116            dest.setTag(tag);
117        }
118
119        String description = source.getDescription();
120        if (!StringUtils.isBlank(description)) {
121            dest.setDescription(description);
122        }
123
124        Integer order = source.getOrder();
125        if (order != null) {
126            dest.setOrder(order);
127        }
128
129        Integer maxSize = source.getMaxSize();
130        if (maxSize != null) {
131            dest.setMaxSize(maxSize);
132        }
133
134        List<String> newFilterIds = new ArrayList<>();
135        newFilterIds.addAll(dest.getFilterIds());
136        newFilterIds.addAll(source.getFilterIds());
137        dest.setFilterIds(newFilterIds);
138
139        if (source.isRenditionSet()) {
140            dest.setRendition(source.isRendition());
141        }
142
143        if (source.isRenditionVisibleSet()) {
144            dest.setRenditionVisible(source.isRenditionVisible());
145        }
146    }
147}